rtic/xtask/src/command.rs

514 lines
16 KiB
Rust
Raw Normal View History

use crate::{debug, ExtraArguments, Package, RunResult, Target, TestRunError};
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,
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,
}
#[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> {
// For future embedded-ci
#[allow(dead_code)]
2021-08-26 10:58:59 +02:00
Run {
cargoarg: &'a Option<&'a str>,
example: &'a str,
target: Target<'a>,
2023-02-28 23:55:02 +01:00
features: Option<String>,
mode: BuildMode,
},
Qemu {
cargoarg: &'a Option<&'a str>,
2021-08-26 10:58:59 +02:00
example: &'a str,
target: Target<'a>,
2023-02-28 23:55:02 +01:00
features: Option<String>,
2021-08-26 10:58:59 +02:00
mode: BuildMode,
},
ExampleBuild {
cargoarg: &'a Option<&'a str>,
example: &'a str,
target: Target<'a>,
2023-02-28 23:55:02 +01:00
features: Option<String>,
2021-08-26 10:58:59 +02:00
mode: BuildMode,
},
ExampleCheck {
cargoarg: &'a Option<&'a str>,
example: &'a str,
target: Target<'a>,
2023-02-28 23:55:02 +01:00
features: Option<String>,
mode: BuildMode,
},
Build {
cargoarg: &'a Option<&'a str>,
2023-02-28 23:55:02 +01:00
package: Option<Package>,
target: Target<'a>,
2023-02-28 23:55:02 +01:00
features: Option<String>,
mode: BuildMode,
},
Check {
cargoarg: &'a Option<&'a str>,
2023-02-28 23:55:02 +01:00
package: Option<Package>,
target: Target<'a>,
2023-02-28 23:55:02 +01:00
features: Option<String>,
2023-02-24 23:14:11 +01:00
mode: BuildMode,
},
Clippy {
cargoarg: &'a Option<&'a str>,
2023-02-28 23:55:02 +01:00
package: Option<Package>,
target: Target<'a>,
2023-02-28 23:55:02 +01:00
features: Option<String>,
},
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>,
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 {
arguments: Option<ExtraArguments>,
2023-02-25 00:28:45 +01:00
},
ExampleSize {
cargoarg: &'a Option<&'a str>,
example: &'a str,
target: Target<'a>,
2023-02-28 23:55:02 +01:00
features: Option<String>,
mode: BuildMode,
arguments: Option<ExtraArguments>,
},
2021-08-26 10:58:59 +02:00
}
impl<'a> CargoCommand<'a> {
fn command(&self) -> &str {
2021-08-26 10:58:59 +02:00
match self {
CargoCommand::Run { .. } | CargoCommand::Qemu { .. } => "run",
CargoCommand::ExampleCheck { .. } | CargoCommand::Check { .. } => "check",
CargoCommand::ExampleBuild { .. } | CargoCommand::Build { .. } => "build",
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
}
}
pub fn executable(&self) -> &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-02-25 00:28:45 +01:00
| CargoCommand::Doc { .. } => "cargo",
CargoCommand::Book { .. } => "mdbook",
2021-08-26 10:58:59 +02:00
}
}
pub fn args(&self) -> Vec<&str> {
match self {
// For future embedded-ci, for now the same as Qemu
2021-08-26 10:58:59 +02:00
CargoCommand::Run {
cargoarg,
2021-08-26 10:58:59 +02:00
example,
target,
features,
mode,
} => {
let mut args = vec!["+nightly"];
if let Some(cargoarg) = cargoarg {
args.extend_from_slice(&[cargoarg]);
}
args.extend_from_slice(&[
self.command(),
"--example",
example,
"--target",
target.triple(),
]);
2021-08-26 10:58:59 +02: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
}
CargoCommand::Qemu {
cargoarg,
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
} => {
let mut args = vec!["+nightly"];
if let Some(cargoarg) = cargoarg {
args.extend_from_slice(&[cargoarg]);
}
args.extend_from_slice(&[
self.command(),
"--example",
example,
"--target",
target.triple(),
]);
2021-08-26 10:58:59 +02:00
if let Some(feature) = features {
args.extend_from_slice(&["--features", feature]);
}
if let Some(flag) = mode.to_flag() {
args.push(flag);
}
args
}
CargoCommand::Build {
cargoarg,
package,
target,
features,
mode,
} => {
let mut args = vec!["+nightly"];
if let Some(cargoarg) = cargoarg {
args.extend_from_slice(&[cargoarg]);
}
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()]);
}
if let Some(feature) = features {
args.extend_from_slice(&["--features", feature]);
}
if let Some(flag) = mode.to_flag() {
args.push(flag);
}
args
}
CargoCommand::Check {
cargoarg,
package,
target: _,
features,
2023-02-24 23:14:11 +01:00
mode,
} => {
let mut args = vec!["+nightly"];
if let Some(cargoarg) = cargoarg {
args.extend_from_slice(&[cargoarg]);
}
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()]);
}
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);
}
args
}
CargoCommand::Clippy {
cargoarg,
package,
target: _,
features,
} => {
let mut args = vec!["+nightly"];
if let Some(cargoarg) = cargoarg {
args.extend_from_slice(&[cargoarg]);
}
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()]);
}
if let Some(feature) = features {
args.extend_from_slice(&["--features", feature]);
}
args
}
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]);
}
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]);
}
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"];
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
}
CargoCommand::Book { arguments } => {
2023-02-25 00:28:45 +01:00
let mut args = vec![];
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
// with default path to book
args.extend_from_slice(&[self.command()]);
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,
} => {
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
}
CargoCommand::ExampleBuild {
cargoarg,
example,
target,
features,
mode,
} => {
let mut args = vec!["+nightly"];
if let Some(cargoarg) = cargoarg {
args.extend_from_slice(&[cargoarg]);
}
args.extend_from_slice(&[
self.command(),
"--example",
example,
"--target",
target.triple(),
]);
if let Some(feature) = features {
args.extend_from_slice(&["--features", feature]);
}
if let Some(flag) = mode.to_flag() {
args.push(flag);
}
args
}
CargoCommand::ExampleCheck {
cargoarg,
example,
target,
features,
mode,
} => {
let mut args = vec!["+nightly"];
if let Some(cargoarg) = cargoarg {
args.extend_from_slice(&[cargoarg]);
}
args.extend_from_slice(&[
self.command(),
"--example",
example,
"--target",
target.triple(),
]);
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
}
CargoCommand::ExampleSize {
cargoarg,
example,
target,
features,
mode,
arguments,
} => {
let mut args = vec!["+nightly"];
if let Some(cargoarg) = cargoarg {
args.extend_from_slice(&[cargoarg]);
}
args.extend_from_slice(&[
self.command(),
"--example",
example,
"--target",
target.triple(),
]);
if let Some(feature_name) = features {
args.extend_from_slice(&["--features", feature_name]);
}
if let Some(flag) = mode.to_flag() {
args.push(flag);
}
if let Some(ExtraArguments::Other(arguments)) = arguments {
// Arguments to cargo size must be passed after "--"
args.extend_from_slice(&["--"]);
for arg in arguments {
args.extend_from_slice(&[arg.as_str()]);
}
}
args
}
2021-08-26 10:58:59 +02:00
}
}
}
impl BuildMode {
#[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",
};
write!(f, "{cmd}")
2021-08-26 10:58:59 +02:00
}
}
pub fn run_command(command: &CargoCommand, stderr_mode: OutputMode) -> anyhow::Result<RunResult> {
2023-04-15 00:09:50 +02:00
let command_display = command.executable();
let args = command.args();
2021-08-26 10:58:59 +02:00
2023-04-15 00:09:50 +02:00
let full_command = format!("\"{command_display}\" {}", args.join(" "));
debug!("👟 {full_command}");
2021-08-26 10:58:59 +02:00
2023-04-15 00:09:50 +02:00
let result = Command::new(command.executable())
.args(command.args())
.stdout(Stdio::piped())
.stderr(stderr_mode)
2023-04-15 00:09:50 +02:00
.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());
2021-08-26 10:58:59 +02:00
Ok(RunResult {
exit_status,
2023-04-15 00:09:50 +02:00
full_command,
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
pub fn run_successful(run: &RunResult, expected_output_file: &str) -> Result<(), TestRunError> {
2021-09-22 13:22:45 +02:00
let mut file_handle =
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 {
file: expected_output_file.to_owned(),
2021-09-22 13:22:45 +02:00
})?;
if expected_output != run.stdout {
2021-09-22 13:22:45 +02:00
Err(TestRunError::FileCmpError {
expected: expected_output.clone(),
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
}
}