-
Notifications
You must be signed in to change notification settings - Fork 51
/
build.rs
66 lines (53 loc) · 1.6 KB
/
build.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use anyhow::Result;
use std::collections::BTreeSet;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
fn main() -> Result<()> {
use cargo_metadata::MetadataCommand;
let mut cmds = BTreeSet::new();
// MetadataCommand doesn't emit this, so we should
println!("cargo:rerun-if-changed=Cargo.toml");
let metadata =
MetadataCommand::new().manifest_path("./Cargo.toml").exec().unwrap();
let out_dir = env::var("OUT_DIR")?;
let dest_path = Path::new(&out_dir).join("cmds.rs");
let mut output = File::create(&dest_path)?;
write!(
output,
r##"
//
// Our generated command description.
//
struct CommandDescription {{
init: fn() -> Command,
docmsg: &'static str,
}}
fn dcmds() -> Vec<CommandDescription> {{
vec![
"##
)?;
for id in &metadata.workspace_members {
let package =
metadata.packages.iter().find(|p| &p.id == id).unwrap().clone();
if let Some(cmd) = package.name.strip_prefix("humility-cmd-") {
cmds.insert(cmd.to_string().replace('-', "_"));
}
}
for cmd in cmds.iter() {
writeln!(
output,
r##" CommandDescription {{
init: cmd_{}::init,
docmsg: "For additional documentation, run \"humility doc {}\"."
}},"##,
cmd, cmd
)?;
}
write!(output, " ]\n}}")?;
Ok(())
}