sastri 0.1.0

Scaffold multi-ecosystem packaging and CI/CD for Rust CLIs (Homebrew, Scoop, PyPI, npm, Hex, mise, asdf, …)
Documentation
use anyhow::Result;

use super::{GeneratedFile, Mechanism, MechanismContext, StepResult};

pub struct Ci;

impl Mechanism for Ci {
    fn id(&self) -> &'static str {
        "ci"
    }

    fn description(&self) -> &'static str {
        "PR/push CI: fmt, clippy, test matrix, llvm-cov coverage artifact"
    }

    fn generate(&self, ctx: &MechanismContext<'_>) -> Result<StepResult> {
        let manifest = &ctx.config.project.cargo_manifest;
        let pkg = &ctx.config.project.name;

        let workflow = format!(
            r#"name: CI

on:
  push:
    branches: [main]
  pull_request:

env:
  CARGO_TERM_COLOR: always

jobs:
  fmt:
    name: Format
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: rustfmt
      - run: cargo fmt --all -- --check

  clippy:
    name: Clippy
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: clippy
      - uses: Swatinem/rust-cache@v2
      - run: cargo clippy --all-targets --all-features --manifest-path {manifest} -- -D warnings

  test:
    name: Test (${{{{ matrix.os }}}})
    runs-on: ${{{{ matrix.os }}}}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, macos-latest, windows-latest]
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
      - uses: Swatinem/rust-cache@v2
      - run: cargo test --all-features --manifest-path {manifest}

  coverage:
    name: Coverage
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: dtolnay/rust-toolchain@stable
        with:
          components: llvm-tools-preview
      - uses: Swatinem/rust-cache@v2
      - uses: taiki-e/install-action@v2
        with:
          tool: cargo-llvm-cov
      - name: Generate lcov
        run: cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info --manifest-path {manifest}
      - name: Coverage summary
        run: cargo llvm-cov --all-features --workspace --summary-only --manifest-path {manifest}
      - uses: actions/upload-artifact@v4
        with:
          name: coverage-lcov
          path: lcov.info
          if-no-files-found: error
"#
        );

        let path = format!(
            "{}/ci.yml",
            ctx.config.paths.workflows_dir.trim_end_matches('/')
        );

        Ok(StepResult {
            files: vec![GeneratedFile {
                relative_path: path,
                contents: workflow,
                executable: false,
            }],
            notes: vec![
                format!("CI covers fmt, clippy, cross-OS tests, and llvm-cov for `{pkg}`."),
                "Coverage report is uploaded as the `coverage-lcov` artifact.".into(),
            ],
        })
    }
}