-
Notifications
You must be signed in to change notification settings - Fork 39
/
cli.rs
617 lines (556 loc) · 22 KB
/
cli.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
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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
// Copyright (c) 2017-2018 ETH Zurich
// Fabian Schuiki <[email protected]>
//! Main command line tool implementation.
use std;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use std::process::Command as SysCommand;
#[cfg(unix)]
use std::fs::{canonicalize, metadata};
#[cfg(windows)]
use dunce::canonicalize;
use clap::parser::ValuesRef;
use clap::{Arg, ArgAction, Command};
use serde_yaml;
use crate::cmd;
use crate::config::{
Config, Locked, LockedPackage, LockedSource, Manifest, Merge, PartialConfig, PrefixPaths,
Validate,
};
use crate::error::*;
use crate::resolver::DependencyResolver;
use crate::sess::{Session, SessionArenas, SessionIo};
use tokio::runtime::Runtime;
/// Inner main function which can return an error.
pub fn main() -> Result<()> {
let app = Command::new(env!("CARGO_PKG_NAME"))
.subcommand_required(true)
.arg_required_else_help(true)
.allow_external_subcommands(true)
.version(env!("CARGO_PKG_VERSION"))
.author(env!("CARGO_PKG_AUTHORS"))
.about("A dependency management tool for hardware projects.")
.after_help(
"Type 'bender <SUBCOMMAND> --help' for more information about a bender subcommand.",
)
.arg(
Arg::new("dir")
.short('d')
.long("dir")
.num_args(1)
.global(true)
.help("Sets a custom root working directory"),
)
.arg(
Arg::new("local")
.long("local")
.global(true)
.num_args(0)
.action(ArgAction::SetTrue)
.help("Disables fetching of remotes (e.g. for air-gapped computers)"),
)
.subcommand(
Command::new("update")
.about("Update the dependencies")
.arg(
Arg::new("fetch")
.short('f')
.long("fetch")
.num_args(0)
.action(ArgAction::SetTrue)
.help("forces fetch of git dependencies"),
)
.arg(
Arg::new("no-checkout")
.long("no-checkout")
.num_args(0)
.action(ArgAction::SetTrue)
.help("Disables checkout of dependencies"),
),
)
.subcommand(cmd::path::new())
.subcommand(cmd::parents::new())
.subcommand(cmd::clone::new())
.subcommand(cmd::packages::new())
.subcommand(cmd::sources::new())
.subcommand(cmd::completion::new())
.subcommand(cmd::config::new())
.subcommand(cmd::script::new())
.subcommand(cmd::checkout::new())
.subcommand(cmd::vendor::new())
.subcommand(cmd::fusesoc::new())
.subcommand(cmd::init::new());
// Add the `--debug` option in debug builds.
let app = if cfg!(debug_assertions) {
app.arg(
Arg::new("debug")
.long("debug")
.global(true)
.num_args(0)
.action(ArgAction::SetTrue)
.help("Print additional debug information"),
)
} else {
app
};
// Parse the arguments.
let matches = app.clone().get_matches();
// Enable debug outputs if needed.
if matches.contains_id("debug") && matches.get_flag("debug") {
ENABLE_DEBUG.store(true, std::sync::atomic::Ordering::Relaxed);
}
if let Some(("init", matches)) = matches.subcommand() {
return cmd::init::run(matches);
}
if let Some(("completion", matches)) = matches.subcommand() {
let mut app = app;
return cmd::completion::run(matches, &mut app);
}
let mut force_fetch = false;
if let Some(("update", intern_matches)) = matches.subcommand() {
force_fetch = intern_matches.get_flag("fetch");
if matches.get_flag("local") && intern_matches.get_flag("fetch") {
warnln!(
"As --local argument is set for bender command, no fetching will be performed."
);
}
}
// Determine the root working directory, which has either been provided via
// the -d/--dir switch, or by searching upwards in the file system
// hierarchy.
let root_dir: PathBuf = match matches.get_one::<String>("dir") {
Some(d) => canonicalize(d).map_err(|cause| {
Error::chain(format!("Failed to canonicalize path {:?}.", d), cause)
})?,
None => find_package_root(Path::new("."))
.map_err(|cause| Error::chain("Cannot find root directory of package.", cause))?,
};
debugln!("main: root dir {:?}", root_dir);
// Parse the manifest file of the package.
let manifest_path = root_dir.join("Bender.yml");
let manifest = read_manifest(&manifest_path)?;
debugln!("main: {:#?}", manifest);
// Gather and parse the tool configuration.
let config = load_config(
&root_dir,
matches!(matches.subcommand(), Some(("update", _))),
)?;
debugln!("main: {:#?}", config);
// Assemble the session.
let sess_arenas = SessionArenas::new();
let sess = Session::new(
&root_dir,
&manifest,
&config,
&sess_arenas,
matches.get_flag("local"),
force_fetch,
);
// Read the existing lockfile.
let lock_path = root_dir.join("Bender.lock");
let locked_existing = if lock_path.exists() {
Some(read_lockfile(&lock_path, &root_dir)?)
} else {
None
};
// Resolve the dependencies if the lockfile does not exist or is outdated.
let locked = match matches.subcommand() {
Some((command, matches)) => {
#[allow(clippy::unnecessary_unwrap)]
// execute pre-dependency-fetch commands
if command == "fusesoc" && matches.get_flag("single") {
return cmd::fusesoc::run_single(&sess, matches);
} else if command == "update" || locked_existing.is_none() {
if manifest.frozen {
return Err(Error::new(format!(
"Refusing to update dependencies because the package is frozen.
Remove the `frozen: true` from {:?} to proceed; there be dragons.",
manifest_path
)));
}
debugln!("main: lockfile {:?} outdated", lock_path);
let res = DependencyResolver::new(&sess);
let locked_new = res.resolve()?;
write_lockfile(&locked_new, &root_dir.join("Bender.lock"), &root_dir)?;
locked_new
} else {
debugln!("main: lockfile {:?} up-to-date", lock_path);
locked_existing.unwrap()
}
}
None => {
return Err(Error::new("Please specify a command.".to_string()));
}
};
sess.load_locked(&locked)?;
// Ensure the locally linked packages are up-to-date.
{
let io = SessionIo::new(&sess);
for (path, pkg_name) in &sess.manifest.workspace.package_links {
debugln!("main: maintaining link to {} at {:?}", pkg_name, path);
// Determine the checkout path for this package.
let pkg_path = io.get_package_path(sess.dependency_with_name(pkg_name)?);
// Checkout if we are running update or package path does not exist yet
if matches.subcommand_name() == Some("update") || !pkg_path.clone().exists() {
let rt = Runtime::new()?;
rt.block_on(io.checkout(sess.dependency_with_name(pkg_name)?))?;
}
// Convert to relative path
let pkg_path = path
.parent()
.and_then(|path| pathdiff::diff_paths(pkg_path.clone(), path))
.unwrap_or(pkg_path);
// Check if there is something at the destination path that needs to be
// removed.
if path.exists() {
let meta = path.symlink_metadata().map_err(|cause| {
Error::chain(
format!("Failed to read metadata of path {:?}.", path),
cause,
)
})?;
if !meta.file_type().is_symlink() {
warnln!(
"Skipping link to package {} at {:?} since there is something there",
pkg_name,
path
);
continue;
}
if path.read_link().map(|d| d != pkg_path).unwrap_or(true) {
debugln!("main: removing existing link {:?}", path);
std::fs::remove_file(path).map_err(|cause| {
Error::chain(
format!("Failed to remove symlink at path {:?}.", path),
cause,
)
})?;
}
}
// Create the symlink if there is nothing at the destination.
if !path.exists() {
stageln!("Linking", "{} ({:?})", pkg_name, path);
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|cause| {
Error::chain(format!("Failed to create directory {:?}.", parent), cause)
})?;
}
let previous_dir = match path.parent() {
Some(parent) => {
let d = std::env::current_dir().unwrap();
std::env::set_current_dir(parent).unwrap();
Some(d)
}
None => None,
};
symlink_dir(&pkg_path, path).map_err(|cause| {
Error::chain(
format!(
"Failed to create symlink to {:?} at path {:?}.",
pkg_path, path
),
cause,
)
})?;
if let Some(d) = previous_dir {
std::env::set_current_dir(d).unwrap();
}
}
}
}
// Dispatch the different subcommands.
match matches.subcommand() {
Some(("path", matches)) => cmd::path::run(&sess, matches),
Some(("parents", matches)) => cmd::parents::run(&sess, matches),
Some(("clone", matches)) => cmd::clone::run(&sess, &root_dir, matches),
Some(("packages", matches)) => cmd::packages::run(&sess, matches),
Some(("sources", matches)) => cmd::sources::run(&sess, matches),
Some(("config", matches)) => cmd::config::run(&sess, matches),
Some(("script", matches)) => cmd::script::run(&sess, matches),
Some(("checkout", matches)) => cmd::checkout::run(&sess, matches),
Some(("update", matches)) => {
if matches.get_flag("no-checkout") {
Ok(())
} else {
cmd::checkout::run(&sess, matches)
}
}
Some(("vendor", matches)) => cmd::vendor::run(&sess, matches),
Some(("fusesoc", matches)) => cmd::fusesoc::run(&sess, matches),
Some((plugin, matches)) => execute_plugin(&sess, plugin, matches.get_many::<OsString>("")),
_ => Ok(()),
}
}
#[cfg(target_family = "unix")]
fn symlink_dir(p: &Path, q: &Path) -> Result<()> {
Ok(std::os::unix::fs::symlink(p, q)?)
}
#[cfg(target_os = "windows")]
fn symlink_dir(p: &Path, q: &Path) -> Result<()> {
Ok(std::os::windows::fs::symlink_dir(p, q)?)
}
/// Find the root directory of a package.
///
/// Traverses the directory hierarchy upwards until a `Bender.yml` file is found.
fn find_package_root(from: &Path) -> Result<PathBuf> {
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
// Canonicalize the path. This will resolve any intermediate links.
let mut path = canonicalize(from)
.map_err(|cause| Error::chain(format!("Failed to canonicalize path {:?}.", from), cause))?;
debugln!("find_package_root: canonicalized to {:?}", path);
// Look up the device at the current path. This information will then be
// used to stop at filesystem boundaries.
#[cfg(unix)]
let limit_rdev: Option<_> = metadata(&path).map(|m| m.dev()).ok();
#[cfg(unix)]
debugln!("find_package_root: limit rdev = {:?}", limit_rdev);
// Step upwards through the path hierarchy.
for _ in 0..100 {
debugln!("find_package_root: looking in {:?}", path);
// Check if we can find a package manifest here.
if path.join("Bender.yml").exists() {
return Ok(path);
}
// Abort if we have reached the filesystem root.
let tested_path = path.clone();
if !path.pop() {
return Err(Error::new(format!(
"No manifest (`Bender.yml` file) found. Stopped searching at filesystem root {:?}.",
path
)));
}
// Abort if we have crossed the filesystem boundary.
#[cfg(unix)]
{
let rdev: Option<_> = metadata(&path).map(|m| m.dev()).ok();
debugln!("find_package_root: rdev = {:?}", rdev);
if rdev != limit_rdev {
return Err(Error::new(format!(
"No manifest (`Bender.yml` file) found. Stopped searching at filesystem boundary {:?}.",
tested_path
)));
}
}
}
Err(Error::new(
"No manifest (`Bender.yml` file) found. Reached maximum number of search steps.",
))
}
/// Read a package manifest from a file.
pub fn read_manifest(path: &Path) -> Result<Manifest> {
use crate::config::PartialManifest;
use std::fs::File;
debugln!("read_manifest: {:?}", path);
let file = File::open(path)
.map_err(|cause| Error::chain(format!("Cannot open manifest {:?}.", path), cause))?;
let partial: PartialManifest = serde_yaml::from_reader(file)
.map_err(|cause| Error::chain(format!("Syntax error in manifest {:?}.", path), cause))?;
let manifest = partial
.validate()
.map_err(|cause| Error::chain(format!("Error in manifest {:?}.", path), cause))?;
manifest.prefix_paths(path.parent().unwrap())
}
/// Load a configuration by traversing a directory hierarchy upwards.
fn load_config(from: &Path, warn_config_loaded: bool) -> Result<Config> {
#[cfg(unix)]
use std::os::unix::fs::MetadataExt;
let mut out = PartialConfig::new();
// Canonicalize the path. This will resolve any intermediate links.
let mut path = canonicalize(from)
.map_err(|cause| Error::chain(format!("Failed to canonicalize path {:?}.", from), cause))?;
debugln!("load_config: canonicalized to {:?}", path);
// Look up the device at the current path. This information will then be
// used to stop at filesystem boundaries.
#[cfg(unix)]
let limit_rdev: Option<_> = metadata(&path).map(|m| m.dev()).ok();
#[cfg(unix)]
debugln!("load_config: limit rdev = {:?}", limit_rdev);
// Step upwards through the path hierarchy.
for _ in 0..100 {
// Load the optional local configuration.
if let Some(cfg) = maybe_load_config(&path.join("Bender.local"), warn_config_loaded)? {
out = out.merge(cfg);
}
debugln!("load_config: looking in {:?}", path);
if let Some(cfg) = maybe_load_config(&path.join(".bender.yml"), warn_config_loaded)? {
out = out.merge(cfg);
}
// Abort if we have reached the filesystem root.
if !path.pop() {
break;
}
// Abort if we have crossed the filesystem boundary.
#[cfg(unix)]
{
let rdev: Option<_> = metadata(&path).map(|m| m.dev()).ok();
debugln!("load_config: rdev = {:?}", rdev);
if rdev != limit_rdev {
break;
}
}
}
// Load the user configuration.
if let Some(mut home) = dirs::home_dir() {
home.push(".config");
home.push("bender.yml");
if let Some(cfg) = maybe_load_config(&home, warn_config_loaded)? {
out = out.merge(cfg);
}
}
// Load the global configuration.
if let Some(cfg) = maybe_load_config(Path::new("/etc/bender.yml"), warn_config_loaded)? {
out = out.merge(cfg);
}
// Assemble and merge the default configuration.
let default_cfg = PartialConfig {
database: Some(from.join(".bender").to_str().unwrap().to_string()),
git: Some("git".into()),
overrides: None,
plugins: None,
};
out = out.merge(default_cfg);
// Validate the configuration.
let mut out = out
.validate()
.map_err(|cause| Error::chain("Invalid configuration:", cause))?;
out.overrides = out
.overrides
.into_iter()
.map(|(k, v)| (k.to_lowercase(), v))
.collect();
Ok(out)
}
/// Load a configuration file if it exists.
fn maybe_load_config(path: &Path, warn_config_loaded: bool) -> Result<Option<PartialConfig>> {
use std::fs::File;
debugln!("maybe_load_config: {:?}", path);
if !path.exists() {
return Ok(None);
}
let file = File::open(path)
.map_err(|cause| Error::chain(format!("Cannot open config {:?}.", path), cause))?;
let partial: PartialConfig = serde_yaml::from_reader(file)
.map_err(|cause| Error::chain(format!("Syntax error in config {:?}.", path), cause))?;
if warn_config_loaded {
warnln!("Using config at {:?} for overrides.", path)
};
Ok(Some(partial.prefix_paths(path.parent().unwrap())?))
}
/// Read a lock file.
fn read_lockfile(path: &Path, root_dir: &Path) -> Result<Locked> {
debugln!("read_lockfile: {:?}", path);
use std::fs::File;
let file = File::open(path)
.map_err(|cause| Error::chain(format!("Cannot open lockfile {:?}.", path), cause))?;
let locked_loaded: Result<Locked> = serde_yaml::from_reader(file)
.map_err(|cause| Error::chain(format!("Syntax error in lockfile {:?}.", path), cause));
// Make relative paths absolute
Ok(Locked {
packages: locked_loaded?
.packages
.iter()
.map(|pack| {
Ok(if let LockedSource::Path(path) = &pack.1.source {
(
pack.0.clone(),
LockedPackage {
revision: pack.1.revision.clone(),
version: pack.1.version.clone(),
source: LockedSource::Path(if path.is_relative() {
path.clone().prefix_paths(root_dir)?
} else {
path.clone()
}),
dependencies: pack.1.dependencies.clone(),
},
)
} else {
(pack.0.clone(), pack.1.clone())
})
})
.collect::<Result<_>>()?,
})
}
/// Write a lock file.
fn write_lockfile(locked: &Locked, path: &Path, root_dir: &Path) -> Result<()> {
debugln!("write_lockfile: {:?}", path);
// Adapt paths within main repo to be relative
let adapted_locked = Locked {
packages: locked
.packages
.iter()
.map(|pack| {
if let LockedSource::Path(path) = &pack.1.source {
(
pack.0.clone(),
LockedPackage {
revision: pack.1.revision.clone(),
version: pack.1.version.clone(),
source: LockedSource::Path(
path.strip_prefix(root_dir).unwrap_or(path).to_path_buf(),
),
dependencies: pack.1.dependencies.clone(),
},
)
} else {
(pack.0.clone(), pack.1.clone())
}
})
.collect(),
};
use std::fs::File;
let file = File::create(path)
.map_err(|cause| Error::chain(format!("Cannot create lockfile {:?}.", path), cause))?;
serde_yaml::to_writer(file, &adapted_locked)
.map_err(|cause| Error::chain(format!("Cannot write lockfile {:?}.", path), cause))?;
Ok(())
}
/// Execute a plugin.
fn execute_plugin(
sess: &Session,
plugin: &str,
matches: Option<ValuesRef<OsString>>,
) -> Result<()> {
debugln!("main: execute plugin `{}`", plugin);
// Obtain a list of declared plugins.
let runtime = Runtime::new()?;
let io = SessionIo::new(sess);
let plugins = runtime.block_on(io.plugins())?;
// Lookup the requested plugin and complain if it does not exist.
let plugin = match plugins.get(plugin) {
Some(p) => p,
None => return Err(Error::new(format!("Unknown command `{}`.", plugin))),
};
debugln!("main: found plugin {:#?}", plugin);
// Assemble a command that executes the plugin with the appropriate
// environment and forwards command line arguments.
let mut cmd = SysCommand::new(&plugin.path);
cmd.env(
"BENDER",
std::env::current_exe()
.map_err(|cause| Error::chain("Failed to determine current executable.", cause))?,
);
cmd.env(
"BENDER_CALL_DIR",
std::env::current_dir()
.map_err(|cause| Error::chain("Failed to determine current directory.", cause))?,
);
cmd.env("BENDER_MANIFEST_DIR", sess.root);
cmd.current_dir(sess.root);
if let Some(args) = matches {
cmd.args(args);
}
debugln!("main: executing plugin {:#?}", cmd);
let stat = cmd.status().map_err(|cause| {
Error::chain(
format!(
"Unable to spawn process for plugin `{}`. Command was {:#?}.",
plugin.name, cmd
),
cause,
)
})?;
// Don't bother to do anything after the plugin was run.
std::process::exit(stat.code().unwrap_or(1));
}