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.
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 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())
.collect::<Result<Vec<_>, _>>()?;
let command = match command {
Some(c) => vec![c],
_ => vec![],
};
thread::spawn(move || {
let mut builder = GeneralBuilder::new();
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());
println!("{}", start_string.bold().green());
match builder.build(&path, &vec![]) {
match builder.build(&path, &command) {
Err(_) => {
println!("{}", "--------- FAIL ---------".bold().red());
}

View File

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