-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoll.rs
More file actions
439 lines (385 loc) · 14.2 KB
/
Copy pathpoll.rs
File metadata and controls
439 lines (385 loc) · 14.2 KB
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
//! Assembles the definition tree the rest of the CLI operates on by polling it
//! out of the repositories that own it, instead of reading a hand-maintained
//! `definitions/` folder.
//!
//! Definitions no longer exist as files at rest in those repositories: they are
//! compiled into the producer binaries (taurus registers them through
//! `inventory`, hercules actions through `Action`). Running the producer is the
//! only way to get them back out, so that is what this does -- clone at a
//! pinned ref, run its export command into a scratch directory, copy the result
//! into the output tree, and delete the clone.
//!
//! Everything downstream (`report`, `publish`, `push`) then reads that tree
//! through [`crate::reader::Reader`] exactly as it read the committed one.
use std::collections::{BTreeMap, BTreeSet};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Mutex;
use serde::Deserialize;
use tabled::Tabled;
use crate::formatter::{error_without_trace, info, success, success_table};
const DEFAULT_CONFIG: &str = "./collector.toml";
#[derive(Deserialize)]
pub struct CollectorConfig {
#[serde(default = "default_out")]
pub out: String,
#[serde(default, rename = "source")]
pub sources: Vec<Source>,
}
#[derive(Deserialize, Clone)]
pub struct Source {
/// Identifies the source on the command line (`--only`) and in output.
/// Not the module name -- one source can produce several modules.
pub name: String,
pub repo: String,
#[serde(default = "default_ref")]
pub r#ref: String,
/// Directory inside the clone the export command runs in, for
/// repositories holding more than one producer (centaurus).
#[serde(default)]
pub workdir: Option<String>,
/// Argv of the export command. The destination path is appended to it.
pub export: Vec<String>,
/// Environment the export command runs with, on top of the inherited one.
#[serde(default)]
pub env: BTreeMap<String, String>,
#[serde(default)]
pub layout: Layout,
/// Modules this source is expected to produce. Polling fails if any of
/// them is missing afterwards, so a producer that silently stops emitting
/// a module cannot slip into a release.
pub modules: Vec<String>,
}
/// Where a producer's export command writes relative to the path it is given.
#[derive(Deserialize, Clone, Copy, Default, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum Layout {
/// Writes a single module's files directly into the given directory, so it
/// is handed `<out>/<module>`. What `hercules::Action::export` does.
#[default]
Module,
/// Writes one directory per module into the given directory, so it is
/// handed `<out>`. What a taurus export over `build_modules()` does.
Tree,
}
fn default_out() -> String {
String::from("./definitions")
}
fn default_ref() -> String {
String::from("main")
}
#[derive(Tabled)]
struct PolledRow {
#[tabled(rename = "Source")]
source: String,
#[tabled(rename = "Ref")]
reference: String,
#[tabled(rename = "Module")]
module: String,
#[tabled(rename = "Definitions")]
definitions: usize,
}
pub fn poll(
config_path: Option<String>,
out: Option<String>,
only: Option<Vec<String>>,
keep: bool,
) {
let config_path = config_path.unwrap_or_else(|| DEFAULT_CONFIG.to_string());
let config = read_config(&config_path);
let out_dir = PathBuf::from(out.unwrap_or(config.out));
let sources = select_sources(config.sources, only);
// Scratch lives outside the output tree so a failed run cannot leave
// half-exported clones where the reader would later pick them up.
let workspace = std::env::temp_dir().join(format!("code0-poll-{}", std::process::id()));
reset_directory(&workspace);
reset_directory(&out_dir);
// Export commands run with their working directory set to the producer's
// checkout, so a relative destination would resolve against that checkout
// instead of here -- the export would land inside the clone and vanish
// with it. The directory exists by now, so this cannot fail for a path we
// just created.
let out_dir = match out_dir.canonicalize() {
Ok(absolute) => absolute,
Err(err) => fail(format!(
"Could not resolve the output directory {}: {err}",
out_dir.display()
)),
};
// A checkout carries the producer's whole build tree, so leaving one
// behind costs hundreds of megabytes. `fail` exits the process rather
// than unwinding, so it has to clean up on the way out itself.
if !keep {
register_workspace_for_cleanup(workspace.clone());
}
let mut rows: Vec<PolledRow> = vec![];
for source in &sources {
info(format!(
"Polling `{}` from {} ({})",
source.name, source.repo, source.r#ref
));
let checkout = workspace.join(&source.name);
clone(source, &checkout);
// Sources share one output tree, so the only way to attribute a module
// directory to the source that wrote it is to diff around its export.
let before = module_directories(&out_dir);
export(source, &checkout, &out_dir);
let produced = module_directories(&out_dir);
let undeclared: Vec<String> = produced
.difference(&before)
.filter(|module| !source.modules.contains(module))
.cloned()
.collect();
if !undeclared.is_empty() {
fail(format!(
"`{}` produced {} it does not declare in {}: {}. Add them to `modules` if they belong in the release.",
source.name,
if undeclared.len() == 1 {
"a module"
} else {
"modules"
},
config_path,
undeclared.join(", ")
));
}
for module in &source.modules {
let module_dir = out_dir.join(module);
if !module_dir.is_dir() {
fail(format!(
"`{}` finished without producing the module `{}` it declares in {}.",
source.name, module, config_path
));
}
verify_module(&module_dir, &source.name, module);
rows.push(PolledRow {
source: source.name.clone(),
reference: source.r#ref.clone(),
module: module.clone(),
definitions: count_definitions(&module_dir),
});
}
}
if keep {
info(format!("Kept checkouts in {}", workspace.display()));
} else {
let _ = fs::remove_dir_all(&workspace);
}
success(format!(
"Polled {} module(s) from {} source(s) into {}.",
rows.len(),
sources.len(),
out_dir.display()
));
success_table(rows);
}
fn read_config(path: &str) -> CollectorConfig {
let raw = match fs::read_to_string(path) {
Ok(raw) => raw,
Err(err) => fail(format!(
"Could not read the collector config `{path}`: {err}"
)),
};
match toml::from_str::<CollectorConfig>(&raw) {
Ok(config) if config.sources.is_empty() => {
fail(format!("`{path}` does not declare any [[source]] entries."))
}
Ok(config) => config,
Err(err) => fail(format!("Could not parse `{path}`: {err}")),
}
}
fn select_sources(sources: Vec<Source>, only: Option<Vec<String>>) -> Vec<Source> {
let Some(only) = only else {
return sources;
};
for wanted in &only {
if !sources.iter().any(|source| &source.name == wanted) {
fail(format!(
"`{}` is not a source in the collector config. Available: {}.",
wanted,
sources
.iter()
.map(|source| source.name.as_str())
.collect::<Vec<_>>()
.join(", ")
));
}
}
sources
.into_iter()
.filter(|source| only.contains(&source.name))
.collect()
}
fn clone(source: &Source, checkout: &Path) {
// A shallow single-branch clone is all the export needs, and it keeps the
// release job from pulling the full history of every producer.
let status = Command::new("git")
.args(["clone", "--depth", "1", "--branch", source.r#ref.as_str()])
.arg(&source.repo)
.arg(checkout)
.status();
match status {
Ok(status) if status.success() => {}
Ok(status) => fail(format!(
"Cloning `{}` from {} ({}) failed with {}.",
source.name, source.repo, source.r#ref, status
)),
Err(err) => fail(format!(
"Could not run git while cloning `{}`: {err}",
source.name
)),
}
}
fn export(source: &Source, checkout: &Path, out_dir: &Path) {
let Some((program, arguments)) = source.export.split_first() else {
fail(format!(
"`{}` declares an empty `export` command.",
source.name
));
};
// A "module" producer writes the module's files directly into whatever
// directory it is handed, so it has to be pointed at `<out>/<module>`
// rather than at the tree root.
let destination = match source.layout {
Layout::Tree => out_dir.to_path_buf(),
Layout::Module => match source.modules.as_slice() {
[module] => out_dir.join(module),
modules => fail(format!(
"`{}` uses layout \"module\" so it must declare exactly one module, but declares {}.",
source.name,
modules.len()
)),
},
};
let working_directory = match &source.workdir {
Some(workdir) => checkout.join(workdir),
None => checkout.to_path_buf(),
};
if !working_directory.is_dir() {
fail(format!(
"`{}` points at the workdir `{}`, which does not exist in the clone.",
source.name,
source.workdir.clone().unwrap_or_default()
));
}
let status = Command::new(program)
.args(arguments)
.arg(&destination)
.envs(&source.env)
.current_dir(&working_directory)
.status();
match status {
Ok(status) if status.success() => {}
Ok(status) => fail(format!(
"The export command for `{}` failed with {}.",
source.name, status
)),
Err(err) => fail(format!(
"Could not run the export command for `{}`: {err}",
source.name
)),
}
}
#[derive(Deserialize)]
struct ModuleIdentity {
identifier: String,
}
/// Normalises the module metadata file and checks the module is the one the
/// config says it is.
///
/// Two things are being guarded here. First, hercules writes a module's
/// metadata as `meta.json`, while [`crate::reader`] and `publish` both use
/// `module.json`; the mismatch is silent, because the reader still finds the
/// directory and just leaves the module config defaulted, quietly losing the
/// identifier, author, icon and version. Second, a "module" layout producer is
/// pointed at `<out>/<module>` -- a path built from the declared name -- so the
/// directory existing afterwards proves nothing. Comparing the identifier the
/// producer wrote against the declared name is the check that actually catches
/// a producer that renamed or dropped a module.
fn verify_module(module_dir: &Path, source: &str, module: &str) {
let canonical = module_dir.join("module.json");
let hercules = module_dir.join("meta.json");
if !canonical.is_file() {
if !hercules.is_file() {
fail(format!(
"`{source}` produced the module `{module}` without a module.json or meta.json."
));
}
if let Err(err) = fs::rename(&hercules, &canonical) {
fail(format!(
"Could not normalise meta.json to module.json for `{module}`: {err}"
));
}
}
let identity = fs::read_to_string(&canonical)
.ok()
.and_then(|raw| serde_json::from_str::<ModuleIdentity>(&raw).ok());
match identity {
Some(identity) if identity.identifier == module => {}
Some(identity) => fail(format!(
"`{source}` declares the module `{module}`, but the module it produced identifies itself as `{}`.",
identity.identifier
)),
None => fail(format!(
"Could not read an identifier out of {}.",
canonical.display()
)),
}
}
fn module_directories(out_dir: &Path) -> BTreeSet<String> {
let Ok(entries) = fs::read_dir(out_dir) else {
return BTreeSet::new();
};
entries
.flatten()
.filter(|entry| entry.path().is_dir())
.filter_map(|entry| entry.file_name().to_str().map(String::from))
.collect()
}
fn count_definitions(module_dir: &Path) -> usize {
let Ok(entries) = fs::read_dir(module_dir) else {
return 0;
};
entries
.flatten()
.filter(|entry| entry.path().is_dir())
.map(|entry| {
fs::read_dir(entry.path())
.map(|files| {
files
.flatten()
.filter(|file| {
file.path().extension().and_then(|ext| ext.to_str()) == Some("json")
})
.count()
})
.unwrap_or(0)
})
.sum()
}
fn reset_directory(path: &Path) {
let _ = fs::remove_dir_all(path);
if let Err(err) = fs::create_dir_all(path) {
fail(format!(
"Could not create the directory {}: {err}",
path.display()
));
}
}
/// Scratch directory to remove if the run exits through [`fail`].
static CLEANUP_WORKSPACE: Mutex<Option<PathBuf>> = Mutex::new(None);
fn register_workspace_for_cleanup(workspace: PathBuf) {
if let Ok(mut guard) = CLEANUP_WORKSPACE.lock() {
*guard = Some(workspace);
}
}
fn fail(message: String) -> ! {
if let Ok(mut guard) = CLEANUP_WORKSPACE.lock()
&& let Some(workspace) = guard.take()
{
let _ = fs::remove_dir_all(&workspace);
}
error_without_trace(message);
std::process::exit(1)
}