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
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
92 changes: 92 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ struct GlobalConfig {
helpers: Helpers,
#[serde(flatten)]
packages: BTreeMap<String, Package>,
variables: Option<Variables>,
SuperCuber marked this conversation as resolved.
Show resolved Hide resolved
}

type IncludedConfig = BTreeMap<String, Package>;
Expand Down Expand Up @@ -211,6 +212,7 @@ pub fn save_dummy_config(
#[cfg(feature = "scripting")]
helpers: Helpers::new(),
packages,
variables: None,
};
debug!("Saving global config...");
// Assume default args so all parents are the same
Expand Down Expand Up @@ -373,6 +375,36 @@ fn merge_configuration_files(
output.files = first_package.files;
output.variables = first_package.variables;

// Defaults package target type to symlink if
// enable_symlink_by_default = true and
// package is not explicitly set as template
if let Some(variables) = global.variables {
if let Some(enable_symlink_by_default) =
variables
.get("enable_symlink_by_default")
.and_then(toml::Value::as_bool) {
if enable_symlink_by_default {
SuperCuber marked this conversation as resolved.
Show resolved Hide resolved
output.files = output
.files
.into_iter()
.map(|(name, target)| -> Result<_, anyhow::Error> {
SuperCuber marked this conversation as resolved.
Show resolved Hide resolved
let t: FileTarget;

match target {
SuperCuber marked this conversation as resolved.
Show resolved Hide resolved
FileTarget::Automatic(target) => {
t = FileTarget::Symbolic(
SymbolicTarget::from(target)
);
},
_ => t = target,
}
Ok((name, t))
})
.collect::<Result<_, _>>()?;
}
}
}

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

#[test]
fn enable_symlink_by_default() {
let global: GlobalConfig = toml::from_str(
r#"
[variables]
enable_symlink_by_default = true

[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())
);
}
}
Loading