name: "Build pull request"
on:
  pull_request:
    types: ['opened', 'synchronize']

jobs:
  publish-tauri:
    strategy:
      fail-fast: false
      matrix:
        include:
          - platform: 'macos-latest'
            args: '--target universal-apple-darwin'
          - platform: 'ubuntu-22.04'
            args: ''
          - platform: 'windows-latest'
            args: ''

    runs-on: ${{ matrix.platform }}
    steps:
    - name: Checkout repository
      uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
      with: 
        submodules: true
    - name: Setup node
      uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
      with:
        node-version-file: ".node-version"
        package-manager-cache: false
    - name: install Rust stable
      uses: dtolnay/rust-toolchain@stable # They use branch based releases
      with:
        targets: ${{ matrix.platform == 'macos-latest' && 'aarch64-apple-darwin,x86_64-apple-darwin' || '' }}
    - name: Install dependencies (ubuntu only)
      if: matrix.platform == 'ubuntu-22.04'
      run: |
        sudo apt-get update
        sudo apt-get install -y libwebkit2gtk-4.1-dev libappindicator3-dev librsvg2-dev patchelf
    - name: Install cinny dependencies
      run: cd cinny && npm ci
    - name: Install tauri dependencies
      run: npm ci
    - name: Build desktop app with Tauri
      uses: tauri-apps/tauri-action@73fb865345c54760d875b94642314f8c0c894afa # v0.6.1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        NODE_OPTIONS: "--max_old_space_size=4096"
      with:
          args: ${{ matrix.args }} --config '{"bundle":{"createUpdaterArtifacts":false}}'
    - name: Upload macOS artifacts
      if: matrix.platform == 'macos-latest'
      uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
      with:
        name: release-artifacts-macos
        path: |
          src-tauri/target/universal-apple-darwin/release/bundle/macos/Cinny*.app
        if-no-files-found: warn

    - name: Upload Linux artifacts
      if: matrix.platform == 'ubuntu-22.04'
      uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
      with:
        name: release-artifacts-linux
        path: |
          src-tauri/target/release/bundle/deb/Cinny_*.deb
          src-tauri/target/release/bundle/rpm/Cinny-*.rpm
          src-tauri/target/release/bundle/appimage/Cinny_*.AppImage
        if-no-files-found: warn

    - name: Upload Windows artifacts
      if: matrix.platform == 'windows-latest'
      uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
      with:
        name: release-artifacts-windows
        path: |
          src-tauri/target/release/bundle/msi/Cinny_*_x64_*.msi
          src-tauri/target/release/bundle/nsis/Cinny_*_x64-setup.exe
        if-no-files-found: warn
