2023-04-15 13:45:58 +02:00
|
|
|
use log::{error, info, Level};
|
|
|
|
|
|
|
|
use crate::{
|
2023-04-15 13:55:56 +02:00
|
|
|
argument_parsing::Globals, cargo_commands::FinalRunResult, ExtraArguments, Package, RunResult,
|
|
|
|
Target, TestRunError,
|
2023-04-15 13:45:58 +02:00
|
|
|
};
|
2021-08-26 10:58:59 +02:00
|
|
|
use core::fmt;
|
2023-04-15 00:09:50 +02:00
|
|
|
use std::{
|
|
|
|
fs::File,
|
|
|
|
io::Read,
|
2023-04-14 23:59:23 +02:00
|
|
|
path::PathBuf,
|
2023-04-15 00:09:50 +02:00
|
|
|
process::{Command, Stdio},
|
|
|
|
};
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
#[allow(dead_code)]
|
2021-08-26 10:58:59 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum BuildMode {
|
|
|
|
Release,
|
|
|
|
Debug,
|
|
|
|
}
|
|
|
|
|
2023-04-15 00:26:16 +02:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
|
|
|
pub enum OutputMode {
|
|
|
|
PipedAndCollected,
|
|
|
|
Inherited,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<OutputMode> for Stdio {
|
|
|
|
fn from(value: OutputMode) -> Self {
|
|
|
|
match value {
|
|
|
|
OutputMode::PipedAndCollected => Stdio::piped(),
|
|
|
|
OutputMode::Inherited => Stdio::inherit(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-22 13:22:45 +02:00
|
|
|
#[derive(Debug)]
|
2021-08-26 10:58:59 +02:00
|
|
|
pub enum CargoCommand<'a> {
|
2023-02-06 13:21:04 +01:00
|
|
|
// For future embedded-ci
|
|
|
|
#[allow(dead_code)]
|
2021-08-26 10:58:59 +02:00
|
|
|
Run {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
|
|
|
example: &'a str,
|
2023-04-15 00:50:46 +02:00
|
|
|
target: Target<'a>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-02-05 01:50:29 +01:00
|
|
|
mode: BuildMode,
|
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
Qemu {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
2021-08-26 10:58:59 +02:00
|
|
|
example: &'a str,
|
2023-04-15 00:50:46 +02:00
|
|
|
target: Target<'a>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2021-08-26 10:58:59 +02:00
|
|
|
mode: BuildMode,
|
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
ExampleBuild {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-06 13:21:04 +01:00
|
|
|
example: &'a str,
|
2023-04-15 00:50:46 +02:00
|
|
|
target: Target<'a>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2021-08-26 10:58:59 +02:00
|
|
|
mode: BuildMode,
|
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
ExampleCheck {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-06 13:21:04 +01:00
|
|
|
example: &'a str,
|
2023-04-15 00:50:46 +02:00
|
|
|
target: Target<'a>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-02-06 13:21:04 +01:00
|
|
|
mode: BuildMode,
|
2023-02-05 01:50:29 +01:00
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
Build {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-28 23:55:02 +01:00
|
|
|
package: Option<Package>,
|
2023-04-15 00:50:46 +02:00
|
|
|
target: Target<'a>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-02-06 13:21:04 +01:00
|
|
|
mode: BuildMode,
|
|
|
|
},
|
|
|
|
Check {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-28 23:55:02 +01:00
|
|
|
package: Option<Package>,
|
2023-04-15 00:50:46 +02:00
|
|
|
target: Target<'a>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-02-24 23:14:11 +01:00
|
|
|
mode: BuildMode,
|
2023-02-06 13:21:04 +01:00
|
|
|
},
|
|
|
|
Clippy {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-28 23:55:02 +01:00
|
|
|
package: Option<Package>,
|
2023-04-15 00:50:46 +02:00
|
|
|
target: Target<'a>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-02-06 13:21:04 +01:00
|
|
|
},
|
2023-02-24 00:10:01 +01:00
|
|
|
Format {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-28 23:55:02 +01:00
|
|
|
package: Option<Package>,
|
2023-02-24 00:10:01 +01:00
|
|
|
check_only: bool,
|
|
|
|
},
|
2023-02-24 22:56:36 +01:00
|
|
|
Doc {
|
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-03-02 22:02:19 +01:00
|
|
|
arguments: Option<ExtraArguments>,
|
2023-02-28 23:55:02 +01:00
|
|
|
},
|
|
|
|
Test {
|
|
|
|
package: Option<Package>,
|
|
|
|
features: Option<String>,
|
|
|
|
test: Option<String>,
|
2023-02-24 22:56:36 +01:00
|
|
|
},
|
2023-02-25 00:28:45 +01:00
|
|
|
Book {
|
2023-03-02 22:02:19 +01:00
|
|
|
arguments: Option<ExtraArguments>,
|
2023-02-25 00:28:45 +01:00
|
|
|
},
|
2023-02-06 13:21:04 +01:00
|
|
|
ExampleSize {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg: &'a Option<&'a str>,
|
2023-02-04 15:22:43 +01:00
|
|
|
example: &'a str,
|
2023-04-15 00:50:46 +02:00
|
|
|
target: Target<'a>,
|
2023-02-28 23:55:02 +01:00
|
|
|
features: Option<String>,
|
2023-02-04 15:22:43 +01:00
|
|
|
mode: BuildMode,
|
2023-03-02 22:02:19 +01:00
|
|
|
arguments: Option<ExtraArguments>,
|
2023-02-04 15:22:43 +01:00
|
|
|
},
|
2023-04-14 23:59:23 +02:00
|
|
|
CheckInDir {
|
|
|
|
mode: BuildMode,
|
|
|
|
dir: PathBuf,
|
|
|
|
},
|
|
|
|
BuildInDir {
|
|
|
|
mode: BuildMode,
|
|
|
|
dir: PathBuf,
|
|
|
|
},
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
|
2023-04-15 12:21:11 +02:00
|
|
|
impl core::fmt::Display for CargoCommand<'_> {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2023-04-15 13:18:18 +02:00
|
|
|
let p = |p: &Option<Package>| {
|
|
|
|
if let Some(package) = p {
|
|
|
|
format!("package {package}")
|
|
|
|
} else {
|
|
|
|
format!("default package")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let feat = |f: &Option<String>| {
|
|
|
|
if let Some(features) = f {
|
|
|
|
format!("\"{features}\"")
|
|
|
|
} else {
|
|
|
|
format!("no features")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let carg = |f: &&Option<&str>| {
|
|
|
|
if let Some(cargoarg) = f {
|
|
|
|
format!("{cargoarg}")
|
|
|
|
} else {
|
|
|
|
format!("no cargo args")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let details = |target: &Target,
|
|
|
|
mode: &BuildMode,
|
|
|
|
features: &Option<String>,
|
|
|
|
cargoarg: &&Option<&str>| {
|
|
|
|
let feat = feat(features);
|
|
|
|
let carg = carg(cargoarg);
|
|
|
|
if cargoarg.is_some() {
|
|
|
|
format!("({target}, {mode}, {feat}, {carg})")
|
|
|
|
} else {
|
|
|
|
format!("({target}, {mode}, {feat})")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
match self {
|
|
|
|
CargoCommand::Run {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => write!(
|
|
|
|
f,
|
|
|
|
"Run example {example} {}",
|
|
|
|
details(target, mode, features, cargoarg)
|
|
|
|
),
|
|
|
|
CargoCommand::Qemu {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => write!(
|
|
|
|
f,
|
|
|
|
"Run example {example} in QEMU {}",
|
|
|
|
details(target, mode, features, cargoarg)
|
|
|
|
),
|
|
|
|
CargoCommand::ExampleBuild {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => write!(
|
|
|
|
f,
|
|
|
|
"Build example {example} {}",
|
|
|
|
details(target, mode, features, cargoarg)
|
|
|
|
),
|
|
|
|
CargoCommand::ExampleCheck {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => write!(
|
|
|
|
f,
|
|
|
|
"Check example {example} {}",
|
|
|
|
details(target, mode, features, cargoarg)
|
|
|
|
),
|
|
|
|
CargoCommand::Build {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => {
|
|
|
|
let package = p(package);
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Build {package} {}",
|
|
|
|
details(target, mode, features, cargoarg)
|
|
|
|
)
|
|
|
|
}
|
2023-04-14 23:59:23 +02:00
|
|
|
CargoCommand::BuildInDir { mode, dir } => {
|
|
|
|
let dir = dir.as_os_str().to_str().unwrap_or("Not displayable");
|
|
|
|
write!(f, "Build {dir} ({mode})")
|
|
|
|
}
|
2023-04-15 13:18:18 +02:00
|
|
|
CargoCommand::Check {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => {
|
|
|
|
let package = p(package);
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Check {package} {}",
|
|
|
|
details(target, mode, features, cargoarg)
|
|
|
|
)
|
|
|
|
}
|
2023-04-14 23:59:23 +02:00
|
|
|
CargoCommand::CheckInDir { mode, dir } => {
|
|
|
|
let dir = dir.as_os_str().to_str().unwrap_or("Not displayable");
|
|
|
|
write!(f, "Check {dir} ({mode})")
|
|
|
|
}
|
2023-04-15 13:18:18 +02:00
|
|
|
CargoCommand::Clippy {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
} => {
|
|
|
|
let package = p(package);
|
|
|
|
let features = feat(features);
|
|
|
|
let carg = carg(cargoarg);
|
2023-04-15 15:32:33 +02:00
|
|
|
if cargoarg.is_some() {
|
|
|
|
write!(f, "Clippy {package} ({target}, {features}, {carg})")
|
|
|
|
} else {
|
|
|
|
write!(f, "Clippy {package} ({target}, {features})")
|
|
|
|
}
|
2023-04-15 13:18:18 +02:00
|
|
|
}
|
|
|
|
CargoCommand::Format {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
check_only,
|
|
|
|
} => {
|
|
|
|
let package = p(package);
|
|
|
|
let carg = carg(cargoarg);
|
|
|
|
|
|
|
|
let carg = if cargoarg.is_some() {
|
|
|
|
format!("(cargo args: {carg})")
|
|
|
|
} else {
|
|
|
|
format!("")
|
|
|
|
};
|
|
|
|
|
|
|
|
if *check_only {
|
|
|
|
write!(f, "Check format for {package} {carg}")
|
|
|
|
} else {
|
|
|
|
write!(f, "Format {package} {carg}")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CargoCommand::Doc {
|
|
|
|
cargoarg,
|
|
|
|
features,
|
|
|
|
arguments,
|
|
|
|
} => {
|
|
|
|
let feat = feat(features);
|
|
|
|
let carg = carg(cargoarg);
|
|
|
|
let arguments = arguments
|
|
|
|
.clone()
|
|
|
|
.map(|a| format!("{a}"))
|
2023-04-15 15:16:43 +02:00
|
|
|
.unwrap_or_else(|| "no extra arguments".into());
|
2023-04-15 13:18:18 +02:00
|
|
|
if cargoarg.is_some() {
|
2023-04-15 15:16:43 +02:00
|
|
|
write!(f, "Document ({feat}, {carg}, {arguments})")
|
2023-04-15 13:18:18 +02:00
|
|
|
} else {
|
2023-04-15 15:16:43 +02:00
|
|
|
write!(f, "Document ({feat}, {arguments})")
|
2023-04-15 13:18:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CargoCommand::Test {
|
|
|
|
package,
|
|
|
|
features,
|
|
|
|
test,
|
|
|
|
} => {
|
|
|
|
let p = p(package);
|
|
|
|
let test = test
|
|
|
|
.clone()
|
|
|
|
.map(|t| format!("test {t}"))
|
|
|
|
.unwrap_or("all tests".into());
|
|
|
|
let feat = feat(features);
|
2023-04-15 13:45:58 +02:00
|
|
|
write!(f, "Run {test} in {p} (features: {feat})")
|
2023-04-15 13:18:18 +02:00
|
|
|
}
|
|
|
|
CargoCommand::Book { arguments: _ } => write!(f, "Build the book"),
|
|
|
|
CargoCommand::ExampleSize {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
arguments: _,
|
|
|
|
} => {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Compute size of example {example} {}",
|
|
|
|
details(target, mode, features, cargoarg)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-04-15 12:21:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-26 10:58:59 +02:00
|
|
|
impl<'a> CargoCommand<'a> {
|
2023-04-15 13:55:56 +02:00
|
|
|
pub fn as_cmd_string(&self) -> String {
|
|
|
|
let executable = self.executable();
|
|
|
|
let args = self.args().join(" ");
|
|
|
|
format!("{executable} {args}")
|
|
|
|
}
|
|
|
|
|
2023-04-14 23:59:23 +02:00
|
|
|
fn command(&self) -> &'static str {
|
2021-08-26 10:58:59 +02:00
|
|
|
match self {
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Run { .. } | CargoCommand::Qemu { .. } => "run",
|
2023-04-14 23:59:23 +02:00
|
|
|
CargoCommand::ExampleCheck { .. }
|
|
|
|
| CargoCommand::Check { .. }
|
|
|
|
| CargoCommand::CheckInDir { .. } => "check",
|
|
|
|
CargoCommand::ExampleBuild { .. }
|
|
|
|
| CargoCommand::Build { .. }
|
|
|
|
| CargoCommand::BuildInDir { .. } => "build",
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::ExampleSize { .. } => "size",
|
|
|
|
CargoCommand::Clippy { .. } => "clippy",
|
2023-02-24 00:10:01 +01:00
|
|
|
CargoCommand::Format { .. } => "fmt",
|
2023-02-24 22:56:36 +01:00
|
|
|
CargoCommand::Doc { .. } => "doc",
|
2023-02-25 00:28:45 +01:00
|
|
|
CargoCommand::Book { .. } => "build",
|
2023-02-28 23:55:02 +01:00
|
|
|
CargoCommand::Test { .. } => "test",
|
2023-02-25 00:28:45 +01:00
|
|
|
}
|
|
|
|
}
|
2023-04-14 23:59:23 +02:00
|
|
|
pub fn executable(&self) -> &'static str {
|
2023-02-25 00:28:45 +01:00
|
|
|
match self {
|
|
|
|
CargoCommand::Run { .. }
|
|
|
|
| CargoCommand::Qemu { .. }
|
|
|
|
| CargoCommand::ExampleCheck { .. }
|
|
|
|
| CargoCommand::Check { .. }
|
|
|
|
| CargoCommand::ExampleBuild { .. }
|
|
|
|
| CargoCommand::Build { .. }
|
|
|
|
| CargoCommand::ExampleSize { .. }
|
|
|
|
| CargoCommand::Clippy { .. }
|
|
|
|
| CargoCommand::Format { .. }
|
2023-02-28 23:55:02 +01:00
|
|
|
| CargoCommand::Test { .. }
|
2023-04-14 23:59:23 +02:00
|
|
|
| CargoCommand::Doc { .. }
|
|
|
|
| CargoCommand::CheckInDir { .. }
|
|
|
|
| CargoCommand::BuildInDir { .. } => "cargo",
|
2023-02-25 00:28:45 +01:00
|
|
|
CargoCommand::Book { .. } => "mdbook",
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn args(&self) -> Vec<&str> {
|
|
|
|
match self {
|
2023-02-06 13:21:04 +01:00
|
|
|
// For future embedded-ci, for now the same as Qemu
|
2021-08-26 10:58:59 +02:00
|
|
|
CargoCommand::Run {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
2021-08-26 10:58:59 +02:00
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => {
|
2023-02-05 01:50:29 +01:00
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
2023-04-15 00:50:46 +02:00
|
|
|
args.extend_from_slice(&[
|
|
|
|
self.command(),
|
|
|
|
"--example",
|
|
|
|
example,
|
|
|
|
"--target",
|
|
|
|
target.triple(),
|
|
|
|
]);
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-02-05 01:50:29 +01:00
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Qemu {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
2023-02-06 13:21:04 +01:00
|
|
|
example,
|
2021-08-26 10:58:59 +02:00
|
|
|
target,
|
|
|
|
features,
|
2021-12-26 10:43:57 +01:00
|
|
|
mode,
|
2021-08-26 10:58:59 +02:00
|
|
|
} => {
|
2023-02-05 01:50:29 +01:00
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
2023-04-15 00:50:46 +02:00
|
|
|
args.extend_from_slice(&[
|
|
|
|
self.command(),
|
|
|
|
"--example",
|
|
|
|
example,
|
|
|
|
"--target",
|
|
|
|
target.triple(),
|
|
|
|
]);
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-02-05 01:50:29 +01:00
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
|
|
|
}
|
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Build {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
2023-02-06 13:21:04 +01:00
|
|
|
package,
|
2023-02-05 01:50:29 +01:00
|
|
|
target,
|
|
|
|
features,
|
2023-02-06 13:21:04 +01:00
|
|
|
mode,
|
2023-02-05 01:50:29 +01:00
|
|
|
} => {
|
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
|
2023-04-15 00:50:46 +02:00
|
|
|
args.extend_from_slice(&[self.command(), "--target", target.triple()]);
|
2023-02-28 23:55:02 +01:00
|
|
|
|
|
|
|
if let Some(package) = package {
|
2023-03-01 00:10:07 +01:00
|
|
|
args.extend_from_slice(&["--package", package.name()]);
|
2023-02-06 13:21:04 +01:00
|
|
|
}
|
2023-02-05 01:50:29 +01:00
|
|
|
|
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
2023-02-05 01:50:29 +01:00
|
|
|
args
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::Check {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
2023-03-01 00:59:06 +01:00
|
|
|
target: _,
|
2023-02-06 13:21:04 +01:00
|
|
|
features,
|
2023-02-24 23:14:11 +01:00
|
|
|
mode,
|
2023-02-06 13:21:04 +01:00
|
|
|
} => {
|
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
2023-03-02 22:02:54 +01:00
|
|
|
args.extend_from_slice(&[self.command()]);
|
2023-02-28 23:55:02 +01:00
|
|
|
|
|
|
|
if let Some(package) = package {
|
2023-03-01 00:10:07 +01:00
|
|
|
args.extend_from_slice(&["--package", package.name()]);
|
2023-02-06 13:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
|
|
|
}
|
2023-02-24 23:14:11 +01:00
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
args
|
|
|
|
}
|
|
|
|
CargoCommand::Clippy {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
2023-03-01 00:59:06 +01:00
|
|
|
target: _,
|
2023-02-06 13:21:04 +01:00
|
|
|
features,
|
|
|
|
} => {
|
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
|
|
|
|
2023-03-02 22:02:54 +01:00
|
|
|
args.extend_from_slice(&[self.command()]);
|
2023-02-28 23:55:02 +01:00
|
|
|
|
|
|
|
if let Some(package) = package {
|
2023-03-01 00:10:07 +01:00
|
|
|
args.extend_from_slice(&["--package", package.name()]);
|
2023-02-06 13:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
2023-03-02 22:02:19 +01:00
|
|
|
CargoCommand::Doc {
|
|
|
|
cargoarg,
|
|
|
|
features,
|
|
|
|
arguments,
|
|
|
|
} => {
|
2023-02-24 22:56:36 +01:00
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
|
|
|
|
2023-03-02 22:02:54 +01:00
|
|
|
args.extend_from_slice(&[self.command()]);
|
2023-02-24 22:56:36 +01:00
|
|
|
|
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
|
|
|
}
|
2023-03-02 22:02:19 +01:00
|
|
|
if let Some(ExtraArguments::Other(arguments)) = arguments {
|
|
|
|
for arg in arguments {
|
|
|
|
args.extend_from_slice(&[arg.as_str()]);
|
|
|
|
}
|
|
|
|
}
|
2023-02-24 22:56:36 +01:00
|
|
|
args
|
|
|
|
}
|
2023-02-28 23:55:02 +01:00
|
|
|
CargoCommand::Test {
|
|
|
|
package,
|
|
|
|
features,
|
|
|
|
test,
|
|
|
|
} => {
|
|
|
|
let mut args = vec!["+nightly"];
|
2023-03-02 22:02:54 +01:00
|
|
|
args.extend_from_slice(&[self.command()]);
|
2023-02-28 23:55:02 +01:00
|
|
|
|
|
|
|
if let Some(package) = package {
|
2023-03-01 00:10:07 +01:00
|
|
|
args.extend_from_slice(&["--package", package.name()]);
|
2023-02-28 23:55:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
|
|
|
}
|
|
|
|
if let Some(test) = test {
|
|
|
|
args.extend_from_slice(&["--test", test]);
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
2023-03-02 22:02:19 +01:00
|
|
|
CargoCommand::Book { arguments } => {
|
2023-02-25 00:28:45 +01:00
|
|
|
let mut args = vec![];
|
|
|
|
|
2023-03-02 22:02:19 +01:00
|
|
|
if let Some(ExtraArguments::Other(arguments)) = arguments {
|
|
|
|
for arg in arguments {
|
|
|
|
args.extend_from_slice(&[arg.as_str()]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// If no argument given, run mdbook build
|
2023-03-04 00:08:11 +01:00
|
|
|
// with default path to book
|
2023-03-02 22:02:54 +01:00
|
|
|
args.extend_from_slice(&[self.command()]);
|
2023-03-04 00:08:11 +01:00
|
|
|
args.extend_from_slice(&["book/en"]);
|
2023-02-25 00:28:45 +01:00
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
2023-02-24 00:10:01 +01:00
|
|
|
CargoCommand::Format {
|
|
|
|
cargoarg,
|
|
|
|
package,
|
|
|
|
check_only,
|
|
|
|
} => {
|
2023-03-02 22:02:54 +01:00
|
|
|
let mut args = vec!["+nightly", self.command()];
|
2023-02-24 00:10:01 +01:00
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
|
|
|
|
2023-02-28 23:55:02 +01:00
|
|
|
if let Some(package) = package {
|
2023-03-01 00:10:07 +01:00
|
|
|
args.extend_from_slice(&["--package", package.name()]);
|
2023-02-24 00:10:01 +01:00
|
|
|
}
|
|
|
|
if *check_only {
|
|
|
|
args.extend_from_slice(&["--check"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
args
|
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::ExampleBuild {
|
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => {
|
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
2023-04-15 00:50:46 +02:00
|
|
|
args.extend_from_slice(&[
|
|
|
|
self.command(),
|
|
|
|
"--example",
|
|
|
|
example,
|
|
|
|
"--target",
|
|
|
|
target.triple(),
|
|
|
|
]);
|
2023-02-06 13:21:04 +01:00
|
|
|
|
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
|
|
|
}
|
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
|
|
|
CargoCommand::ExampleCheck {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
} => {
|
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
2023-04-15 00:50:46 +02:00
|
|
|
args.extend_from_slice(&[
|
|
|
|
self.command(),
|
|
|
|
"--example",
|
|
|
|
example,
|
|
|
|
"--target",
|
|
|
|
target.triple(),
|
|
|
|
]);
|
2023-02-05 01:50:29 +01:00
|
|
|
|
|
|
|
if let Some(feature) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature]);
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
2021-12-26 10:43:57 +01:00
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
2021-08-26 10:58:59 +02:00
|
|
|
args
|
2023-02-04 15:22:43 +01:00
|
|
|
}
|
2023-02-06 13:21:04 +01:00
|
|
|
CargoCommand::ExampleSize {
|
2023-02-05 01:50:29 +01:00
|
|
|
cargoarg,
|
2023-02-04 15:22:43 +01:00
|
|
|
example,
|
|
|
|
target,
|
|
|
|
features,
|
|
|
|
mode,
|
|
|
|
arguments,
|
|
|
|
} => {
|
2023-02-05 01:50:29 +01:00
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
if let Some(cargoarg) = cargoarg {
|
|
|
|
args.extend_from_slice(&[cargoarg]);
|
|
|
|
}
|
2023-04-15 00:50:46 +02:00
|
|
|
args.extend_from_slice(&[
|
|
|
|
self.command(),
|
|
|
|
"--example",
|
|
|
|
example,
|
|
|
|
"--target",
|
|
|
|
target.triple(),
|
|
|
|
]);
|
2023-02-05 01:50:29 +01:00
|
|
|
|
2023-02-04 15:22:43 +01:00
|
|
|
if let Some(feature_name) = features {
|
|
|
|
args.extend_from_slice(&["--features", feature_name]);
|
|
|
|
}
|
|
|
|
if let Some(flag) = mode.to_flag() {
|
|
|
|
args.push(flag);
|
|
|
|
}
|
2023-03-02 22:02:19 +01:00
|
|
|
if let Some(ExtraArguments::Other(arguments)) = arguments {
|
2023-02-04 15:22:43 +01:00
|
|
|
// Arguments to cargo size must be passed after "--"
|
|
|
|
args.extend_from_slice(&["--"]);
|
|
|
|
for arg in arguments {
|
|
|
|
args.extend_from_slice(&[arg.as_str()]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
args
|
|
|
|
}
|
2023-04-14 23:59:23 +02:00
|
|
|
CargoCommand::CheckInDir { mode, dir: _ } => {
|
|
|
|
let mut args = vec!["+nightly"];
|
|
|
|
args.push(self.command());
|
|
|
|
|
|
|
|
if let Some(mode) = mode.to_flag() {
|
|
|
|
args.push(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
args
|
|
|
|
}
|
|
|
|
CargoCommand::BuildInDir { mode, dir: _ } => {
|
|
|
|
let mut args = vec!["+nightly", self.command()];
|
|
|
|
|
|
|
|
if let Some(mode) = mode.to_flag() {
|
|
|
|
args.push(mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
args
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn chdir(&self) -> Option<&PathBuf> {
|
|
|
|
match self {
|
|
|
|
CargoCommand::CheckInDir { dir, .. } | CargoCommand::BuildInDir { dir, .. } => {
|
|
|
|
Some(dir)
|
|
|
|
}
|
|
|
|
_ => None,
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BuildMode {
|
2023-02-04 15:22:43 +01:00
|
|
|
#[allow(clippy::wrong_self_convention)]
|
2021-08-26 10:58:59 +02:00
|
|
|
pub fn to_flag(&self) -> Option<&str> {
|
|
|
|
match self {
|
|
|
|
BuildMode::Release => Some("--release"),
|
|
|
|
BuildMode::Debug => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for BuildMode {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
let cmd = match self {
|
|
|
|
BuildMode::Release => "release",
|
|
|
|
BuildMode::Debug => "debug",
|
|
|
|
};
|
|
|
|
|
2023-02-04 15:22:43 +01:00
|
|
|
write!(f, "{cmd}")
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-15 00:26:16 +02:00
|
|
|
pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Result<RunResult> {
|
2023-04-15 13:45:58 +02:00
|
|
|
log::info!("👟 {command}");
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-04-14 23:59:23 +02:00
|
|
|
let mut process = Command::new(command.executable());
|
|
|
|
|
|
|
|
process
|
2023-04-15 00:09:50 +02:00
|
|
|
.args(command.args())
|
|
|
|
.stdout(Stdio::piped())
|
2023-04-14 23:59:23 +02:00
|
|
|
.stderr(stderr_mode);
|
|
|
|
|
|
|
|
if let Some(dir) = command.chdir() {
|
|
|
|
process.current_dir(dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
let result = process.output()?;
|
2021-08-26 10:58:59 +02:00
|
|
|
|
2023-04-15 00:09:50 +02:00
|
|
|
let exit_status = result.status;
|
|
|
|
let stderr = String::from_utf8(result.stderr).unwrap_or("Not displayable".into());
|
|
|
|
let stdout = String::from_utf8(result.stdout).unwrap_or("Not displayable".into());
|
2023-02-06 13:21:04 +01:00
|
|
|
|
2021-08-26 10:58:59 +02:00
|
|
|
Ok(RunResult {
|
|
|
|
exit_status,
|
2023-02-08 22:09:32 +01:00
|
|
|
stdout,
|
|
|
|
stderr,
|
2021-08-26 10:58:59 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-01-08 19:16:36 +01:00
|
|
|
/// Check if `run` was successful.
|
2021-08-26 10:58:59 +02:00
|
|
|
/// returns Ok in case the run went as expected,
|
|
|
|
/// Err otherwise
|
2023-02-05 01:50:29 +01:00
|
|
|
pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), TestRunError> {
|
2021-09-22 13:22:45 +02:00
|
|
|
let mut file_handle =
|
2023-02-05 01:50:29 +01:00
|
|
|
File::open(expected_output_file).map_err(|_| TestRunError::FileError {
|
|
|
|
file: expected_output_file.to_owned(),
|
2021-09-22 13:22:45 +02:00
|
|
|
})?;
|
2021-08-26 10:58:59 +02:00
|
|
|
let mut expected_output = String::new();
|
2021-09-22 13:22:45 +02:00
|
|
|
file_handle
|
|
|
|
.read_to_string(&mut expected_output)
|
|
|
|
.map_err(|_| TestRunError::FileError {
|
2023-02-05 01:50:29 +01:00
|
|
|
file: expected_output_file.to_owned(),
|
2021-09-22 13:22:45 +02:00
|
|
|
})?;
|
|
|
|
|
2023-02-08 22:09:32 +01:00
|
|
|
if expected_output != run.stdout {
|
2021-09-22 13:22:45 +02:00
|
|
|
Err(TestRunError::FileCmpError {
|
|
|
|
expected: expected_output.clone(),
|
2023-02-08 22:09:32 +01:00
|
|
|
got: run.stdout.clone(),
|
2021-09-22 13:22:45 +02:00
|
|
|
})
|
|
|
|
} else if !run.exit_status.success() {
|
|
|
|
Err(TestRunError::CommandError(run.clone()))
|
2021-08-26 10:58:59 +02:00
|
|
|
} else {
|
2021-09-22 13:22:45 +02:00
|
|
|
Ok(())
|
2021-08-26 10:58:59 +02:00
|
|
|
}
|
|
|
|
}
|
2023-04-15 13:45:58 +02:00
|
|
|
|
2023-04-15 20:19:37 +02:00
|
|
|
pub fn handle_results(globals: &Globals, results: Vec<FinalRunResult>) -> Result<(), ()> {
|
2023-04-15 13:45:58 +02:00
|
|
|
let errors = results.iter().filter_map(|r| {
|
|
|
|
if let FinalRunResult::Failed(c, r) = r {
|
2023-04-15 20:44:06 +02:00
|
|
|
Some((c, &r.stdout, &r.stderr))
|
2023-04-15 13:45:58 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let successes = results.iter().filter_map(|r| {
|
|
|
|
if let FinalRunResult::Success(c, r) = r {
|
2023-04-15 20:44:06 +02:00
|
|
|
Some((c, &r.stdout, &r.stderr))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let command_errors = results.iter().filter_map(|r| {
|
|
|
|
if let FinalRunResult::CommandError(c, e) = r {
|
|
|
|
Some((c, e))
|
2023-04-15 13:45:58 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
let log_stdout_stderr = |level: Level| {
|
2023-04-15 20:44:06 +02:00
|
|
|
move |(command, stdout, stderr): (&CargoCommand, &String, &String)| {
|
2023-04-15 13:45:58 +02:00
|
|
|
if !stdout.is_empty() && !stderr.is_empty() {
|
|
|
|
log::log!(
|
|
|
|
level,
|
|
|
|
"Output for \"{command}\"\nStdout:\n{stdout}\nStderr:\n{stderr}"
|
|
|
|
);
|
|
|
|
} else if !stdout.is_empty() {
|
|
|
|
log::log!(
|
|
|
|
level,
|
|
|
|
"Output for \"{command}\":\nStdout:\n{}",
|
|
|
|
stdout.trim_end()
|
|
|
|
);
|
|
|
|
} else if !stderr.is_empty() {
|
|
|
|
log::log!(
|
|
|
|
level,
|
|
|
|
"Output for \"{command}\"\nStderr:\n{}",
|
|
|
|
stderr.trim_end()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
successes.clone().for_each(log_stdout_stderr(Level::Debug));
|
|
|
|
errors.clone().for_each(log_stdout_stderr(Level::Error));
|
|
|
|
|
2023-04-15 20:44:06 +02:00
|
|
|
successes.for_each(|(cmd, _, _)| {
|
2023-04-14 23:59:23 +02:00
|
|
|
let path = if let Some(dir) = cmd.chdir() {
|
|
|
|
let path = dir.as_os_str().to_str().unwrap_or("Not displayable");
|
|
|
|
format!(" (in {path}")
|
|
|
|
} else {
|
|
|
|
format!("")
|
|
|
|
};
|
|
|
|
|
2023-04-15 13:55:56 +02:00
|
|
|
if globals.verbose > 0 {
|
2023-04-14 23:59:23 +02:00
|
|
|
info!("✅ Success:{path} {cmd}\n {}", cmd.as_cmd_string());
|
2023-04-15 13:55:56 +02:00
|
|
|
} else {
|
2023-04-14 23:59:23 +02:00
|
|
|
info!("✅ Success:{path} {cmd}");
|
2023-04-15 13:55:56 +02:00
|
|
|
}
|
2023-04-15 13:45:58 +02:00
|
|
|
});
|
|
|
|
|
2023-04-15 20:44:06 +02:00
|
|
|
errors.clone().for_each(|(cmd, _, _)| {
|
2023-04-14 23:59:23 +02:00
|
|
|
if let Some(dir) = cmd.chdir() {
|
|
|
|
let path = dir.as_os_str().to_str().unwrap_or("Not displayable");
|
|
|
|
error!("❌ Failed: (in {path}) {cmd}\n {}", cmd.as_cmd_string());
|
|
|
|
} else {
|
|
|
|
error!("❌ Failed: {cmd}\n {}", cmd.as_cmd_string());
|
|
|
|
}
|
2023-04-15 13:45:58 +02:00
|
|
|
});
|
|
|
|
|
2023-04-15 20:44:06 +02:00
|
|
|
command_errors
|
|
|
|
.clone()
|
|
|
|
.for_each(|(cmd, error)| error!("❌ Failed: {cmd}\n {}\n{error}", cmd.as_cmd_string()));
|
|
|
|
|
2023-04-15 13:45:58 +02:00
|
|
|
let ecount = errors.count();
|
2023-04-15 20:44:06 +02:00
|
|
|
let cecount = command_errors.count();
|
|
|
|
if ecount != 0 || cecount != 0 {
|
2023-04-15 20:19:37 +02:00
|
|
|
log::error!("{ecount} commands failed.");
|
|
|
|
Err(())
|
2023-04-15 13:45:58 +02:00
|
|
|
} else {
|
2023-04-15 20:19:37 +02:00
|
|
|
info!("🚀🚀🚀 All tasks succeeded 🚀🚀🚀");
|
2023-04-15 13:45:58 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|