Files

54 lines
1.3 KiB
Rust

use std::path::Path;
pub struct ShellScript {
lines: Vec<String>,
}
impl ShellScript {
pub fn new() -> Self {
Self {
lines: Vec::new(),
}
}
pub fn add_comment(&mut self, comment: &str) {
self.lines.push(format!("# {}", comment));
}
pub fn add_variable(&mut self, name: &str, value: &Path) {
let escaped = escape_path(value);
self.lines.push(format!("{}=\"{}\"", name, escaped));
}
pub fn add_variable_str(&mut self, name: &str, value: &str) {
let escaped = value.replace('"', r#"\""#).replace('$', r#"\$"#);
self.lines.push(format!("{}=\"{}\"", name, escaped));
}
pub fn add_command(&mut self, command: &str) {
self.lines.push(command.to_string());
}
pub fn add_empty_line(&mut self) {
self.lines.push(String::new());
}
pub fn output(&self) -> String {
self.lines.join("\n")
}
}
fn escape_path(path: &Path) -> String {
path.display().to_string().replace('"', r#"\""#).replace('$', r#"\$"#)
}
pub fn print_shell_script_header() {
println!("#!/bin/sh");
println!("# Generated by bilibili-merge dry-run mode");
println!();
}
pub fn print_shell_script(script: &ShellScript) {
println!("{}", script.output());
}