1
+ use crate :: builder:: { Builder , RunConfig , ShouldRun , Step } ;
1
2
use crate :: { t, VERSION } ;
2
3
use crate :: { Config , TargetSelection } ;
3
4
use std:: env:: consts:: EXE_SUFFIX ;
@@ -11,7 +12,7 @@ use std::{
11
12
io:: { self , Write } ,
12
13
} ;
13
14
14
- #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
15
+ #[ derive( Clone , Copy , Debug , Eq , PartialEq , Hash ) ]
15
16
pub enum Profile {
16
17
Compiler ,
17
18
Codegen ,
@@ -50,6 +51,16 @@ impl Profile {
50
51
}
51
52
out
52
53
}
54
+
55
+ pub fn as_str ( & self ) -> & ' static str {
56
+ match self {
57
+ Profile :: Compiler => "compiler" ,
58
+ Profile :: Codegen => "codegen" ,
59
+ Profile :: Library => "library" ,
60
+ Profile :: Tools => "tools" ,
61
+ Profile :: User => "user" ,
62
+ }
63
+ }
53
64
}
54
65
55
66
impl FromStr for Profile {
@@ -71,13 +82,43 @@ impl FromStr for Profile {
71
82
72
83
impl fmt:: Display for Profile {
73
84
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
74
- match self {
75
- Profile :: Compiler => write ! ( f, "compiler" ) ,
76
- Profile :: Codegen => write ! ( f, "codegen" ) ,
77
- Profile :: Library => write ! ( f, "library" ) ,
78
- Profile :: User => write ! ( f, "user" ) ,
79
- Profile :: Tools => write ! ( f, "tools" ) ,
85
+ f. write_str ( self . as_str ( ) )
86
+ }
87
+ }
88
+
89
+ impl Step for Profile {
90
+ type Output = ( ) ;
91
+ const DEFAULT : bool = true ;
92
+
93
+ fn should_run ( mut run : ShouldRun < ' _ > ) -> ShouldRun < ' _ > {
94
+ for choice in Profile :: all ( ) {
95
+ run = run. alias ( choice. as_str ( ) ) ;
80
96
}
97
+ run
98
+ }
99
+
100
+ fn make_run ( run : RunConfig < ' _ > ) {
101
+ // for Profile, `run.paths` will have 1 and only 1 element
102
+ // this is because we only accept at most 1 path from user input.
103
+ // If user calls `x.py setup` without arguments, the interacctive TUI
104
+ // will guide user to provide one.
105
+ let profile: Profile = run
106
+ . paths
107
+ . first ( )
108
+ . unwrap ( )
109
+ . assert_single_path ( )
110
+ . path
111
+ . to_owned ( )
112
+ . into_os_string ( )
113
+ . into_string ( )
114
+ . unwrap ( )
115
+ . parse ( )
116
+ . unwrap ( ) ;
117
+ run. builder . ensure ( profile) ;
118
+ }
119
+
120
+ fn run ( self , builder : & Builder < ' _ > ) {
121
+ setup ( & builder. build . config , self )
81
122
}
82
123
}
83
124
@@ -103,6 +144,7 @@ pub fn setup(config: &Config, profile: Profile) {
103
144
changelog-seen = {}\n ",
104
145
profile, VERSION
105
146
) ;
147
+
106
148
t ! ( fs:: write( path, settings) ) ;
107
149
108
150
let include_path = profile. include_path ( & config. src ) ;
@@ -116,7 +158,7 @@ pub fn setup(config: &Config, profile: Profile) {
116
158
117
159
if !rustup_installed ( ) && profile != Profile :: User {
118
160
eprintln ! ( "`rustup` is not installed; cannot link `stage1` toolchain" ) ;
119
- } else if stage_dir_exists ( & stage_path[ ..] ) {
161
+ } else if stage_dir_exists ( & stage_path[ ..] ) && !config . dry_run {
120
162
attempt_toolchain_link ( & stage_path[ ..] ) ;
121
163
}
122
164
@@ -136,7 +178,9 @@ pub fn setup(config: &Config, profile: Profile) {
136
178
137
179
println ! ( ) ;
138
180
139
- t ! ( install_git_hook_maybe( & config) ) ;
181
+ if !config. dry_run {
182
+ t ! ( install_git_hook_maybe( & config) ) ;
183
+ }
140
184
141
185
println ! ( ) ;
142
186
0 commit comments