darwincode 1.9.78

The open source terminal AI coding agent
name: Rust CI & Release

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

env:
  CARGO_TERM_COLOR: always

jobs:
  build:
    runs-on: ubuntu-latest
    permissions:
      contents: write

    steps:
    - uses: actions/checkout@v4
      with:
        fetch-depth: 0

    - name: Set up Rust
      uses: dtolnay/rust-toolchain@stable
      with:
        components: rustfmt, clippy
        targets: x86_64-pc-windows-gnu

    - name: Check formatting
      run: cargo fmt --all --check

    - name: Run Clippy
      run: cargo clippy --all-targets -- -D warnings

    - name: Install MinGW (Windows cross-compilation toolchain)
      run: |
        sudo apt-get update
        sudo apt-get install -y mingw-w64

    - name: Run tests
      run: cargo test --verbose

    - name: Bump version & Release
      if: github.event_name == 'push' && github.ref == 'refs/heads/main'
      env:
        GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      run: |
        # Capture changelog and contributors from commits in this push BEFORE making the version bump commit
        if [ "${{ github.event.before }}" = "0000000000000000000000000000000000000000" ] || [ -z "${{ github.event.before }}" ]; then
          # Fallback to last 5 commits if before commit is missing
          CHANGELOG=$(git log -n 5 --pretty=format:"• %s by @%an")
          CONTRIBUTORS=$(git log -n 5 --pretty=format:"@%an" | sort -u | paste -sd ", " -)
        else
          # Get all commits in this push
          CHANGELOG=$(git log ${{ github.event.before }}..${{ github.event.after }} --pretty=format:"• %s by @%an")
          CONTRIBUTORS=$(git log ${{ github.event.before }}..${{ github.event.after }} --pretty=format:"@%an" | sort -u | paste -sd ", " -)
        fi

        # Format release notes into a temporary file
        echo -e "### Latest Changes:\n$CHANGELOG\n\n### Contributors:\n$CONTRIBUTORS" > release_notes.md
        cat release_notes.md

        # Extract current version from Cargo.toml
        VERSION=$(grep -m 1 '^version = ' Cargo.toml | cut -d '"' -f 2)
        echo "Current version: $VERSION"

        # Split version into parts
        IFS='.' read -r major minor patch <<< "$VERSION"
        new_patch=$((patch + 1))
        NEW_VERSION="$major.$minor.$new_patch"
        echo "New version: $NEW_VERSION"

        # Update Cargo.toml with the new version
        sed -i "0,/^version = .*/{s/^version = .*/version = \"$NEW_VERSION\"/}" Cargo.toml

        # Build production release binaries
        cargo build --release --verbose
        cargo build --release --target x86_64-pc-windows-gnu --verbose

        # Configure Git
        git config user.name "github-actions[bot]"
        git config user.email "github-actions[bot]@users.noreply.github.com"

        # Commit and push updated Cargo.toml back to main (using [skip ci] to avoid infinite loops)
        git add Cargo.toml
        git commit -m "chore: bump version to $NEW_VERSION [skip ci]"
        git push origin main

        # Create GitHub Release and upload the compiled binaries
        gh release create "v$NEW_VERSION" \
          target/release/darwincode \
          target/x86_64-pc-windows-gnu/release/darwincode.exe \
          --title "Release v$NEW_VERSION" \
          --notes-file release_notes.md