labstream 0.1.0

The Lab Streaming Layer, for Rust programs.
Documentation
name: Release

# A push of a tag that starts with `v` makes the whole release: it checks the
# tag against the manifest, publishes to crates.io, and writes the GitHub
# release.
#
# `cargo publish` cannot be undone. The `verify` job therefore runs first, and
# every later job waits for it.
on:
  push:
    tags:
      - "v*"

permissions:
  # `id-token` lets crates.io trust this workflow without a stored token.
  # `contents` lets the last job write the release.
  id-token: write
  contents: write

env:
  CARGO_TERM_COLOR: always

jobs:
  verify:
    name: Verify the tag
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy
      - uses: Swatinem/rust-cache@v2

      # A tag that does not match the manifest would publish one version under
      # the name of another. crates.io keeps that mistake for ever.
      - name: The tag must match the manifest
        run: |
          tag="${GITHUB_REF_NAME#v}"
          manifest=$(python3 -c \
            "import tomllib; print(tomllib.load(open('Cargo.toml','rb'))['package']['version'])")
          echo "tag $tag, manifest $manifest"
          if [ "$tag" != "$manifest" ]; then
            echo "::error::the tag says $tag and the manifest says $manifest"
            exit 1
          fi

      # A release with no entry tells a reader nothing about what changed.
      - name: The changelog must hold the version
        run: |
          tag="${GITHUB_REF_NAME#v}"
          if ! grep -q "^## \[$tag\]" CHANGELOG.md; then
            echo "::error::CHANGELOG.md holds no section for $tag"
            exit 1
          fi

      # A patch reaches no reader of the crate, so a patch left in the manifest
      # would publish a crate that was never built against what it names.
      - name: The manifest must hold no patch
        run: |
          if grep -q "^\[patch" Cargo.toml; then
            echo "::error::Cargo.toml holds a live [patch] section"
            exit 1
          fi

      - run: cargo fmt --all --check
      - run: cargo clippy --all-targets
        env:
          RUSTFLAGS: -D warnings
      - run: cargo test

  publish:
    name: Publish to crates.io
    needs: verify
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2

      # This exchanges the token of this workflow for one that lasts 30
      # minutes. crates.io must hold a trusted publisher for this crate.
      - uses: rust-lang/crates-io-auth-action@v1
        id: auth
      - run: cargo publish --token ${{ steps.auth.outputs.token }}

  release:
    name: Write the GitHub release
    needs: publish
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      # The notes are the section of CHANGELOG.md for this version. One text
      # then holds the release, and no person writes it twice.
      - name: Take the notes from the changelog
        run: |
          python3 - "$GITHUB_REF_NAME" <<'EOF' > notes.md
          import re, sys
          tag = sys.argv[1].lstrip("v")
          text = open("CHANGELOG.md").read()
          start = text.index(f"## [{tag}]")
          body = text[start:].split("\n", 1)[1]
          end = re.search(r"^## \[", body, re.M)
          print((body[: end.start()] if end else body).strip())
          print("\n## Install\n\n```sh\ncargo add labstream\n```\n")
          EOF

      - name: Write the release
        env:
          GH_TOKEN: ${{ github.token }}
        run: |
          gh release create "$GITHUB_REF_NAME" \
            --title "$GITHUB_REF_NAME" \
            --notes-file notes.md \
            --verify-tag