migrate 0.5.1

Generic file migration tool for applying ordered transformations to a project directory
Documentation
name: CI

on:
  pull_request:
    branches:
      - main
  push:
    branches:
      - main

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt, clippy

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

      - name: Install nextest
        uses: taiki-e/install-action@nextest

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: '20'

      - name: Pre-install tsx for TypeScript tests
        run: npm install -g tsx

      - name: Format check
        run: cargo fmt --check

      - name: Lint
        run: cargo clippy -- -D warnings

      - name: Run tests
        run: cargo nextest run

      - name: Build
        run: cargo build --release

  release:
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    needs: test
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.version-check.outputs.current }}
      changed: ${{ steps.version-check.outputs.changed }}
      tag_exists: ${{ steps.tag-check.outputs.exists }}
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 2

      - name: Check if version changed
        id: version-check
        run: |
          # Get current version from Cargo.toml
          CURRENT_VERSION=$(grep '^version' Cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
          echo "current=$CURRENT_VERSION" >> $GITHUB_OUTPUT

          # Get previous version from parent commit
          git show HEAD^:Cargo.toml > /tmp/old-cargo.toml 2>/dev/null || echo 'version = "0.0.0"' > /tmp/old-cargo.toml
          PREVIOUS_VERSION=$(grep '^version' /tmp/old-cargo.toml | head -1 | sed 's/.*"\(.*\)".*/\1/')
          echo "previous=$PREVIOUS_VERSION" >> $GITHUB_OUTPUT

          # Check if version changed
          if [ "$CURRENT_VERSION" != "$PREVIOUS_VERSION" ]; then
            echo "changed=true" >> $GITHUB_OUTPUT
            echo "Version changed: $PREVIOUS_VERSION -> $CURRENT_VERSION"
          else
            echo "changed=false" >> $GITHUB_OUTPUT
            echo "Version unchanged: $CURRENT_VERSION"
          fi

      - name: Check if tag exists
        if: steps.version-check.outputs.changed == 'true'
        id: tag-check
        run: |
          if git ls-remote --tags origin | grep -q "refs/tags/v${{ steps.version-check.outputs.current }}$"; then
            echo "exists=true" >> $GITHUB_OUTPUT
            echo "Tag v${{ steps.version-check.outputs.current }} already exists"
          else
            echo "exists=false" >> $GITHUB_OUTPUT
          fi

      - name: Create tag
        if: steps.version-check.outputs.changed == 'true' && steps.tag-check.outputs.exists != 'true'
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"
          git tag -a "v${{ steps.version-check.outputs.current }}" -m "Release v${{ steps.version-check.outputs.current }}"
          git push origin "v${{ steps.version-check.outputs.current }}"

  build-binaries:
    if: needs.release.outputs.changed == 'true' && needs.release.outputs.tag_exists != 'true'
    needs: release
    strategy:
      matrix:
        include:
          - target: x86_64-unknown-linux-musl
            os: ubuntu-latest
          - target: aarch64-unknown-linux-musl
            os: ubuntu-latest
          - target: x86_64-apple-darwin
            os: macos-latest
          - target: aarch64-apple-darwin
            os: macos-latest
          - target: x86_64-pc-windows-msvc
            os: windows-latest
    runs-on: ${{ matrix.os }}
    permissions:
      contents: write
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable
        with:
          targets: ${{ matrix.target }}

      - name: Install cross (Linux)
        if: runner.os == 'Linux'
        uses: taiki-e/install-action@cross

      - name: Cache cargo
        uses: Swatinem/rust-cache@v2
        with:
          key: ${{ matrix.target }}

      - name: Build (Linux)
        if: runner.os == 'Linux'
        run: cross build --release --target ${{ matrix.target }}

      - name: Build (macOS)
        if: runner.os == 'macOS'
        run: cargo build --release --target ${{ matrix.target }}

      - name: Build (Windows)
        if: runner.os == 'Windows'
        run: cargo build --release --target ${{ matrix.target }}

      - name: Prepare binary (Unix)
        if: runner.os != 'Windows'
        run: |
          cp target/${{ matrix.target }}/release/migrate migrate-${{ matrix.target }}

      - name: Prepare binary (Windows)
        if: runner.os == 'Windows'
        run: |
          cp target/${{ matrix.target }}/release/migrate.exe migrate-${{ matrix.target }}.exe

      - name: Test binary compatibility (Linux)
        if: runner.os == 'Linux' && matrix.target == 'x86_64-unknown-linux-musl'
        run: |
          # Verify the binary runs on an older system (Debian Bullseye has GLIBC 2.31)
          docker run --rm -v $PWD:/app debian:bullseye /app/migrate-${{ matrix.target }} --version

      - name: Upload to release
        uses: softprops/action-gh-release@v2
        with:
          tag_name: v${{ needs.release.outputs.version }}
          files: migrate-${{ matrix.target }}*
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

  publish-crate:
    if: needs.release.outputs.changed == 'true' && needs.release.outputs.tag_exists != 'true'
    needs: release
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Rust
        uses: dtolnay/rust-toolchain@stable

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

      - name: Publish to crates.io
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}