|
| 1 | +use home; |
1 | 2 | use shell_escape::escape;
|
2 | 3 | use std::ffi::OsStr;
|
3 | 4 | use std::io;
|
@@ -31,6 +32,26 @@ struct FmtContext {
|
31 | 32 | verbose: bool,
|
32 | 33 | }
|
33 | 34 |
|
| 35 | +fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> { |
| 36 | + let mut args = vec!["+nightly", "fmt", "--all"]; |
| 37 | + if context.check { |
| 38 | + args.push("--"); |
| 39 | + args.push("--check"); |
| 40 | + } |
| 41 | + // in case something aliases cargo=cargo --foo, the order of args will be messed up |
| 42 | + // because we will end up with cargo --foo +nightly fmt but the "+nightly" needs to come first |
| 43 | + // get the absolute path to the cargo binary inside $CARGO_HOME |
| 44 | + let cargo_home = home::cargo_home().expect("failed to get cargo home path!"); |
| 45 | + let cargo_path = cargo_home.join("bin").join("cargo"); |
| 46 | + assert!( |
| 47 | + cargo_path.is_file(), |
| 48 | + format!("ERROR: '{}' is not a path to the cargo binary!", cargo_path.display()) |
| 49 | + ); |
| 50 | + let success = exec(context, cargo_path, path, &args)?; |
| 51 | + |
| 52 | + Ok(success) |
| 53 | +} |
| 54 | + |
34 | 55 | pub fn run(check: bool, verbose: bool) {
|
35 | 56 | fn try_run(context: &FmtContext) -> Result<bool, CliError> {
|
36 | 57 | let mut success = true;
|
@@ -134,17 +155,6 @@ fn exec(
|
134 | 155 | Ok(success)
|
135 | 156 | }
|
136 | 157 |
|
137 |
| -fn cargo_fmt(context: &FmtContext, path: &Path) -> Result<bool, CliError> { |
138 |
| - let mut args = vec!["+nightly", "fmt", "--all"]; |
139 |
| - if context.check { |
140 |
| - args.push("--"); |
141 |
| - args.push("--check"); |
142 |
| - } |
143 |
| - let success = exec(context, "cargo", path, &args)?; |
144 |
| - |
145 |
| - Ok(success) |
146 |
| -} |
147 |
| - |
148 | 158 | fn rustfmt_test(context: &FmtContext) -> Result<(), CliError> {
|
149 | 159 | let program = "rustfmt";
|
150 | 160 | let dir = std::env::current_dir()?;
|
|
0 commit comments