/
mission.rs
227 lines (216 loc) · 7.59 KB
/
mission.rs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use {
crate::*,
anyhow::Result,
notify::{
RecommendedWatcher,
RecursiveMode,
Watcher,
},
std::{
collections::HashSet,
iter,
path::PathBuf,
process::Command,
},
};
/// the description of the mission of bacon
/// after analysis of the args, env, and surroundings
#[derive(Debug)]
pub struct Mission<'s> {
pub location_name: String,
pub job_name: String,
pub cargo_execution_directory: PathBuf,
pub workspace_root: PathBuf,
pub job: Job,
files_to_watch: Vec<PathBuf>,
directories_to_watch: Vec<PathBuf>,
pub settings: &'s Settings,
}
impl<'s> Mission<'s> {
pub fn new(
location: &MissionLocation,
job_name: String,
job: Job,
settings: &'s Settings,
) -> Result<Self> {
let location_name = location.name();
let add_all_src = location.intended_is_package;
let mut files_to_watch: Vec<PathBuf> = Vec::new();
let mut directories_to_watch = Vec::new();
if !location.intended_is_package {
directories_to_watch.push(location.intended_dir.clone());
}
for item in &location.packages {
if item.source.is_none() {
let item_path = item
.manifest_path
.parent()
.expect("parent of a target folder is a root folder");
if add_all_src {
let src_watch_iter = iter::once("src");
let other_watch_iter = job.watch.iter().map(String::as_ref);
for dir in src_watch_iter.chain(other_watch_iter) {
let full_path = item_path.join(dir);
if full_path.exists() {
directories_to_watch.push(full_path.into());
} else {
warn!("missing {} dir: {:?}", dir, full_path);
}
}
}
if item.manifest_path.exists() {
files_to_watch.push(item.manifest_path.clone().into());
} else {
warn!("missing manifest file: {:?}", item.manifest_path);
}
}
}
let cargo_execution_directory = location.package_directory.clone();
Ok(Mission {
location_name,
job_name,
cargo_execution_directory,
workspace_root: location.workspace_root.clone(),
job,
files_to_watch,
directories_to_watch,
settings,
})
}
/// Return an Ignorer if required by the job's settings
/// and if the mission takes place in a git repository
pub fn ignorer(&self) -> Option<Ignorer> {
match self.job.apply_gitignore {
Some(false) => {
debug!("No gitignorer because of settings");
None
}
_ => {
// by default we apply gitignore rules
match Ignorer::new(&self.workspace_root) {
Ok(ignorer) => Some(ignorer),
Err(e) => {
// might be normal, eg not in a git repo
debug!("Failed to initialise git ignorer: {e}");
None
}
}
}
}
}
/// Return the path to the bacon-locations file
pub fn bacon_locations_path(&self) -> PathBuf {
self.workspace_root.join(".bacon-locations")
}
/// the action bound to success on this job
pub fn on_success(&self) -> &Option<Action> {
&self.job.on_success
}
pub fn is_success(
&self,
report: &Report,
) -> bool {
report.is_success(self.job.allow_warnings, self.job.allow_failures)
}
/// configure the watcher with files and directories to watch
pub fn add_watchs(
&self,
watcher: &mut RecommendedWatcher,
) -> Result<()> {
for file in &self.files_to_watch {
debug!("add watch file {:?}", file);
watcher.watch(file, RecursiveMode::NonRecursive)?;
}
for dir in &self.directories_to_watch {
debug!("add watch dir {:?}", dir);
watcher.watch(dir, RecursiveMode::Recursive)?;
}
Ok(())
}
/// build (and doesn't call) the external cargo command
pub fn get_command(&self) -> Command {
let mut tokens = self.job.command.iter();
let mut command = Command::new(
tokens.next().unwrap(), // implies a check in the job
);
let mut no_default_features_done = false;
let mut features_done = false;
let mut last_is_features = false;
let tokens = tokens.chain(&self.settings.additional_job_args);
for arg in tokens {
if last_is_features {
if self.settings.all_features {
debug!("ignoring features given along --all-features");
} else {
features_done = true;
// arg is expected there to be the list of features
match (&self.settings.features, self.settings.no_default_features) {
(Some(features), false) => {
// we take the features of both the job and the args
command.arg("--features");
command.arg(merge_features(arg, features));
}
(Some(features), true) => {
// arg add features and remove the job ones
command.arg("--features");
command.arg(features);
}
(None, true) => {
// we pass no feature
}
(None, false) => {
// nothing to change
command.arg("--features");
command.arg(arg);
}
}
}
last_is_features = false;
} else if arg == "--no-default-features" {
no_default_features_done = true;
last_is_features = false;
command.arg(arg);
} else if arg == "--features" {
last_is_features = true;
} else {
command.arg(arg);
}
}
if self.settings.no_default_features && !no_default_features_done {
command.arg("--no-default-features");
}
if self.settings.all_features {
command.arg("--all-features");
}
if !features_done {
if let Some(features) = &self.settings.features {
if self.settings.all_features {
debug!("not using features because of --all-features");
} else {
command.arg("--features");
command.arg(features);
}
}
}
command.current_dir(&self.cargo_execution_directory);
debug!("command: {:#?}", &command);
command
}
/// whether we need stdout and not just stderr
pub fn need_stdout(&self) -> bool {
self.job.need_stdout
}
}
fn merge_features(
a: &str,
b: &str,
) -> String {
let mut features = HashSet::new();
for feature in a.split(',') {
features.insert(feature);
}
for feature in b.split(',') {
features.insert(feature);
}
features.iter().copied().collect::<Vec<&str>>().join(",")
}