pokeys-lib 1.0.4

Pure Rust core library for PoKeys device control - USB/Network connectivity, I/O, PWM, encoders, SPI/I2C protocols
Documentation
name: Release

on:
  push:
    branches: [main]
    paths-ignore:
      - 'docs/**'
      - '*.md'
      - '.github/workflows/docs.yml'

permissions:
  contents: write
  pull-requests: write

jobs:
  release:
    runs-on: ubuntu-latest
    if: "!contains(github.event.head_commit.message, 'chore(release)')"
    steps:
      - uses: actions/checkout@v6
        with:
          fetch-depth: 0
          token: ${{ secrets.GITHUB_TOKEN }}

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

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

      - name: Determine version bump
        id: version
        run: |
          # Get commits since last tag
          LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
          if [ -z "$LAST_TAG" ]; then
            COMMITS=$(git log --oneline)
            LAST_TAG="v0.0.0"
          else
            COMMITS=$(git log ${LAST_TAG}..HEAD --oneline)
          fi

          echo "Last tag: $LAST_TAG"
          echo "Commits since last tag:"
          echo "$COMMITS"

          # Determine bump type based on conventional commits
          BUMP="patch"
          if echo "$COMMITS" | grep -q "^[a-f0-9]* feat"; then
            BUMP="minor"
          fi
          if echo "$COMMITS" | grep -q "BREAKING CHANGE\|^[a-f0-9]* feat!"; then
            BUMP="major"
          fi

          # Get current version from Cargo.toml
          CURRENT_VERSION=$(grep '^version = ' Cargo.toml | sed 's/version = "\(.*\)"/\1/')

          # Calculate new version
          IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION"
          case $BUMP in
            major) NEW_VERSION="$((MAJOR + 1)).0.0" ;;
            minor) NEW_VERSION="$MAJOR.$((MINOR + 1)).0" ;;
            patch) NEW_VERSION="$MAJOR.$MINOR.$((PATCH + 1))" ;;
          esac

          echo "Current version: $CURRENT_VERSION"
          echo "Bump type: $BUMP"
          echo "New version: $NEW_VERSION"

          echo "bump=$BUMP" >> $GITHUB_OUTPUT
          echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
          echo "current=$CURRENT_VERSION" >> $GITHUB_OUTPUT
          echo "last_tag=$LAST_TAG" >> $GITHUB_OUTPUT

      - name: Check if release needed
        run: |
          if [ "${{ steps.version.outputs.version }}" = "${{ steps.version.outputs.current }}" ]; then
            echo "No version bump needed. Current version: ${{ steps.version.outputs.current }}"
            echo "No commits found that require a release."
            exit 0
          else
            echo "Version bump needed: ${{ steps.version.outputs.current }} -> ${{ steps.version.outputs.version }}"
          fi

      - name: Generate changelog
        if: steps.version.outputs.version != steps.version.outputs.current
        id: changelog
        run: |
          LAST_TAG="${{ steps.version.outputs.last_tag }}"
          NEW_VERSION="${{ steps.version.outputs.version }}"

          # Generate changelog from commits
          CHANGELOG=$(git log ${LAST_TAG}..HEAD --pretty=format:"- %s" --grep="^feat\|^fix\|^perf\|^refactor" | sed 's/^- \([a-f0-9]*\) /- /')

          # Create release notes
          cat > release_notes.md << EOF
          ## What's Changed

          $CHANGELOG

          **Full Changelog**: https://github.com/${{ github.repository }}/compare/${LAST_TAG}...v${NEW_VERSION}
          EOF

          # Update CHANGELOG.md
          if [ ! -f CHANGELOG.md ]; then
            echo "# Changelog" > CHANGELOG.md
            echo "" >> CHANGELOG.md
            echo "All notable changes to this project will be documented in this file." >> CHANGELOG.md
            echo "" >> CHANGELOG.md
          fi

          # Prepend new version to changelog
          {
            head -n 4 CHANGELOG.md
            echo "## [${NEW_VERSION}] - $(date +%Y-%m-%d)"
            echo ""
            echo "$CHANGELOG"
            echo ""
            tail -n +5 CHANGELOG.md
          } > CHANGELOG.tmp && mv CHANGELOG.tmp CHANGELOG.md

      - name: Update version
        if: steps.version.outputs.version != steps.version.outputs.current
        run: |
          sed -i 's/^version = ".*"/version = "${{ steps.version.outputs.version }}"/' Cargo.toml
          cargo check # Update Cargo.lock

      - name: Run tests
        if: steps.version.outputs.version != steps.version.outputs.current
        run: |
          cargo test --verbose --lib --bins --tests
          cargo test --doc

      - name: Run clippy
        if: steps.version.outputs.version != steps.version.outputs.current
        run: cargo clippy --lib --bins --tests -- -D warnings

      - name: Check formatting
        if: steps.version.outputs.version != steps.version.outputs.current
        run: cargo fmt --check

      - name: Commit version bump
        if: steps.version.outputs.version != steps.version.outputs.current
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action"
          git add Cargo.toml Cargo.lock CHANGELOG.md
          git commit -m "chore(release): bump version to ${{ steps.version.outputs.version }}"
          git tag -a "v${{ steps.version.outputs.version }}" -m "Release v${{ steps.version.outputs.version }}"
          git push origin main
          git push origin "v${{ steps.version.outputs.version }}"

      - name: Publish to crates.io
        if: steps.version.outputs.version != steps.version.outputs.current
        run: cargo publish --allow-dirty --token ${{ secrets.CRATES_IO_TOKEN }}

      - name: Create GitHub Release
        if: steps.version.outputs.version != steps.version.outputs.current
        uses: softprops/action-gh-release@v3
        with:
          tag_name: v${{ steps.version.outputs.version }}
          body_path: release_notes.md
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}