11use std:: {
2+ error,
23 fs,
34 io,
4- path:: Path ,
5+ path:: {
6+ Path ,
7+ PathBuf ,
8+ } ,
9+ process,
510} ;
611
712use clap:: {
@@ -28,17 +33,17 @@ enum Command {
2833
2934 /// Create distribution-ready files (completions and multicall binaries).
3035 Dist {
31- /// Directory to install shell completions
36+ /// Directory to install shell completions.
3237 #[ arg( long, default_value = "completions" ) ]
33- completions_dir : String ,
38+ completions_dir : PathBuf ,
3439
35- /// Directory to install multicall binaries
40+ /// Directory to install multicall binaries.
3641 #[ arg( long, default_value = "bin" ) ]
37- bin_dir : String ,
42+ bin_dir : PathBuf ,
3843
39- /// Path to the watt binary
44+ /// Path to the watt binary.
4045 #[ arg( long, default_value = "target/release/watt" ) ]
41- watt_binary : String ,
46+ watt_binary : PathBuf ,
4247 } ,
4348}
4449
@@ -107,49 +112,51 @@ fn main() {
107112
108113 match cli. command {
109114 Command :: GenerateCompletions { shell, binary } => {
110- generate_completion ( shell, binary) ;
115+ let mut command = binary. command ( ) ;
116+ command. set_bin_name ( binary. name ( ) ) ;
117+ command. build ( ) ;
118+
119+ shell. generator ( ) . generate ( & command, & mut io:: stdout ( ) ) ;
111120 } ,
112121
113122 Command :: Dist {
114123 completions_dir,
115124 bin_dir,
116125 watt_binary,
117126 } => {
118- if let Err ( e ) =
127+ if let Err ( error ) =
119128 create_dist_files ( & completions_dir, & bin_dir, & watt_binary)
120129 {
121- eprintln ! ( "Error creating distribution files: {e }" ) ;
122- std :: process:: exit ( 1 ) ;
130+ eprintln ! ( "error creating distribution files: {error }" ) ;
131+ process:: exit ( 1 ) ;
123132 }
124133 } ,
125134 }
126135}
127136
128- fn generate_completion ( shell : Shell , binary : Binary ) {
129- let mut command = binary. command ( ) ;
130- command. set_bin_name ( binary. name ( ) ) ;
131- command. build ( ) ;
132-
133- shell. generator ( ) . generate ( & command, & mut io:: stdout ( ) ) ;
134- }
135-
136- /// Create distribution files
137+ /// Create distribution files.
137138fn create_dist_files (
138- completions_dir : & str ,
139- bin_dir : & str ,
140- watt_binary : & str ,
141- ) -> Result < ( ) , Box < dyn std :: error:: Error > > {
142- println ! ( "Creating distribution files..." ) ;
139+ completions_dir : & Path ,
140+ bin_dir : & Path ,
141+ watt_path : & Path ,
142+ ) -> Result < ( ) , Box < dyn error:: Error > > {
143+ println ! ( "creating distribution files..." ) ;
143144
144- // Create directories if they don't exist
145+ // Create directories if they don't exist.
145146 fs:: create_dir_all ( completions_dir) ?;
146147 fs:: create_dir_all ( bin_dir) ?;
147148
148- if !Path :: new ( watt_binary) . exists ( ) {
149- return Err ( format ! ( "Watt binary not found at: {watt_binary}" ) . into ( ) ) ;
149+ if !watt_path. exists ( ) {
150+ return Err (
151+ format ! (
152+ "watt binary not found at: {path}" ,
153+ path = watt_path. display( ) ,
154+ )
155+ . into ( ) ,
156+ ) ;
150157 }
151158
152- println ! ( "Generating shell completions..." ) ;
159+ println ! ( "generating shell completions..." ) ;
153160
154161 let shells = [
155162 Shell :: Bash ,
@@ -162,30 +169,34 @@ fn create_dist_files(
162169
163170 let binaries = [ Binary :: Watt , Binary :: Cpu , Binary :: Power ] ;
164171
165- for & shell in & shells {
166- for & binary in & binaries {
172+ for shell in shells {
173+ for binary in binaries {
174+ let ( prefix, ext) = shell. file_parts ( ) ;
175+ let mut path =
176+ completions_dir. join ( format ! ( "{prefix}{name}" , name = binary. name( ) ) ) ;
177+ path. set_extension ( ext) ;
178+
179+ let mut file = fs:: File :: create ( & path) ?;
180+
167181 let mut command = binary. command ( ) ;
168182 command. set_bin_name ( binary. name ( ) ) ;
169183 command. build ( ) ;
170184
171- let ( prefix, ext) = shell. file_parts ( ) ;
172- let filename = match ext {
173- "" => format ! ( "{prefix}{}" , binary. name( ) ) ,
174- ext => format ! ( "{prefix}{}.{ext}" , binary. name( ) ) ,
175- } ;
176-
177- let completion_file = Path :: new ( completions_dir) . join ( filename) ;
178- let mut file = fs:: File :: create ( & completion_file) ?;
179185 shell. generator ( ) . generate ( & command, & mut file) ;
180186
181- println ! ( " Created : {}" , completion_file . display( ) ) ;
187+ println ! ( " created : {path }" , path = path . display( ) ) ;
182188 }
183189 }
184190
185191 // Create multicall binaries (hardlinks or copies)
186192 // Ime softlinks work too but cube said hardlinks reeeee
187- // and all that jazz.
188- println ! ( "Creating multicall binaries..." ) ;
193+ // and all that jazz. - raf
194+ //
195+ // Since hard links don't occupy any extra space and prevent
196+ // stupid programs from canonicalizing their way into wrong
197+ // behaviour, they should be used. xcode-select on MacOS does
198+ // it, uutils-coreutils does it, busybox does it. - Cube
199+ println ! ( "creating multicall binaries..." ) ;
189200
190201 let multicall_binaries = [ Binary :: Cpu , Binary :: Power ] ;
191202 let bin_path = Path :: new ( bin_dir) ;
@@ -197,39 +208,45 @@ fn create_dist_files(
197208 fs:: remove_file ( & target_path) ?;
198209 }
199210
200- match fs:: hard_link ( watt_binary , & target_path) {
211+ match fs:: hard_link ( watt_path , & target_path) {
201212 Ok ( ( ) ) => {
202213 println ! (
203- " Created hardlink: {} -> { }" , // XXX: is this confusing?
204- target_path. display( ) ,
205- watt_binary
214+ " created hardlink: {target} points to {watt }" ,
215+ target = target_path. display( ) ,
216+ watt = watt_path . display ( ) ,
206217 ) ;
207218 } ,
208219 Err ( e) => {
209220 eprintln ! (
210- " Warning: Could not create hardlink for {}: {e}" ,
211- binary. name( )
221+ " warning: could not create hardlink for {binary }: {e}" ,
222+ binary = binary . name( ) ,
212223 ) ;
213- eprintln ! ( " Falling back to copying binary..." ) ;
224+ eprintln ! ( " warning: falling back to copying binary..." ) ;
214225
215- // Fallback: copy the binary
216- fs:: copy ( watt_binary , & target_path) ?;
226+ // Fallback: copy the binary.
227+ fs:: copy ( watt_path , & target_path) ?;
217228
218- // ...and make it executable
229+ // ...and make it executable.
219230 use std:: os:: unix:: fs:: PermissionsExt ;
220231 let mut perms = fs:: metadata ( & target_path) ?. permissions ( ) ;
221232 perms. set_mode ( perms. mode ( ) | 0o755 ) ;
222233 fs:: set_permissions ( & target_path, perms) ?;
223234
224- println ! ( " Created copy: {}" , target_path. display( ) ) ;
235+ println ! ( " created copy: {}" , target_path. display( ) ) ;
225236 } ,
226237 }
227238 }
228239
229- println ! ( "Distribution files created successfully!" ) ;
240+ println ! ( "distribution files created successfully!" ) ;
230241 println ! ( ) ;
231- println ! ( "Shell completions are in: {completions_dir}/" ) ;
232- println ! ( "Multicall binaries are in: {bin_dir}/" ) ;
242+ println ! (
243+ "shell completions are in: {completions_dir}" ,
244+ completions_dir = completions_dir. display( ) ,
245+ ) ;
246+ println ! (
247+ "multicall binaries are in: {bin_dir}" ,
248+ bin_dir = bin_dir. display( ) ,
249+ ) ;
233250 println ! ( ) ;
234251
235252 Ok ( ( ) )
0 commit comments