name: Release
on:
workflow_dispatch:
push:
tags:
- 'v*.*.*'
jobs:
build:
name: Build
runs-on: ${{ matrix.os }}
strategy:
matrix:
include:
- os: macos-13
target: x86_64-apple-darwin
- os: macos-latest
target: aarch64-apple-darwin
- os: ubuntu-latest
target: x86_64-unknown-linux-gnu
- os: windows-latest
target: x86_64-pc-windows-msvc
steps:
- uses: actions/checkout@v4
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- name: Cache cargo
uses: swatinem/rust-cache@v2
- name: Build release
run: cargo build --release --locked --target ${{ matrix.target }}
- name: Package (Unix)
if: runner.os != 'Windows'
shell: bash
run: |
set -euo pipefail
TARGET=${{ matrix.target }}
VERSION=$(sed -n 's/^version = \"\(.*\)\"/\1/p' Cargo.toml | head -n1)
ARCHIVE="ocloc-${VERSION}-${TARGET}.tar.gz"
tar -C "target/${TARGET}/release" -czf "$ARCHIVE" ocloc
echo "ARCHIVE=$ARCHIVE" >> "$GITHUB_ENV"
- name: Package (Windows)
if: runner.os == 'Windows'
shell: pwsh
run: |
$Target = "${{ matrix.target }}"
$Version = (Select-String -Path Cargo.toml -Pattern '^version = "(.+)"').Matches[0].Groups[1].Value
$Archive = "ocloc-$Version-$Target.zip"
$Bin = Join-Path "target/$Target/release" "ocloc.exe"
Copy-Item $Bin -Destination "ocloc.exe" -Force
Compress-Archive -Path "ocloc.exe" -DestinationPath $Archive -Force
Remove-Item "ocloc.exe"
"ARCHIVE=$Archive" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ocloc-${{ matrix.target }}
path: ${{ env.ARCHIVE }}
release:
name: Create Release and Attach Assets
needs: build
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
path: dist
merge-multiple: true
- name: Compute checksums
run: |
set -euo pipefail
cd dist
( command -v sha256sum >/dev/null && sha256sum ocloc-* > SHA256SUMS.txt ) || \
( command -v shasum >/dev/null && shasum -a 256 ocloc-* > SHA256SUMS.txt )
echo "--- SHA256SUMS ---"
cat SHA256SUMS.txt
- name: List artifacts
run: ls -l dist
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
draft: false
prerelease: false
files: |
dist/ocloc-*.tar.gz
dist/ocloc-*.zip
dist/SHA256SUMS.txt
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
update-homebrew:
name: Update Homebrew Tap
needs: release
runs-on: ubuntu-latest
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
steps:
- name: Checkout tap repo
run: |
set -e
git clone https://x-access-token:${{ secrets.TAP_TOKEN }}@github.com/${{ vars.TAP_REPO || 'adhishthite/homebrew-ocloc' }} tap
- name: Download artifacts
uses: actions/download-artifact@v4
with:
path: dist
merge-multiple: true
- name: Compute checksums
run: |
cd dist
( command -v sha256sum >/dev/null && sha256sum ocloc-* > SHA256SUMS.txt ) || \
( command -v shasum >/dev/null && shasum -a 256 ocloc-* > SHA256SUMS.txt )
cat SHA256SUMS.txt
- name: Update formula
env:
REPO_SLUG: ${{ github.repository }}
TAG: ${{ github.ref_name }}
run: |
set -euo pipefail
MAC_ARM_TARBALL=$(ls dist/ocloc-*-aarch64-apple-darwin.tar.gz | xargs -n1 basename)
MAC_X64_TARBALL=$(ls dist/ocloc-*-x86_64-apple-darwin.tar.gz | xargs -n1 basename)
MAC_ARM_SHA=$(grep "$MAC_ARM_TARBALL" dist/SHA256SUMS.txt | awk '{print $1}')
MAC_X64_SHA=$(grep "$MAC_X64_TARBALL" dist/SHA256SUMS.txt | awk '{print $1}')
VERSION="${TAG#v}"
FORMULA_DIR="tap/Formula"
mkdir -p "$FORMULA_DIR"
{
echo 'class Ocloc < Formula'
echo ' desc "Fast, reliable lines-of-code counter"'
echo " homepage \"https://github.com/${REPO_SLUG}\""
echo " version \"${VERSION}\""
echo ' license "MIT"'
echo ''
echo ' if Hardware::CPU.arm?'
echo " url \"https://github.com/${REPO_SLUG}/releases/download/${TAG}/${MAC_ARM_TARBALL}\""
echo " sha256 \"${MAC_ARM_SHA}\""
echo ' else'
echo " url \"https://github.com/${REPO_SLUG}/releases/download/${TAG}/${MAC_X64_TARBALL}\""
echo " sha256 \"${MAC_X64_SHA}\""
echo ' end'
echo ''
echo ' def install'
echo ' bin.install "ocloc"'
echo ' end'
echo ''
echo ' test do'
echo ' system "#{bin}/ocloc", "--version"'
echo ' end'
echo 'end'
} > "$FORMULA_DIR/ocloc.rb"
cd tap
git config user.name "github-actions"
git config user.email "github-actions@users.noreply.github.com"
git add Formula/ocloc.rb
git commit -m "ocloc $VERSION: update formula"
git push origin HEAD