name: Release

on:
  workflow_dispatch:
    inputs:
      type:
        description: "version bump type"
        default: "patch"
        required: true
        type: choice
        options:
          - patch
          - minor
          - major

env:
  CARGO_TERM_COLOR: always

jobs:
  #todo: cargo test beforehand

  bump:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
    - uses: actions/checkout@v4
    - name: Config git
      run: git config user.name github_actions && git config user.email nil
    - name: Install cargo-workspaces
      run: cargo install cargo-workspaces
    - name: BUMP!
      run: cargo ws version ${{inputs.type}} -y

  binary:
    runs-on: ${{matrix.os}}
    needs: bump
    strategy:
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    env:
      CARGO_TARGET_DIR: ./out
    steps:
      - uses: actions/checkout@v4
      - name: Get latest commit
        run: git pull
      - name: Build binary
        run: cargo build --release --features native-release
      - name: Upload artifact
        uses: actions/upload-artifact@v7.0.1
        with:
          name: ${{matrix.os}}
          path: ${{env.CARGO_TARGET_DIR}}
  
  publish:
    runs-on: ubuntu-latest
    needs: bump
    permissions:
      id-token: write
    steps:
    - uses: actions/checkout@v4
    - name: Get latest commit
      run: git pull
    - name: Authenticate to crates.io
      uses: rust-lang/crates-io-auth-action@v1.0.5
      id: auth
    - name: Publish to crates.io
      run: cargo publish --workspace
      env:
        CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}