1use std::path::Path;
14use std::process::Command;
15
16use super::checksum::{finalize, style_for_path};
17
18pub const SESSION_START_SH: &str = include_str!("templates/session-start.sh");
19pub const POST_TOOL_USE_SH: &str = include_str!("templates/post-tool-use.sh");
20pub const STOP_SH: &str = include_str!("templates/stop.sh");
21pub const RULES_TOML: &str = include_str!("templates/rules.toml");
22pub const SETTINGS_SNIPPET_JSON: &str = include_str!("templates/settings-snippet.json");
23pub const CLAUDE_MD_BLOCK_MD: &str = include_str!("templates/claude-md-block.md");
24pub const PLUGIN_JSON: &str = include_str!("templates/plugin.json");
25pub const HOOKS_JSON: &str = include_str!("templates/hooks.json");
26pub const MCP_JSON: &str = include_str!("templates/mcp.json");
27pub const MARKETPLACE_JSON: &str = include_str!("templates/marketplace.json");
28pub const PLUGIN_SETTINGS_JSON: &str = include_str!("templates/plugin-settings.json");
29pub const SKILL_MD: &str = include_str!("templates/SKILL.md");
30pub const PLUGIN_README_MD: &str = include_str!("templates/plugin-README.md");
31
32pub const CTX_VERSION: &str = env!("CARGO_PKG_VERSION");
34
35pub fn render(tmpl: &str, vars: &[(&str, &str)]) -> String {
37 let mut out = tmpl.to_string();
38 for (key, value) in vars {
39 out = out.replace(&format!("{{{{{key}}}}}"), value);
40 }
41 out
42}
43
44pub fn author_name() -> String {
46 env!("CARGO_PKG_AUTHORS")
47 .split(':')
48 .map(|author| match author.split_once('<') {
49 Some((name, _)) => name.trim(),
50 None => author.trim(),
51 })
52 .filter(|name| !name.is_empty())
53 .collect::<Vec<_>>()
54 .join(", ")
55}
56
57pub fn default_branch(root: &Path) -> String {
61 let output = Command::new("git")
62 .args(["symbolic-ref", "--short", "refs/remotes/origin/HEAD"])
63 .current_dir(root)
64 .output();
65 if let Ok(output) = output {
66 if output.status.success() {
67 let name = String::from_utf8_lossy(&output.stdout).trim().to_string();
68 let name = name.strip_prefix("origin/").unwrap_or(&name).to_string();
69 if !name.is_empty() {
70 return name;
71 }
72 }
73 }
74 "main".to_string()
75}
76
77pub fn standard_vars<'a>(default_branch: &'a str, author: &'a str) -> Vec<(&'static str, &'a str)> {
79 vec![
80 ("CTX_VERSION", CTX_VERSION),
81 ("AUTHOR_NAME", author),
82 ("DEFAULT_BRANCH", default_branch),
83 ]
84}
85
86pub fn render_hook_with_version(name: &str, version: &str, default_branch: &str) -> Option<String> {
93 let tmpl = match name {
94 "session-start" => SESSION_START_SH,
95 "post-tool-use" => POST_TOOL_USE_SH,
96 "stop" => STOP_SH,
97 _ => return None,
98 };
99 let rendered = render(
100 tmpl,
101 &[("CTX_VERSION", version), ("DEFAULT_BRANCH", default_branch)],
102 );
103 let rel = format!("{name}.sh");
104 Some(finalize(&rendered, style_for_path(&rel), version))
105}
106
107#[cfg(test)]
108mod tests {
109 use super::*;
110
111 #[test]
112 fn test_render_replaces_all_tokens() {
113 let out = render(
114 "v{{CTX_VERSION}} by {{AUTHOR_NAME}} on {{DEFAULT_BRANCH}}",
115 &[
116 ("CTX_VERSION", "1.0.0"),
117 ("AUTHOR_NAME", "Jane"),
118 ("DEFAULT_BRANCH", "main"),
119 ],
120 );
121 assert_eq!(out, "v1.0.0 by Jane on main");
122 }
123
124 #[test]
125 fn test_author_name_strips_email() {
126 let name = author_name();
129 assert!(!name.is_empty());
130 assert!(!name.contains('<'), "author: {}", name);
131 assert!(!name.contains('@'), "author: {}", name);
132 }
133
134 #[test]
135 fn test_default_branch_falls_back_to_main() {
136 let temp = tempfile::tempdir().unwrap();
138 assert_eq!(default_branch(temp.path()), "main");
139 }
140
141 #[test]
142 fn test_render_hook_with_version_bakes_guard() {
143 let content = render_hook_with_version("stop", "9.9.9", "main").unwrap();
144 assert!(content.contains("ctx harness compat --require \"9.9.9\""));
145 assert!(content.contains("generated by ctx v9.9.9"));
146 assert!(!content.contains("{{"), "unrendered token in: {}", content);
147 assert!(render_hook_with_version("nope", "1.0.0", "main").is_none());
148 }
149}