Skip to content

Commit

Permalink
fix: parse for pattern includeed replacement targets
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Apr 5, 2024
1 parent 517a6bb commit 3a37819
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/config/cargo_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io::prelude::*;
use std::path::PathBuf;

use anyhow::{anyhow, Result};
use toml::{de::Error, Table};
use toml_edit::{value, DocumentMut};

use super::{Config, ParseAvaliable};
Expand Down Expand Up @@ -37,15 +38,19 @@ impl ParseAvaliable for Property {
}

fn get_config(&self) -> Result<Config> {
let mut item = self.doc.as_item();
let mut item = &self.doc.to_string().parse::<Table>().unwrap();
for k in ["package", "metadata", "age"] {
let child = item.get(k);
if child.is_none() || !child.unwrap().is_table() {
if child.is_none() {
return Err(anyhow!("It does not have valid values."));
}
item = child.unwrap();
let child = child.unwrap();
if !child.is_table() {
return Err(anyhow!("It does not have valid values."));
}
item = child.as_table().unwrap();
}
let config = toml::from_str::<Config>(item.to_string().as_str());
let config: Result<Config, Error> = toml::from_str(item.to_string().as_str());
if config.is_err() {
return Err(anyhow!(config.unwrap_err()));
}
Expand Down
13 changes: 9 additions & 4 deletions src/config/pyproject_toml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::io::prelude::*;
use std::path::PathBuf;

use anyhow::{anyhow, Result};
use toml::{de::Error, Table};
use toml_edit::{value, DocumentMut};

use super::{Config, ParseAvaliable};
Expand Down Expand Up @@ -37,15 +38,19 @@ impl ParseAvaliable for Property {
}

fn get_config(&self) -> Result<Config> {
let mut item = self.doc.as_item();
let mut item = &self.doc.to_string().parse::<Table>().unwrap();
for k in ["tool", "age"] {
let child = item.get(k);
if child.is_none() || !child.unwrap().is_table() {
if child.is_none() {
return Err(anyhow!("It does not have valid values."));
}
item = child.unwrap();
let child = child.unwrap();
if !child.is_table() {
return Err(anyhow!("It does not have valid values."));
}
item = child.as_table().unwrap();
}
let config = toml::from_str::<Config>(item.to_string().as_str());
let config: Result<Config, Error> = toml::from_str(item.to_string().as_str());
if config.is_err() {
return Err(anyhow!(config.unwrap_err()));
}
Expand Down
16 changes: 16 additions & 0 deletions tests/return-0/single-line-with-cargo-toml/after/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "dummy"
version = "0.1.0"
edition = "2021"

[package.metadata.age]
current_version = "0.2.0"

[[package.metadata.age.files]]
path = "example.txt"
search = "version = '{{current_version}}'"
replace = "version = '{{new_version}}'"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = '0.2.0'
16 changes: 16 additions & 0 deletions tests/return-0/single-line-with-cargo-toml/before/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "dummy"
version = "0.1.0"
edition = "2021"

[package.metadata.age]
current_version = "0.1.0"

[[package.metadata.age.files]]
path = "example.txt"
search = "version = '{{current_version}}'"
replace = "version = '{{new_version}}'"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = '0.1.0'
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = '0.2.0'
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = "dummy"
version = "0.1.0"

[tool.age]
current_version = "0.2.0"

[[tool.age.files]]
path = "example.txt"
search = "version = '{{current_version}}'"
replace = "version = '{{new_version}}'"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = '0.1.0'
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[project]
name = "dummy"
version = "0.1.0"

[tool.age]
current_version = "0.1.0"

[[tool.age.files]]
path = "example.txt"
search = "version = '{{current_version}}'"
replace = "version = '{{new_version}}'"

0 comments on commit 3a37819

Please sign in to comment.