name: Publish

on:
  push:
    tags:
    - 'v*'

jobs:
  publish:
    runs-on: ubuntu-latest
    environment:
      name: crates.io
      url: ${{ steps.set_url.outputs.env_url }}

    steps:
    - uses: actions/checkout@v4
    - name: Set up Rust
      uses: dtolnay/rust-toolchain@stable
    - name: Build
      run: cargo build --all
    - name: Check version
      id: version
      run: |
        pkg_version="$(grep '^version =' "Cargo.toml" | sed 's/version = "\(.*\)"/\1/g')"
        tag_version="$(echo "$GITHUB_REF" | sed -e "s#refs/tags/v##g")"
        if [ "$pkg_version" != "$tag_version" ]; then
          echo "Package version '$pkg_version' does not match tag version '$tag_version'!" >&2
          exit 1
        fi
        echo "version=$tag_version" >> "$GITHUB_OUTPUT"
    - name: Publish
      run: cargo publish --token ${{ secrets.CRATES_IO_TOKEN }}
    - name: Set environment url
      id: set_url
      run: |
        version="${{ steps.version.outputs.version }}"
        echo "env_url=https://crates.io/crates/arthouse/$version" >> "$GITHUB_OUTPUT"
