name: Release

on:
  push:
    tags:
      - "v*"

# Make sure release jobs are only ever run once at a time (and are never
# cancelled when new jobs for the same group are queued).
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}

jobs:
  check-versions:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v6
      - name: Check versions
        run: |
          set -euo pipefail

          export RELEASE_VERSION="${GITHUB_REF#refs/*/v}"
          export CRATE_VERSION="$(cargo metadata --no-deps --format-version 1 | jq -r .packages[0].version)"
          export CODEMIRROR_VERSION="$(jq -r .version packages/mpl-codemirror/package.json)"
          if [ "$CRATE_VERSION" != "$RELEASE_VERSION" ]; then
            echo "Version mismatch! Release is at $RELEASE_VERSION, crate is at $CRATE_VERSION"
            exit 1
          elif [ "$CODEMIRROR_VERSION" != "$RELEASE_VERSION" ]; then
            echo "Version mismatch! Release is at $RELEASE_VERSION, mpl-codemirror is at $CRATE_VERSION"
            exit 1
          fi
  publish-npm:
    runs-on: ubuntu-latest
    needs: [check-versions]
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
      - name: Setup Node.js
        uses: actions/setup-node@v6
        with:
          node-version: "24"
          cache: "npm"
          cache-dependency-path: packages/mpl-codemirror/package-lock.json
          registry-url: https://registry.npmjs.org
          scope: "@axiomhq"
      - name: Install wasm-pack
        run: cargo install wasm-pack --locked
      - name: Build WASM
        run: wasm-pack build --out-name mpl --out-dir packages/mpl --scope axiomhq --target web --profile wasm-release --no-opt --features wasm 
      - name: Update mpl package.json
        run: |
          set -euo pipefail

          jq '
            .name = "@axiomhq/mpl"
            | .files = ["mpl.js", "mpl.d.ts", "mpl_bg.wasm", "mpl_bg.wasm.d.ts", "LICENSE-MIT", "LICENSE-APACHE"]
            | .repository.url = "git+https://github.com/axiomhq/mpl.git"
          ' packages/mpl/package.json > packages/mpl/package.json.tmp
          mv packages/mpl/package.json.tmp packages/mpl/package.json
      - name: Type-check WASM bindings
        working-directory: packages/mpl
        run: |
          npm install --no-save typescript
          npx tsc --noEmit -t esnext mpl.d.ts
      - name: Build mpl-codemirror
        working-directory: packages/mpl-codemirror
        run: npm install && npm run build
      - name: Publish mpl to npmjs.org
        working-directory: packages/mpl
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTOMATION_TOKEN }}
        run: npm publish --access public
      - name: Publish mpl-codemirror to npmjs.org
        working-directory: packages/mpl-codemirror
        env:
          NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTOMATION_TOKEN }}
        run: npm publish --access public
  publish-crates-io:
    runs-on: ubuntu-latest
    needs: [check-versions]
    steps:
      - uses: actions/checkout@v6
      - uses: actions-rust-lang/setup-rust-toolchain@v1
      - run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}

