Skip to content

Commit

Permalink
adding global variable to enable symlinks by default if type not spec…
Browse files Browse the repository at this point in the history
…ified (#173)
  • Loading branch information
ducks authored Jun 18, 2024
1 parent e262fa9 commit 29396e7
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 0 deletions.
200 changes: 200 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ pub type Variables = toml::value::Table;
#[cfg(feature = "scripting")]
pub type Helpers = BTreeMap<String, PathBuf>;

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum DefaultTargetType {
Symbolic,
Template,
#[default]
Automatic,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Settings {
#[serde(default)]
default_target_type: DefaultTargetType,
}

#[derive(Debug, Clone)]
pub struct Configuration {
pub files: Files,
Expand All @@ -90,6 +105,9 @@ pub struct Configuration {
/// turned into a list of all the files inside the structure that
/// are readable.
pub recurse: bool,

#[allow(dead_code)]
pub settings: Settings,
}

#[derive(Debug, Deserialize, Serialize, Default)]
Expand All @@ -110,6 +128,8 @@ struct GlobalConfig {
helpers: Helpers,
#[serde(flatten)]
packages: BTreeMap<String, Package>,
#[serde(default)]
settings: Settings,
}

type IncludedConfig = BTreeMap<String, Package>;
Expand Down Expand Up @@ -211,6 +231,7 @@ pub fn save_dummy_config(
#[cfg(feature = "scripting")]
helpers: Helpers::new(),
packages,
settings: Settings::default(),
};
debug!("Saving global config...");
// Assume default args so all parents are the same
Expand Down Expand Up @@ -328,6 +349,7 @@ fn merge_configuration_files(
variables: Variables::default(),
packages: packages_map,
recurse: true,
settings: Settings::default(),
};

// Merge all the packages
Expand Down Expand Up @@ -371,6 +393,20 @@ fn merge_configuration_files(
output.files = first_package.files;
output.variables = first_package.variables;

for value in output.files.values_mut() {
if let FileTarget::Automatic(target) = value {
*value = match global.settings.default_target_type {
DefaultTargetType::Symbolic => {
FileTarget::Symbolic(SymbolicTarget::from(target.clone()))
}
DefaultTargetType::Template => {
FileTarget::ComplexTemplate(TemplateTarget::from(target.clone()))
}
_ => continue,
};
}
}

// Add local.toml's patches
output.files.extend(local.files);
recursive_extend_map(&mut output.variables, local.variables);
Expand Down Expand Up @@ -630,4 +666,168 @@ mod test {
)
.unwrap_err();
}

#[test]
fn settting_default_target_type_symbolic() {
let global: GlobalConfig = toml::from_str(
r#"
[settings]
default_target_type = "symbolic"
[cat]
depends = []
[cat.files]
cat = '~/.QuarticCat'
[derby]
depends = []
[derby.files]
derby = { target = '~/.DerbyLantern', type = 'template' }
"#,
)
.unwrap();

let local: LocalConfig = toml::from_str(
r#"
packages = ['cat', 'derby']
"#,
)
.unwrap();

let merged_config = merge_configuration_files(global, local, None);

let config = merged_config.unwrap();

let cat = config.files.get(&PathBuf::from("cat")).unwrap();

let derby = config.files.get(&PathBuf::from("derby")).unwrap();

assert_eq!(
cat,
&FileTarget::Symbolic(PathBuf::from("~/.QuarticCat").into())
);

assert_ne!(
derby,
&FileTarget::Symbolic(PathBuf::from("~/.DerbyLantern").into())
);

assert_eq!(
derby,
&FileTarget::ComplexTemplate(PathBuf::from("~/.DerbyLantern").into())
);
}

#[test]
fn setting_default_target_type_template() {
let global: GlobalConfig = toml::from_str(
r#"
[settings]
default_target_type = "template"
[cat]
depends = []
[cat.files]
cat = '~/.QuarticCat'
[derby]
depends = []
[derby.files]
derby = { target = '~/.DerbyLantern', type = 'symbolic' }
"#,
)
.unwrap();

let local: LocalConfig = toml::from_str(
r#"
packages = ['cat', 'derby']
"#,
)
.unwrap();

let merged_config = merge_configuration_files(global, local, None);

let config = merged_config.unwrap();

let cat = config.files.get(&PathBuf::from("cat")).unwrap();

let derby = config.files.get(&PathBuf::from("derby")).unwrap();

assert_eq!(
cat,
&FileTarget::ComplexTemplate(PathBuf::from("~/.QuarticCat").into())
);

assert_ne!(
derby,
&FileTarget::ComplexTemplate(PathBuf::from("~/.DerbyLantern").into())
);

assert_eq!(
derby,
&FileTarget::Symbolic(PathBuf::from("~/.DerbyLantern").into())
);
}

#[test]
fn setting_default_target_type_automatic() {
let global: GlobalConfig = toml::from_str(
r#"
[cat]
depends = []
[cat.files]
cat = '~/.QuarticCat'
[derby]
depends = []
[derby.files]
derby = { target = '~/.DerbyLantern', type = 'template' }
[sliver]
depends = []
[sliver.files]
sliver = { target = '~/.SliverBodacious', type = 'symbolic' }
"#,
)
.unwrap();

let local: LocalConfig = toml::from_str(
r#"
packages = ['cat', 'derby', 'sliver']
"#,
)
.unwrap();

let merged_config = merge_configuration_files(global, local, None);

let config = merged_config.unwrap();

let cat = config.files.get(&PathBuf::from("cat")).unwrap();

let derby = config.files.get(&PathBuf::from("derby")).unwrap();

let sliver = config.files.get(&PathBuf::from("sliver")).unwrap();

assert_eq!(
cat,
&FileTarget::Automatic(PathBuf::from("~/.QuarticCat").into())
);

assert_eq!(
derby,
&FileTarget::ComplexTemplate(PathBuf::from("~/.DerbyLantern").into())
);

assert_eq!(
sliver,
&FileTarget::Symbolic(PathBuf::from("~/.SliverBodacious").into())
);
}
}
4 changes: 4 additions & 0 deletions src/handlebars_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ fn add_dotter_variable(

#[cfg(test)]
mod test {
use crate::config::Settings;

use super::*;

#[test]
Expand All @@ -362,6 +364,7 @@ mod test {
helpers: Helpers::new(),
packages: maplit::btreemap! { "default".into() => true, "disabled".into() => false },
recurse: true,
settings: Settings::default(),
};
let handlebars = create_new_handlebars(&mut config).unwrap();

Expand All @@ -388,6 +391,7 @@ mod test {
helpers: Helpers::new(),
packages: BTreeMap::new(),
recurse: true,
settings: Settings::default(),
};
let handlebars = create_new_handlebars(&mut config).unwrap();

Expand Down

0 comments on commit 29396e7

Please sign in to comment.