@@ -20,6 +20,9 @@ enum Cli {
2020 /// Build using the specified version of `spirv-std`.
2121 #[ clap( long) ]
2222 rust_gpu_version : Option < String > ,
23+ /// The version of glam to use
24+ #[ clap( long) ]
25+ glam_version : Option < String > ,
2326 } ,
2427}
2528
@@ -128,52 +131,112 @@ impl ShaderCrateTemplateCargoTomlWriter {
128131 Ok ( ( ) )
129132 }
130133
134+ /// Add or replace a dependency in the shader-crate-template
135+ fn set_dependency (
136+ & mut self ,
137+ package : String ,
138+ version : & DependencyVersion ,
139+ ) -> anyhow:: Result < ( ) > {
140+ if let Some ( version) = version. to_toml ( ) {
141+ let dependencies = self . get_cargo_dependencies_table ( ) ;
142+ dependencies. insert ( package, version) ;
143+ self . write_shader_crate_cargo_toml_changes ( ) ?;
144+ }
145+ Ok ( ( ) )
146+ }
147+
131148 /// 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) ;
149+ fn set_spirv_std_version ( & mut self , version : & str ) -> anyhow:: Result < ( ) > {
150+ self . set_dependency (
151+ "spirv-std" . into ( ) ,
152+ & DependencyVersion :: parse (
153+ version. into ( ) ,
154+ Some ( "https://github.com/Rust-GPU/rust-gpu" . into ( ) ) ,
155+ ) ?,
156+ )
157+ }
158+
159+ /// Replace the `glam` dependency version
160+ fn set_dependency_glam ( & mut self , version : & str ) -> anyhow:: Result < ( ) > {
161+ self . set_dependency (
162+ "glam" . into ( ) ,
163+ & DependencyVersion :: parse (
164+ version. into ( ) ,
165+ Some ( "https://github.com/bitshifter/glam-rs" . into ( ) ) ,
166+ ) ?,
167+ )
168+ }
169+ }
170+
171+ /// The version of a dependency
172+ #[ non_exhaustive]
173+ pub enum DependencyVersion {
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 ( String ) ,
178+ /// A git dependency on a specific rev
179+ Git {
180+ /// git repo
181+ git : String ,
182+ /// git commit revision
183+ rev : String ,
184+ } ,
185+ }
186+
187+ impl DependencyVersion {
188+ /// Try to parse a version from a string
189+ ///
190+ /// # Errors
191+ /// if `version` is a commit rev, `git` must be specified
192+ pub fn parse ( version : String , git : Option < String > ) -> anyhow:: Result < Self > {
193+ if version == "latest" {
194+ Ok ( Self :: Latest )
195+ } else if version. contains ( '.' ) {
196+ Ok ( Self :: Crates ( version) )
138197 } 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- ] ) ) ;
198+ Ok ( Self :: Git {
199+ git : git. context ( "specifying a revision requires a git repo" ) ?,
200+ rev : version,
201+ } )
202+ }
203+ }
204+
205+ /// Convert this version to a toml value, may fail if we want the latest version
206+ #[ must_use]
207+ pub fn to_toml ( & self ) -> Option < toml:: Value > {
208+ match self {
209+ Self :: Latest => None ,
210+ Self :: Crates ( version) => Some ( toml:: Value :: String ( version. clone ( ) ) ) ,
211+ Self :: Git { git, rev } => Some ( toml:: Value :: Table ( toml:: Table :: from_iter ( [
212+ ( "git" . to_owned ( ) , toml:: Value :: String ( git. clone ( ) ) ) ,
213+ ( "rev" . to_owned ( ) , toml:: Value :: String ( rev. clone ( ) ) ) ,
214+ ] ) ) ) ,
147215 }
148- self . write_shader_crate_cargo_toml_changes ( ) ?;
149- Ok ( ( ) )
150216 }
151217}
152218
153219/// Run the xtask.
154- fn main ( ) {
220+ fn main ( ) -> anyhow :: Result < ( ) > {
155221 env_logger:: builder ( ) . init ( ) ;
156-
157222 let cli = Cli :: parse ( ) ;
158-
159- match cli {
223+ match & cli {
160224 Cli :: TestBuild {
161- rust_gpu_version : maybe_rust_gpu_version,
225+ rust_gpu_version,
226+ glam_version,
162227 } => {
163228 log:: info!( "installing cargo gpu" ) ;
164- cmd ( [ "cargo" , "install" , "--path" , "crates/cargo-gpu" ] ) . unwrap ( ) ;
229+ cmd ( [ "cargo" , "install" , "--path" , "crates/cargo-gpu" ] ) ? ;
165230
166231 log:: info!( "setup project" ) ;
167- let dir = tempfile:: TempDir :: with_prefix ( "test-shader-output" ) . unwrap ( ) ;
168232 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- }
233+ let dir = tempfile:: TempDir :: with_prefix ( "test-shader-output" ) ?;
234+ overwriter. replace_output_dir ( dir. path ( ) ) ?;
235+ if let Some ( rust_gpu_version) = rust_gpu_version. as_ref ( ) {
236+ overwriter. set_spirv_std_version ( rust_gpu_version) ?;
237+ }
238+ if let Some ( glam_version) = glam_version. as_ref ( ) {
239+ overwriter. set_dependency_glam ( glam_version) ?;
177240 }
178241
179242 log:: info!( "building with auto-install" ) ;
@@ -186,12 +249,12 @@ fn main() {
186249 "--auto-install-rust-toolchain" ,
187250 "--rebuild-codegen" ,
188251 "--force-overwrite-lockfiles-v4-to-v3" ,
189- ] )
190- . unwrap ( ) ;
252+ ] ) ?;
191253
192- cmd ( [ "ls" , "-lah" , dir. path ( ) . to_str ( ) . unwrap ( ) ] ) . unwrap ( ) ;
254+ cmd ( [ "ls" , "-lah" , dir. path ( ) . to_str ( ) . unwrap ( ) ] ) ? ;
193255 //NOTE: manifest.json is the default value here, which should be valid
194- cmd ( [ "cat" , dir. path ( ) . join ( "manifest.json" ) . to_str ( ) . unwrap ( ) ] ) . unwrap ( ) ;
256+ cmd ( [ "cat" , dir. path ( ) . join ( "manifest.json" ) . to_str ( ) . unwrap ( ) ] ) ? ;
195257 }
196258 }
259+ Ok ( ( ) )
197260}
0 commit comments