name: Artifacts

on:
  push:
    branches:
      - main
  release:
    types:
      - published

permissions: write-all

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  linux:
    runs-on: ubuntu-latest
    steps:
      - name: Allow the workflow to access the files of the repository
        uses: actions/checkout@v3
        with:
          submodules: true
      - name: Update Rustup
        run: rustup update
      - name: Use an intelligent Rust cache for building the project
        uses: Swatinem/rust-cache@v2
      - name: Build the release version of `pgschemapc` for the x86 architecture
        run: cargo build --release
      - name: Upload the generated Artifact for it to be stored in its corresponding release (x86)
        uses: actions/upload-artifact@v4
        with:
          name: ${{ vars.BINARY_NAME }}_x86_64_linux_gnu
          path: target/release/${{ vars.BINARY_NAME }}
      - name: Rename the binary according to the version and architecture
        run: mv target/release/${{ vars.BINARY_NAME }} ${{ vars.BINARY_NAME }}_${{ github.event.release.tag_name }}_x86_64_linux_gnu
        if: github.event_name == 'release'
      - name: Upload the generated binaries to their corresponding release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            ${{ vars.BINARY_NAME }}_${{ github.event.release.tag_name }}_x86_64_linux_gnu
        if: github.event_name == 'release'

  mac:
    runs-on: macos-latest
    env:
      DEVELOPER_DIR: '/Applications/Xcode.app/Contents/Developer'
      SDKROOT: '/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk'
      MACOSX_DEPLOYMENT_TARGET: '10.14'
    steps:
      - name: Allow the workflow to access the files of the repository
        uses: actions/checkout@v3
        with:
          submodules: true
      - name: Use an intelligent Rust cache for building the project
        uses: Swatinem/rust-cache@v2
      - name: Build the release version of `pgschemapc` for the x86 architecture
        run: cargo build --release
      - name: Build the release version of `pgschemapc` for the aarch64 architecture
        run: cargo build --release --target aarch64-apple-darwin
      - name: Upload the generated Artifact for it to be stored in its corresponding release (x86)
        uses: actions/upload-artifact@v4
        with:
          name: ${{ vars.BINARY_NAME }}_x86_64_apple
          path: target/release/${{ vars.BINARY_NAME }}
      - name: Upload the generated Artifact for it to be stored in its corresponding release (aarch64)
        uses: actions/upload-artifact@v4
        with:
          name: ${{ vars.BINARY_NAME }}_aarch64_apple
          path: target/aarch64-apple-darwin/release/${{ vars.BINARY_NAME }}
      - name: Rename the binary according to the version and architecture
        run: mv target/release/${{ vars.BINARY_NAME }} ${{ vars.BINARY_NAME }}_${{ github.event.release.tag_name }}_x86_64_apple
        if: github.event_name == 'release'
      - name: Rename the binary according to the version and architecture
        run: mv target/aarch64-apple-darwin/release/${{ vars.BINARY_NAME }} ${{ vars.BINARY_NAME }}_${{ github.event.release.tag_name }}_aarch64_apple
        if: github.event_name == 'release'
      - name: Upload the generated binaries to their corresponding release
        uses: softprops/action-gh-release@v1
        with:
          files: |
            ${{ vars.BINARY_NAME }}_${{ github.event.release.tag_name }}_x86_64_apple
            ${{ vars.BINARY_NAME }}_${{ github.event.release.tag_name }}_aarch64_apple
        if: github.event_name == 'release'

  windows:
    runs-on: windows-latest
    steps:
      - name: Allow the workflow to access the files of the repository
        uses: actions/checkout@v3
        with:
          submodules: true
      - name: Update Rustup
        run: rustup update
      - name: Use an intelligent Rust cache for building the project
        uses: Swatinem/rust-cache@v2
      - name: Remove the MySys64 libraries and utilities
        run: Remove-Item -LiteralPath "C:\msys64\" -Force -Recurse
      - name: Build the release version of `pgschemapc` for the x86 architecture
        run: cargo build --release
      - name: Upload the generated Artifact for it to be stored in its corresponding release (x86)
        uses: actions/upload-artifact@v4
        with:
          name: ${{ vars.BINARY_NAME }}_x86_64_windows_msvc
          path: target/release/${{ vars.BINARY_NAME }}.exe
      - name: Rename the binary according to the version and architecture
        run: mv target/release/${{ vars.BINARY_NAME }}.exe ${{ vars.BINARY_NAME }}_${{ github.event.release.tag_name }}_x86_64_windows_msvc.exe
        if: github.event_name == 'release'
      - name: Upload the generated binary to its corresponding release
        uses: softprops/action-gh-release@v1
        with:
          files: ${{ vars.BINARY_NAME }}_${{ github.event.release.tag_name }}_x86_64_windows_msvc.exe
        if: github.event_name == 'release'

  deb:
    runs-on: ubuntu-latest
    steps:
    - name: Allow the workflow to access the files of the repository
      uses: actions/checkout@v3
    - name: Install cargo deb
      run: cargo install cargo-deb
    - name: Build the .deb file using cargo deb
      run: cargo deb
    - name: Upload the .deb Artifact
      uses: actions/upload-artifact@v4
      with:
        name: ${{ vars.BINARY_NAME }}
        path: target/debian/*.deb
    - name: Rename the binary according to the version and architecture
      run: mv target/debian/*.deb ${{ vars.BINARY_NAME }}_${{ github.event.release.tag_name }}_amd64.deb
    - name: Upload the generated binary to its corresponding release
      uses: softprops/action-gh-release@v1
      with:
        files: ${{ vars.BINARY_NAME }}_${{ github.event.release.tag_name }}_amd64.deb
      if: github.event_name == 'release'

 # docker:
 #   runs-on: ubuntu-latest
 #   steps:
 #     - name: Set up QEMU
 #       uses: docker/setup-qemu-action@v3
 #     - name: Set up Docker Buildx
 #       uses: docker/setup-buildx-action@v3
 #     - name: Login to Docker Hub
 #       uses: docker/login-action@v3
 #       with:
 #         username: ${{ secrets.DOCKERHUB_USERNAME }}
 #         password: ${{ secrets.DOCKERHUB_TOKEN }}
 #     - name: Build and push
 #       uses: docker/build-push-action@v6
 #       with:
 #         push: true
 #         no-cache: true
 #         tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ vars.BINARY_NAME }}:latest
