forjar 1.6.1

Rust-native Infrastructure as Code — bare-metal first, BLAKE3 state, provenance tracing
Documentation
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
//! FJ-3600: Distribution artifact generation.
//!
//! Generates shell installers, Homebrew formulas, cargo-binstall metadata,
//! Nix flakes, GitHub Actions, and OS package specs from `dist:` config.

use super::dist_generators::*;
use super::dist_generators_b::*;
use std::path::Path;

/// Entry point for `forjar dist`.
pub(crate) fn cmd_dist(args: &super::commands::DistArgs) -> Result<(), String> {
    let file = &args.file;
    let content = std::fs::read_to_string(file)
        .map_err(|e| format!("cannot read {}: {e}", file.display()))?;
    let config: crate::core::types::ForjarConfig =
        serde_yaml_ng::from_str(&content).map_err(|e| format!("YAML parse error: {e}"))?;

    let dist = config
        .dist
        .as_ref()
        .ok_or_else(|| "no 'dist:' section in config — add dist: to forjar.yaml".to_string())?;

    // PMAT-081: only github_release is implemented — fail fast instead of
    // generating artifacts with broken empty-repo github.com URLs.
    super::dist_verify::validate_dist_source(dist)?;

    // FJ-3607 Tier 2: --verify-containers runs the generated installer in
    // ubuntu+alpine containers (implies Tier 1). Degrades to a clean skip
    // when no container runtime is available.
    if args.verify_containers {
        return super::dist_verify_tier2::run_verify_containers(dist, args);
    }

    // PMAT-082/FJ-3607 Tier 1: --verify generates to a temp dir and
    // statically verifies instead of writing artifacts.
    if args.verify {
        return super::dist_verify::run_verify(dist, args);
    }

    let gen_all = args.all;
    let gen_installer = args.installer || gen_all;
    let gen_homebrew = args.homebrew || gen_all;
    let gen_binstall = args.binstall || gen_all;
    let gen_nix = args.nix || gen_all;
    let gen_github_action = args.github_action || gen_all;
    let gen_deb = args.deb || gen_all;
    let gen_rpm = args.rpm || gen_all;
    let output = args.output.as_deref();
    let output_dir = args.output_dir.as_deref();
    let json = args.json;

    if !gen_installer
        && !gen_homebrew
        && !gen_binstall
        && !gen_nix
        && !gen_github_action
        && !gen_deb
        && !gen_rpm
    {
        return Err(
            "specify at least one artifact: --installer, --homebrew, --binstall, --nix, --github-action, --deb, --rpm, or --all"
                .to_string(),
        );
    }

    // PMAT-080: Homebrew + Nix embed real checksums — resolve them up front
    // for the pinned --version tag (hard error instead of placeholders).
    let release = if gen_homebrew || gen_nix {
        Some(super::dist_checksums::resolve_release(
            dist,
            args.version.as_deref(),
            args.checksums_file.as_deref(),
        )?)
    } else {
        None
    };

    let out_dir = output_dir.unwrap_or(Path::new("dist"));
    let mut artifacts: Vec<GeneratedArtifact> = Vec::new();

    if gen_installer {
        let default_path = out_dir.join("install.sh");
        let path = output.unwrap_or(&default_path);
        let content = generate_installer(dist);
        write_artifact(path, &content)?;
        artifacts.push(GeneratedArtifact::new("installer", path, content.len()));
    }

    if gen_homebrew {
        let rel = release.as_ref().ok_or("internal: release not resolved")?;
        let path = out_dir.join("homebrew.rb");
        let content = generate_homebrew(dist, rel)?;
        write_artifact(&path, &content)?;
        artifacts.push(GeneratedArtifact::new("homebrew", &path, content.len()));
    }

    if gen_binstall {
        let path = out_dir.join("binstall.toml");
        let content = generate_binstall(dist);
        write_artifact(&path, &content)?;
        artifacts.push(GeneratedArtifact::new("binstall", &path, content.len()));
    }

    if gen_nix {
        let rel = release.as_ref().ok_or("internal: release not resolved")?;
        let path = out_dir.join("flake.nix");
        let content = generate_nix(dist, rel)?;
        write_artifact(&path, &content)?;
        artifacts.push(GeneratedArtifact::new("nix", &path, content.len()));
    }

    if gen_github_action {
        let path = out_dir.join("action.yml");
        let content = generate_github_action(dist);
        write_artifact(&path, &content)?;
        artifacts.push(GeneratedArtifact::new(
            "github-action",
            &path,
            content.len(),
        ));
    }

    if gen_deb {
        let dir = out_dir.join("debian");
        generate_deb(dist, &dir)?;
        artifacts.push(GeneratedArtifact::new("deb", &dir, 0));
    }

    if gen_rpm {
        let path = out_dir.join(format!("{}.spec", dist.binary));
        let content = generate_rpm(dist);
        write_artifact(&path, &content)?;
        artifacts.push(GeneratedArtifact::new("rpm", &path, content.len()));
    }

    if json {
        print_json(&artifacts);
    } else {
        print_summary(&artifacts);
    }

    Ok(())
}

struct GeneratedArtifact {
    kind: String,
    path: String,
    size: usize,
}

impl GeneratedArtifact {
    fn new(kind: &str, path: &Path, size: usize) -> Self {
        Self {
            kind: kind.to_string(),
            path: path.display().to_string(),
            size,
        }
    }
}

fn print_json(artifacts: &[GeneratedArtifact]) {
    let items: Vec<String> = artifacts
        .iter()
        .map(|a| {
            format!(
                r#"{{"kind":"{}","path":"{}","size":{}}}"#,
                a.kind, a.path, a.size
            )
        })
        .collect();
    println!(
        r#"{{"artifacts":[{}],"count":{}}}"#,
        items.join(","),
        artifacts.len()
    );
}

fn print_summary(artifacts: &[GeneratedArtifact]) {
    println!("Generated {} distribution artifact(s):", artifacts.len());
    for a in artifacts {
        if a.size > 0 {
            println!("  {}{} ({} bytes)", a.kind, a.path, a.size);
        } else {
            println!("  {}{}", a.kind, a.path);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::dist_checksums::{parse_sha256sums, ResolvedRelease};
    use crate::core::types::{DistBinaryTarget, DistConfig, DistHomebrewConfig};

    fn sample_release() -> ResolvedRelease {
        let sums = "\
1111aaaa1111aaaa1111aaaa1111aaaa1111aaaa1111aaaa1111aaaa1111aaaa  forjar-1.4.3-x86_64-unknown-linux-gnu.tar.gz
2222bbbb2222bbbb2222bbbb2222bbbb2222bbbb2222bbbb2222bbbb2222bbbb  forjar-1.4.3-x86_64-unknown-linux-musl.tar.gz
3333cccc3333cccc3333cccc3333cccc3333cccc3333cccc3333cccc3333cccc  forjar-1.4.3-aarch64-apple-darwin.tar.gz
";
        ResolvedRelease {
            version: "1.4.3".into(),
            checksums: parse_sha256sums(sums),
        }
    }

    fn sample_dist() -> DistConfig {
        DistConfig {
            source: "github_release".into(),
            repo: "paiml/forjar".into(),
            binary: "forjar".into(),
            targets: vec![
                DistBinaryTarget {
                    os: "linux".into(),
                    arch: "x86_64".into(),
                    asset: "forjar-{version}-x86_64-unknown-linux-gnu.tar.gz".into(),
                    libc: Some("gnu".into()),
                },
                DistBinaryTarget {
                    os: "linux".into(),
                    arch: "x86_64".into(),
                    asset: "forjar-{version}-x86_64-unknown-linux-musl.tar.gz".into(),
                    libc: Some("musl".into()),
                },
                DistBinaryTarget {
                    os: "darwin".into(),
                    arch: "aarch64".into(),
                    asset: "forjar-{version}-aarch64-apple-darwin.tar.gz".into(),
                    libc: None,
                },
            ],
            install_dir: "/usr/local/bin".into(),
            install_dir_fallback: "~/.local/bin".into(),
            checksums: Some("SHA256SUMS".into()),
            checksum_algo: "sha256".into(),
            description: "Rust-native Infrastructure as Code".into(),
            homepage: "https://forjar.dev".into(),
            license: "MIT OR Apache-2.0".into(),
            maintainer: "Pragmatic AI Labs".into(),
            version_cmd: Some("forjar --version".into()),
            latest_tag: true,
            post_install: Some("echo done".into()),
            homebrew: Some(DistHomebrewConfig {
                tap: "paiml/tap".into(),
                dependencies: vec![],
                caveats: Some("Run: forjar init".into()),
            }),
            nix: None,
        }
    }

    #[test]
    fn installer_contains_shebang() {
        let script = generate_installer(&sample_dist());
        assert!(script.starts_with("#!/bin/sh\n"));
    }

    #[test]
    fn installer_contains_set_eu() {
        let script = generate_installer(&sample_dist());
        assert!(script.contains("set -eu"));
    }

    #[test]
    fn installer_contains_binary_name() {
        let script = generate_installer(&sample_dist());
        assert!(script.contains(r#"BINARY="forjar""#));
    }

    #[test]
    fn installer_contains_repo() {
        let script = generate_installer(&sample_dist());
        assert!(script.contains(r#"REPO="paiml/forjar""#));
    }

    #[test]
    fn installer_contains_detect_os() {
        let script = generate_installer(&sample_dist());
        assert!(script.contains("detect_os()"));
    }

    #[test]
    fn installer_contains_detect_arch() {
        let script = generate_installer(&sample_dist());
        assert!(script.contains("detect_arch()"));
    }

    #[test]
    fn installer_contains_checksum_verify() {
        let script = generate_installer(&sample_dist());
        assert!(script.contains("verify_checksum"));
        assert!(script.contains("SHA256SUMS"));
    }

    #[test]
    fn installer_no_checksum_when_none() {
        let mut dist = sample_dist();
        dist.checksums = None;
        let script = generate_installer(&dist);
        assert!(script.contains("no checksums configured"));
    }

    #[test]
    fn installer_contains_version_verify() {
        let script = generate_installer(&sample_dist());
        // Anchored to the just-installed binary, not PATH resolution.
        assert!(script.contains(r#""$DEST/$BINARY" --version"#));
    }

    #[test]
    fn installer_contains_asset_cases() {
        let script = generate_installer(&sample_dist());
        assert!(script.contains("linux/x86_64)"));
        assert!(script.contains("darwin/aarch64)"));
    }

    #[test]
    fn installer_contains_fallback_dir() {
        let script = generate_installer(&sample_dist());
        // Quoted "~" never tilde-expands; the generator emits $HOME.
        assert!(script.contains("$HOME/.local/bin"));
    }

    #[test]
    fn installer_contains_post_install() {
        let script = generate_installer(&sample_dist());
        assert!(script.contains("post_install"));
        assert!(script.contains("echo done"));
    }

    #[test]
    fn homebrew_contains_class_name() {
        let formula = generate_homebrew(&sample_dist(), &sample_release()).unwrap();
        assert!(formula.contains("class Forjar < Formula"));
    }

    #[test]
    fn homebrew_contains_description() {
        let formula = generate_homebrew(&sample_dist(), &sample_release()).unwrap();
        assert!(formula.contains("Rust-native Infrastructure as Code"));
    }

    #[test]
    fn homebrew_skips_musl_targets() {
        let formula = generate_homebrew(&sample_dist(), &sample_release()).unwrap();
        assert!(!formula.contains("musl"));
    }

    #[test]
    fn homebrew_contains_caveats() {
        let formula = generate_homebrew(&sample_dist(), &sample_release()).unwrap();
        assert!(formula.contains("forjar init"));
    }

    #[test]
    fn homebrew_contains_real_version_and_checksums() {
        let formula = generate_homebrew(&sample_dist(), &sample_release()).unwrap();
        assert!(formula.contains(r#"version "1.4.3""#));
        assert!(
            formula.contains("1111aaaa1111aaaa1111aaaa1111aaaa1111aaaa1111aaaa1111aaaa1111aaaa")
        );
        assert!(!formula.contains("PLACEHOLDER"));
    }

    #[test]
    fn homebrew_missing_checksum_is_hard_error() {
        let mut release = sample_release();
        release.checksums.clear();
        let err = generate_homebrew(&sample_dist(), &release).unwrap_err();
        assert!(err.contains("forjar-1.4.3-x86_64-unknown-linux-gnu.tar.gz"));
        assert!(err.contains("--checksums-file"));
    }

    #[test]
    fn binstall_contains_pkg_url() {
        let toml = generate_binstall(&sample_dist());
        assert!(toml.contains("[package.metadata.binstall]"));
        assert!(toml.contains("pkg-url"));
    }

    #[test]
    fn nix_contains_description() {
        let flake = generate_nix(&sample_dist(), &sample_release()).unwrap();
        assert!(flake.contains("Rust-native Infrastructure as Code"));
    }

    #[test]
    fn nix_skips_musl() {
        let flake = generate_nix(&sample_dist(), &sample_release()).unwrap();
        assert!(!flake.contains("musl"));
    }

    #[test]
    fn nix_contains_real_version_and_checksums() {
        let flake = generate_nix(&sample_dist(), &sample_release()).unwrap();
        assert!(flake.contains(r#"version = "1.4.3";"#));
        assert!(flake.contains("3333cccc3333cccc3333cccc3333cccc3333cccc3333cccc3333cccc3333cccc"));
        assert!(!flake.contains("PLACEHOLDER"));
    }

    #[test]
    fn cmd_dist_homebrew_without_version_errors() {
        let dir = tempfile::tempdir().unwrap();
        let config = dir.path().join("forjar.yaml");
        std::fs::write(
            &config,
            "version: \"1.0\"\nname: t\nmachines:\n  local:\n    hostname: l\n    addr: localhost\n    user: root\nresources: {}\ndist:\n  source: github_release\n  repo: acme/tool\n  binary: mytool\n  targets:\n    - os: linux\n      arch: x86_64\n      asset: \"mytool-{version}-x86_64-unknown-linux-gnu.tar.gz\"\n  install_dir: /usr/local/bin\n",
        )
        .unwrap();
        let args = super::super::commands::DistArgs {
            file: config,
            installer: false,
            homebrew: true,
            binstall: false,
            nix: false,
            github_action: false,
            deb: false,
            rpm: false,
            all: false,
            verify: false,
            verify_containers: false,
            version: None,
            checksums_file: None,
            output: None,
            output_dir: Some(dir.path().join("out")),
            json: false,
        };
        let err = cmd_dist(&args).unwrap_err();
        assert!(err.contains("--version"), "got: {err}");
    }

    #[test]
    fn github_action_contains_name() {
        let action = generate_github_action(&sample_dist());
        assert!(action.contains("name: Setup forjar"));
    }

    #[test]
    fn github_action_only_linux_targets() {
        let action = generate_github_action(&sample_dist());
        // Should have linux target, not darwin
        assert!(action.contains("x86_64-unknown-linux-gnu"));
        assert!(!action.contains("darwin"));
    }

    #[test]
    fn rpm_contains_name() {
        let spec = generate_rpm(&sample_dist());
        assert!(spec.contains("Name:    forjar"));
    }

    #[test]
    fn rpm_contains_license() {
        let spec = generate_rpm(&sample_dist());
        assert!(spec.contains("MIT OR Apache-2.0"));
    }

    #[test]
    fn to_class_name_simple() {
        assert_eq!(to_class_name("forjar"), "Forjar");
    }

    #[test]
    fn to_class_name_hyphenated() {
        assert_eq!(to_class_name("my-tool"), "MyTool");
    }

    #[test]
    fn to_rust_triple_linux_gnu() {
        let t = DistBinaryTarget {
            os: "linux".into(),
            arch: "x86_64".into(),
            asset: "test".into(),
            libc: Some("gnu".into()),
        };
        assert_eq!(to_rust_triple(&t), "x86_64-unknown-linux-gnu");
    }

    #[test]
    fn to_rust_triple_darwin() {
        let t = DistBinaryTarget {
            os: "darwin".into(),
            arch: "aarch64".into(),
            asset: "test".into(),
            libc: None,
        };
        assert_eq!(to_rust_triple(&t), "aarch64-apple-darwin");
    }
}