22#![ allow( clippy:: unwrap_used, reason = "this is basically a test" ) ]
33//! `cargo gpu build`, analogous to `cargo build`
44
5- use spirv_builder:: SpirvBuilder ;
6- use std:: path:: PathBuf ;
7-
8- #[ cfg( feature = "watch" ) ]
95use anyhow:: Context as _;
10- #[ cfg( feature = "watch" ) ]
11- use spirv_builder:: { CompileResult , ModuleResult } ;
12- #[ cfg( feature = "watch" ) ]
6+ use spirv_builder:: { CompileResult , ModuleResult , SpirvBuilder } ;
137use std:: io:: Write as _;
8+ use std:: path:: PathBuf ;
149
1510use crate :: install:: Install ;
16- use crate :: lockfile:: LockfileMismatchHandler ;
17-
18- #[ cfg( feature = "watch" ) ]
1911use crate :: linkage:: Linkage ;
12+ use crate :: lockfile:: LockfileMismatchHandler ;
2013
2114/// Args for just a build
2215#[ derive( Debug , Clone , serde:: Deserialize , serde:: Serialize ) ]
@@ -101,6 +94,22 @@ impl Build {
10194 std:: env:: current_dir( ) ?. display( )
10295 ) ;
10396
97+ self . build_or_watch ( )
98+ }
99+
100+ /// Builds shader crate locally or watches it for changes
101+ /// depending on presence of `watch` feature and `BuildArgs::watch` flag.
102+ fn build_or_watch ( & self ) -> anyhow:: Result < ( ) > {
103+ let build = || -> anyhow:: Result < ( ) > {
104+ crate :: user_output!(
105+ "Compiling shaders at {}...\n " ,
106+ self . install. shader_crate. display( )
107+ ) ;
108+ let result = self . build . spirv_builder . build ( ) ?;
109+ self . parse_compilation_result ( & result) ?;
110+ Ok ( ( ) )
111+ } ;
112+
104113 #[ cfg( feature = "watch" ) ]
105114 if self . build . watch {
106115 let this = self . clone ( ) ;
@@ -115,19 +124,16 @@ impl Build {
115124 . context ( "unreachable" ) ??;
116125 std:: thread:: park ( ) ;
117126 } else {
118- crate :: user_output!(
119- "Compiling shaders at {}...\n " ,
120- self . install. shader_crate. display( )
121- ) ;
122- let result = self . build . spirv_builder . build ( ) ?;
123- self . parse_compilation_result ( & result) ?;
127+ build ( ) ?;
124128 }
125129
130+ #[ cfg( not( feature = "watch" ) ) ]
131+ build ( ) ?;
132+
126133 Ok ( ( ) )
127134 }
128135
129136 /// Parses compilation result from `SpirvBuilder` and writes it out to a file
130- #[ cfg( feature = "watch" ) ]
131137 fn parse_compilation_result ( & self , result : & CompileResult ) -> anyhow:: Result < ( ) > {
132138 let shaders = match & result. module {
133139 ModuleResult :: MultiModule ( modules) => {
0 commit comments