name: Release

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

permissions:
  contents: write

jobs:
  release:
    name: Validate and release
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Install Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy

      - name: Cache cargo
        uses: Swatinem/rust-cache@v2

      - name: Fetch SysML v2 Release
        run: bash ./scripts/fetch-sysml-v2-release.sh

      - name: cargo test
        run: cargo test
        env:
          SYSML_V2_RELEASE_DIR: ${{ github.workspace }}/sysml-v2-release

      - name: cargo clippy
        run: cargo clippy -- -W clippy::all

      - name: cargo test (full validation suite)
        run: cargo test --test validation -- --include-ignored
        env:
          SYSML_V2_RELEASE_DIR: ${{ github.workspace }}/sysml-v2-release

      - name: Package crate
        run: cargo package --allow-dirty --no-verify

      - name: Publish to crates.io
        if: startsWith(github.ref, 'refs/tags/v')
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

      - name: Upload packaged crate
        uses: actions/upload-artifact@v4
        with:
          name: crate-package
          path: target/package/*.crate

      - name: Create GitHub Release
        uses: softprops/action-gh-release@v2
        if: startsWith(github.ref, 'refs/tags/')
        with:
          generate_release_notes: true
          files: target/package/*.crate
