aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/src/builds
diff options
context:
space:
mode:
authorWeicao-CatilGrass <1992414357@qq.com>2026-05-11 19:56:10 +0800
committerWeicao-CatilGrass <1992414357@qq.com>2026-05-11 19:56:10 +0800
commite42567b25093907cfd939edc92ace94a5d59b398 (patch)
treef2514fdd18277b5620b0fd6512cfc95569cfce15 /mingling_core/src/builds
parent99d5a62aa3655f8676021a9bf70af3d12c9457bc (diff)
Add `builds` feature and install completion scripts
Diffstat (limited to 'mingling_core/src/builds')
-rw-r--r--mingling_core/src/builds/comp.rs23
1 files changed, 23 insertions, 0 deletions
diff --git a/mingling_core/src/builds/comp.rs b/mingling_core/src/builds/comp.rs
index 228ef81..ad70cd2 100644
--- a/mingling_core/src/builds/comp.rs
+++ b/mingling_core/src/builds/comp.rs
@@ -1,3 +1,5 @@
+use std::path::PathBuf;
+
use just_template::tmpl_param;
use crate::ShellFlag;
@@ -84,6 +86,27 @@ pub fn build_comp_script_to(
std::fs::write(&output_path, tmpl.to_string())
}
+/// Generate a shell completion script and write it to a specified file path.
+///
+/// This function takes a shell flag, a binary name, and an output file path,
+/// selects the appropriate template, substitutes the binary name into the template,
+/// and writes the resulting completion script directly to the specified file path.
+///
+/// # Example
+/// ```
+/// build_comp_script_to_file(&ShellFlag::Bash, "myapp", "target/completions/myapp_comp.sh").unwrap();
+/// ```
+pub fn build_comp_script_to_file(
+ shell_flag: &ShellFlag,
+ bin_name: &str,
+ output_path: impl Into<PathBuf>,
+) -> Result<(), std::io::Error> {
+ let (tmpl_str, _ext) = get_tmpl(shell_flag);
+ let mut tmpl = just_template::Template::from(tmpl_str);
+ tmpl_param!(tmpl, bin_name = bin_name);
+ std::fs::write(output_path.into(), tmpl.to_string())
+}
+
fn get_tmpl(shell_flag: &ShellFlag) -> (&'static str, &'static str) {
match shell_flag {
ShellFlag::Bash => (TMPL_COMP_BASH, ".sh"),