diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-09-25 13:52:53 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2021-09-25 13:52:53 +0200 |
commit | 6270077d3da2ca8932ac06f5d3001c0deed4d238 (patch) | |
tree | c5fec14ab09417c68bc4f7c0602ea7ee50d723fa | |
parent | 5e3b1c8fdb5886cd75dc52a2631949ab3dca5a16 (diff) | |
download | rebel-6270077d3da2ca8932ac06f5d3001c0deed4d238.tar rebel-6270077d3da2ca8932ac06f5d3001c0deed4d238.zip |
recipe: allow omitting recipe name in task dependencies inside a single recipe
-rw-r--r-- | src/recipe.rs | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/src/recipe.rs b/src/recipe.rs index c27d44e..051bb10 100644 --- a/src/recipe.rs +++ b/src/recipe.rs @@ -1,10 +1,24 @@ -use std::{collections::HashMap, fmt, fs::File, io, path::Path, result}; +use std::{cell::RefCell, collections::HashMap, fmt, fs::File, io, path::Path, result}; use serde::{Deserialize, Deserializer}; use walkdir::WalkDir; use crate::{task::TaskDef, types::TaskID}; +thread_local! { + pub static CURRENT_RECIPE: RefCell<Option<String>> = RefCell::new(None); +} + +fn current_recipe() -> String { + CURRENT_RECIPE.with(|current| { + current + .borrow() + .as_ref() + .expect("No current recipe") + .clone() + }) +} + pub fn deserialize_task_id<'de, D>(deserializer: D) -> result::Result<TaskID, D::Error> where D: Deserializer<'de>, @@ -14,14 +28,11 @@ where task: String, } let id = RecipeTaskID::deserialize(deserializer)?; - let (recipe, task) = id - .task - .split_once(":") - .ok_or(serde::de::Error::custom("Invalid task ID"))?; - Ok(TaskID { - recipe: recipe.to_string(), - task: task.to_string(), - }) + let (recipe, task) = match id.task.split_once(":") { + Some((recipe, task)) => (recipe.to_string(), task.to_string()), + None => (current_recipe(), id.task), + }; + Ok(TaskID { recipe, task }) } #[derive(Debug, Deserialize)] @@ -86,8 +97,16 @@ pub fn read_recipes<P: AsRef<Path>>(path: P) -> Result<HashMap<TaskID, TaskDef>> _ => continue, }; + CURRENT_RECIPE.with(|current| { + *current.borrow_mut() = Some(basename.to_string()); + }); + let recipe = read_recipe(path)?; + CURRENT_RECIPE.with(|current| { + *current.borrow_mut() = None; + }); + for (label, task) in recipe.tasks { let task_id = TaskID { recipe: basename.to_string(), |