Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/derived/template.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name = "Java domain"
description = "Dynamic folder structure"
kickstart_version = 1

[[variables]]
name = "package"
default = "my.domain.test"
prompt = "Enter your package name (dot-separated):"
validation = "^[a-z]+(\\.[a-z][a-z0-9_]*)*$"

[[variables]]
name = "package_path"
default = "{{ package | replace(from='.', to='/')}}"
prompt = "Will not be asked since this is a derived variable"
Comment thread
vedoa marked this conversation as resolved.
Outdated
derived = true
7 changes: 7 additions & 0 deletions examples/derived/{{package_path}}/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package {{package}};

public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
39 changes: 39 additions & 0 deletions src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct Variable {
pub validation: Option<String>,
/// Only ask this variable if that condition is true
pub only_if: Option<Condition>,
/// Whether this variable is derived and should not be prompted
pub derived: Option<bool>,
}

/// A hook is a file that will get executed
Expand Down Expand Up @@ -421,4 +423,41 @@ mod tests {

assert_eq!(got_value, &Value::String(expected_value))
}

#[test]
fn can_handle_derived_variable_with_no_input() {
let tpl: TemplateDefinition = toml::from_str(
r#"
name = "Test template"
description = "Testing derived variable behavior"
kickstart_version = 1

[[variables]]
name = "project_name"
default = "My project"
prompt = "What's the name of your project?"

[[variables]]
name = "slug"
default = "{{project_name | slugify}}"
prompt = "Slug for the project"
derived = true
"#,
)
.unwrap();

assert_eq!(tpl.variables.len(), 2);

let res = tpl.default_values();
assert!(res.is_ok());
let res = res.unwrap();

// Check that both variables exist
assert!(res.contains_key("project_name"));
assert!(res.contains_key("slug"));

// Check that slug was rendered from project_name
let expected_slug = Value::String("my-project".to_string());
assert_eq!(res.get("slug"), Some(&expected_slug));
}
}
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ fn ask_questions(template: &Template, no_input: bool) -> Result<HashMap<String,
let mut vals = HashMap::new();

for var in &template.definition.variables {
if var.derived.unwrap_or(false) {
let default = template.get_default_for(&var.name, &vals)?;
vals.insert(var.name.clone(), default);
continue;
}

if !template.should_ask_variable(&var.name, &vals)? {
continue;
}
Expand Down