Skip to content

Commit

Permalink
Rename project name global to output name, use project name as name o…
Browse files Browse the repository at this point in the history
…f project
  • Loading branch information
andriygm committed Oct 16, 2024
1 parent 8e09d3e commit 5ddbe80
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 30 deletions.
8 changes: 5 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ A spackle project is defined by a `spackle.toml` file at the root directory. Bel

<span style="color: darkseagreen;">{s}</span> = 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, <span style="color: darkseagreen;">{s}</span> fields).
Global slots are available in all slot environments (`.j2` file contents, file names, <span style="color: darkseagreen;">{s}</span> 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"
Expand Down
4 changes: 2 additions & 2 deletions src/copy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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();
Expand Down
11 changes: 2 additions & 9 deletions src/hook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -230,12 +229,6 @@ pub fn run_hooks_stream(
data: &HashMap<String, String>,
run_as_user: Option<User>,
) -> Result<impl Stream<Item = HookStreamResult>, 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();

Expand Down Expand Up @@ -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()
},
];
Expand All @@ -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,
)
Expand Down
25 changes: 11 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>,
Expand All @@ -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)
Expand Down Expand Up @@ -155,7 +157,8 @@ impl Project {
data: &HashMap<String, String>,
) -> Result<copy::CopyResult, copy::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));

copy::copy(&self.path, out_dir, &self.config.ignore, &data)
}
Expand All @@ -166,7 +169,8 @@ impl Project {
data: &HashMap<String, String>,
) -> Result<Vec<Result<template::RenderedFile, template::FileError>>, 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)
}
Expand All @@ -181,7 +185,8 @@ impl Project {
run_as_user: Option<User>,
) -> Result<impl Stream<Item = hook::HookStreamResult>, 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(),
Expand All @@ -205,16 +210,8 @@ impl Project {
run_as_user: Option<User>,
) -> Result<Vec<hook::HookResult>, 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,
Expand Down
1 change: 1 addition & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ pub fn validate(dir: &PathBuf, slots: &Vec<Slot>) -> Result<(), ValidateError> {
)
.map_err(ValidateError::TeraError)?;
context.insert("_project_name".to_string(), "");
context.insert("_output_name".to_string(), "");

let errors = tera
.get_template_names()
Expand Down
2 changes: 1 addition & 1 deletion tests/data/templated/file.j2
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
{{ slot_1 }}
{{ _project_name }}
{{ _output_name }}
2 changes: 1 addition & 1 deletion tests/data/universal/{{_project_name}}.j2
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{_project_name}}
{{_output_name}}

0 comments on commit 5ddbe80

Please sign in to comment.