@@ -12,21 +12,18 @@ mod ast_src;
12
12
13
13
use std:: {
14
14
env,
15
- io:: Write ,
16
15
path:: { Path , PathBuf } ,
17
- process:: { Command , Stdio } ,
18
16
} ;
17
+
19
18
use walkdir:: { DirEntry , WalkDir } ;
20
19
21
20
use crate :: {
22
21
codegen:: Mode ,
23
- not_bash:: { date_iso, fs2, pushd, rm_rf, run} ,
22
+ not_bash:: { date_iso, fs2, pushd, pushenv , rm_rf, run} ,
24
23
} ;
25
24
26
25
pub use anyhow:: { bail, Context as _, Result } ;
27
26
28
- const RUSTFMT_TOOLCHAIN : & str = "stable" ;
29
-
30
27
pub fn project_root ( ) -> PathBuf {
31
28
Path :: new (
32
29
& env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap_or_else ( |_| env ! ( "CARGO_MANIFEST_DIR" ) . to_owned ( ) ) ,
@@ -54,77 +51,44 @@ pub fn rust_files(path: &Path) -> impl Iterator<Item = PathBuf> {
54
51
55
52
pub fn run_rustfmt ( mode : Mode ) -> Result < ( ) > {
56
53
let _dir = pushd ( project_root ( ) ) ;
54
+ let _e = pushenv ( "RUSTUP_TOOLCHAIN" , "stable" ) ;
57
55
ensure_rustfmt ( ) ?;
58
-
59
- if Command :: new ( "cargo" )
60
- . env ( "RUSTUP_TOOLCHAIN" , RUSTFMT_TOOLCHAIN )
61
- . args ( & [ "fmt" , "--" ] )
62
- . args ( if mode == Mode :: Verify { & [ "--check" ] [ ..] } else { & [ ] } )
63
- . stderr ( Stdio :: inherit ( ) )
64
- . status ( ) ?
65
- . success ( )
66
- {
67
- Ok ( ( ) )
68
- } else {
69
- bail ! ( "Rustfmt failed" ) ;
70
- }
56
+ match mode {
57
+ Mode :: Overwrite => run ! ( "cargo fmt" ) ,
58
+ Mode :: Verify => run ! ( "cargo fmt -- --check" ) ,
59
+ } ?;
60
+ Ok ( ( ) )
71
61
}
72
62
73
63
fn reformat ( text : impl std:: fmt:: Display ) -> Result < String > {
64
+ let _e = pushenv ( "RUSTUP_TOOLCHAIN" , "stable" ) ;
74
65
ensure_rustfmt ( ) ?;
75
- let mut rustfmt = Command :: new ( "rustfmt" )
76
- . env ( "RUSTUP_TOOLCHAIN" , RUSTFMT_TOOLCHAIN )
77
- . args ( & [ "--config-path" ] )
78
- . arg ( project_root ( ) . join ( "rustfmt.toml" ) )
79
- . args ( & [ "--config" , "fn_single_line=true" ] )
80
- . stdin ( Stdio :: piped ( ) )
81
- . stdout ( Stdio :: piped ( ) )
82
- . spawn ( ) ?;
83
- write ! ( rustfmt. stdin. take( ) . unwrap( ) , "{}" , text) ?;
84
- let output = rustfmt. wait_with_output ( ) ?;
85
- let stdout = String :: from_utf8 ( output. stdout ) ?;
66
+ let stdout = run ! (
67
+ "rustfmt --config-path {} --config fn_single_line=true" , project_root( ) . join( "rustfmt.toml" ) . display( ) ;
68
+ <text. to_string( ) . as_bytes( )
69
+ ) ?;
86
70
let preamble = "Generated file, do not edit by hand, see `xtask/src/codegen`" ;
87
- Ok ( format ! ( "//! {}\n \n {}" , preamble, stdout) )
71
+ Ok ( format ! ( "//! {}\n \n {}\n " , preamble, stdout) )
88
72
}
89
73
90
74
fn ensure_rustfmt ( ) -> Result < ( ) > {
91
- match Command :: new ( "rustfmt" )
92
- . args ( & [ "--version" ] )
93
- . env ( "RUSTUP_TOOLCHAIN" , RUSTFMT_TOOLCHAIN )
94
- . stdout ( Stdio :: piped ( ) )
95
- . stderr ( Stdio :: null ( ) )
96
- . spawn ( )
97
- . and_then ( |child| child. wait_with_output ( ) )
98
- {
99
- Ok ( output)
100
- if output. status . success ( )
101
- && std:: str:: from_utf8 ( & output. stdout ) ?. contains ( RUSTFMT_TOOLCHAIN ) =>
102
- {
103
- Ok ( ( ) )
104
- }
105
- _ => {
106
- bail ! (
107
- "Failed to run rustfmt from toolchain '{0}'. \
108
- Please run `rustup component add rustfmt --toolchain {0}` to install it.",
109
- RUSTFMT_TOOLCHAIN ,
110
- ) ;
111
- }
75
+ let out = run ! ( "rustfmt --version" ) ?;
76
+ if !out. contains ( "stable" ) {
77
+ bail ! (
78
+ "Failed to run rustfmt from toolchain 'stable'. \
79
+ Please run `rustup component add rustfmt --toolchain stable` to install it.",
80
+ )
112
81
}
82
+ Ok ( ( ) )
113
83
}
114
84
115
85
pub fn run_clippy ( ) -> Result < ( ) > {
116
- match Command :: new ( "cargo" )
117
- . args ( & [ "clippy" , "--version" ] )
118
- . stderr ( Stdio :: null ( ) )
119
- . stdout ( Stdio :: null ( ) )
120
- . status ( )
121
- {
122
- Ok ( status) if status. success ( ) => ( ) ,
123
- _ => bail ! (
86
+ if run ! ( "cargo clippy --version" ) . is_err ( ) {
87
+ bail ! (
124
88
"Failed run cargo clippy. \
125
89
Please run `rustup component add clippy` to install it.",
126
- ) ,
127
- } ;
90
+ )
91
+ }
128
92
129
93
let allowed_lints = [
130
94
"clippy::collapsible_if" ,
@@ -138,33 +102,18 @@ pub fn run_clippy() -> Result<()> {
138
102
139
103
pub fn run_fuzzer ( ) -> Result < ( ) > {
140
104
let _d = pushd ( "./crates/ra_syntax" ) ;
105
+ let _e = pushenv ( "RUSTUP_TOOLCHAIN" , "nightly" ) ;
141
106
if run ! ( "cargo fuzz --help" ) . is_err ( ) {
142
107
run ! ( "cargo install cargo-fuzz" ) ?;
143
108
} ;
144
109
145
110
// Expecting nightly rustc
146
- match Command :: new ( "rustc" )
147
- . args ( & [ "--version" ] )
148
- . env ( "RUSTUP_TOOLCHAIN" , "nightly" )
149
- . stdout ( Stdio :: piped ( ) )
150
- . stderr ( Stdio :: null ( ) )
151
- . spawn ( )
152
- . and_then ( |child| child. wait_with_output ( ) )
153
- {
154
- Ok ( output)
155
- if output. status . success ( )
156
- && std:: str:: from_utf8 ( & output. stdout ) ?. contains ( "nightly" ) => { }
157
- _ => bail ! ( "fuzz tests require nightly rustc" ) ,
111
+ let out = run ! ( "rustc --version" ) ?;
112
+ if !out. contains ( "nightly" ) {
113
+ bail ! ( "fuzz tests require nightly rustc" )
158
114
}
159
115
160
- let status = Command :: new ( "cargo" )
161
- . env ( "RUSTUP_TOOLCHAIN" , "nightly" )
162
- . args ( & [ "fuzz" , "run" , "parser" ] )
163
- . stderr ( Stdio :: inherit ( ) )
164
- . status ( ) ?;
165
- if !status. success ( ) {
166
- bail ! ( "{}" , status) ;
167
- }
116
+ run ! ( "cargo fuzz run parser" ) ?;
168
117
Ok ( ( ) )
169
118
}
170
119
0 commit comments