|
| 1 | +use clap::Parser; |
| 2 | +use derive_more::Display; |
| 3 | +use pipe_trait::Pipe; |
| 4 | +use std::{ |
| 5 | + fmt, |
| 6 | + fs::{read_to_string, File}, |
| 7 | + io::{self, Write}, |
| 8 | + path::{Path, PathBuf}, |
| 9 | + process::ExitCode, |
| 10 | +}; |
| 11 | + |
| 12 | +const SHARED: &str = include_str!("../template/ai-instructions/shared.md"); |
| 13 | +const CLAUDE: &str = include_str!("../template/ai-instructions/claude.md"); |
| 14 | +const COPILOT: &str = include_str!("../template/ai-instructions/copilot.md"); |
| 15 | +const AGENTS: &str = include_str!("../template/ai-instructions/agents.md"); |
| 16 | + |
| 17 | +#[derive(Clone, Copy)] |
| 18 | +struct Fragments(&'static [&'static str]); |
| 19 | + |
| 20 | +impl fmt::Display for Fragments { |
| 21 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 22 | + let Fragments(fragments) = self; |
| 23 | + for fragment in *fragments { |
| 24 | + f.write_str(fragment)?; |
| 25 | + } |
| 26 | + Ok(()) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl Fragments { |
| 31 | + fn matches(&self, actual: &str) -> bool { |
| 32 | + let Fragments(fragments) = self; |
| 33 | + let mut remaining = actual; |
| 34 | + for fragment in *fragments { |
| 35 | + match remaining.strip_prefix(fragment) { |
| 36 | + Some(rest) => remaining = rest, |
| 37 | + None => return false, |
| 38 | + } |
| 39 | + } |
| 40 | + remaining.is_empty() |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +const FILES: &[(&str, Fragments)] = &[ |
| 45 | + ("CLAUDE.md", Fragments(&[SHARED, CLAUDE])), |
| 46 | + ( |
| 47 | + ".github/copilot-instructions.md", |
| 48 | + Fragments(&[SHARED, COPILOT]), |
| 49 | + ), |
| 50 | + ("AGENTS.md", Fragments(&[SHARED, AGENTS])), |
| 51 | +]; |
| 52 | + |
| 53 | +#[derive(Debug, Display)] |
| 54 | +enum RuntimeError { |
| 55 | + #[display("Failed to write {path}: {error}")] |
| 56 | + WriteFile { |
| 57 | + path: &'static str, |
| 58 | + error: io::Error, |
| 59 | + }, |
| 60 | + #[display("Failed to read {path}: {error}")] |
| 61 | + ReadFile { |
| 62 | + path: &'static str, |
| 63 | + error: io::Error, |
| 64 | + }, |
| 65 | + #[display("Some AI instruction files were outdated.")] |
| 66 | + Outdated, |
| 67 | +} |
| 68 | + |
| 69 | +impl RuntimeError { |
| 70 | + fn hint(&self, args: &Args) -> Option<impl fmt::Display> { |
| 71 | + match self { |
| 72 | + RuntimeError::ReadFile { .. } | RuntimeError::WriteFile { .. } => None, |
| 73 | + RuntimeError::Outdated => Some(format!( |
| 74 | + "Run `./run.sh pdu-ai-instructions --generate {}` to update.", |
| 75 | + args.repository.display(), |
| 76 | + )), |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +/// The CLI arguments. |
| 82 | +#[derive(Debug, Parser)] |
| 83 | +#[clap(about = "Check or generate AI instruction files from templates")] |
| 84 | +struct Args { |
| 85 | + /// Generate the AI instruction files instead of checking them. |
| 86 | + #[clap(long)] |
| 87 | + generate: bool, |
| 88 | + |
| 89 | + /// Path to the top-level directory of the repository. |
| 90 | + repository: PathBuf, |
| 91 | +} |
| 92 | + |
| 93 | +fn main() -> ExitCode { |
| 94 | + let args = Args::parse(); |
| 95 | + let result = match args.generate { |
| 96 | + true => write_files(&args.repository), |
| 97 | + false => check_files(&args.repository), |
| 98 | + }; |
| 99 | + if let Err(error) = result { |
| 100 | + eprintln!("error: {error}"); |
| 101 | + if let Some(hint) = error.hint(&args) { |
| 102 | + eprintln!("hint: {hint}"); |
| 103 | + } |
| 104 | + return ExitCode::FAILURE; |
| 105 | + } |
| 106 | + ExitCode::SUCCESS |
| 107 | +} |
| 108 | + |
| 109 | +fn write_files(repository: &Path) -> Result<(), RuntimeError> { |
| 110 | + for (path, fragments) in FILES { |
| 111 | + let mut output = repository |
| 112 | + .join(path) |
| 113 | + .pipe(File::create) |
| 114 | + .map_err(|error| RuntimeError::WriteFile { path, error })?; |
| 115 | + write!(output, "{fragments}").map_err(|error| RuntimeError::WriteFile { path, error })?; |
| 116 | + eprintln!("info: Generated file {path}"); |
| 117 | + } |
| 118 | + Ok(()) |
| 119 | +} |
| 120 | + |
| 121 | +fn check_files(repository: &Path) -> Result<(), RuntimeError> { |
| 122 | + let mut result: Result<(), RuntimeError> = Ok(()); |
| 123 | + for &(path, fragments) in FILES { |
| 124 | + let actual = repository |
| 125 | + .join(path) |
| 126 | + .pipe(read_to_string) |
| 127 | + .map_err(|error| RuntimeError::ReadFile { path, error })?; |
| 128 | + if !fragments.matches(&actual) { |
| 129 | + eprintln!("error: File {path} is out-of-date"); |
| 130 | + result = Err(RuntimeError::Outdated); |
| 131 | + } |
| 132 | + } |
| 133 | + result |
| 134 | +} |
0 commit comments