11use std:: fs;
22use 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
78pub 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+
915pub 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
2345pub 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