diff --git a/src/app.rs b/src/app.rs index f5c7a11..ca05927 100644 --- a/src/app.rs +++ b/src/app.rs @@ -22,8 +22,14 @@ pub fn update(base_config: &Config, new_version: &Version) -> Result<()> { replace: "current_version = \\\"{{new_version}}\\\"".to_string(), }); for f in files { - let search_text = Tera::one_off(&f.search, &ctx, true).unwrap(); - let replace_text = Tera::one_off(&f.replace, &ctx, true).unwrap(); + let search_text = Tera::one_off(&f.search, &ctx, true) + .unwrap() + .trim_start() + .to_string(); + let replace_text = Tera::one_off(&f.replace, &ctx, true) + .unwrap() + .trim_start() + .to_string(); let new_text = build_updates(read_to_string(&f.path).unwrap(), search_text, replace_text); let mut out = File::create(&f.path)?; let _ = out.write(new_text.as_bytes()); @@ -41,11 +47,22 @@ fn make_context(current_version: &Version, new_version: &Version) -> Context { } fn build_updates(input: String, seach_text: String, replace_text: String) -> String { + let lines = seach_text.split("\n").count(); + println!("{}", lines); + let mut buf: Vec = Vec::new(); let mut output: Vec = Vec::new(); for line in input.split("\n") { - if line == seach_text { + if buf.len() == lines { + output.push(buf.pop().unwrap()); + } + buf.push(line.to_string()); + if buf.join("\n") == seach_text { output.push(replace_text.to_string()); - } else { + buf.clear(); + } + } + if buf.len() > 0 { + for line in buf { output.push(line.to_string()); } } diff --git a/tests/test_update.py b/tests/test_update.py index 91b2e0f..ca8ae3b 100644 --- a/tests/test_update.py +++ b/tests/test_update.py @@ -72,3 +72,33 @@ def test_valid_conf__single_line(cmd, tmp_path: Path): # noqa: D103 proc = cmd("0.2.0") assert proc.returncode == 0 assert data_txt.read_text() == "0.2.0" + + +def test_valid_conf__multi_line(cmd, tmp_path: Path): # noqa: D103 + conf_text = textwrap.dedent( + '''\ + current_version = "0.0.0" + + [[files]] + path = "data.txt" + search = """ + Target + ====== + """ + replace = """ + Target + ====== + + v {{new_version}} + ----------------- + """ + ''' + ) + (tmp_path / ".age.toml").write_text(conf_text) + data_txt = tmp_path / "data.txt" + data_txt.write_text("Target\n======\n") + + proc = cmd("0.2.0") + assert proc.returncode == 0 + assert len(data_txt.read_text().split("\n")) == 6 + assert "v 0.2.0" in data_txt.read_text()