From 5ddbe80be1502cfb412ff807e4666469359f2e63 Mon Sep 17 00:00:00 2001 From: Andriy Massimilla Date: Wed, 16 Oct 2024 12:50:32 -0400 Subject: [PATCH] Rename project name global to output name, use project name as name of project --- docs/configuration.md | 8 +++++--- src/copy.rs | 4 ++-- src/hook.rs | 11 ++-------- src/lib.rs | 25 ++++++++++------------- src/template.rs | 1 + tests/data/templated/file.j2 | 2 +- tests/data/universal/{{_project_name}}.j2 | 2 +- 7 files changed, 23 insertions(+), 30 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index ef5b1a3..5345cbf 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -6,18 +6,20 @@ A spackle project is defined by a `spackle.toml` file at the root directory. Bel {s} = slot environment (`{{ }}` will be replaced by slot values) -### Universal slots +### Global slots -Universal slots are available in all slot environments (`.j2` file contents, file names, {s} fields). +Global slots are available in all slot environments (`.j2` file contents, file names, {s} fields). - `_project_name` `string` + - The name of the project itself +- `_output_name` `string` - The name of the output directory ## Project-level config ### name `string` -The name of the project. This also sets the `project_name` universal slot, so keep that in mind. If this isn't set, the project name will be inferred from the directory name. +The name of the project. This also sets the `_project_name` global slot, so keep that in mind. If this isn't set, the project name will be inferred from the directory name. ```toml name = "my_cool_project" diff --git a/src/copy.rs b/src/copy.rs index a0482b9..1f024f1 100644 --- a/src/copy.rs +++ b/src/copy.rs @@ -210,7 +210,7 @@ mod tests { fs::write( src_dir.join(format!("{}.tmpl", "{{template_name}}")), // copy will not do any replacement so contents should remain as is - "{{_project_name}}", + "{{_output_name}}", ) .unwrap(); assert!(src_dir.join("{{template_name}}.tmpl").exists()); @@ -221,7 +221,7 @@ mod tests { &vec![], &HashMap::from([ ("template_name".to_string(), "template".to_string()), - ("_project_name".to_string(), "foo".to_string()), + ("_output_name".to_string(), "foo".to_string()), ]), ) .unwrap(); diff --git a/src/hook.rs b/src/hook.rs index cda19e7..4c8cd71 100644 --- a/src/hook.rs +++ b/src/hook.rs @@ -10,7 +10,6 @@ use tokio::pin; use tokio_stream::{Stream, StreamExt}; use users::User; -use crate::get_output_name; use crate::needs::{is_satisfied, Needy}; #[derive(Serialize, Deserialize, Debug, Clone)] @@ -230,12 +229,6 @@ pub fn run_hooks_stream( data: &HashMap, run_as_user: Option, ) -> Result, Error> { - let mut slot_data = data.clone(); - slot_data.insert( - "_project_name".to_string(), - get_output_name(&dir.as_ref().to_path_buf()), - ); - let mut skipped_hooks = Vec::new(); let mut queued_hooks = Vec::new(); @@ -714,7 +707,7 @@ mod tests { }, Hook { key: "2".to_string(), - command: vec!["echo".to_string(), "{{ _project_name }}".to_string()], + command: vec!["echo".to_string(), "{{ _output_name }}".to_string()], ..Hook::default() }, ]; @@ -726,7 +719,7 @@ mod tests { &HashMap::from([ ("field_1".to_string(), "echo".to_string()), ("field_2".to_string(), "test".to_string()), - ("_project_name".to_string(), "spackle".to_string()), + ("_output_name".to_string(), "spackle".to_string()), ]), None, ) diff --git a/src/lib.rs b/src/lib.rs index 5390442..5a27a72 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -116,6 +116,7 @@ impl Project { /// /// out_dir is the path to what will become the filled directory pub fn generate( + &self, project_dir: &PathBuf, out_dir: &PathBuf, slot_data: &HashMap, @@ -127,7 +128,8 @@ impl Project { let config = config::load_dir(project_dir).map_err(GenerateError::BadConfig)?; let mut slot_data = slot_data.clone(); - slot_data.insert("_project_name".to_string(), get_output_name(out_dir)); + slot_data.insert("_project_name".to_string(), self.get_name()); + slot_data.insert("_output_name".to_string(), get_output_name(out_dir)); // Copy all non-template files to the output directory copy::copy(project_dir, &out_dir, &config.ignore, &slot_data) @@ -155,7 +157,8 @@ impl Project { data: &HashMap, ) -> Result { let mut data = data.clone(); - data.insert("_project_name".to_string(), get_output_name(out_dir)); + data.insert("_project_name".to_string(), self.get_name()); + data.insert("_output_name".to_string(), get_output_name(out_dir)); copy::copy(&self.path, out_dir, &self.config.ignore, &data) } @@ -166,7 +169,8 @@ impl Project { data: &HashMap, ) -> Result>, tera::Error> { let mut data = data.clone(); - data.insert("_project_name".to_string(), get_output_name(out_dir)); + data.insert("_project_name".to_string(), self.get_name()); + data.insert("_output_name".to_string(), get_output_name(out_dir)); template::fill(&self.path, out_dir, &data) } @@ -181,7 +185,8 @@ impl Project { run_as_user: Option, ) -> Result, RunHooksError> { let mut data = data.clone(); - data.insert("_project_name".to_string(), get_output_name(out_dir)); + data.insert("_project_name".to_string(), self.get_name()); + data.insert("_output_name".to_string(), get_output_name(out_dir)); let result = hook::run_hooks_stream( out_dir.to_owned(), @@ -205,16 +210,8 @@ impl Project { run_as_user: Option, ) -> Result, hook::Error> { let mut data = data.clone(); - data.insert("_project_name".to_string(), get_output_name(out_dir)); - data.insert( - "_output_name".to_string(), - // TODO better handle unwrap - out_dir - .file_name() - .unwrap_or_default() - .to_string_lossy() - .into_owned(), - ); + data.insert("_project_name".to_string(), self.get_name()); + data.insert("_output_name".to_string(), get_output_name(out_dir)); let result = hook::run_hooks( &self.config.hooks, diff --git a/src/template.rs b/src/template.rs index 8d368c6..a2a35b9 100644 --- a/src/template.rs +++ b/src/template.rs @@ -148,6 +148,7 @@ pub fn validate(dir: &PathBuf, slots: &Vec) -> Result<(), ValidateError> { ) .map_err(ValidateError::TeraError)?; context.insert("_project_name".to_string(), ""); + context.insert("_output_name".to_string(), ""); let errors = tera .get_template_names() diff --git a/tests/data/templated/file.j2 b/tests/data/templated/file.j2 index e18d6b2..3cdd987 100644 --- a/tests/data/templated/file.j2 +++ b/tests/data/templated/file.j2 @@ -1,2 +1,2 @@ {{ slot_1 }} -{{ _project_name }} \ No newline at end of file +{{ _output_name }} \ No newline at end of file diff --git a/tests/data/universal/{{_project_name}}.j2 b/tests/data/universal/{{_project_name}}.j2 index e2db8d8..5e1dc46 100644 --- a/tests/data/universal/{{_project_name}}.j2 +++ b/tests/data/universal/{{_project_name}}.j2 @@ -1 +1 @@ -{{_project_name}} \ No newline at end of file +{{_output_name}} \ No newline at end of file