pkg-checker 0.6.6

A Rust tool for checking and updating globally installed Cargo packages with interactive mode and smart prerelease detection
name: Publish to Crates.io

on:
  push:
    tags:
      - "v*" # 当推送以 v 开头的标签时触发

jobs:
  publish:
    name: Publish to Crates.io
    runs-on: ubuntu-latest
    environment: release # Optional: for enhanced security
    permissions:
      id-token: write # Required for OIDC token exchange
      contents: read # Required for reading repository contents

    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          fetch-depth: 0 # 获取完整的 git 历史

      - name: Setup Git
        run: |
          git config --global user.name "GitHub Actions"
          git config --global user.email "actions@github.com"

      - name: Generate changelog
        id: changelog
        run: |
          # 获取当前标签
          CURRENT_TAG=${GITHUB_REF#refs/tags/}
          echo "current_tag=$CURRENT_TAG" >> $GITHUB_OUTPUT

          # 获取上一个标签
          PREVIOUS_TAG=$(git describe --tags --abbrev=0 HEAD~1 2>/dev/null || echo "")
          echo "previous_tag=$PREVIOUS_TAG" >> $GITHUB_OUTPUT

          # 生成 changelog
          if [ -n "$PREVIOUS_TAG" ]; then
            echo "## 🎉 新功能" > release_notes.md
            echo "" >> release_notes.md
            echo "### 📋 更新内容" >> release_notes.md
            echo "" >> release_notes.md
            
            # 获取提交信息并格式化
            git log --pretty=format:"- %s" $PREVIOUS_TAG..HEAD | \
            sed 's/^feat: /✨ /' | \
            sed 's/^fix: /🐛 /' | \
            sed 's/^chore: /🔧 /' | \
            sed 's/^refactor: /♻️ /' | \
            sed 's/^docs: /📚 /' | \
            sed 's/^style: /💄 /' | \
            sed 's/^test: /🧪 /' | \
            sed 's/^perf: /⚡ /' | \
            sed 's/^ci: /👷 /' | \
            sed 's/^build: /📦 /' | \
            sed 's/^revert: /⏪ /' >> release_notes.md
            
            echo "" >> release_notes.md
            echo "### 🔗 相关链接" >> release_notes.md
            echo "- [GitHub Repository](https://github.com/jenkinpan/pkg-checker-rs)" >> release_notes.md
            echo "- [Crates.io](https://crates.io/crates/pkg-checker)" >> release_notes.md
            echo "- [Documentation](https://docs.rs/pkg-checker)" >> release_notes.md
            echo "" >> release_notes.md
            echo "## 📦 安装" >> release_notes.md
            echo "" >> release_notes.md
            echo "\`\`\`bash" >> release_notes.md
            echo "cargo install pkg-checker" >> release_notes.md
            echo "\`\`\`" >> release_notes.md
          else
            echo "## 🎉 首次发布" > release_notes.md
            echo "" >> release_notes.md
            echo "pkg-checker 是一个用 Rust 编写的工具,用于检查和管理全局安装的 Cargo 包更新。" >> release_notes.md
            echo "" >> release_notes.md
            echo "### ✨ 主要功能" >> release_notes.md
            echo "- 🔍 自动检测已安装的全局 Cargo 包" >> release_notes.md
            echo "- 📦 检查每个包的最新版本" >> release_notes.md
            echo "- 🎨 彩色输出,清晰显示更新状态" >> release_notes.md
            echo "- ⚡ 异步处理,快速检查多个包" >> release_notes.md
            echo "- 🔄 默认交互式更新模式,一键更新包" >> release_notes.md
            echo "- 🧠 智能预发布版本检测和询问" >> release_notes.md
            echo "- 📋 详细的更新摘要功能" >> release_notes.md
            echo "" >> release_notes.md
            echo "## 📦 安装" >> release_notes.md
            echo "" >> release_notes.md
            echo "\`\`\`bash" >> release_notes.md
            echo "cargo install pkg-checker" >> release_notes.md
            echo "\`\`\`" >> release_notes.md
            echo "" >> release_notes.md
            echo "## 🔗 相关链接" >> release_notes.md
            echo "- [GitHub Repository](https://github.com/jenkinpan/pkg-checker-rs)" >> release_notes.md
            echo "- [Crates.io](https://crates.io/crates/pkg-checker)" >> release_notes.md
            echo "- [Documentation](https://docs.rs/pkg-checker)" >> release_notes.md
          fi

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

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

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

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

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

      - name: Run clippy
        run: cargo clippy --workspace -- -D warnings

      - name: Clean working directory
        run: |
          # 清理临时文件
          rm -f release_notes.md
          # 确保工作目录干净
          git status --porcelain

      - name: Publish to Crates.io
        uses: rust-lang/crates-io-auth-action@v1
        id: auth

      - name: Publish to Crates.io
        run: cargo publish
        env:
          CARGO_REGISTRY_TOKEN: ${{ steps.auth.outputs.token }}

      - name: Upload release notes
        run: |
          echo "Crates.io publish completed successfully!"
          echo "Tag: ${{ steps.changelog.outputs.current_tag }}"
          echo "Previous tag: ${{ steps.changelog.outputs.previous_tag }}"
          echo ""
          echo "Package published to crates.io: https://crates.io/crates/pkg-checker"