j178 / pre-commit-rs

pre-commit re-implemented in Rust
MIT License
48 stars 4 forks source link

Feature Request: implement it like `husky` #22

Open pplmx opened 2 weeks ago

pplmx commented 2 weeks ago

Proposal:

Implement a tool or mechanism for Rust projects that functions like Husky, enforcing the execution of pre-build hooks whenever a cargo command (e.g., cargo build, cargo test) is run. This ensures that contributors must run these hooks, which helps automate checks and maintain code quality.

Key Requirements:

Benefits:

Expected Outcome:

A tool or script that integrates with cargo to run specified hooks, ensuring all contributors comply with pre-defined workflows to enhance project reliability and maintain code standards.

j178 commented 2 weeks ago

Thanks for the proposal! This project is still in the early development stage, so I'll concentrate on implementing most of the pre-commit functionalities first. I'll consider your suggestion once the main functions are in place.

bxb100 commented 1 week ago

I think we can leverage runner (source: https://github.com/rust-lang/cargo/blob/e5ce5e55886a8deec088413f532e7e4d157bcabc/src/cargo/core/compiler/compilation.rs#L259C3-L282C6), and publish other binary like pre-commit-runner, set config .cargo/config.toml

[target.'cfg(all())']
runner = ['pre-commit-runner']

tree

.
└── runner
    └── main.rs

main.rs:

use clap::Parser;
use std::process::{Command, Stdio};

#[derive(Parser, Debug)]
struct RunnerCommand {
    #[arg()]
    cmd: Vec<String>,
}

fn main() {
    let cli = RunnerCommand::parse();

    // run pre-commit
    Command::new("pre-commit")
        .arg("run")
        .arg("--all-files")
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .output()
        .expect("failed to execute `pre-commit run --all-files`");

    let cmd = cli.cmd.first().unwrap();
    let mut command = Command::new(cmd);

    cli.cmd.iter().skip(1).for_each(|c| {
        command.arg(c);
    });

    command
        .stdin(Stdio::inherit())
        .stdout(Stdio::inherit())
        .stderr(Stdio::inherit())
        .output()
        .unwrap_or_else(|_| panic!("failed to execute {}", cli.cmd.join(" ")));
}