fluent-test 0.4.3

A fluent, Jest-like testing library for Rust
Documentation
name: Publish to crates.io

on:
  # This workflow runs after the CI workflow completes successfully
  workflow_run:
    workflows: ["CI"]
    branches: [master]
    types:
      - completed

jobs:
  publish:
    name: Publish to crates.io
    # Only run if the CI workflow completed successfully
    if: ${{ github.event.workflow_run.conclusion == 'success' }}
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0 # Fetch all history for tags
          token: ${{ secrets.GH_PAT }} # Use a personal access token with repo scope
      
      - name: Set up Rust
        uses: dtolnay/rust-toolchain@stable
      
      - name: Cache dependencies
        uses: Swatinem/rust-cache@v2
      
      - name: Get versions from Cargo.toml files
        id: get_version
        run: |
          MAIN_VERSION=$(grep '^version =' Cargo.toml | sed 's/version = "\(.*\)"/\1/')
          MACROS_VERSION=$(grep '^version =' fluent-test-macros/Cargo.toml | sed 's/version = "\(.*\)"/\1/')
          
          echo "local_version=$MAIN_VERSION" >> $GITHUB_OUTPUT
          echo "macros_version=$MACROS_VERSION" >> $GITHUB_OUTPUT
          echo "Main crate version: $MAIN_VERSION"
          echo "Macros crate version: $MACROS_VERSION"
          
          # Verify versions match
          if [ "$MAIN_VERSION" != "$MACROS_VERSION" ]; then
            echo "Warning: Main crate version ($MAIN_VERSION) does not match macros crate version ($MACROS_VERSION)"
          fi
      
      - name: Check latest git tag
        id: check_tag
        run: |
          # Get all version tags and sort them by version
          LATEST_TAG=$(git tag -l "v*" | sort -V | tail -n1 || echo "v0.0.0")
          LATEST_TAG_VERSION=${LATEST_TAG#v} # Remove 'v' prefix
          
          echo "latest_tag=$LATEST_TAG" >> $GITHUB_OUTPUT
          echo "latest_version=$LATEST_TAG_VERSION" >> $GITHUB_OUTPUT
          echo "Latest tag: $LATEST_TAG (version: $LATEST_TAG_VERSION)"
      
      - name: Compare versions
        id: version_check
        run: |
          LOCAL_VERSION="${{ steps.get_version.outputs.local_version }}"
          LATEST_VERSION="${{ steps.check_tag.outputs.latest_version }}"
          
          # Compare versions using sort -V (handles semantic versioning)
          if [ "$(printf '%s\n' "$LATEST_VERSION" "$LOCAL_VERSION" | sort -V | head -n1)" != "$LOCAL_VERSION" ] && 
             [ "$LOCAL_VERSION" != "$LATEST_VERSION" ]; then
            echo "newer_version=true" >> $GITHUB_OUTPUT
            echo "Local version $LOCAL_VERSION is newer than latest tagged version $LATEST_VERSION"
          else
            echo "newer_version=false" >> $GITHUB_OUTPUT
            echo "Local version $LOCAL_VERSION is not newer than latest tagged version $LATEST_VERSION"
          fi
      
      - name: Tag new version
        if: steps.version_check.outputs.newer_version == 'true'
        env:
          GH_TOKEN: ${{ secrets.GH_PAT }}
        run: |
          TAG_NAME="v${{ steps.get_version.outputs.local_version }}"
          git config user.name "GitHub Actions"
          git config user.email "actions@github.com"
          git tag -a "$TAG_NAME" -m "Version ${{ steps.get_version.outputs.local_version }}"
          git push origin "$TAG_NAME"
          echo "Created and pushed tag $TAG_NAME"
      
      - name: Publish to crates.io
        if: steps.version_check.outputs.newer_version == 'true'
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: |
          echo "Publishing version ${{ steps.get_version.outputs.local_version }} to crates.io..."
          
          # First publish the macros crate
          echo "Publishing fluent-test-macros crate first..."
          cd fluent-test-macros
          cargo publish --no-verify
          echo "Successfully published fluent-test-macros to crates.io"
          
          # Wait a bit for crates.io to index the published crate
          echo "Waiting for crates.io to index the macros crate..."
          sleep 30
          
          # Then publish the main crate
          cd ..
          echo "Publishing main fluent-test crate..."
          cargo publish --no-verify
          echo "Successfully published fluent-test version ${{ steps.get_version.outputs.local_version }} to crates.io"
      
      - name: No publish needed
        if: steps.version_check.outputs.newer_version != 'true'
        run: |
          echo "Version ${{ steps.get_version.outputs.local_version }} is not newer than the latest tagged version ${{ steps.check_tag.outputs.latest_version }}"
          echo "No publishing required."