-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathmain.rs
175 lines (156 loc) · 4.53 KB
/
main.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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
//! # Manage Rust third-party crates
//!
//! This tool takes a specification of third-party packages to be exported to fbsource,
//! and updates the Buck build files for them.
//!
//! ## Directory layout
//!
//! This works in a directory with the following layout:
//!
//! - Cargo.toml - specification of crates
//! - Cargo.lock - locked version
//! - vendor/ - vendored sources
//!
//! (TBD - rest of it)
use std::path::PathBuf;
use clap::Parser;
use clap::Subcommand;
use crate::config::VendorConfig;
mod audit_sec;
mod buck;
mod buckify;
mod cargo;
mod cfg;
mod collection;
mod config;
mod fixups;
mod glob;
mod index;
mod lockfile;
mod platform;
mod remap;
mod srcfiles;
mod universe;
mod vendor;
#[derive(Debug, Parser)]
#[command(bin_name = "reindeer")]
pub struct Args {
/// Path to `cargo` command
#[arg(long, value_name = "PATH")]
cargo_path: Option<PathBuf>,
/// Path to `rustc` command
#[arg(long, value_name = "PATH")]
rustc_path: Option<PathBuf>,
/// Extra cargo options
#[arg(long, value_name = "ARGUMENT")]
cargo_options: Vec<String>,
/// Path to third-party dir
#[arg(long, default_value = ".", value_name = "PATH")]
third_party_dir: PathBuf,
#[command(subcommand)]
subcommand: SubCommand,
}
#[derive(Debug, Subcommand)]
enum SubCommand {
/// Update Cargo.lock with new dependencies
Update {},
/// Vendor crate needed for build
Vendor {
/// Don't delete older crates in the vendor directory
#[arg(long)]
no_delete: bool,
/// Show reported security problems for crates as they're being vendored
#[arg(long)]
audit_sec: bool,
/// Use cached version of the advisory repo
#[arg(long)]
no_fetch: bool,
},
/// Generate Buck build rules for Cargo packages
Buckify {
/// Emit generated build rules to stdout, not overwriting existing file.
///
/// Suppresses generation of other output files.
#[arg(long)]
stdout: bool,
},
/// Show security report for vendored crates
Auditsec {
/// Use cached version of the advisory repo
#[arg(long, short = 'n')]
no_fetch: bool,
},
}
/// Computed paths
#[derive(Debug)]
pub struct Paths {
third_party_dir: PathBuf,
manifest_path: PathBuf,
lockfile_path: PathBuf,
cargo_home: PathBuf,
}
fn try_main() -> anyhow::Result<()> {
let args = Args::parse();
let third_party_dir = dunce::canonicalize(&args.third_party_dir)?;
let mut config = config::read_config(&third_party_dir)?;
let paths = Paths {
manifest_path: third_party_dir.join("Cargo.toml"),
lockfile_path: third_party_dir.join("Cargo.lock"),
cargo_home: third_party_dir.join(".cargo"),
third_party_dir,
};
log::debug!("Args = {:#?}, paths {:#?}", args, paths);
match &args.subcommand {
SubCommand::Vendor {
no_delete,
audit_sec,
no_fetch,
} => {
vendor::cargo_vendor(&config, *no_delete, *audit_sec, *no_fetch, &args, &paths)?;
}
SubCommand::Auditsec { no_fetch } => {
audit_sec::audit_sec(&paths, *no_fetch)?;
}
SubCommand::Update { .. } => {
let _ = cargo::run_cargo(
&config,
Some(&paths.cargo_home),
&paths.third_party_dir,
&args,
&[
"generate-lockfile",
"--manifest-path",
paths.manifest_path.to_str().unwrap(),
],
)?;
}
SubCommand::Buckify { stdout } => {
if matches!(
config.vendor,
VendorConfig::LocalRegistry | VendorConfig::Source(_)
) && !vendor::is_vendored(&config, &paths)?
{
// If you ran `reindeer buckify` without `reindeer vendor`, then
// default to generating non-vendored targets.
config.vendor = VendorConfig::Off;
}
buckify::buckify(&config, &args, &paths, *stdout)?;
}
}
Ok(())
}
fn main() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("warn"))
.format_timestamp(None)
.init();
if let Err(err) = try_main() {
log::error!("{:?}", err);
std::process::exit(1);
}
}