use crate::actions::plan::Action;
use crate::app::AppSnapshot;
use crate::generate::catalog::Catalog;
use crate::modules::module::ModuleState;
use crate::modules::target::Target;
use crate::run::history::Entry;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Ran {
Action {
action: Action,
module: Option<String>,
},
Generator {
name: String,
module: Option<String>,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RecentRun {
pub ran: Ran,
pub started_ms: i64,
}
pub fn recent_runs(
runs: &[Entry],
catalog: &Catalog,
app: &AppSnapshot,
limit: usize,
) -> Vec<RecentRun> {
let mut recent: Vec<RecentRun> = Vec::new();
for run in runs {
if recent.len() == limit {
break;
}
let Some(ran) = ran(&run.args, catalog, app) else {
continue;
};
if recent.iter().all(|kept| kept.ran != ran) {
recent.push(RecentRun {
ran,
started_ms: run.started_ms,
});
}
}
recent
}
fn ran(args: &[String], catalog: &Catalog, app: &AppSnapshot) -> Option<Ran> {
let (first, rest) = args.split_first()?;
let options: Vec<&str> = rest
.iter()
.map(String::as_str)
.take_while(|word| *word != "--")
.collect();
let arguments: Vec<&str> = if options.len() < rest.len() {
rest[options.len() + 1..]
.iter()
.map(String::as_str)
.collect()
} else {
options
.iter()
.copied()
.filter(|word| !word.starts_with('-'))
.collect()
};
let module = |word: Option<&&str>| Some(app.module(word?)?.name.clone());
if let Some(action) = Action::from_artisan(first) {
let module = if options.contains(&"--all") {
None
} else {
Some(module(arguments.first())?)
};
let everyday = match &module {
Some(name) => {
let on = app.module(name)?.state != ModuleState::Off;
Action::for_module(on).contains(&action)
}
None => Action::for_all().contains(&action),
};
let target = module.clone().map_or(Target::All, Target::Module);
if !everyday && !Action::destructive(&target).contains(&action) {
return None;
}
return Some(Ran::Action { action, module });
}
let generator = catalog.get(first)?;
let module = match generator
.arguments
.iter()
.position(|argument| argument.name == "module")
{
Some(at) => Some(module(arguments.get(at))?),
None => None,
};
Some(Ran::Generator {
name: generator.name.clone(),
module,
})
}
#[cfg(test)]
mod tests {
use super::*;
use crate::run::history::Ended;
use std::path::Path;
fn run(words: &str, started_ms: i64) -> Entry {
let args: Vec<String> = words.split_whitespace().map(str::to_owned).collect();
Entry {
id: started_ms as u64,
label: words.to_owned(),
args,
started_ms,
ended: Ended::Done { ms: 5 },
cut: false,
}
}
#[test]
fn the_latest_distinct_actions_and_generators_of_modules_in_the_app() {
let root = Path::new(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../../fixtures/shop-api"
));
let app = crate::read_app(root).unwrap();
let runs = [
run("module:migrate Checkout --pretend", 9),
run("about", 8),
run("module:migrate Nowhere", 7),
run("module:enable Checkout", 7),
run("module:make-model Invoice", 6),
run("module:migrate Checkout", 5),
run("module:make-model --migration -- -Invoice Billing", 4),
run("module:migrate --all", 3),
run("module:make Loyalty --api", 2),
];
let recent = recent_runs(&runs, Catalog::v13_3(), &app, 3);
assert_eq!(
recent,
[
RecentRun {
ran: Ran::Action {
action: Action::Migrate,
module: Some("Checkout".into()),
},
started_ms: 9,
},
RecentRun {
ran: Ran::Generator {
name: "module:make-model".into(),
module: Some("Billing".into()),
},
started_ms: 4,
},
RecentRun {
ran: Ran::Action {
action: Action::Migrate,
module: None,
},
started_ms: 3,
},
]
);
assert_eq!(
recent_runs(&runs[runs.len() - 1..], Catalog::v13_3(), &app, 3)[0].ran,
Ran::Generator {
name: crate::MODULE_MAKE.into(),
module: None,
}
);
}
}