name: "deploy"

on:
  push:
    branches:
      - main


jobs:
  changes:
    runs-on: ubuntu-latest
    outputs:
      svelte: ${{steps.svelte.outputs.src}}
      tauri: ${{steps.tauri.outputs.src}}
    steps:
    - uses: actions/checkout@v3
    - uses: dorny/paths-filter@v2
      id: svelte
      with:
        filters: |
          src:
            - 'packages/gui/src/**'
            - 'packages/ui/**'
    - uses: dorny/paths-filter@v2
      id: tauri
      with:
        filters: |
          src:
            - 'packages/gui/src-tauri/**'
  build_svelte:
    needs: changes
    if: needs.changes.outputs.svelte == 'true'
    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: 16
          cache: 'pnpm'
          cache-dependency-path: pnpm-lock.yaml
      - name: install Rust stable
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
      - name: install app dependencies and build it
        run: pnpm install && pnpm --filter gui build
      - 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
  build_tauri:
    needs: changes
    if: needs.changes.outputs.tauri == 'true'
    strategy:
      fail-fast: false
      matrix:
        platform: [macos-latest, ubuntu-latest]

    runs-on: ${{ matrix.platform }}
    steps:
    - uses: actions/checkout@v2

    - uses: pnpm/action-setup@v2
      with:
        version: 7
    - name: setup node
      uses: actions/setup-node@v1
      with:
        node-version: 16
        cache: 'pnpm'
        cache-dependency-path: pnpm-lock.yaml
    - name: install Rust stable
      uses: actions-rs/toolchain@v1
      with:
        toolchain: stable
    - uses: Swatinem/rust-cache@v2
      with:
        # The prefix cache key, this can be changed to start a new cache manually.
        # default: "v0-rust"
        prefix-key: ${{ matrix.platform }}
        shared-key: prod
        cache-targets: false
    - name: install dependencies (ubuntu only)
      if: matrix.platform == 'ubuntu-latest'
      run: |
        sudo apt-get update
        sudo apt-get install -y libgtk-3-dev webkit2gtk-4.0 libappindicator3-dev librsvg2-dev patchelf
    - name: install app dependencies
      run: pnpm install

    - name: Cache Tauri Target
      uses: actions/cache@v3
      with:
        path: |
          ./packages/gui/src-tauri/target
        key: ${{ matrix.platform }}-build-target-prod
        restore-keys: |
          ${{ matrix.platform }}-build-target-prod

    - name: test build tauri
      run: pnpm --filter gui tauri build

    - name: Get current unix ts - seconds
      id: date
      run: echo "unix_seconds=$(date +'%s')" >> $GITHUB_OUTPUT
    - 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: mac-os cp package images from prod to gui bucket
      if: matrix.platform == 'macos-latest'
      run: |
        aws s3 cp ./packages/gui/src-tauri/target/release/bundle/dmg/gui_0.1.0_x64.dmg \
          s3://preview.gui.tea.xyz/release/gui_${{ steps.date.outputs.unix_seconds }}.dmg
  
    - name: ubuntu cp package images from prod to gui bucket
      if: matrix.platform == 'ubuntu-latest'
      run: |
        aws s3 cp ./packages/gui/src-tauri/target/release/bundle/deb/gui_0.1.0_amd64.deb \
          s3://preview.gui.tea.xyz/release/gui_${{ steps.date.outputs.unix_seconds }}.deb