name: release

on:
  workflow_dispatch:
  push:
    tags:
      - "v*"

jobs:
  release:
    runs-on: ${{ matrix.info.runs-on }}
    permissions:
      contents: write
    strategy:
      matrix:
        info:
          - os: "macOS-arm"
            runs-on: "macos-latest"
            package-extension: "tar.gz"
            executable-extension: ""
          - os: "macOS-intel"
            runs-on: "macos-26-intel"
            package-extension: "tar.gz"
            executable-extension: ""
          - os: "linux-x86_64"
            runs-on: "ubuntu-latest"
            package-extension: "tar.gz"
            executable-extension: ""
          - os: "linux-arm"
            runs-on: "ubuntu-24.04-arm"
            package-extension: "tar.gz"
            executable-extension: ""
          - os: "Windows-x86_64"
            runs-on: "windows-latest"
            package-extension: "zip"
            executable-extension: ".exe"
          - os: "Windows-arm"
            runs-on: "windows-11-arm"
            package-extension: "zip"
            executable-extension: ".exe"
    steps:
    - name: checkout
      uses: actions/checkout@v6
    - name: check version
      # cut off the v part of the tag to only search for the number
      run: grep --quiet "$(echo "${{ github.ref_name }}" | cut -c2-)" Cargo.toml 
    - name: cache
      uses: actions/cache@v5
      with:
        path: |
          ~/.cargo/registry
          ~/.cargo/git
          target
        key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
    - name: test
      run: cargo test --release
    - name: make completion/manpage folders
      run: mkdir completions; mkdir manpage
    - name: build
      run: cargo build --release --locked
      env:
        HEXAPODA_COMPLETIONS: completions
        HEXAPODA_MANPAGE: manpage
    - name: package-tar-gz
      if: ${{ matrix.info.package-extension == 'tar.gz' }}
      run: tar --create --gzip --file "hexapoda-${{ matrix.info.os }}-${{ github.ref_name }}.tar.gz" "completions" "manpage" -C "target/release/" "hexapoda${{ matrix.info.executable-extension }}"
    - name: package-zip
      if: ${{ matrix.info.package-extension == 'zip' }}
      run: tar --create --auto-compress --file "hexapoda-${{ matrix.info.os }}-${{ github.ref_name }}.zip" "completions" "manpage" -C "target/release/" "hexapoda${{ matrix.info.executable-extension }}"
    - name: release
      uses: softprops/action-gh-release@v3
      with:
        draft: true
        name: "${{ github.ref_name }}"
        files: "hexapoda-${{ matrix.info.os }}-${{ github.ref_name }}.${{ matrix.info.package-extension }}"

  publish:
    runs-on: ubuntu-latest
    needs: release
    permissions:
      id-token: write # Required for OIDC token exchange
    steps:
    - uses: actions/checkout@v6
    - name: check version
      # cut off the v part of the tag to only search for the number
      # include the " = " to not match on main, only v* tags
      run: grep -q " = \"$(echo "${{ github.ref_name }}" | cut -c2-)\"" Cargo.toml 
    - uses: rust-lang/crates-io-auth-action@v1
      id: auth
    - run: cargo publish
      env:
        CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}
