From 550295e0906e7ea874b3b2c35fb17519b429369c Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Wed, 3 Nov 2021 21:08:23 +0100 Subject: driver: store multiple recipe with the same ID, select highest version number We are using the Debian version number scheme for comparision. --- Cargo.lock | 7 +++++++ crates/driver/Cargo.toml | 1 + crates/driver/src/context.rs | 22 ++++++++++++++++++---- crates/driver/src/recipe.rs | 6 +++--- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3a8373c..fffbaf8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -172,6 +172,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "deb-version" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b390250ca2b6862735ef39a7b37b0266a8d5cd9d1e579260c95b1dc27761d6ad" + [[package]] name = "digest" version = "0.8.1" @@ -499,6 +505,7 @@ name = "rebel" version = "0.1.0" dependencies = [ "clap", + "deb-version", "enum-kinds", "handlebars", "indoc", diff --git a/crates/driver/Cargo.toml b/crates/driver/Cargo.toml index 215c451..36afe9f 100644 --- a/crates/driver/Cargo.toml +++ b/crates/driver/Cargo.toml @@ -12,6 +12,7 @@ common = { path = "../common", package = "rebel-common" } runner = { path = "../runner", package = "rebel-runner" } clap = "3.0.0-beta.2" +deb-version = "0.1.1" enum-kinds = "0.5.1" handlebars = "4.1.3" indoc = "1.0.3" diff --git a/crates/driver/src/context.rs b/crates/driver/src/context.rs index c3ca79f..ccb8581 100644 --- a/crates/driver/src/context.rs +++ b/crates/driver/src/context.rs @@ -140,13 +140,13 @@ fn platform_relation(args: &TaskArgs, from: &str, to: &str) -> Option, globals: TaskArgs, - tasks: HashMap, + tasks: HashMap>, rootfs: (ArchiveHash, String), rootfs_tasks: HashMap, } impl Context { - pub fn new(tasks: HashMap, pins: Pins) -> error::Result { + pub fn new(tasks: HashMap>, pins: Pins) -> error::Result { let platforms: HashMap<_, _> = [ arg( "build", @@ -270,15 +270,29 @@ impl Context { Some(task_def) } + fn select_task(tasks: &[TaskDef]) -> &TaskDef { + tasks + .iter() + .max_by(|task1, task2| { + deb_version::compare_versions( + task1.meta.version.as_deref().unwrap_or_default(), + task2.meta.version.as_deref().unwrap_or_default(), + ) + }) + .unwrap() + } + fn get_with_args<'ctx>(&'ctx self, id: &'ctx TaskID, args: &TaskArgs) -> Result<&TaskDef> { if let Some(rootfs_task) = self.rootfs_task(id, args) { return Ok(rootfs_task); } - self.tasks.get(id).ok_or(Error { + let tasks = self.tasks.get(id).ok_or(Error { task: id, kind: ErrorKind::TaskNotFound, - }) + })?; + + Ok(Self::select_task(tasks)) } pub fn get<'ctx>(&'ctx self, task: &TaskRef<'ctx>) -> Result<&TaskDef> { diff --git a/crates/driver/src/recipe.rs b/crates/driver/src/recipe.rs index 745e2f6..277a393 100644 --- a/crates/driver/src/recipe.rs +++ b/crates/driver/src/recipe.rs @@ -51,8 +51,8 @@ fn is_yml(path: &Path) -> bool { path.extension() == Some("yml".as_ref()) } -pub fn read_recipes>(path: P) -> Result> { - let mut tasks = HashMap::new(); +pub fn read_recipes>(path: P) -> Result>> { + let mut tasks = HashMap::>::new(); for entry in WalkDir::new(path).into_iter().filter_map(|e| e.ok()) { let path = entry.path(); @@ -85,7 +85,7 @@ pub fn read_recipes>(path: P) -> Result> task: label, }; task.meta = meta.clone(); - tasks.insert(task_id, task); + tasks.entry(task_id).or_default().push(task); } } -- cgit v1.2.3