Skip to content

Commit

Permalink
feat: Define regex option in [[files]]
Browse files Browse the repository at this point in the history
- This is flag to use regex-based replacement
- Default is false to keep compatibility
- when value is true, it is not working until
  • Loading branch information
attakei committed Apr 11, 2024
1 parent 9e0945b commit 8549acc
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 20 deletions.
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ pub struct FileConfig {
pub path: PathBuf,
pub search: String,
pub replace: String,
pub regex: Option<bool>,
}

impl FileConfig {
pub fn regex(&self) -> bool {
self.regex.is_some() && self.regex.unwrap()
}
}

pub trait ParseAvailable {
Expand Down
2 changes: 1 addition & 1 deletion src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl Workspace {
fn init_writer(&self, ctx: &Context) -> Writer {
let mut writer = Writer::new(&ctx.for_tera());
for f in &self.config.files {
writer.add_target(&f.path, &f.search, &f.replace);
writer.add_target(f);
}
writer
}
Expand Down
45 changes: 26 additions & 19 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::path::{Path, PathBuf};
use anyhow::Result;
use tera::{Context, Tera};

use crate::config::FileConfig;

/**
* File writer.
*/
Expand Down Expand Up @@ -34,6 +36,7 @@ pub struct WriteRule {
pub search: String,
/** Replacement content that is rendered by Tera. */
pub replace: String,
pub regex: bool,
}

impl Writer {
Expand All @@ -44,21 +47,23 @@ impl Writer {
}
}

pub fn add_target(&mut self, path: &Path, search: &str, replace: &str) {
let path_key = path.display().to_string();
pub fn add_target(&mut self, config: &FileConfig) {
let path_key = config.path.display().to_string();
if !self.targets.contains_key(&path_key) {
self.targets
.insert(path_key.clone(), WriteTarget::new(path));
.insert(path_key.clone(), WriteTarget::new(&config.path));
}
let target = self.targets.get_mut(&path_key).unwrap();
target.add_rule(
Tera::one_off(search, &self.context, true)
let rule = WriteRule {
search: Tera::one_off(&config.search, &self.context, true)
.unwrap()
.to_string(),
Tera::one_off(replace, &self.context, true)
replace: Tera::one_off(&config.replace, &self.context, true)
.unwrap()
.to_string(),
);
regex: config.regex(),
};
target.add_rule(rule);
}

pub fn update_all(&self) -> Result<()> {
Expand Down Expand Up @@ -87,8 +92,8 @@ impl WriteTarget {
Ok(())
}

pub fn add_rule(&mut self, search: String, replace: String) {
self.rules.push(WriteRule { search, replace });
pub fn add_rule(&mut self, rule: WriteRule) {
self.rules.push(rule);
}
}

Expand Down Expand Up @@ -137,16 +142,18 @@ mod tests {
ctx.insert("new_version", &Version::new(0, 2, 0));
let mut writer = Writer::new(&ctx);
let filepath = PathBuf::from("dummy.txt");
writer.add_target(
&filepath,
&String::from("target-1"),
&String::from("replace-2"),
);
writer.add_target(
&filepath,
&String::from("target-2"),
&String::from("replace-2"),
);
writer.add_target(&FileConfig {
path: filepath.clone(),
search: String::from("target-1"),
replace: String::from("replace-2"),
regex: Some(false),
});
writer.add_target(&FileConfig {
path: filepath.clone(),
search: String::from("target-2"),
replace: String::from("replace-2"),
regex: Some(false),
});
assert_eq!(writer.targets.len(), 1);
}
}
7 changes: 7 additions & 0 deletions tests/return-0/single-line-no-regex/after/.age.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
current_version = "0.2.0"

[[files]]
regex = false
path = "example.txt"
search = "version = '{{current_version}}'"
replace = "version = '{{new_version}}'"
1 change: 1 addition & 0 deletions tests/return-0/single-line-no-regex/after/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = '0.2.0'
7 changes: 7 additions & 0 deletions tests/return-0/single-line-no-regex/before/.age.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
current_version = "0.1.0"

[[files]]
regex = false
path = "example.txt"
search = "version = '{{current_version}}'"
replace = "version = '{{new_version}}'"
1 change: 1 addition & 0 deletions tests/return-0/single-line-no-regex/before/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = '0.1.0'

0 comments on commit 8549acc

Please sign in to comment.