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
// SPDX-License-Identifier: MIT AND Apache-2.0
// Allow all other modules to use environment macros.
#[macro_use]
mod environment;
mod api;
mod docs;
mod fmt;
mod generate;
mod git;
mod integration;
mod lint;
mod lock;
mod prerelease;
mod run;
mod semantic_version;
mod test;
mod toolchain;
mod toolchains;
mod tools;
mod tree;
mod version;
use std::process;
use clap::{Parser, Subcommand};
use lock::{GeneratableLockFile, LockFile};
use toolchain::Toolchain;
use xshell::Shell;
#[derive(Parser)]
#[command(name = "cargo-rbmt")]
#[command(about = "Rust Bitcoin Maintainer Tools", long_about = None)]
struct Cli {
/// Filter which packages are operated on in the workspace. Can be a package's manifest name or directory.
#[arg(short = 'p', long = "package", global = true)]
packages: Vec<String>,
/// Ignore the workspace's pinned rbmt version constraint.
#[arg(short = 'i', long, global = true)]
ignore_version: bool,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Check for public API changes in stabilizing crates.
Api {
/// Lockfile to use for dependencies.
#[arg(long = "lockfile", alias = "lock-file", value_enum, default_value_t = LockFile::Recent)]
lockfile: LockFile,
/// Git ref to use as baseline for semver comparison (tag, branch, or commit).
#[arg(long)]
baseline: Option<String>,
/// Write API snapshot files.
#[arg(long)]
snapshot: bool,
},
/// Format files using rustfmt with the nightly toolchain.
Fmt {
/// Check formatting without modifying files.
#[arg(long)]
check: bool,
},
/// Run the linter (clippy) for workspace and all crates.
Lint {
/// Lockfile to use for dependencies.
#[arg(long = "lockfile", alias = "lock-file", value_enum, default_value_t = LockFile::Recent)]
lockfile: LockFile,
},
/// Build documentation at rust-bitcoin standards.
Docs {
/// Lockfile to use for dependencies.
#[arg(long = "lockfile", alias = "lock-file", value_enum, default_value_t = LockFile::Recent)]
lockfile: LockFile,
/// Build with stable toolchain instead of nightly and skip docs.rs validation.
#[arg(long)]
no_docsrs: bool,
/// Open documentation in browser after building.
#[arg(long)]
open: bool,
},
/// Run tests with specified toolchain.
Test {
/// Lockfile to use for dependencies.
#[arg(long = "lockfile", alias = "lock-file", value_enum, default_value_t = LockFile::Recent)]
lockfile: LockFile,
/// Toolchain to use: stable, nightly, or msrv.
#[arg(long, value_enum, default_value_t = Toolchain::Stable)]
toolchain: Toolchain,
/// Test every commit between the given baseline ref and HEAD to verify bisectability.
#[arg(long)]
baseline: Option<String>,
/// Cargo arguments (everything after `--`).
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
cargo_args: Vec<String>,
},
/// Run bitcoin core integration tests.
Integration,
/// Update dependency versions in lockfiles.
Lock {
/// Lockfile types to generate.
#[arg(long, value_delimiter = ',', default_values = ["minimal", "recent"])]
lockfiles: Vec<GeneratableLockFile>,
},
/// Run arbitrary cargo commands with toolchain and lockfile management.
Run {
/// Lockfile to use for dependencies.
#[arg(long = "lockfile", alias = "lock-file", value_enum, default_value_t = LockFile::Recent)]
lockfile: LockFile,
/// Toolchain to use: stable, nightly, or msrv.
#[arg(long, value_enum, default_value_t = Toolchain::Stable)]
toolchain: Toolchain,
/// Run the command on every commit between the given baseline ref and HEAD to ensure consistency.
#[arg(long)]
baseline: Option<String>,
/// Cargo command and arguments (everything after `--`).
#[arg(trailing_var_arg = true, allow_hyphen_values = true)]
args: Vec<String>,
},
/// Run pre-release readiness checks.
Prerelease {
/// Run checks even for packages that have pre-release checks disabled.
#[arg(long)]
force: bool,
/// Git ref to use as baseline for version bump detection (tag, branch, or commit).
#[arg(long, default_value = "master")]
baseline: String,
},
/// Install and manage nightly, stable, and MSRV toolchains.
Toolchains {
/// Update the nightly toolchain version.
#[arg(long)]
update_nightly: bool,
/// Update the stable toolchain version.
#[arg(long)]
update_stable: bool,
/// Print the workspace MSRV and exit without installing any toolchains.
#[arg(long)]
msrv: bool,
/// Print the nightly toolchain version and exit without installing any toolchains.
#[arg(long)]
nightly: bool,
/// Print the stable toolchain version and exit without installing any toolchains.
#[arg(long)]
stable: bool,
/// Force fresh toolchain installs, maay help fix missing components.
#[arg(long)]
force: bool,
},
/// Install tools pinned in [workspace.metadata.rbmt.tools].
Tools {
/// Install each tool at its latest version and update pins in the root manifest.
#[arg(long)]
update: bool,
/// Only operate on these tools (default: all tools in the manifest).
tools: Vec<String>,
},
/// Print version and build information.
Version,
/// Generate files and check for changes.
Generate,
/// Show internal workspace packages in release order (deepest dependency chain first).
Tree {
/// Git ref to use as baseline; only show packages with changes since this ref (tag, branch, or commit).
#[arg(long)]
baseline: Option<String>,
},
}
fn main() {
// Cargo automatically adds the subcommand name as an extra argument.
// `cargo rbmt test` becomes `cargo-rbmt rbmt test`, so filter it out.
let args = std::env::args()
.enumerate()
.filter(|(i, arg)| !(*i == 1 && arg == "rbmt"))
.map(|(_, arg)| arg);
let cli = Cli::parse_from(args);
let sh = Shell::new().unwrap();
// Check version requirement early before running any commands.
if !cli.ignore_version {
if let Err(e) = version::check(&sh) {
eprintln!("Error: {}", e);
process::exit(1);
}
}
match cli.command {
Commands::Version => println!("{}", env!("RBMT_BUILD_VERSION")),
Commands::Api { lockfile, baseline, snapshot } => {
if let Err(e) = api::run(&sh, lockfile, &cli.packages, baseline.as_deref(), snapshot) {
eprintln!("Error running API check: {}", e);
process::exit(1);
}
}
Commands::Fmt { check } =>
if let Err(e) = fmt::run(&sh, check, &cli.packages) {
eprintln!("Error running fmt task: {}", e);
process::exit(1);
},
Commands::Lint { lockfile } =>
if let Err(e) = lint::run(&sh, lockfile, &cli.packages) {
eprintln!("Error running lint task: {}", e);
process::exit(1);
},
Commands::Docs { lockfile, no_docsrs, open } => {
let mode = if no_docsrs { docs::DocsMode::Docs } else { docs::DocsMode::DocsRs };
if let Err(e) = docs::run(&sh, lockfile, &cli.packages, mode, open) {
eprintln!("Error building docs: {}", e);
process::exit(1);
}
}
Commands::Test { lockfile, toolchain, baseline, cargo_args } =>
if let Err(e) =
test::run(&sh, lockfile, toolchain, baseline.as_deref(), &cli.packages, &cargo_args)
{
eprintln!("Error running tests: {}", e);
process::exit(1);
},
Commands::Integration =>
if let Err(e) = integration::run(&sh, &cli.packages) {
eprintln!("Error running integration tests: {}", e);
process::exit(1);
},
Commands::Lock { lockfiles } =>
if let Err(e) = lock::run(&sh, &lockfiles) {
eprintln!("Error updating lockfiles: {}", e);
process::exit(1);
},
Commands::Run { lockfile, toolchain, baseline, args } =>
if let Err(e) =
run::run(&sh, lockfile, toolchain, baseline.as_deref(), &cli.packages, &args)
{
eprintln!("Error running cargo command: {}", e);
process::exit(1);
},
Commands::Prerelease { force, baseline } =>
if let Err(e) = prerelease::run(&sh, &cli.packages, force, &baseline) {
eprintln!("Error running pre-release checks: {}", e);
process::exit(1);
},
Commands::Toolchains { update_nightly, update_stable, msrv, nightly, stable, force } =>
if let Err(e) =
toolchains::run(&sh, update_nightly, update_stable, msrv, nightly, stable, force)
{
eprintln!("Error setting up toolchains: {}", e);
process::exit(1);
},
Commands::Tools { update, tools } =>
if let Err(e) = tools::run(&sh, update, &tools) {
eprintln!("Error managing tools: {}", e);
process::exit(1);
},
Commands::Generate =>
if let Err(e) = generate::run(&sh, &cli.packages) {
eprintln!("Error running file generation: {}", e);
process::exit(1);
},
Commands::Tree { baseline } =>
if let Err(e) = tree::run(&sh, &cli.packages, baseline.as_deref()) {
eprintln!("Error generating dependency tree: {}", e);
process::exit(1);
},
}
}