Watch with custom command

This commit is contained in:
Thomas Forgione 2023-10-12 16:25:37 +02:00
parent 5797a9a80b
commit 498129df37
2 changed files with 36 additions and 11 deletions

View File

@ -120,7 +120,11 @@ pub fn builder_arguments_from_string(uri: &str) -> (PathBuf, Vec<String>) {
} }
/// Watches a directory and builds it when a modification occurs. /// Watches a directory and builds it when a modification occurs.
pub fn watch<P: AsRef<Path>>(p: P, ignore: Vec<String>) -> Result<(), Error> { pub fn watch<P: AsRef<Path>>(
p: P,
ignore: Vec<String>,
command: Option<String>,
) -> Result<(), Error> {
let path = PathBuf::from(p.as_ref()).canonicalize()?; let path = PathBuf::from(p.as_ref()).canonicalize()?;
let ignore = ignore let ignore = ignore
@ -128,6 +132,11 @@ pub fn watch<P: AsRef<Path>>(p: P, ignore: Vec<String>) -> Result<(), Error> {
.map(|x| PathBuf::from(x).canonicalize()) .map(|x| PathBuf::from(x).canonicalize())
.collect::<Result<Vec<_>, _>>()?; .collect::<Result<Vec<_>, _>>()?;
let command = match command {
Some(c) => vec![c],
_ => vec![],
};
thread::spawn(move || { thread::spawn(move || {
let mut builder = GeneralBuilder::new(); let mut builder = GeneralBuilder::new();
let (tx, rx) = channel(); let (tx, rx) = channel();
@ -161,7 +170,7 @@ pub fn watch<P: AsRef<Path>>(p: P, ignore: Vec<String>) -> Result<(), Error> {
let start_string = format!("---- STARTING BUILD ---- from {}", path.display()); let start_string = format!("---- STARTING BUILD ---- from {}", path.display());
println!("{}", start_string.bold().green()); println!("{}", start_string.bold().green());
match builder.build(&path, &vec![]) { match builder.build(&path, &command) {
Err(_) => { Err(_) => {
println!("{}", "--------- FAIL ---------".bold().red()); println!("{}", "--------- FAIL ---------".bold().red());
} }

View File

@ -25,6 +25,7 @@ fn main() {
.peekable(); .peekable();
let mut watching = None; let mut watching = None;
let mut command = None;
let mut ignore = vec![]; let mut ignore = vec![];
let exit_code = loop { let exit_code = loop {
@ -35,36 +36,51 @@ fn main() {
let next = next.as_deref(); let next = next.as_deref();
let watch_is_some = watching.is_some(); let watch_is_some = watching.is_some();
let command_is_some = command.is_some();
match (arg, next, watch_is_some) { match (arg, next, watch_is_some, command_is_some) {
(None, _, _) => { (None, _, _, _) => {
break None; break None;
} }
(Some("-w") | Some("--watch"), Some(value), false) => { (Some("-w") | Some("--watch"), Some(value), false, _) => {
watching = Some(value.to_string()); watching = Some(value.to_string());
} }
(Some("-w") | Some("--watch"), Some(_), true) => { (Some("-w") | Some("--watch"), Some(_), true, _) => {
error!("argument watch present multiple times"); error!("argument watch present multiple times");
break Some(1); break Some(1);
} }
(Some("-w") | Some("--watch"), None, _) => { (Some("-w") | Some("--watch"), None, _, _) => {
error!("argument watch without value"); error!("argument watch without value");
break Some(1); break Some(1);
} }
(Some("-i") | Some("--ignore"), Some(value), _) => { (Some("-i") | Some("--ignore"), Some(value), _, _) => {
ignore.push(value.to_string()); ignore.push(value.to_string());
} }
(Some("-i") | Some("--ignore"), None, _) => { (Some("-i") | Some("--ignore"), None, _, _) => {
error!("argument ignore without value"); error!("argument ignore without value");
break Some(1); break Some(1);
} }
(Some(value), _, _) => { (Some("-c") | Some("--command"), Some(value), _, false) => {
command = Some(value.to_string());
}
(Some("-c") | Some("--command"), Some(_), _, true) => {
error!("argument command present multiple times");
break Some(1);
}
(Some("-c") | Some("--command"), None, _, _) => {
error!("argument command without value");
break Some(1);
}
(Some(value), _, _, _) => {
error!("argument \"{}\" unknown", value); error!("argument \"{}\" unknown", value);
break Some(1); break Some(1);
} }
@ -78,7 +94,7 @@ fn main() {
} }
if let Some(watching) = watching { if let Some(watching) = watching {
if let Err(e) = watch(&watching, ignore) { if let Err(e) = watch(&watching, ignore, command) {
error!("{}", e); error!("{}", e);
exit(1); exit(1);
} }