crabular 0.7.0

A high-performance ASCII table library for Rust
Documentation
name: Release

on:
  push:
    tags:
      - "v*"

env:
  CARGO_TERM_COLOR: always

jobs:
  check-version:
    name: Check Version Consistency
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.get-version.outputs.version }}
      tag-version: ${{ steps.get-tag.outputs.version }}
    steps:
      - uses: actions/checkout@v4

      - name: Get version from Cargo.toml
        id: get-version
        run: |
          VERSION=$(grep '^version = ' Cargo.toml | head -1 | sed 's/version = "\(.*\)"/\1/')
          echo "version=$VERSION" >> $GITHUB_OUTPUT
          echo "Cargo.toml version: $VERSION"

      - name: Get version from tag
        id: get-tag
        run: |
          TAG_VERSION=${GITHUB_REF#refs/tags/v}
          echo "version=$TAG_VERSION" >> $GITHUB_OUTPUT
          echo "Tag version: $TAG_VERSION"

      - name: Verify versions match
        run: |
          if [ "${{ steps.get-version.outputs.version }}" != "${{ steps.get-tag.outputs.version }}" ]; then
            echo "Version mismatch: Cargo.toml has ${{ steps.get-version.outputs.version }}, tag has ${{ steps.get-tag.outputs.version }}"
            exit 1
          fi
          echo "Versions match: ${{ steps.get-version.outputs.version }}"

  check-release:
    name: Check if GitHub Release Exists
    permissions:
      contents: read
    runs-on: ubuntu-latest
    needs: check-version
    outputs:
      release-exists: ${{ steps.check-release.outputs.release-exists }}
    steps:
      - uses: actions/checkout@v4

      - name: Check if GitHub release exists
        id: check-release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          TAG_NAME="v${{ needs.check-version.outputs.version }}"
          echo "Checking if GitHub release for tag $TAG_NAME exists..."
          HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
            -H "Authorization: token $GITHUB_TOKEN" \
            "https://api.github.com/repos/${{ github.repository }}/releases/tags/$TAG_NAME")
          if [ "$HTTP_STATUS" = "200" ]; then
            echo "GitHub release for $TAG_NAME already exists"
            echo "release-exists=true" >> $GITHUB_OUTPUT
          else
            echo "GitHub release for $TAG_NAME does not exist"
            echo "release-exists=false" >> $GITHUB_OUTPUT
          fi

  test:
    name: Test Before Release
    runs-on: ubuntu-latest
    needs: check-version
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: 1.93
          components: rustfmt,clippy

      - name: Cache dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Run CI checks
        run: make ci

  publish:
    name: Publish to crates.io
    runs-on: ubuntu-latest
    needs: [check-version, check-release, test]
    if: needs.check-release.outputs.release-exists == 'false'
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Rust
        uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: 1.93

      - name: Cache dependencies
        uses: actions/cache@v4
        with:
          path: |
            ~/.cargo/registry
            ~/.cargo/git
            target
          key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

      - name: Check and publish crabular
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          VERSION="${{ needs.check-version.outputs.version }}"
          echo "Checking if crabular@$VERSION is already published..."
          HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -H "User-Agent: crabular-release-workflow" "https://crates.io/api/v1/crates/crabular/$VERSION")
          echo "HTTP_STATUS: $HTTP_STATUS"
          if [ "$HTTP_STATUS" = "200" ]; then
            echo "crabular@$VERSION already published, skipping"
          else
            echo "Publishing crabular@$VERSION..."
            cargo publish
            echo "Waiting for crates.io propagation..."
            sleep 30
          fi

      - name: Check and publish crabular-cli
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          VERSION="${{ needs.check-version.outputs.version }}"
          echo "Checking if crabular-cli@$VERSION is already published..."
          HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -H "User-Agent: crabular-release-workflow" "https://crates.io/api/v1/crates/crabular-cli/$VERSION")
          echo "HTTP_STATUS: $HTTP_STATUS"
          if [ "$HTTP_STATUS" = "200" ]; then
            echo "crabular-cli@$VERSION already published, skipping"
          else
            echo "Publishing crabular-cli@$VERSION..."
            cargo publish -p crabular-cli
          fi

      # crabular-wasm is a build-time crate for npm publishing only
      # It should be published to npm via wasm-pack, not to crates.io
      # See crabular-wasm/README.md for npm publishing instructions

      - name: Check and publish crabular-wasm to npm
        env:
          NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
        run: |
          VERSION="${{ needs.check-version.outputs.version }}"
          echo "Checking if crabular@$VERSION is already published to npm..."
          NPM_VERSION=$(npm view crabular@$VERSION version 2>/dev/null || echo "")
          if [ "$NPM_VERSION" = "$VERSION" ]; then
            echo "crabular@$VERSION already published to npm, skipping"
          else
            echo "Publishing crabular@$VERSION to npm..."
            cd crabular-wasm
            npm install -g wasm-pack
            # Override package name to 'crabular' for npm
            wasm-pack build --target bundler --out-name crabular
            cd pkg
            # Update package.json name to crabular
            sed -i 's/"name": "crabular-wasm"/"name": "crabular"/' package.json
            echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
            npm publish --access public
            echo "Published crabular@$VERSION to npm"
          fi

  create-release:
    name: Create GitHub Release
    permissions:
      contents: write
    runs-on: ubuntu-latest
    needs: [check-version, check-release, test, publish]
    if: |
      always() &&
      needs.check-release.outputs.release-exists == 'false' &&
      needs.check-release.result == 'success' &&
      needs.test.result == 'success' &&
      needs.publish.result == 'success'
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Verify all crates exist on crates.io
        run: |
          VERSION="${{ needs.check-version.outputs.version }}"

          echo "Verifying crabular@$VERSION exists on crates.io..."
          HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -H "User-Agent: crabular-release-workflow" "https://crates.io/api/v1/crates/crabular/$VERSION")
          echo "HTTP_STATUS: $HTTP_STATUS"
          if [ "$HTTP_STATUS" != "200" ]; then
            echo "crabular@$VERSION not found on crates.io"
            exit 1
          fi

          echo "Verifying crabular-cli@$VERSION exists on crates.io..."
          HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" -H "User-Agent: crabular-release-workflow" "https://crates.io/api/v1/crates/crabular-cli/$VERSION")
          echo "HTTP_STATUS: $HTTP_STATUS"
          if [ "$HTTP_STATUS" != "200" ]; then
            echo "crabular-cli@$VERSION not found on crates.io"
            exit 1
          fi

          # crabular-wasm is npm-only, not published to crates.io
          echo "All crates verified on crates.io"

      - name: Verify crabular exists on npm
        run: |
          VERSION="${{ needs.check-version.outputs.version }}"
          echo "Verifying crabular@$VERSION exists on npm..."
          NPM_VERSION=$(npm view crabular@$VERSION version 2>/dev/null || echo "")
          if [ "$NPM_VERSION" != "$VERSION" ]; then
            echo "crabular@$VERSION not found on npm"
            exit 1
          fi
          echo "crabular@$VERSION verified on npm"

      - name: Generate changelog
        id: changelog
        run: |
          VERSION="${{ needs.check-version.outputs.version }}"
          echo "Generating changelog for version $VERSION..."

          # Try to extract from CHANGELOG.md
          if [ -f CHANGELOG.md ]; then
            awk "/^## \[$VERSION\]/{flag=1; next} /^## \[/{flag=0} flag" CHANGELOG.md > changelog.txt
            if [ -s changelog.txt ]; then
              echo "Extracted changelog from CHANGELOG.md"
              echo "found-changelog=true" >> $GITHUB_OUTPUT
              exit 0
            fi
          fi

          # Fallback to git log
          echo "No CHANGELOG.md entry found for $VERSION, using git log"
          PREV_TAG=$(git tag --sort=-version:refname | grep -v "^${{ github.ref_name }}$" | head -1)

          if [ -z "$PREV_TAG" ]; then
            echo "No previous tag found, using all commits"
            git log --pretty=format:"- %s (%h)" > changelog.txt
          else
            echo "Previous tag: $PREV_TAG"
            git log --pretty=format:"- %s (%h)" $PREV_TAG..${{ github.ref_name }} > changelog.txt
          fi

          echo "Generated changelog with $(wc -l < changelog.txt) entries"
          echo "found-changelog=true" >> $GITHUB_OUTPUT

      - name: Create Release
        if: steps.changelog.outputs.found-changelog == 'true'
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          PRERELEASE_FLAG=""
          if [[ "${{ needs.check-version.outputs.version }}" == *"-"* ]]; then
            PRERELEASE_FLAG="--prerelease"
          fi

          gh release create "${{ github.ref_name }}" \
            --title "Release ${{ needs.check-version.outputs.version }}" \
            --notes-file changelog.txt \
            $PRERELEASE_FLAG