Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding global variable to enable symlinks by default if type not specified #173

Merged
merged 5 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
199 changes: 199 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,8 @@ pub struct Configuration {
/// turned into a list of all the files inside the structure that
/// are readable.
pub recurse: bool,

pub settings: Settings,
}

#[derive(Debug, Deserialize, Serialize, Default)]
Expand All @@ -110,6 +127,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 +230,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 @@ -330,6 +350,7 @@ fn merge_configuration_files(
variables: Variables::default(),
packages: packages_map,
recurse: true,
settings: Settings::default(),
};

// Merge all the packages
Expand Down Expand Up @@ -373,6 +394,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 @@ -632,4 +667,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
Loading