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
//! rustbridge CLI - Build tool and code generator
//!
//! Commands:
//! - `rustbridge build` - Build a plugin
//! - `rustbridge generate` - Generate host language bindings
//! - `rustbridge generate-header` - Generate C headers from Rust structs
//! - `rustbridge new` - Create a new plugin project
//! - `rustbridge check` - Validate a rustbridge.toml manifest
//! - `rustbridge bundle` - Create, inspect, or extract plugin bundles
use clap::{Parser, Subcommand};
mod build;
mod bundle;
mod codegen;
mod header_gen;
mod keygen;
mod manifest;
mod new;
#[derive(Parser)]
#[command(name = "rustbridge")]
#[command(author, version, about = "Build tool for rustbridge plugins", long_about = None)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
#[allow(clippy::large_enum_variant)] // BundleAction has many options; boxing would complicate clap usage
enum Commands {
/// Build a rustbridge plugin
Build {
/// Path to the plugin project (default: current directory)
#[arg(short, long)]
path: Option<String>,
/// Build in release mode
#[arg(short, long)]
release: bool,
/// Target platform (e.g., x86_64-unknown-linux-gnu)
#[arg(short, long)]
target: Option<String>,
},
/// Generate schemas and bindings from Rust message types
Generate {
#[command(subcommand)]
action: GenerateAction,
},
/// Create a new rustbridge plugin project
New {
/// Project name
name: String,
/// Project directory (default: ./<name>)
#[arg(short, long)]
path: Option<String>,
/// Also generate Kotlin consumer project (requires Java 21+)
#[arg(long)]
kotlin: bool,
/// Also generate Java FFM consumer project (requires Java 21+)
#[arg(long)]
java_ffm: bool,
/// Also generate Java JNI consumer project (requires Java 17+)
#[arg(long)]
java_jni: bool,
/// Also generate C# consumer project (requires .NET 8+)
#[arg(long)]
csharp: bool,
/// Also generate Python consumer project
#[arg(long)]
python: bool,
/// Generate all consumer projects
#[arg(long)]
all: bool,
},
/// Validate a rustbridge.toml manifest
Check {
/// Path to rustbridge.toml (default: ./rustbridge.toml)
#[arg(short, long)]
manifest: Option<String>,
},
/// Generate C header from Rust #[repr(C)] structs
GenerateHeader {
/// Path to Rust source file containing #[repr(C)] structs
#[arg(short, long)]
source: String,
/// Output path for generated C header (default: messages.h)
#[arg(short, long, default_value = "messages.h")]
output: String,
/// Verify the generated header compiles with a C compiler
#[arg(short, long)]
verify: bool,
},
/// Generate a new minisign key pair for signing bundles
Keygen {
/// Output path for secret key (default: ~/.rustbridge/signing.key)
#[arg(short, long)]
output: Option<String>,
/// Force overwrite if key already exists
#[arg(short, long)]
force: bool,
},
/// Create, inspect, or extract plugin bundles
Bundle {
#[command(subcommand)]
action: BundleAction,
},
}
#[derive(Subcommand)]
enum GenerateAction {
/// Generate JSON Schema from Rust message types
JsonSchema {
/// Path to Rust source file(s) containing message types
#[arg(short, long)]
input: String,
/// Output path for generated JSON schema
#[arg(short, long)]
output: String,
},
}
#[derive(Subcommand)]
enum BundleAction {
/// Create a new bundle from libraries
Create {
/// Plugin name
#[arg(short, long)]
name: String,
/// Plugin version (semver)
#[arg(short, long)]
version: String,
/// Library to include (can be repeated)
/// Format: PLATFORM:PATH or PLATFORM:VARIANT:PATH
/// Examples:
/// --lib linux-x86_64:target/release/libplugin.so (release variant)
/// --lib linux-x86_64:debug:target/debug/libplugin.so (debug variant)
#[arg(short, long = "lib", value_name = "PLATFORM[:VARIANT]:PATH")]
libraries: Vec<String>,
/// Output bundle path (default: <name>-<version>.rbp)
#[arg(short, long)]
output: Option<String>,
/// Schema file to include: SOURCE:ARCHIVE_NAME (can be repeated)
/// Example: --schema messages.h:messages.h
#[arg(short, long, value_name = "SOURCE:ARCHIVE_NAME")]
schema: Vec<String>,
/// Path to signing key for code signing (optional)
/// Example: --sign-key ~/.rustbridge/signing.key
#[arg(long, value_name = "KEY_PATH")]
sign_key: Option<String>,
/// Auto-generate C header from Rust source file and embed in bundle
/// Example: --generate-header src/binary_messages.rs:messages.h
#[arg(long, value_name = "SOURCE:HEADER_NAME")]
generate_header: Option<String>,
/// Auto-generate JSON Schema from Rust source file and embed in bundle
/// Example: --generate-schema src/messages.rs:schema.json
#[arg(long, value_name = "SOURCE:SCHEMA_NAME")]
generate_schema: Option<String>,
/// License notices file to include in the bundle
#[arg(long, value_name = "PATH")]
notices: Option<String>,
/// Plugin's own license file to include in the bundle
/// Example: --license LICENSE
#[arg(long, value_name = "PATH")]
license: Option<String>,
/// Skip automatic build metadata collection
#[arg(long)]
no_metadata: bool,
/// SBOM file to include: SOURCE:ARCHIVE_NAME (can be repeated)
/// Example: --sbom sbom.cdx.json:sbom.cdx.json --sbom sbom.spdx.json:sbom.spdx.json
#[arg(long, value_name = "SOURCE:ARCHIVE_NAME")]
sbom: Vec<String>,
/// Custom metadata as KEY=VALUE (can be repeated)
/// Adds arbitrary key/value pairs to build_info.custom for informational purposes.
/// Example: --metadata repository=https://github.com/user/project
/// Example: --metadata ci_job_id=12345
#[arg(long, value_name = "KEY=VALUE")]
metadata: Vec<String>,
},
/// Combine multiple bundles into one
Combine {
/// Input bundle files (at least 2)
#[arg(required = true, num_args = 2..)]
bundles: Vec<String>,
/// Output bundle path
#[arg(short, long)]
output: String,
/// Path to signing key for re-signing the combined bundle
#[arg(long, value_name = "KEY_PATH")]
sign_key: Option<String>,
/// Schema mismatch handling: error (default), warn, ignore
#[arg(long, value_name = "MODE", default_value = "error")]
schema_mismatch: String,
},
/// Create a slimmed bundle with subset of platforms/variants
Slim {
/// Input bundle path
#[arg(short, long)]
input: String,
/// Output bundle path
#[arg(short, long)]
output: String,
/// Platforms to keep (comma-separated)
/// Example: --platforms linux-x86_64,darwin-aarch64
#[arg(long, value_name = "PLATFORMS")]
platforms: Option<String>,
/// Variants to keep (comma-separated, default: release)
/// Example: --variants release,debug
#[arg(long, value_name = "VARIANTS", default_value = "release")]
variants: String,
/// Exclude documentation files
#[arg(long)]
exclude_docs: bool,
/// Path to signing key for re-signing the slimmed bundle
#[arg(long, value_name = "KEY_PATH")]
sign_key: Option<String>,
},
/// List contents of a bundle
List {
/// Path to the bundle file
bundle: String,
/// Show build info
#[arg(long)]
show_build: bool,
/// Show all variants
#[arg(long)]
show_variants: bool,
},
/// Extract library from a bundle
Extract {
/// Path to the bundle file
bundle: String,
/// Target platform (default: current platform)
#[arg(short, long)]
platform: Option<String>,
/// Variant to extract (default: release)
#[arg(long, default_value = "release")]
variant: String,
/// Output directory for extracted library
#[arg(short, long, default_value = ".")]
output: String,
},
}
fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
match cli.command {
Commands::Build {
path,
release,
target,
} => {
build::run(path, release, target)?;
}
Commands::Generate { action } => match action {
GenerateAction::JsonSchema { input, output } => {
use codegen::{MessageType, generate_json_schema};
use std::path::Path;
println!("Generating JSON Schema from {input}");
// Parse message types from Rust source
let messages = MessageType::parse_file(Path::new(&input))?;
println!(" Found {} message type(s)", messages.len());
// Generate JSON Schema
let schema = generate_json_schema(&messages)?;
// Write to output file
std::fs::write(&output, serde_json::to_string_pretty(&schema)?)?;
println!(" JSON Schema written to {output}");
}
},
Commands::New {
name,
path,
kotlin,
java_ffm,
java_jni,
csharp,
python,
all,
} => {
let options = new::NewOptions {
kotlin: kotlin || all,
java_ffm: java_ffm || all,
java_jni: java_jni || all,
csharp: csharp || all,
python: python || all,
};
new::run(&name, path, options)?;
}
Commands::Check { manifest } => {
manifest::check(manifest)?;
}
Commands::GenerateHeader {
source,
output,
verify,
} => {
header_gen::run(&source, &output, verify)?;
}
Commands::Keygen { output, force } => {
keygen::run(output, force)?;
}
Commands::Bundle { action } => match action {
BundleAction::Create {
name,
version,
libraries,
output,
schema,
sign_key,
generate_header,
generate_schema,
notices,
license,
no_metadata,
sbom,
metadata,
} => {
// Parse library arguments (PLATFORM:PATH or PLATFORM:VARIANT:PATH)
let libs: Vec<(String, String, String)> = libraries
.iter()
.map(|s| {
let parts: Vec<&str> = s.splitn(3, ':').collect();
match parts.len() {
2 => {
// PLATFORM:PATH -> (platform, "release", path)
Ok((
parts[0].to_string(),
"release".to_string(),
parts[1].to_string(),
))
}
3 => {
// PLATFORM:VARIANT:PATH -> (platform, variant, path)
Ok((
parts[0].to_string(),
parts[1].to_string(),
parts[2].to_string(),
))
}
_ => anyhow::bail!(
"Invalid library format: {s}. Expected PLATFORM:PATH or PLATFORM:VARIANT:PATH"
),
}
})
.collect::<anyhow::Result<_>>()?;
// Parse schema arguments (SOURCE:ARCHIVE_NAME)
let schemas: Vec<(String, String)> = schema
.iter()
.map(|s| {
let parts: Vec<&str> = s.splitn(2, ':').collect();
if parts.len() != 2 {
anyhow::bail!(
"Invalid schema format: {s}. Expected SOURCE:ARCHIVE_NAME"
);
}
Ok((parts[0].to_string(), parts[1].to_string()))
})
.collect::<anyhow::Result<_>>()?;
// Parse SBOM arguments (SOURCE:ARCHIVE_NAME)
let sbom_files: Vec<(String, String)> = sbom
.iter()
.map(|s| {
let parts: Vec<&str> = s.splitn(2, ':').collect();
if parts.len() != 2 {
anyhow::bail!("Invalid sbom format: {s}. Expected SOURCE:ARCHIVE_NAME");
}
Ok((parts[0].to_string(), parts[1].to_string()))
})
.collect::<anyhow::Result<_>>()?;
// Parse custom metadata arguments (KEY=VALUE)
let custom_metadata: Vec<(String, String)> = metadata
.iter()
.map(|s| {
let parts: Vec<&str> = s.splitn(2, '=').collect();
if parts.len() != 2 {
anyhow::bail!("Invalid metadata format: {s}. Expected KEY=VALUE");
}
Ok((parts[0].to_string(), parts[1].to_string()))
})
.collect::<anyhow::Result<_>>()?;
bundle::create(
&name,
&version,
&libs,
output,
&schemas,
sign_key,
generate_header,
generate_schema,
notices,
license,
no_metadata,
&sbom_files,
&custom_metadata,
)?;
}
BundleAction::Combine {
bundles,
output,
sign_key,
schema_mismatch,
} => {
bundle::combine(&bundles, &output, sign_key, &schema_mismatch)?;
}
BundleAction::Slim {
input,
output,
platforms,
variants,
exclude_docs,
sign_key,
} => {
let platform_list: Option<Vec<String>> =
platforms.map(|p| p.split(',').map(|s| s.trim().to_string()).collect());
let variant_list: Vec<String> =
variants.split(',').map(|s| s.trim().to_string()).collect();
bundle::slim(
&input,
&output,
platform_list,
variant_list,
exclude_docs,
sign_key,
)?;
}
BundleAction::List {
bundle: bundle_path,
show_build,
show_variants,
} => {
bundle::list(&bundle_path, show_build, show_variants)?;
}
BundleAction::Extract {
bundle: bundle_path,
platform,
variant,
output: output_dir,
} => {
bundle::extract(&bundle_path, platform, &variant, &output_dir)?;
}
},
}
Ok(())
}