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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use std::path::PathBuf;
use std::process::Command;
use clap::{ArgAction, Parser};
#[derive(Clone, Debug, Default, Parser)]
pub struct CommonOptions {
#[clap(short = 'q', long)]
pub quiet: bool,
#[clap(short = 'j', long, value_name = "N")]
pub jobs: Option<usize>,
#[clap(long)]
pub keep_going: bool,
#[clap(short = 'r', long)]
pub release: bool,
#[clap(long, value_name = "PROFILE-NAME")]
pub profile: Option<String>,
#[clap(short = 'F', long, action = ArgAction::Append)]
pub features: Vec<String>,
#[clap(long)]
pub all_features: bool,
#[clap(long)]
pub no_default_features: bool,
#[clap(
long,
value_name = "TRIPLE",
env = "CARGO_BUILD_TARGET",
action = ArgAction::Append
)]
pub target: Vec<String>,
#[clap(long, value_name = "DIRECTORY", value_parser)]
pub target_dir: Option<PathBuf>,
#[clap(long, value_name = "PATH", value_parser)]
pub manifest_path: Option<PathBuf>,
#[clap(long)]
pub ignore_rust_version: bool,
#[clap(long, value_name = "FMT", action = ArgAction::Append)]
pub message_format: Vec<String>,
#[clap(long)]
pub unit_graph: bool,
#[clap(short = 'v', long, action = ArgAction::Count)]
pub verbose: u8,
#[clap(long, value_name = "WHEN")]
pub color: Option<String>,
#[clap(long)]
pub frozen: bool,
#[clap(long)]
pub locked: bool,
#[clap(long)]
pub offline: bool,
#[clap(long, value_name = "KEY=VALUE", action = ArgAction::Append)]
pub config: Vec<String>,
#[clap(short = 'Z', value_name = "FLAG", action = ArgAction::Append)]
pub unstable_flags: Vec<String>,
#[clap(
long,
value_name = "FMTS",
min_values = 0,
value_delimiter = ',',
require_equals = true
)]
pub timings: Option<Vec<String>>,
}
impl CommonOptions {
pub fn apply(&self, cmd: &mut Command) {
if self.quiet {
cmd.arg("--quiet");
}
if let Some(jobs) = self.jobs {
cmd.arg("--jobs").arg(jobs.to_string());
}
if self.keep_going {
cmd.arg("--keep-going");
}
if self.release {
cmd.arg("--release");
}
if let Some(profile) = self.profile.as_ref() {
cmd.arg("--profile").arg(profile);
}
for feature in &self.features {
cmd.arg("--features").arg(feature);
}
if self.all_features {
cmd.arg("--all-features");
}
if self.no_default_features {
cmd.arg("--no-default-features");
}
let rust_targets = self
.target
.iter()
.map(|target| target.split_once('.').map(|(t, _)| t).unwrap_or(target))
.collect::<Vec<&str>>();
rust_targets.iter().for_each(|target| {
cmd.arg("--target").arg(&target);
});
if let Some(dir) = self.target_dir.as_ref() {
cmd.arg("--target-dir").arg(dir);
}
if let Some(path) = self.manifest_path.as_ref() {
cmd.arg("--manifest-path").arg(path);
}
if self.ignore_rust_version {
cmd.arg("--ignore-rust-version");
}
for fmt in &self.message_format {
cmd.arg("--message-format").arg(fmt);
}
if self.unit_graph {
cmd.arg("--unit-graph");
}
if self.verbose > 0 {
cmd.arg(format!("-{}", "v".repeat(self.verbose as usize)));
}
if let Some(color) = self.color.as_ref() {
cmd.arg("--color").arg(color);
}
if self.frozen {
cmd.arg("--frozen");
}
if self.locked {
cmd.arg("--locked");
}
if self.offline {
cmd.arg("--offline");
}
for config in &self.config {
cmd.arg("--config").arg(config);
}
for flag in &self.unstable_flags {
cmd.arg("-Z").arg(flag);
}
if let Some(timings) = &self.timings {
if timings.is_empty() {
cmd.arg("--timings");
} else {
let timings: Vec<_> = timings.iter().map(|x| x.as_str()).collect();
cmd.arg(format!("--timings={}", timings.join(",")));
}
}
}
pub(crate) fn cargo_command() -> Command {
Command::new("cargo")
}
}