Skip to content

Commit 7f0ef0a

Browse files
committed
Improve variable replacement
1 parent 43300fd commit 7f0ef0a

File tree

5 files changed

+47
-23
lines changed

5 files changed

+47
-23
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ anyhow = "1.0.94"
1313
base16ct = { version = "0.2.0", features = ["std"] }
1414
blake3 = "1.5.5"
1515
bzip2 = "0.4.4"
16-
camino = "1.1.9"
16+
camino = { version = "1.1.9", features = ["serde1"] }
1717
clap = { version = "4.5.23", features = ["derive"] }
1818
flate2 = "1.0.35"
1919
indicatif = "0.17.9"

src/commands/build.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use xz2::read::XzDecoder;
1818
use crate::{
1919
check_hash,
2020
package::{Info, Package, Source, StepVariant},
21-
replace_vars,
2221
};
2322

2423
pub async fn build() -> Result<()> {
@@ -28,9 +27,7 @@ pub async fn build() -> Result<()> {
2827
bail!("package.toml not found in the specified path.");
2928
}
3029

31-
let package: Package = toml_edit::de::from_str(&fs::read_to_string(package_path)?)?;
32-
33-
// dbg!(&package);
30+
let package = Package::parse(&fs::read_to_string(package_path)?)?;
3431

3532
let info = package.info;
3633
info!(
@@ -73,7 +70,7 @@ pub async fn build() -> Result<()> {
7370
StepVariant::Move { path } => {
7471
fs::create_dir_all(&path)?;
7572

76-
working_dir = path
73+
working_dir = path.into();
7774
}
7875
}
7976
}
@@ -85,7 +82,7 @@ pub async fn build() -> Result<()> {
8582
}
8683

8784
async fn fetch_and_verify_source(client: &Client, source: &Source, info: &Info) -> Result<PathBuf> {
88-
let url: Url = replace_vars(&source.url, info)?.as_ref().try_into()?;
85+
let url: Url = source.url.as_str().try_into()?;
8986

9087
let target_path = PathBuf::from(url.path_segments().unwrap().last().unwrap());
9188

src/lib.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,6 @@ pub fn check_hash<P: AsRef<Path>>(path: P, hash: &str) -> Result<bool> {
3434
Ok(hash == computed_hash)
3535
}
3636

37-
pub fn replace_vars<'a>(haystack: &'a str, info: &Info) -> Result<Cow<'a, str>> {
38-
let res = if let Some(captures) = VARIABLE_REGEX.captures(haystack) {
39-
match &captures[1] {
40-
"version" => VARIABLE_REGEX.replace_all(haystack, &info.version),
41-
_ => bail!("Wrong matcher"),
42-
}
43-
} else {
44-
Cow::Borrowed(haystack)
45-
};
46-
47-
Ok(res)
48-
}
49-
5037
fn _create_tarball<P: AsRef<Path>>(package_path: P, package: &Package) -> Result<()> {
5138
let tarball_name = format!("{}_{}.peach", package.info.name, package.info.version);
5239
let tarball_path = package_path.as_ref().join(&tarball_name);

src/package.rs

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
use std::{collections::HashMap, fmt::Display, path::PathBuf, process::Command, str::FromStr};
1+
use std::{collections::HashMap, fmt::Display, process::Command, str::FromStr, sync::LazyLock};
22

3-
use anyhow::anyhow;
3+
use anyhow::{anyhow, Result};
4+
use camino::Utf8PathBuf;
5+
use regex::{Captures, Regex};
46
use serde::{Deserialize, Serialize};
57
use serde_with::{serde_as, DisplayFromStr};
68
use spdx::Expression;
@@ -60,7 +62,7 @@ pub enum StepVariant {
6062
command: String,
6163
},
6264
Move {
63-
path: PathBuf,
65+
path: Utf8PathBuf,
6466
},
6567
}
6668

@@ -102,3 +104,38 @@ impl Runner {
102104
}
103105
}
104106
}
107+
108+
static VARIABLE_REGEX: LazyLock<Regex> =
109+
LazyLock::new(|| Regex::new(r"%\{([^}]+)\}").expect("invalid regex"));
110+
111+
impl Package {
112+
pub fn parse(s: &str) -> Result<Self> {
113+
let mut package: Package = toml_edit::de::from_str(s)?;
114+
115+
let mut variables = HashMap::new();
116+
variables.insert("version", package.info.version.as_str());
117+
118+
for source in package.sources.iter_mut() {
119+
source.url = replace_vars(&source.url, &variables)
120+
}
121+
122+
for step in package.steps.iter_mut() {
123+
match &mut step.variant {
124+
StepVariant::Command { .. } => {}
125+
StepVariant::Move { path } => {
126+
*path = replace_vars(path.as_str(), &variables).into();
127+
}
128+
}
129+
}
130+
131+
Ok(package)
132+
}
133+
}
134+
135+
fn replace_vars<'h>(haystack: &'h str, variables: &HashMap<&str, &str>) -> String {
136+
VARIABLE_REGEX
137+
.replace_all(haystack, |caps: &Captures| {
138+
variables.get(&caps[1]).expect("Unknown variable") // FIXME: error handling
139+
})
140+
.into_owned()
141+
}

0 commit comments

Comments
 (0)