name: "test"
on: [pull_request]


jobs:
  changes:
    runs-on: ubuntu-latest
    outputs:
      svelte: ${{steps.svelte.outputs.src}}
      tauri: ${{steps.tauri.outputs.src}}
      preview_folder: ${{steps.preview.outputs.folder}}
    steps:
    - uses: actions/checkout@v3
    - uses: dorny/paths-filter@v2
      id: svelte
      with:
        filters: |
          src:
            - 'modules/gui/src/**'
            - 'modules/ui/**'
    - uses: dorny/paths-filter@v2
      id: tauri
      with:
        filters: |
          src:
            - 'modules/gui/src-tauri/**'
            - 'modules/gui/src/**'
            - 'modules/ui/src/**'
    - name: get s3 preview folder
      id: preview
      run: echo "folder=${{ github.event.number }}-merge" >> $GITHUB_OUTPUT
  no_preview:
    needs: changes
    if: needs.changes.outputs.svelte == 'false'
    runs-on: ubuntu-latest
    steps:
      - name: comment preview site
        uses: mshick/add-pr-comment@v2
        with:
          message-id: preview-comment-${{needs.changes.outputs.preview_folder}}
          message: |
            no preview or changes related to UI
  test:
    needs: changes
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          persist-credentials: false
      - uses: pnpm/action-setup@v2
        with:
          version: 7
      - name: setup node
        uses: actions/setup-node@v1
        with:
          node-version: 18
          cache: 'pnpm'
          cache-dependency-path: pnpm-lock.yaml
      - name: install app dependencies
        run: pnpm install
      - name: unit test
        run: pnpm --filter gui run coverage
      - name: lint
        run: pnpm -r lint
  build_svelte:
    needs: changes
    if: needs.changes.outputs.svelte == 'true'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: build tauri for Linux
        uses: docker://getneil/tea-builder:latest
      - 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 web files into preview folder
        env:
          prefix: ${{ needs.changes.outputs.preview_folder }}
        run: |
          aws s3 sync ./modules/gui/build \
            "s3://preview.gui.tea.xyz/$prefix"
      - name: Install package
        run: sudo apt-get install -y jq coreutils
      - name: setup preview
        id: preview_setup
        run: .github/create-invalidate-preview.sh ${{ needs.changes.outputs.preview_folder }}

      - name: comment preview site
        uses: mshick/add-pr-comment@v2
        with:
          message-id: preview-comment-${{needs.changes.outputs.preview_folder}}
          message: |
            **preview is at**: 
            <a href="https://${{steps.preview_setup.outputs.domain}}" target="_blank">here</a>
            ```bash
            https://${{steps.preview_setup.outputs.domain}}
            ```
            copy-paste into a browser to view

  build_tauri:
    needs: changes
    if: needs.changes.outputs.tauri == 'true'
    runs-on: ${{ matrix.platform.os }}
    strategy:
      matrix:
        platform:
          - os: macos-11
            name: darwin+x86-64
            id: mac_latest
          - os: ubuntu-latest
            name: linux+x86-64
            id: linux
          - os: [self-hosted, macOS, ARM64]
            name: darwin+aarch64
            id: mac_m1
          # err: Package atk was not found in the pkg-config search path.
          # requires atk >= 2.18
          # - os: [self-hosted, linux, ARM64]
          #   name: linux+aarch64
    container: ${{ matrix.platform.container }}
    steps:
      - uses: actions/checkout@v3

      - name: build tauri for MacOS
        # uses: teaxyz/setup@v0
        if: startsWith(matrix.platform.name, 'darwin')
        # FROM: https://tauri.app/v1/guides/distribution/sign-macos
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          ENABLE_CODE_SIGNING: ${{ secrets.GUI_APPLE_CERTIFICATE }}
          APPLE_CERTIFICATE: ${{ secrets.GUI_APPLE_CERTIFICATE }}
          APPLE_CERTIFICATE_PASSWORD: ${{ secrets.GUI_APPLE_CERTIFICATE_PASSWORD }}
          APPLE_SIGNING_IDENTITY: ${{ secrets.APPLE_SIGNING_IDENTITY }}
          # TODO: uncomment once isolated m1 runner is setup with latest xcode
          # only used for notarization
          # APPLE_ID: ${{ secrets.APPLE_ID }}
          # APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}
        run: |
          sh <(curl https://tea.xyz) -ES xc build

      - name: build tauri for Linux
        # uses: teaxyz/setup@v0
        if: startsWith(matrix.platform.name, 'linux')
        # TODO: https://tauri.app/v1/guides/distribution/sign-linux
        uses: docker://getneil/tea-builder:latest
      
      - name: build platform output
        id: build_platform
        env:
          platform: ${{ matrix.platform.name }}
        run: |
          EXTENSION=dmg
          BUILD_PLATFORM=$(echo $platform | sed -e "s/darwin+//g" | sed -e "s/linux+//g")
          [[ $BUILD_PLATFORM = "x86-64" ]] && BUILD_PLATFORM="x64" || BUILD_PLATFORM=$BUILD_PLATFORM
          [[ $platform = "linux+x86-64" ]] && BUILD_PLATFORM="amd64" || BUILD_PLATFORM=$BUILD_PLATFORM
          [[ $platform = "linux+x86-64" ]] && EXTENSION="deb"
          echo "build_platform=$BUILD_PLATFORM" >> $GITHUB_OUTPUT
          echo "extension=$EXTENSION" >> $GITHUB_OUTPUT

      - name: zip .app for MacOS
        if: matrix.platform.name == 'darwin+x86-64' || matrix.platform.name == 'darwin+aarch64'
        run: |
          cd ./modules/gui/src-tauri/target/release/bundle/macos/ && zip -r tea.zip tea.app

      - 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: cp package images from prod to preview bucket
        env:
          prefix: ${{ needs.changes.outputs.preview_folder }}
          platform: ${{ steps.build_platform.outputs.build_platform }}
          extension: ${{ steps.build_platform.outputs.extension }}
        run: |
          aws s3 cp "./modules/gui/src-tauri/target/release/bundle/$extension/tea_0.1.0_$platform.$extension" \
            "s3://preview.gui.tea.xyz/$prefix/tea_$platform.$extension"
  
      - name: cp package zip for MacOS
        if: matrix.platform.name == 'darwin+x86-64' || matrix.platform.name == 'darwin+aarch64'
        env:
          prefix: ${{ needs.changes.outputs.preview_folder }}
          platform: ${{ steps.build_platform.outputs.build_platform }}
        run: |
          aws s3 cp ./modules/gui/src-tauri/target/release/bundle/macos/tea.zip "s3://preview.gui.tea.xyz/$prefix/tea_$platform.zip"

      - name: comment install for Linux
        if: startsWith(matrix.platform.name, 'linux')
        uses: mshick/add-pr-comment@v2
        with:
          message-id: ${{ matrix.platform.id }}-comment
          message: |
            **installer for Linux ${{ matrix.platform.name }} is at**: 
            <a href="http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/${{ needs.changes.outputs.preview_folder }}/tea_${{steps.build_platform.outputs.build_platform}}.${{steps.build_platform.outputs.extension}}" target="_blank">here</a>

            ```bash
            http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/${{ needs.changes.outputs.preview_folder }}/tea_${{ steps.build_platform.outputs.build_platform }}.${{ steps.build_platform.outputs.extension }}
            ```
            copy-paste into a browser to download

      - name: comment install for MacOS
        if: startsWith(matrix.platform.name, 'darwin')
        uses: mshick/add-pr-comment@v2
        with:
          message-id: ${{ matrix.platform.id }}-comment
          message: |
            **installers for MacOS ${{ matrix.platform.name }} is at**: 
            <a href="http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/${{ needs.changes.outputs.preview_folder }}/tea_${{steps.build_platform.outputs.build_platform}}.zip" target="_blank">.zip</a>
            or
            <a href="http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/${{ needs.changes.outputs.preview_folder }}/tea_${{steps.build_platform.outputs.build_platform}}.dmg" target="_blank">.dmg</a>

            ```bash
            http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/${{ needs.changes.outputs.preview_folder }}/tea_${{ steps.build_platform.outputs.build_platform }}.zip
            http://preview.gui.tea.xyz.s3-website-us-east-1.amazonaws.com/${{ needs.changes.outputs.preview_folder }}/tea_${{ steps.build_platform.outputs.build_platform }}.dmg
            ```
            copy-paste into a browser to download