aboutsummaryrefslogtreecommitdiff
path: root/mingling_core/src
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-04-28 15:01:39 +0800
committer魏曹先生 <1992414357@qq.com>2026-04-28 15:01:39 +0800
commit4e293ccccc91f89fca5857a87c03afd83e7824f3 (patch)
treef618c271614d53df914de57369b4f28adfda24fa /mingling_core/src
parent2b311d3d6806e0b8d8e8292bfde53b1b85b48b3b (diff)
Extract `build_comp_script` logic into `build_comp_script_to`
Diffstat (limited to 'mingling_core/src')
-rw-r--r--mingling_core/src/builds/comp.rs27
1 files changed, 23 insertions, 4 deletions
diff --git a/mingling_core/src/builds/comp.rs b/mingling_core/src/builds/comp.rs
index 754aff4..228ef81 100644
--- a/mingling_core/src/builds/comp.rs
+++ b/mingling_core/src/builds/comp.rs
@@ -55,13 +55,32 @@ pub fn build_comp_scripts(name: &str) -> Result<(), std::io::Error> {
/// build_comp_script(&ShellFlag::Bash, "myapp").unwrap();
/// ```
pub fn build_comp_script(shell_flag: &ShellFlag, bin_name: &str) -> Result<(), std::io::Error> {
+ let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
+ let target_dir = out_dir.join("../../../").to_path_buf();
+ build_comp_script_to(shell_flag, bin_name, &target_dir.to_string_lossy())
+}
+
+/// Generate a shell completion script to a specified directory.
+///
+/// This function takes a shell flag, a binary name, and a target directory path,
+/// selects the appropriate template, substitutes the binary name into the template,
+/// and writes the resulting completion script to the specified directory.
+///
+/// # Example
+/// ```
+/// build_comp_script_to(&ShellFlag::Bash, "myapp", "target/completions").unwrap();
+/// ```
+pub fn build_comp_script_to(
+ shell_flag: &ShellFlag,
+ bin_name: &str,
+ target_dir: &str,
+) -> 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);
- let out_dir = std::path::PathBuf::from(std::env::var("OUT_DIR").unwrap());
- let target_dir = out_dir.join("../../../").to_path_buf();
- let output_path = target_dir.join(format!("{}_comp{}", bin_name, ext));
- std::fs::create_dir_all(&target_dir)?;
+ let target_path = std::path::PathBuf::from(target_dir);
+ std::fs::create_dir_all(&target_path)?;
+ let output_path = target_path.join(format!("{}_comp{}", bin_name, ext));
std::fs::write(&output_path, tmpl.to_string())
}