name: Export Electronics

on:
  push:
  pull_request:

jobs:
  export-electronics:
    name: Export Electronics
    runs-on: ubuntu-20.04

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up outputs directory
        run: mkdir -p electronics/build/outputs

      - name: Install dependencies
        run: ./electronics/scripts/dependencies.sh

      - name: Export JLCPCB fabrication files [view_base]
        run: |
          ./electronics/scripts/export_jlcpcb.py electronics/view_base/view_base.kicad_pcb
          cp -r electronics/build/view_base-jlc electronics/build/outputs

      - name: Export PCB overview PDF [view_base]
        run: |
          ./electronics/scripts/generate_pdf.py electronics/view_base/view_base.kicad_pcb
          cp electronics/build/view_base-pcb-packet.pdf electronics/build/outputs
        env:
          PYTHONUNBUFFERED: 1

      - name: Render PCB 3D [view_base]
        run: |
          ./electronics/scripts/export_3d.py electronics/view_base/view_base.kicad_pcb --suffix front --width 1280 --height 1280 --skip-virtual --color-soldermask 0.95 0.95 0.95 --color-silk 0.1 0.1 0.1 --skip-raytrace transform z+ z+
          cp electronics/build/view_base-front-3d.png electronics/build/outputs
          ./scripts/annotate_image.sh electronics/build/outputs/view_base-front-3d.png
          ./electronics/scripts/export_3d.py electronics/view_base/view_base.kicad_pcb --suffix back --width 1280 --height 1280 --skip-virtual --color-soldermask 0.95 0.95 0.95 --color-silk 0.1 0.1 0.1 --skip-raytrace transform z+ z+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+
          cp electronics/build/view_base-back-3d.png electronics/build/outputs
          ./scripts/annotate_image.sh electronics/build/outputs/view_base-back-3d.png
        env:
          PYTHONUNBUFFERED: 1

      - name: Export JLCPCB fabrication files [view_screen]
        run: |
          ./electronics/scripts/export_jlcpcb.py electronics/view_screen/view_screen.kicad_pcb
          cp -r electronics/build/view_screen-jlc electronics/build/outputs

      - name: Export PCB overview PDF [view_screen]
        run: |
          ./electronics/scripts/generate_pdf.py electronics/view_screen/view_screen.kicad_pcb
          cp electronics/build/view_screen-pcb-packet.pdf electronics/build/outputs
        env:
          PYTHONUNBUFFERED: 1

      - name: Render PCB 3D [view_screen]
        run: |
          ./electronics/scripts/export_3d.py electronics/view_screen/view_screen.kicad_pcb --suffix front --width 1280 --height 1280 --skip-virtual --color-soldermask 0.95 0.95 0.95 --color-silk 0.1 0.1 0.1 --skip-raytrace transform z+ z+
          cp electronics/build/view_screen-front-3d.png electronics/build/outputs
          ./scripts/annotate_image.sh electronics/build/outputs/view_screen-front-3d.png
          ./electronics/scripts/export_3d.py electronics/view_screen/view_screen.kicad_pcb --suffix back --width 1280 --height 1280 --skip-virtual --color-soldermask 0.95 0.95 0.95 --color-silk 0.1 0.1 0.1 --skip-raytrace transform z+ z+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+ ry+
          cp electronics/build/view_screen-back-3d.png electronics/build/outputs
          ./scripts/annotate_image.sh electronics/build/outputs/view_screen-back-3d.png
        env:
          PYTHONUNBUFFERED: 1

      - name: Export artifacts (KiBot) [view_base]
        uses: INTI-CMNB/KiBot@fb6d637c17fbba2daf005a9a7972e4c02c1c997c
        with:
          config: electronics/view_base/view_base.kibot.yml
          dir: electronics/build/outputs
          schema: 'electronics/view_base/view_base.sch'
          board: 'electronics/view_base/view_base.kicad_pcb'

      - name: Export artifacts (KiBot) [view_screen]
        uses: INTI-CMNB/KiBot@fb6d637c17fbba2daf005a9a7972e4c02c1c997c
        with:
          config: electronics/view_screen/view_screen.kibot.yml
          dir: electronics/build/outputs
          schema: 'electronics/view_screen/view_screen.sch'
          board: 'electronics/view_screen/view_screen.kicad_pcb'

      - name: Archive artifacts
        uses: actions/upload-artifact@v2
        if: always()
        with:
          name: electronics
          path: |
            electronics/build

      - name: Configure AWS Credentials
        if: github.event_name == 'push' && github.repository_owner == 'scottbez1'
        uses: aws-actions/configure-aws-credentials@v1
        with:
          aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
          aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          aws-region: us-east-1

      - name: Sync artifacts to S3
        if: github.event_name == 'push' && github.repository_owner == 'scottbez1'
        run: |
          aws s3 sync electronics/build/outputs s3://smartknob-artifacts/${GITHUB_REF#refs/heads/}/electronics --delete --cache-control max-age=0,no-cache

