88
99use anyhow:: Context as _;
1010use clap:: Parser as _;
11+ use std:: borrow:: Cow ;
1112
1213/// Path to the shader crate
1314const SHADER_CRATE_PATH : & str = "crates/shader-crate-template" ;
@@ -20,6 +21,9 @@ enum Cli {
2021 /// Build using the specified version of `spirv-std`.
2122 #[ clap( long) ]
2223 rust_gpu_version : Option < String > ,
24+ /// The version of glam to use
25+ #[ clap( long) ]
26+ glam_version : Option < String > ,
2327 } ,
2428}
2529
@@ -128,52 +132,104 @@ impl ShaderCrateTemplateCargoTomlWriter {
128132 Ok ( ( ) )
129133 }
130134
135+ /// Add or replace a dependency in the shader-crate-template
136+ fn set_dependency (
137+ & mut self ,
138+ package : String ,
139+ version : DependencyVersion ,
140+ ) -> anyhow:: Result < ( ) > {
141+ if let Some ( version) = version. to_toml ( ) {
142+ let dependencies = self . get_cargo_dependencies_table ( ) ;
143+ dependencies. insert ( package, version) ;
144+ self . write_shader_crate_cargo_toml_changes ( ) ?;
145+ }
146+ Ok ( ( ) )
147+ }
148+
131149 /// Replace the `spirv-std` dependency version
132- fn replace_spirv_std_version ( & mut self , version : String ) -> anyhow:: Result < ( ) > {
133- let dependencies = self . get_cargo_dependencies_table ( ) ;
134- let spirv_std = dependencies. get_mut ( "spirv-std" ) . unwrap ( ) ;
135- if version. contains ( '.' ) {
136- // semver
137- * spirv_std = toml:: Value :: String ( version) ;
150+ fn set_spirv_std_version ( & mut self , version : & str ) -> anyhow:: Result < ( ) > {
151+ self . set_dependency (
152+ "spirv-std" . into ( ) ,
153+ DependencyVersion :: parse (
154+ version. into ( ) ,
155+ "https://github.com/Rust-GPU/rust-gpu" . into ( ) ,
156+ ) ,
157+ )
158+ }
159+
160+ /// Replace the `glam` dependency version
161+ fn set_dependency_glam ( & mut self , version : & str ) -> anyhow:: Result < ( ) > {
162+ self . set_dependency (
163+ "glam" . into ( ) ,
164+ DependencyVersion :: parse (
165+ version. into ( ) ,
166+ "https://github.com/bitshifter/glam-rs" . into ( ) ,
167+ ) ,
168+ )
169+ }
170+ }
171+
172+ /// The version of a dependency
173+ pub enum DependencyVersion < ' a > {
174+ /// Don't change anything, don't replace the dependency nor add it when it's not there.
175+ Latest ,
176+ /// A version dependency for crates.io
177+ Crates ( Cow < ' a , str > ) ,
178+ /// A git dependency on a specific rev
179+ Git {
180+ /// git repo
181+ git : Cow < ' a , str > ,
182+ /// git commit revision
183+ rev : Cow < ' a , str > ,
184+ } ,
185+ }
186+
187+ impl < ' a > DependencyVersion < ' a > {
188+ /// Try to parse a version from a string
189+ pub fn parse ( version : Cow < ' a , str > , git : Cow < ' a , str > ) -> Self {
190+ if version == "latest" {
191+ Self :: Latest
192+ } else if version. contains ( '.' ) {
193+ Self :: Crates ( version)
138194 } else {
139- // git rev
140- * spirv_std = toml:: Value :: Table ( toml:: Table :: from_iter ( [
141- (
142- "git" . to_owned ( ) ,
143- toml:: Value :: String ( "https://github.com/Rust-GPU/rust-gpu" . to_owned ( ) ) ,
144- ) ,
145- ( "rev" . to_owned ( ) , toml:: Value :: String ( version) ) ,
146- ] ) ) ;
195+ Self :: Git { git, rev : version }
196+ }
197+ }
198+
199+ /// Convert this version to a toml value, may fail if we want the latest version
200+ pub fn to_toml ( & self ) -> Option < toml:: Value > {
201+ match self {
202+ Self :: Latest => None ,
203+ Self :: Crates ( version) => Some ( toml:: Value :: String ( version. to_string ( ) ) ) ,
204+ Self :: Git { git, rev } => Some ( toml:: Value :: Table ( toml:: Table :: from_iter ( [
205+ ( "git" . to_owned ( ) , toml:: Value :: String ( git. to_string ( ) ) ) ,
206+ ( "rev" . to_owned ( ) , toml:: Value :: String ( rev. to_string ( ) ) ) ,
207+ ] ) ) ) ,
147208 }
148- self . write_shader_crate_cargo_toml_changes ( ) ?;
149- Ok ( ( ) )
150209 }
151210}
152211
153212/// Run the xtask.
154- fn main ( ) {
213+ fn main ( ) -> anyhow :: Result < ( ) > {
155214 env_logger:: builder ( ) . init ( ) ;
156-
157215 let cli = Cli :: parse ( ) ;
158-
159- match cli {
216+ match & cli {
160217 Cli :: TestBuild {
161218 rust_gpu_version : maybe_rust_gpu_version,
219+ glam_version,
162220 } => {
163221 log:: info!( "installing cargo gpu" ) ;
164- cmd ( [ "cargo" , "install" , "--path" , "crates/cargo-gpu" ] ) . unwrap ( ) ;
222+ cmd ( [ "cargo" , "install" , "--path" , "crates/cargo-gpu" ] ) ? ;
165223
166224 log:: info!( "setup project" ) ;
167- let dir = tempfile:: TempDir :: with_prefix ( "test-shader-output" ) . unwrap ( ) ;
168225 let mut overwriter = ShaderCrateTemplateCargoTomlWriter :: new ( ) ;
169- overwriter. replace_output_dir ( dir. path ( ) ) . unwrap ( ) ;
170-
171- if let Some ( rust_gpu_version) = maybe_rust_gpu_version {
172- if rust_gpu_version != "latest" {
173- overwriter
174- . replace_spirv_std_version ( rust_gpu_version)
175- . unwrap ( ) ;
176- }
226+ let dir = tempfile:: TempDir :: with_prefix ( "test-shader-output" ) ?;
227+ overwriter. replace_output_dir ( dir. path ( ) ) ?;
228+ if let Some ( rust_gpu_version) = maybe_rust_gpu_version. as_ref ( ) {
229+ overwriter. set_spirv_std_version ( & rust_gpu_version) ?;
230+ }
231+ if let Some ( glam_version) = glam_version. as_ref ( ) {
232+ overwriter. set_dependency_glam ( & glam_version) ?;
177233 }
178234
179235 log:: info!( "building with auto-install" ) ;
@@ -186,12 +242,12 @@ fn main() {
186242 "--auto-install-rust-toolchain" ,
187243 "--rebuild-codegen" ,
188244 "--force-overwrite-lockfiles-v4-to-v3" ,
189- ] )
190- . unwrap ( ) ;
245+ ] ) ?;
191246
192- cmd ( [ "ls" , "-lah" , dir. path ( ) . to_str ( ) . unwrap ( ) ] ) . unwrap ( ) ;
247+ cmd ( [ "ls" , "-lah" , dir. path ( ) . to_str ( ) . unwrap ( ) ] ) ? ;
193248 //NOTE: manifest.json is the default value here, which should be valid
194- cmd ( [ "cat" , dir. path ( ) . join ( "manifest.json" ) . to_str ( ) . unwrap ( ) ] ) . unwrap ( ) ;
249+ cmd ( [ "cat" , dir. path ( ) . join ( "manifest.json" ) . to_str ( ) . unwrap ( ) ] ) ? ;
195250 }
196251 }
252+ Ok ( ( ) )
197253}
0 commit comments