Skip to content

Commit

Permalink
feat: Implement regex mode replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Apr 11, 2024
1 parent 8549acc commit d5b72ee
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 2 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ chrono = "0.4.35"
clap = { version = "4.5.2", features = ["derive"] }
env_logger = "0.11.3"
log = "0.4.21"
regex = "1.10.4"
semver = { version = "1.0.22", features = ["serde"] }
serde = { version = "1.0.197", features = ["derive"] }
tera = { version = "1.19.1", features = ["builtins"] }
Expand Down
6 changes: 6 additions & 0 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::io::prelude::*;
use std::path::{Path, PathBuf};

use anyhow::Result;
use regex::Regex;
use tera::{Context, Tera};

use crate::config::FileConfig;
Expand Down Expand Up @@ -99,6 +100,11 @@ impl WriteTarget {

impl WriteRule {
fn update(&self, target: String) -> String {
// If regex is enabled, func runs using Regex directly.
if self.regex {
let search = Regex::new(&self.search).unwrap();
return search.replace(&target, &self.replace).to_string();
}
let lines = self.search.split('\n').count();
let mut buf: VecDeque<String> = VecDeque::new();
let mut output: Vec<String> = Vec::new();
Expand Down
13 changes: 13 additions & 0 deletions tests/return-0/multi-line-regex/after/.age.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
current_version = "0.2.0"

[[files]]
path = "example.txt"
regex = true
search = """
version = '{{current_version}}'
(.+)
"""
replace = """
version = '{{new_version}}'
world
"""
4 changes: 4 additions & 0 deletions tests/return-0/multi-line-regex/after/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version = '0.2.0'
world

This line is kept.
13 changes: 13 additions & 0 deletions tests/return-0/multi-line-regex/before/.age.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
current_version = "0.1.0"

[[files]]
path = "example.txt"
regex = true
search = """
version = '{{current_version}}'
(.+)
"""
replace = """
version = '{{new_version}}'
world
"""
4 changes: 4 additions & 0 deletions tests/return-0/multi-line-regex/before/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
version = '0.1.0'
hello

This line is kept.

0 comments on commit d5b72ee

Please sign in to comment.