From 5ac465007864e397a96da8e97a0de2647f8ec1da Mon Sep 17 00:00:00 2001 From: theMackabu Date: Mon, 25 Nov 2024 21:07:55 -0800 Subject: [PATCH] reduce explicit lifetime --- Maidfile.toml | 6 +- maid/client/cli.rs | 23 +++--- maid/client/cli/dispatch.rs | 35 +++++++- maid/client/cli/{run.rs => script.rs} | 113 ++++++++++---------------- maid/client/cli/tasks.rs | 4 +- maid/client/main.rs | 12 ++- maid/shared/models/client.rs | 24 ++++-- 7 files changed, 119 insertions(+), 98 deletions(-) rename maid/client/cli/{run.rs => script.rs} (61%) diff --git a/Maidfile.toml b/Maidfile.toml index 5d9687d..18f9735 100644 --- a/Maidfile.toml +++ b/Maidfile.toml @@ -25,7 +25,7 @@ VERSION='2.0.0' # Advanced task definition [tasks.build] info = "Build binaries" -depends = ["clean"] +depends = ["log:clean"] script = [ "cargo zigbuild --release --no-default-features --features client --color always", # "cargo zigbuild --release --all-features --color always", @@ -50,6 +50,6 @@ pull = "bin" # Basic task definition [tasks] -clean.script = ["rm -rf bin", "mkdir bin"] -install.script =["maid build -q","sudo cp bin/maid /usr/local/bin", "maid -V"] +clean.script = ["du -h bin", "rm -rf bin", "mkdir bin", "du -h bin"] +install.script = ["maid build -q","sudo cp bin/maid /usr/local/bin", "maid -V"] api_server = { path = "bin", depends = ["build"], script = "./maid_server" } \ No newline at end of file diff --git a/maid/client/cli.rs b/maid/client/cli.rs index 10d328b..3006186 100644 --- a/maid/client/cli.rs +++ b/maid/client/cli.rs @@ -1,5 +1,5 @@ pub(crate) mod dispatch; -pub(crate) mod run; +pub(crate) mod script; pub(crate) mod tasks; use crate::{parse, server, task}; @@ -8,7 +8,7 @@ use maid::{ helpers, log::prelude::*, models::{ - client::{CacheConfig, Task}, + client::{CacheConfig, Dependency, Task}, shared::{Cache, Project}, }, }; @@ -83,7 +83,7 @@ pub(crate) fn env(path: &String) { } } -pub(crate) fn exec(task: &str, args: &Vec, path: &String, silent: bool, is_dep: bool, is_remote: bool, log_level: Option, force: bool) { +pub(crate) fn exec(task: &str, args: &Vec, path: &String, silent: bool, is_dep: bool, is_remote: bool, log_level: Option, force: bool, log_deps: bool) { debug!("Starting maid {}", env!("CARGO_PKG_VERSION")); if task.is_empty() { @@ -123,9 +123,12 @@ pub(crate) fn exec(task: &str, args: &Vec, path: &String, silent: bool, let pb = task::progress::init(ticks, template, 80); for (index, item) in deps.iter().enumerate() { + let is_verbose_dep = item.starts_with("log:"); + let name = item.strip_prefix("log:").unwrap_or(item.as_str()); + pb.set_prefix(format!("[{}/{}]", index + 1, deps.len())); - pb.set_message(fmtstr!("{} {item}", "running dependency".bright_yellow())); - exec(&item, args, path, true, true, is_remote, log_level, force); + pb.set_message(fmtstr!("{} {name}", "running dependency".bright_yellow())); + exec(&name, args, path, true, true, is_remote, log_level, force, is_verbose_dep); } if !is_dep { @@ -244,6 +247,7 @@ pub(crate) fn exec(task: &str, args: &Vec, path: &String, silent: bool, if is_remote { server::cli::remote(Task { + silent, maidfile: values.clone(), name: string!(task), project: project_root, @@ -251,11 +255,11 @@ pub(crate) fn exec(task: &str, args: &Vec, path: &String, silent: bool, script: values.tasks[task].script.clone(), path: task_path.clone(), args: args.clone(), - silent, - is_dep, + dep: Dependency { active: is_dep, verbose: log_deps }, }); } else { - run::task(Task { + dispatch::task(Task { + silent, maidfile: values.clone(), name: string!(task), project: project_root, @@ -263,8 +267,7 @@ pub(crate) fn exec(task: &str, args: &Vec, path: &String, silent: bool, script: values.tasks[task].script.clone(), path: task_path.clone(), args: args.clone(), - silent, - is_dep, + dep: Dependency { active: is_dep, verbose: log_deps }, }); } } diff --git a/maid/client/cli/dispatch.rs b/maid/client/cli/dispatch.rs index 4dbcc26..f720257 100644 --- a/maid/client/cli/dispatch.rs +++ b/maid/client/cli/dispatch.rs @@ -1,5 +1,8 @@ -use maid::log::prelude::*; -use maid::models::client::UpdateData; +use maid::{ + helpers, + log::prelude::*, + models::client::{Runner, Task, UpdateData}, +}; use inquire::Text; use macros_rs::fs::file_exists; @@ -88,3 +91,31 @@ pub(crate) fn init() { println!("{}", "maidfile already exists, aborting".yellow()) } } + +pub(crate) fn task(task: Task) { + let mut script: Vec = Vec::new(); + + if let Some(cmd) = task.script.as_str() { + script.push(cmd.to_string()); + } else if let Some(array) = task.script.as_array() { + for value in array { + match value.as_str() { + Some(cmd) => script.push(cmd.to_string()), + None => error!("Unable to parse Maidfile. Missing string value."), + } + } + } else { + helpers::status::error(task.script.type_str()); + } + + super::script::run_wrapped(Runner { + script, + dep: task.dep, + name: task.name, + path: task.path, + args: task.args, + silent: task.silent, + project: task.project, + maidfile: task.maidfile, + }); +} diff --git a/maid/client/cli/run.rs b/maid/client/cli/script.rs similarity index 61% rename from maid/client/cli/run.rs rename to maid/client/cli/script.rs index c7e7482..946675f 100644 --- a/maid/client/cli/run.rs +++ b/maid/client/cli/script.rs @@ -1,61 +1,71 @@ -use crate::shell::IntoArgs; - use maid::{ helpers, log::prelude::*, - models::{ - client::{Runner, Task}, - shared::Cache, - }, + models::{client::Runner, shared::Cache}, table, }; +use std::{ + env, + io::Error, + path::Path, + process::{Child, Command, ExitStatus, Stdio}, + time::Instant, +}; + +use crate::shell::IntoArgs; use fs_extra::dir::get_size; use human_bytes::human_bytes; -use std::env; -use std::io::Error; -use std::path::Path; -use std::process::{Child, Command, ExitStatus, Stdio}; -use std::time::Instant; use text_placeholder::Template; -use toml::Value; -fn run_script(runner: Runner) { - let mut cmd: Child; +pub(crate) fn run_wrapped(runner: Runner) { let start = Instant::now(); + + let mut cmd: Child; let mut status_array: Vec> = vec![]; - for string in runner.script.clone() { + for string in runner.script { let start = Instant::now(); - let table = table::create(runner.maidfile.clone(), runner.args, runner.project.clone()); - let script = Template::new_with_placeholder(string, "%{", "}").fill_with_hashmap(&table); - let (name, args) = match script.try_into_args() { - Ok(result) => { - let mut args = result.clone(); - args.remove(0); - (result[0].clone(), args) - } + let working_dir = runner.project.join(Path::new(&runner.path)); + let table = table::create(runner.maidfile.to_owned(), &runner.args, runner.project.to_owned()); + let script = Template::new_with_placeholder(&string, "%{", "}").fill_with_hashmap(&table); + + let (name, args) = match script.try_into_args() { + Ok(mut args) => (args.remove(0), args), Err(err) => error!(%err, "Script could not be parsed into args"), }; - debug!("Original Script: {}", string); - debug!("Parsed Script: {}", script); + debug!("Original Script: {string}"); + debug!("Parsed Script: {script}"); debug!("Execute Command: '{name} {}'", args.join(" ")); - let working_dir = runner.project.join(&Path::new(runner.path)); match env::set_current_dir(&working_dir) { - Ok(_) => debug!("Working directory: {:?}", &working_dir), - Err(err) => error!(%err, "Failed to set working directory {:?}", &working_dir), + Ok(_) => debug!("Working directory: {working_dir:?}"), + Err(err) => error!(%err, "Failed to set working directory {working_dir:?}"), }; - if runner.is_dep { - cmd = match Command::new(&name).stdout(Stdio::null()).stderr(Stdio::null()).stdin(Stdio::null()).args(args.clone()).spawn() { + if runner.dep.active { + let is_verbose = runner.dep.verbose; + + cmd = match Command::new(&name) + .stdout(if is_verbose { Stdio::inherit() } else { Stdio::null() }) + .stderr(if is_verbose { Stdio::inherit() } else { Stdio::null() }) + .stdin(if is_verbose { Stdio::inherit() } else { Stdio::null() }) + .args(args.to_owned()) + .spawn() + { Ok(output) => output, Err(err) => error!(%err, "Cannot start command {name}."), }; } else { - cmd = match Command::new(&name).args(args.clone()).stdout(Stdio::inherit()).stderr(Stdio::inherit()).stdin(Stdio::inherit()).spawn() { + cmd = match Command::new(&name) + .args(args.to_owned()) + .stdout(Stdio::inherit()) + .stderr(Stdio::inherit()) + .stdin(Stdio::inherit()) + .spawn() + { Ok(output) => output, Err(err) => error!(%err, "Cannot start command {name}."), }; @@ -73,7 +83,7 @@ fn run_script(runner: Runner) { None => error!("Failed to fetch final status code."), }; - let cache = match &runner.maidfile.tasks[runner.name].cache { + let cache = match &runner.maidfile.tasks[&runner.name].cache { Some(cache) => cache.clone(), None => Cache { path: "".to_string(), target: vec![] }, }; @@ -125,42 +135,3 @@ fn run_script(runner: Runner) { } } } - -pub(crate) fn task(task: Task) { - let mut script: Vec<&str> = vec![]; - - if task.script.is_str() { - match task.script.as_str() { - Some(cmd) => script.push(cmd), - None => error!("Unable to parse Maidfile. Missing string value."), - }; - } else if task.script.is_array() { - match IntoIterator::into_iter(match task.script.as_array() { - Some(iter) => iter, - None => error!("Unable to parse Maidfile. Missing array value."), - }) { - mut iter => loop { - match Iterator::next(&mut iter) { - Some(val) => match val.as_str() { - Some(cmd) => script.push(cmd), - None => error!("Unable to parse Maidfile. Missing string value."), - }, - None => break, - }; - }, - } - } else { - helpers::status::error(task.script.type_str()) - } - - run_script(Runner { - name: &task.name, - path: &task.path, - args: &task.args, - silent: task.silent, - is_dep: task.is_dep, - project: &task.project, - maidfile: &task.maidfile, - script, - }); -} diff --git a/maid/client/cli/tasks.rs b/maid/client/cli/tasks.rs index 12803d2..530903d 100644 --- a/maid/client/cli/tasks.rs +++ b/maid/client/cli/tasks.rs @@ -63,7 +63,7 @@ pub(crate) fn list_all(path: &String, silent: bool, log_level: Option, fo match Select::new("Select a task to run:", options).prompt() { Ok(task) => { debug!("Starting {}", task.name); - cli::exec(&String::from(task.name), &vec![String::from("")], &path, silent, false, false, log_level, force); + cli::exec(&String::from(task.name), &vec![String::from("")], &path, silent, false, false, log_level, force, false); } Err(_) => println!("{}", "Aborting...".white()), @@ -76,7 +76,7 @@ pub(crate) fn list_remote(path: &String, silent: bool, log_level: Option) match Select::new("Select a remote task to run:", options).prompt() { Ok(task) => { debug!("Starting {}", task.name); - cli::exec(&String::from(task.name), &vec![String::from("")], &path, silent, false, true, log_level, false); + cli::exec(&String::from(task.name), &vec![String::from("")], &path, silent, false, true, log_level, false, false); } Err(_) => println!("{}", "Aborting...".white()), diff --git a/maid/client/main.rs b/maid/client/main.rs index f924c54..b22f4d5 100644 --- a/maid/client/main.rs +++ b/maid/client/main.rs @@ -143,5 +143,15 @@ fn main() { return cli::dispatch::watch(Path::new(&path)); // migrate watch path into executer below } - cli::exec(cli.task[0].trim(), &cli.task, &cli.path, cli.verbose.is_silent(), false, cli.remote, cli.verbose.log_level(), cli.force) + cli::exec( + cli.task[0].trim(), + &cli.task, + &cli.path, + cli.verbose.is_silent(), + false, + cli.remote, + cli.verbose.log_level(), + cli.force, + false, + ) } diff --git a/maid/shared/models/client.rs b/maid/shared/models/client.rs index 50498bb..296d7f2 100644 --- a/maid/shared/models/client.rs +++ b/maid/shared/models/client.rs @@ -19,19 +19,25 @@ pub struct Task { pub path: String, pub args: Vec, pub silent: bool, - pub is_dep: bool, + pub dep: Dependency, +} + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct Dependency { + pub active: bool, + pub verbose: bool, } #[derive(Clone, Debug)] -pub struct Runner<'a, T> { - pub maidfile: &'a Maidfile, - pub name: &'a String, - pub script: Vec<&'a str>, - pub path: &'a String, - pub args: &'a Vec, - pub project: &'a PathBuf, +pub struct Runner { + pub maidfile: Maidfile, + pub name: String, + pub script: Vec, + pub path: String, + pub args: Vec, + pub project: PathBuf, pub silent: bool, - pub is_dep: bool, + pub dep: Dependency, } #[derive(Debug)] -- GitLab