on:
  push:
    branches:
      - main
defaults:
  run:
    shell: bash
jobs:
  cargo-test:
    runs-on: ${{ matrix.runs-on }}
    strategy:
      matrix:
        runs-on:
          - macos-latest
          - ubuntu-latest
          - windows-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: denoland/setup-deno@v2
      - run: cargo test
  info:
    needs: cargo-test
    runs-on: macos-latest
    outputs:
      name: ${{ steps.vars.outputs.name }}
      version: ${{ steps.vars.outputs.version }}
      description: ${{ steps.vars.outputs.description }}
      published: ${{ steps.vars.outputs.published }}
    steps:
      - uses: actions/checkout@v4
      - run: brew install callum-oakley/tap/jsq
      - id: vars
        run: |
          name="$(cat Cargo.toml | jsq -t '$.package.name')"
          version="$(cat Cargo.toml | jsq -t '$.package.version')"
          description="$(cat Cargo.toml | jsq -t '$.package.description')"
          published="$(
            curl -s "https://crates.io/api/v1/crates/${name}" |
              version="${version}" jsq -j '$.versions?.map(a => a.num).includes($version)'
          )"
          echo name="${name}" | tee -a "${GITHUB_OUTPUT}"
          echo version="${version}" | tee -a "${GITHUB_OUTPUT}"
          echo description="${description}" | tee -a "${GITHUB_OUTPUT}"
          echo published="${published}" | tee -a "${GITHUB_OUTPUT}"
  release-create:
    needs: info
    if: ${{ needs.info.outputs.published != 'true' }}
    runs-on: macos-latest
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    steps:
      - uses: actions/checkout@v4
      - run: gh release create v${{ needs.info.outputs.version }} --generate-notes
  release-upload:
    needs:
      - info
      - release-create
    runs-on: ${{ matrix.runs-on }}
    env:
      GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    strategy:
      matrix:
        include:
          - os: macos
            runs-on: macos-latest
            bin: target/release/${{ needs.info.outputs.name }}
          - os: linux
            runs-on: ubuntu-latest
            bin: target/release/${{ needs.info.outputs.name }}
          - os: windows
            runs-on: windows-latest
            bin: target/release/${{ needs.info.outputs.name }}.exe
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo build --release
      - run: |
          archive='${{ needs.info.outputs.name }}-${{ needs.info.outputs.version }}-${{ matrix.os }}'
          mkdir "${archive}"
          cp ${{ matrix.bin }} "${archive}"
          tar czf "${archive}.tar.gz" "${archive}"
          gh release upload v${{ needs.info.outputs.version }} "${archive}.tar.gz"
  homebrew-publish:
    needs:
      - info
      - release-upload
    runs-on: macos-latest
    env:
      GH_TOKEN: ${{ secrets.HOMEBREW_PUBLISH_TOKEN }}
    steps:
      - run: |
          gh api repos/callum-oakley/homebrew-tap/dispatches \
            -f 'event_type=publish' \
            -f 'client_payload[name]=${{ needs.info.outputs.name }}' \
            -f 'client_payload[version]=${{ needs.info.outputs.version }}' \
            -f 'client_payload[description]=${{ needs.info.outputs.description }}' \
            -f 'client_payload[depends_on]=deno'
  cargo-publish:
    needs: info
    if: ${{ needs.info.outputs.published != 'true' }}
    runs-on: macos-latest
    env:
      CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - run: cargo publish
