Skip to content

Commit 1840db7

Browse files
committed
refactor: make profile template context global
1 parent f9ea3f9 commit 1840db7

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

src/dots.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ impl Dot {
2424
&self,
2525
vars: &Variables,
2626
auto_ignored: Vec<PathBuf>,
27-
profiles: &[String],
2827
) -> Result<LinkResult> {
2928
let source = &self.source()?;
3029
let target = &self.copy_path_unchecked();
@@ -47,7 +46,7 @@ impl Dot {
4746
}
4847

4948
// Recursively copy dotfile to .dots directory
50-
self.traverse_and_copy(source, target, ignored_paths.as_slice(), &vars, profiles)
49+
self.traverse_and_copy(source, target, ignored_paths.as_slice(), &vars)
5150
}
5251

5352
fn load_local_vars(source: &Path) -> Variables {
@@ -73,7 +72,6 @@ impl Dot {
7372
target: &PathBuf,
7473
ignored: &[PathBuf],
7574
vars: &Variables,
76-
profiles: &[String],
7775
) -> Result<LinkResult> {
7876
if ignored.contains(source) {
7977
return Ok(LinkResult::Ignored {
@@ -85,7 +83,7 @@ impl Dot {
8583
if source.is_file() {
8684
fs::create_dir_all(target.parent().unwrap())?;
8785

88-
match vars.to_dot(source, profiles) {
86+
match vars.to_dot(source) {
8987
Ok(content) if target.exists() => self.update(source, target, content),
9088
Ok(content) => self.create(source, target, content),
9189
Err(e) if target.exists() => {
@@ -95,10 +93,10 @@ impl Dot {
9593
// Those should be symlinked directly once this is implemented
9694
// https://github.com/oknozor/toml-bombadil/issues/138
9795
}
98-
ErrorKind::Msg(message) => println!("\t{}", message.red()),
9996
_ => {
10097
if let Some(source) = e.source() {
101-
println!("\t{}", source);
98+
let message = format!("{source}");
99+
println!("{}", message.red());
102100
}
103101
}
104102
}
@@ -124,7 +122,6 @@ impl Dot {
124122
&target.join(entry_name),
125123
ignored,
126124
vars,
127-
&[],
128125
);
129126

130127
match result {
@@ -401,7 +398,6 @@ mod tests {
401398
&PathBuf::from("dotfiles_with_multiple_nested_dir/.dots/dir"),
402399
&[],
403400
&Variables::default(),
404-
&[],
405401
)?;
406402

407403
run_cmd!(tree -a;)?;
@@ -435,7 +431,6 @@ mod tests {
435431
&PathBuf::from("dotfiles_non_utf8/.dots/ferris.png"),
436432
&[],
437433
&Variables::default(),
438-
&[],
439434
)?;
440435

441436
run_cmd!(tree -a;)?;
@@ -475,7 +470,6 @@ mod tests {
475470
PathBuf::from("source_dot/file.md"),
476471
],
477472
&Variables::default(),
478-
&[],
479473
)?;
480474

481475
// Assert
@@ -537,7 +531,7 @@ mod tests {
537531
vars: Dot::default_vars(),
538532
};
539533

540-
dot.install(&Variables::default(), vec![], &[])?;
534+
dot.install(&Variables::default(), vec![])?;
541535

542536
assert_that!(PathBuf::from(".dots")).exists();
543537
assert_that!(PathBuf::from(".dots/source_dot")).exists();
@@ -561,7 +555,7 @@ mod tests {
561555
let vars: Variables = toml::from_str(r#"name = "Tom Bombadil""#)?;
562556

563557
// Act
564-
dot.install(&vars, vec![], &[])?;
558+
dot.install(&vars, vec![])?;
565559
let dot = PathBuf::from(".dots/dotfiles/dot");
566560

567561
// Assert
@@ -598,7 +592,7 @@ mod tests {
598592
vars: PathBuf::from("my_vars.toml"),
599593
};
600594

601-
dot.install(&Variables::default(), vec![], &[])?;
595+
dot.install(&Variables::default(), vec![])?;
602596

603597
let content = fs::read_to_string(".dots/dir/template")?;
604598
assert_that!(content).is_equal_to(&"Hello Tom\n".to_string());
@@ -629,7 +623,7 @@ mod tests {
629623
};
630624

631625
// Arrange
632-
dot.install(&Variables::default(), vec![], &[])?;
626+
dot.install(&Variables::default(), vec![])?;
633627

634628
// Assert
635629
let content = fs::read_to_string(PathBuf::from(

src/lib.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::templating::Variables;
1111
use anyhow::{anyhow, Result};
1212
use colored::*;
1313
use ignore_files::IgnoreFilter;
14-
use serde_json::json;
14+
use serde_json::{json, Value};
1515
use settings::dots::Dot;
1616
use settings::Settings;
1717
use std::collections::HashMap;
@@ -172,12 +172,15 @@ impl Bombadil {
172172
self.vars.with_secrets(decrypted);
173173
}
174174

175+
let profiles_values = serde_json::to_value(&self.profile_enabled)?;
176+
let mut profiles_context = tera::Map::new();
177+
profiles_context.insert("profiles".to_string(), profiles_values);
178+
self.vars.extend(Variables {
179+
inner: Value::Object(profiles_context),
180+
});
181+
175182
for (key, dot) in self.dots.iter() {
176-
match dot.install(
177-
&self.vars,
178-
self.get_auto_ignored_files(key),
179-
self.profile_enabled.as_slice(),
180-
) {
183+
match dot.install(&self.vars, self.get_auto_ignored_files(key)) {
181184
Err(err) => errored.push(err),
182185
Ok(linked) => {
183186
match linked {
@@ -316,7 +319,7 @@ impl Bombadil {
316319
|| matches!(t, &Tag::FileEventKind(FileEventKind::Modify(_)))
317320
|| matches!(t, &Tag::FileEventKind(FileEventKind::Remove(_)))
318321
}) {
319-
println!("{}", "Detected changes, re-linking dots".green());
322+
println!("{}", "Detected changes, re-linking dots ...".green());
320323
// Finally, install the dots like usual
321324
b.install().map_err(|e| RuntimeError::Handler {
322325
ctx: "bombadil install",
@@ -380,19 +383,25 @@ impl Bombadil {
380383
.cloned()
381384
.collect();
382385

383-
let sub_profiles: Vec<Profile> = profiles
386+
let sub_profiles: Vec<(String, Profile)> = profiles
384387
.iter()
385388
.flat_map(|profile| {
386389
profile
387390
.extra_profiles
388391
.iter()
389-
.flat_map(|sub_profile| self.profiles.get(sub_profile))
390-
.collect::<Vec<&Profile>>()
392+
.flat_map(|sub_profile| {
393+
self.profiles
394+
.get(sub_profile)
395+
.map(|p| (sub_profile.clone(), p.clone()))
396+
})
397+
.collect::<Vec<(String, Profile)>>()
391398
})
392-
.cloned()
393399
.collect();
394400

395-
profiles.extend(sub_profiles);
401+
for (sub, profile) in sub_profiles {
402+
self.profile_enabled.push(sub);
403+
profiles.push(profile);
404+
}
396405

397406
// Merge profile dots
398407
for profile in profiles.iter() {

src/templating.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ impl Variables {
130130

131131
/// Read file in the given path and return its content
132132
/// with variable replaced by their values.
133-
pub(crate) fn to_dot(&self, path: &Path, profiles: &[String]) -> tera::Result<String> {
133+
pub(crate) fn to_dot(&self, path: &Path) -> tera::Result<String> {
134134
// Read file content
135135
let file = File::open(path)?;
136136
let mut buf_reader = BufReader::new(file);
@@ -140,9 +140,7 @@ impl Variables {
140140
// Create the tera context from variables and secrets.
141141
let mut context = tera::Context::new();
142142
let variable_context = Context::from_serialize(self.inner.clone())?;
143-
let profiles_context = serde_json::to_value(profiles)?;
144143
context.extend(variable_context);
145-
context.insert("profiles", &profiles_context);
146144
let mut tera = Tera::default();
147145
let filename = path.as_os_str().to_str().expect("Non UTF8 filename");
148146

@@ -199,7 +197,7 @@ mod test {
199197
let variables = Value::Object(variables);
200198

201199
let dot = Variables { inner: variables }
202-
.to_dot(Path::new("tests/dotfiles_simple/template.css"), &[])
200+
.to_dot(Path::new("tests/dotfiles_simple/template.css"))
203201
.unwrap();
204202

205203
assert_eq!(
@@ -225,7 +223,7 @@ mod test {
225223
variables.push_secret("pass", "hunter2");
226224

227225
let dot_content = variables
228-
.to_dot(Path::new("tests/dotfiles_with_secret/template"), &[])
226+
.to_dot(Path::new("tests/dotfiles_with_secret/template"))
229227
.unwrap();
230228

231229
assert_eq!(
@@ -244,7 +242,7 @@ mod test {
244242
let content = Variables {
245243
inner: Value::Object(Map::new()),
246244
}
247-
.to_dot(Path::new("tests/dotfiles_non_utf8/ferris.png"), &[]);
245+
.to_dot(Path::new("tests/dotfiles_non_utf8/ferris.png"));
248246

249247
assert_that!(content).is_err();
250248
}

0 commit comments

Comments
 (0)