kodegen_bundler_bundle 0.10.9

Multi-platform bundler for creating native installers (.deb, .rpm, .dmg, .msi, AppImage)
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
<div align="center">
  <img src="assets/img/banner.png" alt="Kodegen AI Banner" width="100%" />
</div>

# kodegen-bundler-bundle

**Multi-platform package bundler for Rust applications.**

[![License](https://img.shields.io/badge/license-Apache%202.0%20OR%20MIT-blue.svg)](LICENSE.md)
[![Rust](https://img.shields.io/badge/rust-nightly--2024--10--20-orange.svg)](https://rust-lang.github.io/rustup/)

## Overview

`kodegen-bundler-bundle` is a standalone binary that creates platform-specific installation packages for Rust applications. It supports Linux (.deb, .rpm, AppImage), macOS (.app, .dmg), and Windows (.msi, .exe) package formats.

This bundler is designed to be called programmatically by release workflows (like `kodegen-bundler-release`) with explicit output path contracts.

## Features

- 📦 **Multi-Platform Support** - Linux (deb/rpm/AppImage), macOS (app/dmg), Windows (msi/exe)
- 🎯 **Caller-Specified Output Paths** - Full control over artifact location and naming
- 🔒 **Contract-Based Interface** - Exit code 0 guarantees artifact exists at specified path
- 🚀 **Fast Execution** - Optimized bundling with minimal overhead
- 🛡️ **Directory Management** - Automatic parent directory creation
- 📊 **Diagnostic Output** - Detailed stdout/stderr for debugging

## Installation

```bash
# Install from crates.io
cargo install kodegen_bundler_bundle

# OR build from source
git clone https://github.com/cyrup-ai/kodegen-bundler-bundle
cd kodegen-bundler-bundle
cargo install --path .
```

## Basic Usage

The bundler accepts exactly **three arguments** and handles everything else internally:

```bash
# Bundle from local repository (reads Cargo.toml for GitHub URL, clones to tmp, builds, bundles)
kodegen_bundler_bundle \
  --source . \
  --platform deb \
  --output-binary /tmp/artifacts/myapp_1.0.0_arm64.deb

# Bundle from GitHub org/repo (clones to tmp, builds, bundles)
kodegen_bundler_bundle \
  --source cyrup-ai/kodegen \
  --platform dmg \
  --output-binary /tmp/artifacts/kodegen_0.1.2_arm64.dmg

# Bundle from GitHub URL (clones to tmp, builds, bundles)
kodegen_bundler_bundle \
  --source https://github.com/cyrup-ai/kodegen \
  --platform nsis \
  --output-binary C:\builds\kodegen_setup.exe
```

**Exit code 0 = artifact guaranteed to exist at `--output-binary` path.**

## Output Path Contract

### The `--output-binary` Flag

When `--output-binary` is specified, the bundler establishes a **strict contract** with the caller:

#### Bundler Responsibilities

1. **Create parent directories** - All directories in the output path are created if they don't exist
2. **Move artifact** - The created artifact is moved (not copied) to the exact specified path
3. **Verify existence** - Before returning, bundler verifies the file exists at the specified path
4. **Return exit code 0** - Exit code 0 **guarantees** the file exists at the specified path

#### Contract Guarantees

```
If bundler returns exit code 0:
  ✓ File exists at --output-binary path
  ✓ File is complete and valid
  ✓ All parent directories created
  ✓ Original artifact removed from temp location

If bundler returns non-zero exit code:
  ✗ File may not exist at specified path
  ✗ Check stderr for error details
```

#### Communication Protocol

- **Exit codes**: Contractual communication (0 = success, file exists; non-zero = failure)
- **stdout**: Diagnostic information only (artifact paths, progress messages)
- **stderr**: Error details and warnings (diagnostic only, not contractual)

**Important**: Callers should **only** rely on exit codes for contract enforcement. stdout and stderr are for human consumption and debugging, not programmatic parsing.

## CLI Reference

### The Three Required Arguments

```bash
--source <SOURCE>           # Where to get the code (3 formats):
                           # 1. Local path: . or /path/to/repo
                           #    → Reads Cargo.toml repository field
                           #    → Clones from GitHub to /tmp/kodegen-bundle-{uuid}
                           # 2. GitHub org/repo: cyrup-ai/kodegen
                           #    → Clones from GitHub to /tmp/kodegen-bundle-{uuid}
                           # 3. GitHub URL: https://github.com/cyrup-ai/kodegen
                           #    → Clones from GitHub to /tmp/kodegen-bundle-{uuid}
                           # 
                           # ALL sources clone to tmp - NEVER builds in-place

--platform <PLATFORM>       # Target platform: deb, rpm, appimage, dmg, nsis

--output-binary <PATH>      # Full output path for final artifact
                           # Example: /tmp/artifacts/myapp_1.0.0_arm64.deb
                           # Bundler creates parent dirs automatically
                           # Exit code 0 guarantees file exists at this path
```

### What the Bundler Handles Internally

The bundler automatically:
- Clones repository to tmp directory (`/tmp/kodegen-bundle-{uuid}`)
- Reads binary name from Cargo.toml
- Reads version from Cargo.toml
- Detects target architecture
- Builds the binary (`cargo build --release`)
- Creates the platform package
- Moves artifact to `--output-binary` path
- Cleans up tmp directory
- Returns exit code 0 only if artifact exists

**Caller responsibilities**: Specify source, platform, output path
**Bundler responsibilities**: Everything else

## Supported Platforms

| Platform | Extension | Description |
|----------|-----------|-------------|
| `deb` | `.deb` | Debian/Ubuntu packages |
| `rpm` | `.rpm` | RedHat/Fedora/CentOS packages |
| `appimage` | `.AppImage` | Portable Linux executables |
| `dmg` | `.dmg` | macOS disk image installers |
| `app` | `.app` | macOS application bundles |
| `nsis` | `.exe` | Windows NSIS installers |

## Usage Examples

### Bundle from Local Repository

```bash
# Reads repository URL from Cargo.toml, clones to tmp, builds, bundles
kodegen_bundler_bundle \
  --source /path/to/project \
  --platform deb \
  --output-binary /tmp/artifacts/myapp_1.0.0_amd64.deb

# Exit code 0 = file guaranteed at /tmp/artifacts/myapp_1.0.0_amd64.deb
```

### Bundle from GitHub Org/Repo

```bash
# Clones cyrup-ai/kodegen from GitHub to tmp, builds, bundles
kodegen_bundler_bundle \
  --source cyrup-ai/kodegen \
  --platform deb \
  --output-binary /tmp/artifacts/kodegen_2.0.0_arm64.deb

# Exit code 0 = file guaranteed at /tmp/artifacts/kodegen_2.0.0_arm64.deb
```

### Bundle from GitHub URL

```bash
# Clones from full GitHub URL to tmp, builds, bundles
kodegen_bundler_bundle \
  --source https://github.com/cyrup-ai/kodegen \
  --platform dmg \
  --output-binary ./dist/kodegen-3.1.4-arm64.dmg

# Bundler automatically creates ./dist/ directory
```

### Cross-Platform Bundling

```bash
# Build Linux package from macOS (uses Docker internally)
kodegen_bundler_bundle \
  --source . \
  --platform deb \
  --output-binary /tmp/myapp.deb

# Build Windows installer from Linux (uses Docker with Wine)
kodegen_bundler_bundle \
  --source cyrup-ai/myapp \
  --platform nsis \
  --output-binary C:\builds\myapp_setup.exe
```

## Integration with Release Workflows

The bundler is designed to integrate seamlessly with release automation tools like `kodegen-bundler-release`.

### Typical Workflow Integration

1. **Release workflow detects target architecture** (compile-time cfg attributes)
2. **Release constructs output path** with explicit architecture in filename
3. **Release invokes bundler** with `--output-binary` flag
4. **Bundler creates directories** and moves artifact to specified path
5. **Bundler returns exit 0** only if file exists
6. **Release verifies contract** by checking file existence

### Example: Release Workflow Calling Bundler

```rust
// In kodegen-bundler-release/src/cli/commands/release/impl.rs

// Release workflow constructs output path with architecture
let arch = detect_target_architecture()?;  // "arm64", "amd64", etc.
let version = "2.0.0";  // From Cargo.toml (bundler reads this internally too)
let filename = format!("kodegen_{}_{}.deb", version, arch);
let output_path = artifacts_dir.join(&filename);

// Call bundler with three arguments
let output = Command::new("kodegen_bundler_bundle")
    .arg("--source").arg("cyrup-ai/kodegen")  // ← GitHub org/repo
    .arg("--platform").arg("deb")
    .arg("--output-binary").arg(&output_path)
    .output()?;

// Contract enforcement: exit 0 = file exists
if output.status.success() {
    if !output_path.exists() {
        return Err("Bundler contract violation: exit 0 but file missing");
    }
    // File guaranteed to exist at output_path
    // Bundler handled: cloning, building, bundling, cleanup
}
```

## Architecture Handling

### Caller Responsibility

The **caller** (e.g., release workflow) is responsible for:
- Specifying source (local path, GitHub org/repo, or GitHub URL)
- Specifying target platform (deb, rpm, dmg, etc.)
- Constructing the output filename with architecture (e.g., `myapp_1.0.0_arm64.deb`)
- Passing the complete output path to bundler

### Bundler Responsibility

The **bundler** handles everything else:
- Cloning repository to `/tmp/kodegen-bundle-{uuid}` from GitHub
- Reading binary name from Cargo.toml
- Reading version from Cargo.toml  
- Detecting target architecture
- Building the binary (`cargo build --release`)
- Creating the platform package
- Creating parent directories in the output path
- Moving the artifact to the specified location
- Cleaning up tmp directory
- Verifying the file exists before returning exit 0

### Why This Design?

This separation ensures:
- **Caller** only needs to know: source location, platform, and output path
- **Bundler** is completely self-contained and handles all implementation details
- **Contract** is enforced through exit codes: 0 = file exists, non-zero = failure
- **Future-proof** for new platforms and architectures (no caller changes needed)
- **Isolation** - builds always happen in tmp, never modifies source directory

## Error Handling

### Exit Codes

| Exit Code | Meaning |
|-----------|---------|
| `0` | Success - if --output-binary specified, file guaranteed to exist |
| `1` | General error - check stderr |
| Non-zero | Specific error - check stderr for details |

### Common Errors

#### Directory Creation Failed

```
Error: Failed to create output directory /path/to/output: Permission denied
```

**Solution**: Check write permissions on the parent directory.

#### Artifact Move Failed

```
Error: Failed to move artifact from /tmp/bundle.deb to /output/app.deb: No such file or directory
```

**Solution**: Verify source artifact was created successfully. Check bundler logs.

#### Contract Violation

```
Error: Move reported success but file does not exist at /output/app.deb
```

**Solution**: This indicates a bundler bug. Report to maintainers.

## Required Project Structure

For bundling to work, your project must have:

```
your-project/
├── Cargo.toml                    # [package.metadata.bundle] section
├── src/
│   └── main.rs
├── assets/
│   └── img/
│       ├── icon.icns             # macOS
│       ├── icon.ico              # Windows
│       └── icon_*x*.png          # Linux (multiple sizes)
└── target/
    └── release/
        └── your-binary           # Built binary
```

See [kodegen-bundler-release README](../kodegen-bundler-release/README.md) for detailed asset requirements.

## Configuration

### TOML Configuration Structure

Bundle settings are configured in your project's `Cargo.toml` under `[package.metadata.bundle]`. The configuration uses a **flat structure** where platform-specific settings are direct children of the bundle section.

**Important**: Platform settings like `deb`, `rpm`, `appimage`, `macos`, and `windows` are **direct fields** under `[package.metadata.bundle]`, not nested under intermediate sections like `.linux`.

### Complete Configuration Example

```toml
[package.metadata.bundle]
# Universal settings
identifier = "com.example.myapp"
publisher = "Example Inc."
icon = ["assets/img/icon_32x32.png", "assets/img/icon_128x128.png"]
resources = ["assets/data"]
copyright = "Copyright © 2025 Example Inc."
category = "Utility"
short_description = "My awesome application"
long_description = "A detailed description of my application"

# Linux: Debian/Ubuntu packages
[package.metadata.bundle.deb]
depends = ["libc6 (>= 2.31)", "libssl3"]
section = "utils"
priority = "optional"

# Linux: RedHat/Fedora/CentOS packages
[package.metadata.bundle.rpm]
depends = ["glibc >= 2.31", "openssl-libs"]
release = "1"

# Linux: AppImage portable executables
[package.metadata.bundle.appimage]
bins = ["myapp"]

# macOS: Application bundles and disk images
[package.metadata.bundle.macos]
frameworks = []
minimum_system_version = "10.13"
signing_identity = "Developer ID Application: Example Inc. (TEAM123)"

[package.metadata.bundle.macos.dmg]
background = "assets/dmg-background.png"
window_size = { width = 660, height = 400 }

# Windows: MSI and NSIS installers
[package.metadata.bundle.windows]
wix_language = "en-US"

[package.metadata.bundle.windows.nsis]
installer_mode = "perUser"
compression = "lzma"
```

### Platform-Specific Configuration Details

#### Debian Packages (`[package.metadata.bundle.deb]`)

```toml
[package.metadata.bundle.deb]
depends = ["libc6 (>= 2.31)"]    # Runtime dependencies
section = "utils"                 # Package category
priority = "optional"             # Installation priority
```

**Note**: The path is `[package.metadata.bundle.deb]`, **not** `[package.metadata.bundle.linux.deb]`.

#### RPM Packages (`[package.metadata.bundle.rpm]`)

```toml
[package.metadata.bundle.rpm]
depends = ["glibc >= 2.31"]      # Runtime dependencies
release = "1"                     # RPM release number
```

**Note**: The path is `[package.metadata.bundle.rpm]`, **not** `[package.metadata.bundle.linux.rpm]`.

#### AppImage (`[package.metadata.bundle.appimage]`)

```toml
[package.metadata.bundle.appimage]
bins = ["myapp", "myapp-cli"]    # Binaries to include
```

**Note**: The path is `[package.metadata.bundle.appimage]`, **not** `[package.metadata.bundle.linux.appimage]`.

#### macOS Bundles (`[package.metadata.bundle.macos]`)

```toml
[package.metadata.bundle.macos]
frameworks = []                                    # Additional frameworks
minimum_system_version = "10.13"                   # Minimum macOS version
signing_identity = "Developer ID Application: ..." # Code signing identity

[package.metadata.bundle.macos.dmg]
background = "assets/dmg-background.png"           # DMG background image
window_size = { width = 660, height = 400 }        # DMG window size
```

#### Windows Installers (`[package.metadata.bundle.windows]`)

```toml
[package.metadata.bundle.windows]
wix_language = "en-US"           # MSI installer language

[package.metadata.bundle.windows.nsis]
installer_mode = "perUser"        # "perUser" or "perMachine"
compression = "lzma"              # "none", "zlib", or "lzma"
```

### Minimal Configuration

The bundler works with minimal configuration, using sensible defaults:

```toml
[package.metadata.bundle]
identifier = "com.example.myapp"
publisher = "Example Inc."
icon = ["assets/img/icon.png"]
```

All platform-specific sections are optional and will use defaults if not specified.

### TOML Path Reference

**Correct paths** for platform-specific configuration:

- `[package.metadata.bundle.deb]` - Debian settings
-`[package.metadata.bundle.rpm]` - RPM settings
-`[package.metadata.bundle.appimage]` - AppImage settings
-`[package.metadata.bundle.macos]` - macOS settings
-`[package.metadata.bundle.macos.dmg]` - DMG-specific settings
-`[package.metadata.bundle.windows]` - Windows settings
-`[package.metadata.bundle.windows.nsis]` - NSIS-specific settings

**Incorrect paths** (do not use):

- `[package.metadata.bundle.linux.deb]` - Wrong, no `.linux` parent
-`[package.metadata.bundle.linux.rpm]` - Wrong, no `.linux` parent
-`[package.metadata.bundle.linux.appimage]` - Wrong, no `.linux` parent

## Building from Source

### Prerequisites

- **Rust nightly** (edition 2024): `rustup install nightly && rustup default nightly`
- **Platform-specific tools**:
  - **Linux**: dpkg-dev, rpm, fakeroot
  - **macOS**: Xcode Command Line Tools
  - **Windows**: WiX Toolset, NSIS

### Build Commands

```bash
# Build release binary
cargo build --release

# Run tests
cargo test

# Format and lint
cargo fmt
cargo clippy -- -D warnings
```

## License

Dual-licensed under **Apache-2.0 OR MIT**.

See [LICENSE.md](LICENSE.md) for details.

## Credits

Part of the [KODEGEN.ᴀɪ](https://kodegen.ai) project - blazing-fast MCP tools for AI-powered code generation.

## Support

- **Issues**: [GitHub Issues]https://github.com/cyrup-ai/kodegen-bundler-bundle/issues
- **Documentation**: [docs.rs]https://docs.rs/kodegen_bundler_bundle
- **Website**: [kodegen.ai]https://kodegen.ai