Skip to content

Commit

Permalink
Include disabled packages in dotter.packages (#131)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperCuber authored Jul 21, 2023
1 parent ad9462b commit 9bd26ae
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
10 changes: 8 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub type Helpers = BTreeMap<String, PathBuf>;
pub struct Configuration {
pub files: Files,
pub variables: Variables,
pub packages: Vec<String>,
pub packages: BTreeMap<String, bool>,

#[cfg(feature = "scripting")]
pub helpers: Helpers,
Expand Down Expand Up @@ -304,6 +304,12 @@ fn merge_configuration_files(
enabled_packages.extend(new_packages);
}

let packages_map = global
.packages
.keys()
.map(|k| (k.to_string(), enabled_packages.contains(k)))
.collect();

// Apply packages filter
global.packages.retain(|k, _| enabled_packages.contains(k));

Expand All @@ -312,7 +318,7 @@ fn merge_configuration_files(
helpers: global.helpers,
files: Files::default(),
variables: Variables::default(),
packages: enabled_packages.into_iter().collect(),
packages: packages_map,
recurse: true,
};

Expand Down
23 changes: 18 additions & 5 deletions src/handlebars_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use anyhow::{Context as AnyhowContext, Result};
use handlebars::{Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderError};
use toml::value::{Table, Value};

use std::collections::BTreeSet;
use std::collections::{BTreeMap, BTreeSet};
use std::path::PathBuf;
use std::process::{Command, Stdio};

Expand Down Expand Up @@ -298,14 +298,18 @@ fn files_as_toml(files: &Files) -> Value {
)
}

fn add_dotter_variable(variables: &mut Variables, files: &Files, packages: &[String]) {
fn add_dotter_variable(
variables: &mut Variables,
files: &Files,
packages: &BTreeMap<String, bool>,
) {
let mut dotter = Table::new();
dotter.insert(
"packages".into(),
Value::Table(
packages
.iter()
.map(|p| (p.to_string(), Value::Boolean(true)))
.map(|(p, e)| (p.to_string(), Value::Boolean(*e)))
.collect(),
),
);
Expand Down Expand Up @@ -345,7 +349,7 @@ mod test {
files: Files::new(),
variables: maplit::btreemap! { "foo".into() => 2.into() },
helpers: Helpers::new(),
packages: vec!["default".into()],
packages: maplit::btreemap! { "default".into() => true, "disabled".into() => false },
recurse: true,
};
let handlebars = create_new_handlebars(&mut config).unwrap();
Expand All @@ -366,6 +370,15 @@ mod test {
eval_condition(&handlebars, &config.variables, "dotter.packages.nonexist").unwrap(),
false
);
assert_eq!(
eval_condition(
&handlebars,
&config.variables,
"(and true dotter.packages.disabled)"
)
.unwrap(),
false
);
}

#[test]
Expand All @@ -374,7 +387,7 @@ mod test {
files: Files::new(),
variables: Variables::new(),
helpers: Helpers::new(),
packages: vec!["default".into()],
packages: BTreeMap::new(),
recurse: true,
};
let handlebars = create_new_handlebars(&mut config).unwrap();
Expand Down

0 comments on commit 9bd26ae

Please sign in to comment.