rloc 0.2.0

A fast, modern Rust implementation of cloc (Count Lines of Code)
Documentation
name: CI and Release

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

permissions:
  contents: write

env:
  CARGO_TERM_COLOR: always

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

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

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

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

      - name: Run Clippy (library)
        run: cargo clippy --lib --all-features -- -D warnings

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

      - name: Run tests
        run: cargo test --all-features

      - name: Build docs
        run: cargo doc --no-deps --all-features

  build:
    name: Build (${{ matrix.target }})
    needs: test
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: ubuntu-latest
            target: x86_64-unknown-linux-musl
            artifact_name: rloc
            asset_name: rloc-linux-amd64
            use_cross: true
          - os: ubuntu-latest
            target: aarch64-unknown-linux-musl
            artifact_name: rloc
            asset_name: rloc-linux-arm64
            use_cross: true
          - os: windows-latest
            target: x86_64-pc-windows-msvc
            artifact_name: rloc.exe
            asset_name: rloc-windows-amd64
            use_cross: false
          - os: windows-latest
            target: aarch64-pc-windows-msvc
            artifact_name: rloc.exe
            asset_name: rloc-windows-arm64
            use_cross: false
          - os: macos-latest
            target: x86_64-apple-darwin
            artifact_name: rloc
            asset_name: rloc-macos-amd64
            use_cross: false
          - os: macos-latest
            target: aarch64-apple-darwin
            artifact_name: rloc
            asset_name: rloc-macos-arm64
            use_cross: false

    steps:
      - uses: actions/checkout@v4

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

      - name: Install Cross
        if: matrix.use_cross == true
        run: cargo install cross --git https://github.com/cross-rs/cross --locked

      - name: Build
        if: runner.os != 'Windows'
        shell: bash
        run: |
          if [[ "${{ matrix.use_cross }}" == "true" ]]; then
            cross build --release --target ${{ matrix.target }}
          else
            cargo build --release --target ${{ matrix.target }}
          fi

      - name: Build
        if: runner.os == 'Windows'
        shell: pwsh
        run: |
          if ('${{ matrix.use_cross }}' -eq 'true') {
            cross build --release --target '${{ matrix.target }}'
          } else {
            cargo build --release --target '${{ matrix.target }}'
          }

      - name: Package binary
        shell: bash
        run: |
          echo "Packaging for OS: ${{ runner.os }}"
          cd target/${{ matrix.target }}/release
          if [ "${{ runner.os }}" == "Windows" ]; then
            7z a ../../../${{ matrix.asset_name }}.zip ${{ matrix.artifact_name }}
            echo "ASSET_PATH=${{ matrix.asset_name }}.zip" >> $GITHUB_ENV
            echo "Created ../../../${{ matrix.asset_name }}.zip"
          else
            tar -czf ../../../${{ matrix.asset_name }}.tar.gz ${{ matrix.artifact_name }}
            echo "ASSET_PATH=${{ matrix.asset_name }}.tar.gz" >> $GITHUB_ENV
            echo "Created ../../../${{ matrix.asset_name }}.tar.gz"
          fi
          ls ../../../
          cd ../../../

      - name: Upload Release Asset
        uses: actions/upload-artifact@v4
        with:
          name: ${{ matrix.asset_name }}
          path: ${{ env.ASSET_PATH }}
          if-no-files-found: error

  publish:
    name: Publish to crates.io
    if: github.event_name == 'push' && github.ref == 'refs/heads/main'
    needs: [test, build]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0

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

      - name: Check if version changed
        id: version_check
        run: |
          CURRENT_VERSION=$(grep '^version = ' Cargo.toml | sed 's/version = "\(.*\)"/\1/')
          echo "current_version=${CURRENT_VERSION}" >> $GITHUB_OUTPUT

          # Check if this version tag already exists
          if git rev-parse "v${CURRENT_VERSION}" >/dev/null 2>&1; then
            echo "Version v${CURRENT_VERSION} already released"
            echo "should_publish=false" >> $GITHUB_OUTPUT
          else
            echo "New version v${CURRENT_VERSION} detected"
            echo "should_publish=true" >> $GITHUB_OUTPUT
          fi

      - name: Publish to crates.io
        if: steps.version_check.outputs.should_publish == 'true'
        env:
          CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
        run: cargo publish

      - name: Download all release assets
        if: steps.version_check.outputs.should_publish == 'true'
        uses: actions/download-artifact@v4
        with:
          path: artifacts

      - name: List downloaded files (for debugging)
        if: steps.version_check.outputs.should_publish == 'true'
        run: |
          echo "Listing downloaded artifacts:"
          ls -R artifacts

      - name: Create GitHub Release
        if: steps.version_check.outputs.should_publish == 'true'
        uses: softprops/action-gh-release@v2
        with:
          tag_name: v${{ steps.version_check.outputs.current_version }}
          name: v${{ steps.version_check.outputs.current_version }}
          files: artifacts/*/*
          generate_release_notes: true
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}