Skip to content

Commit 59685ab

Browse files
committed
use github api and toml crate instead of ad-hoc parsing
1 parent 416b5ac commit 59685ab

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

Cargo.lock

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

tools/build-examples/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ edition = "2021"
55
publish = false
66

77
[dependencies]
8-
reqwest = { version = "0.12.12", features = ["blocking"] }
8+
reqwest = { version = "0.12.12", features = ["blocking", "json"] }
99
regex = "1.11.1"
10-
10+
toml = "0.8.20"
11+
serde = { version = "1.0.218", features = ["derive"] }

tools/build-examples/src/lib.rs

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,45 @@
11
use std::fs;
22
use std::path::Path;
33

4-
use regex::Regex;
4+
use serde::Deserialize;
5+
use toml::Table;
56

67
/// Examples that don't use Trunk for building
78
pub const NO_TRUNK_EXAMPLES: [&str; 3] = ["simple_ssr", "ssr_router", "wasi_ssr_module"];
89

10+
#[derive(Deserialize)]
11+
struct GitHubRelease {
12+
tag_name: String,
13+
}
14+
915
pub fn get_latest_wasm_opt_version() -> String {
10-
let url = "https://github.com/WebAssembly/binaryen/releases";
16+
let url = "https://api.github.com/repos/WebAssembly/binaryen/releases/latest";
1117
let client = reqwest::blocking::Client::new();
12-
let res = client.get(url).send().unwrap();
13-
let body = res.text().unwrap();
14-
let re = Regex::new(r#"version_(\d+)"#).unwrap();
15-
let captures = re.captures_iter(&body);
16-
let mut versions: Vec<u32> = captures
17-
.map(|c| c.get(1).unwrap().as_str().parse().unwrap())
18-
.collect();
19-
versions.sort();
20-
format!("version_{}", versions.last().unwrap())
18+
19+
// github api requires a user agent
20+
// https://docs.github.com/en/rest/using-the-rest-api/troubleshooting-the-rest-api?apiVersion=2022-11-28#user-agent-required
21+
let req_builder = client.get(url).header("User-Agent", "yew-wasm-opt-checker");
22+
23+
// Send the request
24+
let res = req_builder
25+
.send()
26+
.expect("Failed to send request to GitHub API");
27+
28+
if !res.status().is_success() {
29+
// Get more details about the error
30+
let status = res.status();
31+
let error_text = res
32+
.text()
33+
.unwrap_or_else(|_| "Could not read error response".to_string());
34+
35+
panic!(
36+
"GitHub API request failed with status: {}. Details: {}",
37+
status, error_text
38+
);
39+
}
40+
41+
let release: GitHubRelease = res.json().expect("Failed to parse GitHub API response");
42+
release.tag_name
2143
}
2244

2345
pub fn is_wasm_opt_outdated(path: &Path, latest_version: &str) -> bool {
@@ -33,12 +55,8 @@ pub fn is_wasm_opt_outdated(path: &Path, latest_version: &str) -> bool {
3355
};
3456

3557
// Check if wasm_opt is configured and up-to-date
36-
let re = Regex::new(r#"(?m)^\[tools\]\s*\nwasm_opt\s*=\s*"(version_\d+)""#).unwrap();
37-
match re.captures(&content) {
38-
Some(captures) => {
39-
let current_version = captures.get(1).unwrap().as_str();
40-
current_version != latest_version
41-
}
42-
None => true,
43-
}
58+
let table: Table = toml::from_str(&content).unwrap();
59+
let tools = table.get("tools").unwrap().as_table().unwrap();
60+
let wasm_opt = tools.get("wasm_opt").unwrap().as_str().unwrap();
61+
wasm_opt != latest_version
4462
}

0 commit comments

Comments
 (0)