sabiql 2.0.0

A fast, driver-less TUI for browsing and editing PostgreSQL and SQLite databases
name: Release

on:
  push:
    tags:
      - 'v*'

jobs:
  support-target-check:
    name: Check ${{ matrix.target }}
    runs-on: ubuntu-latest
    permissions:
      contents: read
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: aarch64-linux-android

    steps:
      - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
        with:
          persist-credentials: false

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@631a55b12751854ce901bb631d5902ceb48146f7 # stable

      - name: Add target
        run: rustup target add ${{ matrix.target }}

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          key: support-${{ matrix.target }}

      - name: Check target
        run: cargo check --workspace --target ${{ matrix.target }}

  build:
    name: Build ${{ matrix.target }}
    needs: support-target-check
    runs-on: ${{ matrix.os }}
    permissions:
      contents: read
    strategy:
      fail-fast: false
      matrix:
        include:
          - target: x86_64-apple-darwin
            os: macos-latest
            archive: tar.gz
          - target: aarch64-apple-darwin
            os: macos-latest
            archive: tar.gz
          - target: x86_64-unknown-linux-gnu
            os: ubuntu-latest
            archive: tar.gz
          - target: aarch64-unknown-linux-gnu
            os: ubuntu-latest
            archive: tar.gz
          - target: x86_64-pc-windows-msvc
            os: windows-latest
            archive: zip

    steps:
      - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
        with:
          persist-credentials: false

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@631a55b12751854ce901bb631d5902ceb48146f7 # stable
        with:
          targets: ${{ matrix.target }}

      - name: Add target
        run: rustup target add ${{ matrix.target }}

      - name: Install cross-compilation tools (Linux ARM64)
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          sudo apt-get update
          sudo apt-get install -y gcc-aarch64-linux-gnu

      - name: Configure cross-compilation (Linux ARM64)
        if: matrix.target == 'aarch64-unknown-linux-gnu'
        run: |
          echo '[target.aarch64-unknown-linux-gnu]' >> ~/.cargo/config.toml
          echo 'linker = "aarch64-linux-gnu-gcc"' >> ~/.cargo/config.toml

      - uses: Swatinem/rust-cache@c19371144df3bb44fab255c43d04cbc2ab54d1c4 # v2.9.1
        with:
          key: ${{ matrix.target }}

      - name: Build
        run: cargo build --release --features self-update --target ${{ matrix.target }}

      - name: Package (Unix)
        if: matrix.archive == 'tar.gz'
        run: |
          cd target/${{ matrix.target }}/release
          tar czvf ../../../sabiql-${{ matrix.target }}.tar.gz sabiql
          cd ../../..

      - name: Package (Windows)
        if: matrix.archive == 'zip'
        shell: pwsh
        run: |
          cd target/${{ matrix.target }}/release
          Compress-Archive -Path sabiql.exe -DestinationPath ../../../sabiql-${{ matrix.target }}.zip
          cd ../../..

      - name: Generate checksum (Unix)
        if: runner.os != 'Windows'
        run: |
          if [ "${{ matrix.archive }}" = "tar.gz" ]; then
            shasum -a 256 sabiql-${{ matrix.target }}.tar.gz > sabiql-${{ matrix.target }}.tar.gz.sha256
          fi

      - name: Generate checksum (Windows)
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          $hash = (Get-FileHash -Algorithm SHA256 sabiql-${{ matrix.target }}.zip).Hash.ToLower()
          "$hash  sabiql-${{ matrix.target }}.zip" | Out-File -Encoding ASCII sabiql-${{ matrix.target }}.zip.sha256

      - name: Upload artifact
        uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
        with:
          name: sabiql-${{ matrix.target }}
          path: |
            sabiql-${{ matrix.target }}.${{ matrix.archive }}
            sabiql-${{ matrix.target }}.${{ matrix.archive }}.sha256

  release:
    name: Create Release
    needs: build
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
        with:
          persist-credentials: false

      - name: Download all artifacts
        uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
        with:
          path: artifacts

      - name: Upload assets and publish release
        uses: softprops/action-gh-release@3bb12739c298aeb8a4eeaf626c5b8d85266b0e65 # v2
        with:
          files: |
            artifacts/**/*.tar.gz
            artifacts/**/*.zip
            artifacts/**/*.sha256
          draft: false

  publish-crate:
    name: Publish to crates.io
    needs: release
    runs-on: ubuntu-latest
    permissions:
      contents: read
    steps:
      - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0
        with:
          persist-credentials: false

      - name: Install Rust toolchain
        uses: dtolnay/rust-toolchain@631a55b12751854ce901bb631d5902ceb48146f7 # stable

      - name: Check publishable crate order
        run: bash ./scripts/check-publish-order.sh > /dev/null

      - name: Publish workspace crates to crates.io
        run: |
          while IFS= read -r crate; do
            cargo publish --manifest-path "$crate/Cargo.toml"
            if [ "$crate" != "." ]; then
              sleep 30
            fi
          done < <(bash ./scripts/check-publish-order.sh)
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Verify published crates independently
        run: |
          sleep 30
          while IFS= read -r crate; do
            target_dir="$RUNNER_TEMP/package-${crate//\//-}"
            CARGO_TARGET_DIR="$target_dir" cargo package \
              --manifest-path "$crate/Cargo.toml" \
              --all-features \
              --locked
          done < <(bash ./scripts/check-publish-order.sh)