2
0

main.rs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //! Repository maintenance. Run with `cargo xtask <command>`.
  2. mod policy;
  3. use std::path::{Path, PathBuf};
  4. use std::process::Command;
  5. use anyhow::{Context, Result};
  6. use clap::{Parser, Subcommand};
  7. #[derive(Parser)]
  8. #[command(name = "xtask")]
  9. struct Cli {
  10. #[command(subcommand)]
  11. command: Cmd,
  12. }
  13. #[derive(Subcommand)]
  14. enum Cmd {
  15. /// Enforce the asset policy: the repository holds only small text files
  16. /// we wrote. Checks tracked and untracked-but-not-ignored files.
  17. Policy {
  18. /// Check only what is staged for commit (used by the pre-commit hook).
  19. #[arg(long)]
  20. staged: bool,
  21. },
  22. /// Point git at .githooks/ so every commit runs the policy check.
  23. InstallHooks,
  24. }
  25. fn main() -> Result<()> {
  26. let root = workspace_root();
  27. match Cli::parse().command {
  28. Cmd::Policy { staged } => policy::run(&root, staged),
  29. Cmd::InstallHooks => {
  30. let status = Command::new("git")
  31. .args(["config", "core.hooksPath", ".githooks"])
  32. .current_dir(&root)
  33. .status()
  34. .context("running git")?;
  35. anyhow::ensure!(
  36. status.success(),
  37. "git config failed (is this a git repository?)"
  38. );
  39. println!("hooks installed: commits now run `cargo xtask policy --staged`");
  40. Ok(())
  41. }
  42. }
  43. }
  44. fn workspace_root() -> PathBuf {
  45. Path::new(env!("CARGO_MANIFEST_DIR"))
  46. .parent()
  47. .expect("xtask lives in the workspace")
  48. .into()
  49. }