1
- use std:: { env, process:: Command } ;
1
+ // Copyright 2023 Google LLC
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ //! This binary allows us to execute tasks within the project by running
16
+ //! `cargo xtask <task>`. It can thus be used as a task automation tool.
17
+ //! For example instead of repeatedly running `cargo install` from the CLI
18
+ //! to install all the necessary tools for the project we can just run
19
+ //! `cargo xtask install-tools` and the logic defined here will install
20
+ //! the tools.
2
21
3
- type DynError = Box < dyn std:: error:: Error > ;
22
+ use anyhow:: { anyhow, Ok , Result } ;
23
+ use clap:: Parser ;
24
+ use std:: { env, process:: Command } ;
4
25
5
- fn main ( ) {
26
+ fn main ( ) -> Result < ( ) > {
6
27
if let Err ( e) = execute_task ( ) {
7
28
eprintln ! ( "{e}" ) ;
8
29
std:: process:: exit ( -1 ) ;
9
30
}
31
+ Ok ( ( ) )
10
32
}
11
33
12
- fn execute_task ( ) -> Result < ( ) , DynError > {
13
- let task = env:: args ( ) . nth ( 1 ) ;
14
- match task. as_deref ( ) {
15
- Some ( "install-tools" ) => install_tools ( ) ?,
34
+ #[ derive( Parser , Debug ) ]
35
+ #[ command(
36
+ about = "Binary for executing tasks within the Comprehensive Rust project"
37
+ ) ]
38
+ struct Args {
39
+ #[ arg( required = true , help = "The task to execute" ) ]
40
+ task : String ,
41
+ }
42
+
43
+ fn execute_task ( ) -> Result < ( ) > {
44
+ let task = Args :: parse ( ) . task ;
45
+ match task. as_str ( ) {
46
+ "install-tools" => install_tools ( ) ?,
16
47
_ => {
17
- return Err ( Box :: from ( get_help_string ( task. as_deref ( ) ) ) ) ;
48
+ return Err ( anyhow ! ( unrecognized_task_string ( task. as_str ( ) ) ) ) ;
18
49
}
19
50
}
20
51
Ok ( ( ) )
21
52
}
22
53
23
- fn install_tools ( ) -> Result < ( ) , DynError > {
54
+ fn install_tools ( ) -> Result < ( ) > {
24
55
println ! ( "Installing project tools..." ) ;
25
56
26
- let install_args: Vec < Vec < & str > > = vec ! [
57
+ let install_args = vec ! [
27
58
// The --locked flag is important for reproducible builds. It also
28
59
// avoids breakage due to skews between mdbook and mdbook-svgbob.
29
60
vec![ "mdbook" , "--locked" , "--version" , "0.4.44" ] ,
@@ -49,21 +80,17 @@ fn install_tools() -> Result<(), DynError> {
49
80
args. join( " " ) ,
50
81
status. code( ) . unwrap( )
51
82
) ;
52
- return Err ( Box :: from ( error_message) ) ;
83
+ return Err ( anyhow ! ( error_message) ) ;
53
84
}
54
85
}
55
86
56
87
Ok ( ( ) )
57
88
}
58
89
59
- fn get_help_string ( task : Option < & str > ) -> String {
60
- if let Some ( t) = task {
61
- format ! (
62
- "Unrecognized task '{t}'. Available tasks:
90
+ fn unrecognized_task_string ( task : & str ) -> String {
91
+ format ! (
92
+ "Unrecognized task '{task}'. Available tasks:
63
93
64
94
install-tools Installs the tools the project depends on."
65
- )
66
- } else {
67
- "Missing task. To execute a task run `cargo xtask [task]`." . to_string ( )
68
- }
95
+ )
69
96
}
0 commit comments