1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::{features::FeatureMatrix, Error};
use lazy_static::lazy_static;
use std::{
env, ffi::OsString, io::Write, path::Path, process::Command, process::Stdio,
};
use yansi::Color::*;
pub(crate) struct Task<'t> {
matrix: FeatureMatrix<'t>,
manifest_path: Option<&'t Path>,
package_name: &'t str,
args: &'t [String],
command: &'t str,
kind: TaskKind,
}
#[derive(Copy, Clone)]
pub enum TaskKind {
DryRun,
PrintJobs,
Execute,
}
impl<'t> Task<'t> {
pub(crate) fn new(
kind: TaskKind,
command: &'t str,
manifest_path: Option<&'t Path>,
package_name: &'t str,
args: &'t [String],
matrix: FeatureMatrix<'t>,
) -> Self {
Self {
matrix,
manifest_path,
package_name,
args,
command,
kind,
}
}
pub(crate) fn run(self) -> Result<(), Error> {
match self.kind {
TaskKind::DryRun => self.execute(true),
TaskKind::PrintJobs => {
self.print_jobs();
Ok(())
}
TaskKind::Execute => self.execute(false),
}
}
fn execute(self, dry_run: bool) -> Result<(), Error> {
let mut exit_status = None;
for feature_set in self.matrix {
let feature_set = feature_set.to_string();
let cmd = if dry_run {
None
} else {
let mut cmd = Command::new(CARGO.as_os_str());
cmd.arg(self.command)
.args(self.args.iter())
.stderr(Stdio::piped())
.stdout(Stdio::piped())
.arg("--package")
.arg(self.package_name);
if let Some(manifest_path) = self.manifest_path {
cmd.arg("--manifest-path").arg(manifest_path);
}
if !feature_set.is_empty() {
cmd.arg("--features").arg(&feature_set);
}
Some(cmd)
};
print!(
"{}{} {}{} {}{}{}......",
Cyan.paint("running: cmd="),
self.command,
Cyan.paint("package="),
self.package_name,
Cyan.paint("features=["),
feature_set,
Cyan.paint("]")
);
let on_success =
|| println!("{}", Black.style().bg(Green).paint("OK"));
match cmd {
None => on_success(),
Some(mut cmd) => {
let output = cmd.output().map_err(|err| Error::Io {
message: "failed to get output of cargo command",
source: err,
})?;
if output.status.success() {
on_success();
} else {
println!("{}", Black.style().bg(Red).paint("Fail"));
exit_status = Some(output.status);
std::io::stderr()
.write_all(&output.stderr)
.expect("failed to write to stderr");
std::io::stdout()
.write_all(&output.stdout)
.expect("failed to write to stdout");
}
}
};
}
match exit_status {
Some(exit_status) => Err(Error::Fail(exit_status)),
None => Ok(()),
}
}
fn print_jobs(self) {
let prefix = format!(
"{} {} --package {} {}",
CARGO.to_string_lossy(),
self.command,
self.package_name,
self.args.join(" ")
);
for feature_set in self.matrix {
if feature_set.is_empty() {
println!("{}", prefix);
} else {
println!("{} --features {}", prefix, feature_set);
}
}
}
}
lazy_static! {
static ref CARGO: OsString =
env::var_os("CARGO").unwrap_or_else(|| "cargo".into());
}