From d5b72ee3d4536f61e109166130c325b32f74084c Mon Sep 17 00:00:00 2001 From: Kazuya Takei Date: Thu, 11 Apr 2024 23:06:58 +0900 Subject: [PATCH] feat: Implement regex mode replacement --- Cargo.lock | 5 +++-- Cargo.toml | 1 + src/writer.rs | 6 ++++++ tests/return-0/multi-line-regex/after/.age.toml | 13 +++++++++++++ tests/return-0/multi-line-regex/after/example.txt | 4 ++++ tests/return-0/multi-line-regex/before/.age.toml | 13 +++++++++++++ tests/return-0/multi-line-regex/before/example.txt | 4 ++++ 7 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 tests/return-0/multi-line-regex/after/.age.toml create mode 100644 tests/return-0/multi-line-regex/after/example.txt create mode 100644 tests/return-0/multi-line-regex/before/.age.toml create mode 100644 tests/return-0/multi-line-regex/before/example.txt diff --git a/Cargo.lock b/Cargo.lock index 9327b9a..f391f36 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,7 @@ dependencies = [ "clap", "env_logger", "log", + "regex", "semver", "serde", "tera", @@ -657,9 +658,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.3" +version = "1.10.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" +checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c" dependencies = [ "aho-corasick", "memchr", diff --git a/Cargo.toml b/Cargo.toml index 8ae2233..292ce6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/src/writer.rs b/src/writer.rs index 162f918..68180eb 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -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; @@ -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 = VecDeque::new(); let mut output: Vec = Vec::new(); diff --git a/tests/return-0/multi-line-regex/after/.age.toml b/tests/return-0/multi-line-regex/after/.age.toml new file mode 100644 index 0000000..f069c07 --- /dev/null +++ b/tests/return-0/multi-line-regex/after/.age.toml @@ -0,0 +1,13 @@ +current_version = "0.2.0" + +[[files]] +path = "example.txt" +regex = true +search = """ +version = '{{current_version}}' +(.+) +""" +replace = """ +version = '{{new_version}}' +world +""" diff --git a/tests/return-0/multi-line-regex/after/example.txt b/tests/return-0/multi-line-regex/after/example.txt new file mode 100644 index 0000000..6d379b6 --- /dev/null +++ b/tests/return-0/multi-line-regex/after/example.txt @@ -0,0 +1,4 @@ +version = '0.2.0' +world + +This line is kept. diff --git a/tests/return-0/multi-line-regex/before/.age.toml b/tests/return-0/multi-line-regex/before/.age.toml new file mode 100644 index 0000000..0c708ac --- /dev/null +++ b/tests/return-0/multi-line-regex/before/.age.toml @@ -0,0 +1,13 @@ +current_version = "0.1.0" + +[[files]] +path = "example.txt" +regex = true +search = """ +version = '{{current_version}}' +(.+) +""" +replace = """ +version = '{{new_version}}' +world +""" diff --git a/tests/return-0/multi-line-regex/before/example.txt b/tests/return-0/multi-line-regex/before/example.txt new file mode 100644 index 0000000..648a597 --- /dev/null +++ b/tests/return-0/multi-line-regex/before/example.txt @@ -0,0 +1,4 @@ +version = '0.1.0' +hello + +This line is kept.