11use std:: env;
22use std:: fs:: { self } ;
3- use std:: path:: PathBuf ;
3+ use std:: path:: { Path , PathBuf } ;
4+ use std:: process:: Command ;
45
56fn workspace_dir ( ) -> anyhow:: Result < PathBuf > {
67 let manifest_dir = env:: var ( "CARGO_MANIFEST_DIR" ) . expect ( "CARGO_MANIFEST_DIR not set" ) ;
@@ -20,11 +21,132 @@ fn workspace_dir() -> anyhow::Result<PathBuf> {
2021 }
2122 }
2223}
24+
25+ fn build_fixtures_rust ( workspace_dir : & Path ) -> anyhow:: Result < ( ) > {
26+ let examples_dir = workspace_dir. join ( "examples" ) ;
27+ let fixtures_dir = workspace_dir. join ( "crates/wash-runtime/tests/fixtures" ) ;
28+
29+ // Create fixtures directory if it doesn't exist
30+ fs:: create_dir_all ( & fixtures_dir) ?;
31+
32+ if !examples_dir. exists ( ) {
33+ println ! ( "No examples dir found at {}" , examples_dir. display( ) ) ;
34+ return Ok ( ( ) ) ;
35+ }
36+
37+ let include_list = [
38+ "http-counter" ,
39+ "cron-service" ,
40+ "cron-component" ,
41+ "http-blobstore" ,
42+ "http-webgpu" ,
43+ ] ;
44+
45+ // Iterate through example directories
46+ for entry in fs:: read_dir ( & examples_dir) ? {
47+ let entry = entry?;
48+ let path = entry. path ( ) ;
49+
50+ if !path. is_dir ( ) {
51+ continue ;
52+ }
53+
54+ let cargo_toml = path. join ( "Cargo.toml" ) ;
55+ if !cargo_toml. exists ( ) {
56+ continue ;
57+ }
58+
59+ let name = path
60+ . file_name ( )
61+ . and_then ( |n| n. to_str ( ) )
62+ . ok_or_else ( || anyhow:: anyhow!( "Invalid directory name" ) ) ?;
63+
64+ // Check if name is in include list
65+ if !include_list. contains ( & name) {
66+ continue ;
67+ }
68+
69+ println ! ( "cargo:warning=Building example: {}" , name) ;
70+
71+ // Build the example
72+ let status = Command :: new ( "cargo" )
73+ . args ( [ "build" , "--target" , "wasm32-wasip2" , "--release" ] )
74+ . current_dir ( & path)
75+ . status ( ) ;
76+
77+ match status {
78+ Ok ( s) if s. success ( ) => {
79+ // Copy wasm artifacts
80+ let artifact_dir = path. join ( "target/wasm32-wasip2/release" ) ;
81+ if artifact_dir. exists ( ) {
82+ for wasm_entry in fs:: read_dir ( & artifact_dir) ? {
83+ let wasm_entry = wasm_entry?;
84+ let wasm_path = wasm_entry. path ( ) ;
85+
86+ if wasm_path. extension ( ) . and_then ( |s| s. to_str ( ) ) == Some ( "wasm" ) {
87+ let dest = fixtures_dir. join ( wasm_path. file_name ( ) . unwrap ( ) ) ;
88+ fs:: copy ( & wasm_path, & dest) ?;
89+ }
90+ }
91+ }
92+ }
93+ Ok ( _) => {
94+ eprintln ! ( "Failed to build {}" , name) ;
95+ continue ;
96+ }
97+ Err ( e) => {
98+ eprintln ! ( "Failed to execute cargo for {}: {}" , name, e) ;
99+ continue ;
100+ }
101+ }
102+ }
103+
104+ println ! ( "cargo:warning=Finished building fixtures." ) ;
105+ Ok ( ( ) )
106+ }
107+
108+ fn check_and_rebuild_fixtures ( workspace_dir : & Path ) -> anyhow:: Result < ( ) > {
109+ let examples_dir = workspace_dir. join ( "examples" ) ;
110+ let fixtures_dir = workspace_dir. join ( "crates/wash-runtime/tests/fixtures" ) ;
111+
112+ // Tell cargo to rerun this build script if examples change
113+ if examples_dir. exists ( ) {
114+ println ! ( "cargo:rerun-if-changed={}" , examples_dir. display( ) ) ;
115+
116+ // Track specific example directories we care about
117+ let tracked_examples = [
118+ "http-counter" ,
119+ "cron-service" ,
120+ "cron-component" ,
121+ "http-blobstore" ,
122+ "http-webgpu" ,
123+ ] ;
124+ for example in tracked_examples {
125+ let example_dir = examples_dir. join ( example) ;
126+ if example_dir. exists ( ) {
127+ println ! ( "cargo:rerun-if-changed={}" , example_dir. display( ) ) ;
128+ }
129+ }
130+ }
131+
132+ // Also rerun if fixtures themselves are missing/changed
133+ println ! ( "cargo:rerun-if-changed={}" , fixtures_dir. display( ) ) ;
134+
135+ println ! ( "cargo:warning=Rebuilding test fixtures from examples..." ) ;
136+ build_fixtures_rust ( workspace_dir) ?;
137+
138+ Ok ( ( ) )
139+ }
140+
23141fn main ( ) {
24142 let out_dir = PathBuf :: from (
25143 env:: var ( "OUT_DIR" ) . expect ( "failed to look up `OUT_DIR` from environment variables" ) ,
26144 ) ;
27145 let workspace_dir = workspace_dir ( ) . expect ( "failed to get workspace dir" ) ;
146+
147+ // Rebuild fixtures if examples changed
148+ check_and_rebuild_fixtures ( & workspace_dir) . expect ( "failed to check/rebuild fixtures" ) ;
149+
28150 let top_proto_dir = workspace_dir. join ( "proto" ) ;
29151 let proto_dir = top_proto_dir. join ( "wasmcloud/runtime/v2" ) ;
30152
0 commit comments