rustclamp-tooling 0.5.0

Developer inspection commands for resolved Clamp process models.
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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
//! Command-line interface for resolved Clamp process inspection.

mod envfile;
mod make;
mod mcp;

use serde_json::{Value, json};
use std::{
    env, fs,
    io::{self, IsTerminal, Write},
    process::Command,
    process::ExitCode,
};

use rustclamp_tooling::{
    doctor_text, graph_text, inspect_text, tree_text, validate_document, why_text,
};

fn main() -> ExitCode {
    let args = env::args().skip(1).collect::<Vec<_>>();
    if args.is_empty() {
        return match welcome() {
            Ok(code) => ExitCode::from(code),
            Err(message) => {
                eprintln!("clamp: {message}");
                ExitCode::from(2)
            }
        };
    }
    match run(args) {
        Ok(code) => ExitCode::from(code),
        Err(message) => {
            eprintln!("clamp: {message}\n\n{}", usage());
            ExitCode::from(2)
        }
    }
}

fn welcome() -> Result<u8, String> {
    println!(
        r#"
___           _    ___ _
| _ \_  _ ___| |_ / __| |__ _ _ __  _ __
|   / || (_-<|  _| (__| / _` | '  \| '_ \
|_|_\\_,_/__/ \__|\___|_\__,_|_|_|_| .__/
                                     |_|

  1. Create a project
     Templates: Hello World or application
  2. Check this project
  3. Run tests
  4. Build this project
  5. Run this project
  6. Inspect architecture JSON
  7. Show command help
  0. Exit
"#
    );
    print!("Choose an option: ");
    io::stdout()
        .flush()
        .map_err(|error| format!("cannot write menu: {error}"))?;
    let mut choice = String::new();
    io::stdin()
        .read_line(&mut choice)
        .map_err(|error| format!("cannot read menu choice: {error}"))?;
    let choice = choice.trim();
    if choice == "0" || (choice.is_empty() && !io::stdin().is_terminal()) {
        return Ok(0);
    }
    if choice == "1" {
        print!("Project name: ");
        io::stdout()
            .flush()
            .map_err(|error| format!("cannot write prompt: {error}"))?;
        let mut name = String::new();
        io::stdin()
            .read_line(&mut name)
            .map_err(|error| format!("cannot read project name: {error}"))?;
        print!("Template [blank/app/web/tui/package] (blank): ");
        io::stdout()
            .flush()
            .map_err(|error| format!("cannot write template prompt: {error}"))?;
        let mut template = String::new();
        io::stdin()
            .read_line(&mut template)
            .map_err(|error| format!("cannot read template: {error}"))?;
        let template = match template.trim() {
            "" => "blank",
            other => other,
        };
        return run(vec![
            "init".into(),
            name.trim().into(),
            format!("--{template}"),
        ]);
    }
    if choice == "6" {
        print!("Inspection JSON file: ");
        io::stdout()
            .flush()
            .map_err(|error| format!("cannot write prompt: {error}"))?;
        let mut path = String::new();
        io::stdin()
            .read_line(&mut path)
            .map_err(|error| format!("cannot read file path: {error}"))?;
        return run(vec!["inspect".into(), path.trim().into()]);
    }
    let command = match choice {
        "2" => "check",
        "3" => "test",
        "4" => "build",
        "5" => "run",
        "7" => "--help",
        _ => return Err(format!("unknown menu option {choice:?}")),
    };
    run(vec![command.into()])
}

fn run(args: Vec<String>) -> Result<u8, String> {
    let command = args.first().map(String::as_str).ok_or("missing command")?;
    if command == "--version" || command == "-V" {
        println!("clamp {}", env!("CARGO_PKG_VERSION"));
        return Ok(0);
    }
    if command == "--help" || command == "help" {
        println!("{}", usage());
        return Ok(0);
    }
    if command == "mcp" {
        mcp::serve(io::stdin().lock(), io::stdout().lock())
            .map_err(|error| format!("mcp: {error}"))?;
        return Ok(0);
    }
    if matches!(command, "key:generate" | "env:encrypt" | "env:decrypt") {
        let root = env::current_dir().map_err(|error| format!("cannot read folder: {error}"))?;
        let options = envfile::Options::parse(&args[1..])?;
        let message = match command {
            "key:generate" => envfile::key_generate(&root, &options),
            "env:encrypt" => envfile::encrypt(&root, &options),
            _ => envfile::decrypt(&root, &options, std::env::var(envfile::KEY_VARIABLE).ok()),
        }?;
        println!("{message}");
        return Ok(0);
    }
    if let Some(kind) = command.strip_prefix("make:") {
        let root = env::current_dir().map_err(|error| format!("cannot read folder: {error}"))?;
        println!("{}", make::make(&root, kind, args.get(1))?);
        return Ok(0);
    }
    if matches!(
        command,
        "migrate" | "migrate:rollback" | "migrate:status" | "db:seed"
    ) {
        // The app runs its own database commands; see `rustclamp::db::command`.
        let status = Command::new("cargo")
            .args(["run", "--quiet", "--", command])
            .status()
            .map_err(|error| format!("cannot start cargo: {error}"))?;
        return Ok(status.code().unwrap_or(1).clamp(0, 255) as u8);
    }
    if command == "init" {
        let project = args.get(1).ok_or("init requires a project name or path")?;
        let flags: Vec<&str> = args[2..].iter().map(String::as_str).collect();
        let (template, frontend) = match flags.as_slice() {
            [] | ["--blank"] | ["--template", "blank" | "hello-world"] => ("blank", None),
            ["--app"] | ["--template", "app" | "application"] => ("app", None),
            ["--web"] | ["--template", "web"] => ("web", None),
            ["--vue"] | ["--template", "vue"] => ("web", Some("vue")),
            ["--react"] | ["--template", "react"] => ("web", Some("react")),
            ["--tui"] | ["--template", "tui"] => ("tui", None),
            ["--package"] | ["--template", "package"] => ("package", None),
            ["--profile", _] => ("profile", None),
            _ => {
                return Err(
                    "usage: clamp init NAME [--blank|--app|--web|--vue|--react|--tui|--package|--profile cli,service,worker]"
                        .into(),
                );
            }
        };
        let profiles = match flags.as_slice() {
            ["--profile", list] => profiles(list)?,
            _ => Vec::new(),
        };
        // Written directly rather than with `cargo new`, which would also add the
        // project to any workspace above it; `[workspace]` keeps it standalone.
        let root = std::path::Path::new(project);
        let name = root
            .file_name()
            .and_then(|name| name.to_str())
            .filter(|name| {
                name.starts_with(|c: char| c.is_ascii_alphabetic())
                    && name
                        .chars()
                        .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
            })
            .ok_or("project name must start with a letter and use only letters, digits, - or _")?;
        if root.exists() {
            return Err(format!("{} already exists", root.display()));
        }
        fs::create_dir_all(root.join("src"))
            .map_err(|error| format!("cannot create project directory: {error}"))?;
        let features = match template {
            "web" => ", features = [\"web\", \"db\", \"crypto\"]".to_owned(),
            "package" => ", features = [\"web\"]".to_owned(),
            "profile" => {
                let features: std::collections::BTreeSet<_> =
                    profiles.iter().flat_map(|profile| profile.2).collect();
                match features.len() {
                    0 => String::new(),
                    _ => format!(", features = {:?}", Vec::from_iter(features)),
                }
            }
            _ => String::new(),
        };
        fs::write(
            root.join("Cargo.toml"),
            format!(
                "[package]\nname = \"{name}\"\nversion = \"0.1.0\"\nedition = \"2024\"\n\n[workspace]\n\n[dependencies]\nrustclamp = {{ git = \"https://github.com/rustclamp/rustclamp\", branch = \"main\"{features} }}\n"
            ),
        )
        .map_err(|error| format!("cannot write manifest: {error}"))?;
        fs::write(root.join(".gitignore"), "/target\n")
            .map_err(|error| format!("cannot write .gitignore: {error}"))?;
        // ponytail: git is optional; a failed `git init` leaves a working project
        let _ = Command::new("git")
            .args(["init", "--quiet"])
            .arg(root)
            .status();
        if template == "package" {
            // A library: nothing to run, so no `cargo dev` alias or Procfile.
            create_package(root, name)?;
            println!("Created RustClamp package at {}", root.display());
            println!("Next: cd {} && cargo test", root.display());
            return Ok(0);
        }
        fs::create_dir_all(root.join(".cargo"))
            .map_err(|error| format!("cannot create .cargo directory: {error}"))?;
        fs::write(root.join(".cargo/config.toml"), "[alias]\ndev = \"run\"\n")
            .map_err(|error| format!("cannot write Cargo aliases: {error}"))?;
        fs::write(root.join("Procfile.dev"), "app: cargo run\n")
            .map_err(|error| format!("cannot write Procfile.dev: {error}"))?;
        match template {
            "app" => create_application(root)?,
            "web" => create_web(root, name, frontend)?,
            "tui" => create_tui(root)?,
            "profile" => create_profiles(root, name, &profiles, flags[1])?,
            _ => {
                fs::write(
                    root.join("src/main.rs"),
                    "use rustclamp::prelude::*;\n\nfn main() {\n    Clamp::run(|| println!(\"Hello from Clamp!\"));\n}\n",
                )
                .map_err(|error| format!("cannot write generated entrypoint: {error}"))?;
                fs::write(
                    root.join("README.md"),
                    "# Clamp application\n\nCreated with `clamp init`. Run it with `cargo run`.\n",
                )
                .map_err(|error| format!("cannot write generated README: {error}"))?;
            }
        }
        println!("Created RustClamp {template} project at {}", root.display());
        if template == "web" {
            println!("Next: cd {} && clamp dev", root.display());
        } else {
            println!("Next: cd {} && cargo run", root.display());
        }
        return Ok(0);
    }
    if command == "self-update" || command == "global-update" {
        // Reinstall from the checkout this binary was built from, else the latest release (Unix) or GitHub main.
        let mut source = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
        // tooling depends on ../../core and ../../kernel, so a GitHub install
        // clones all three repos side by side, as install.sh does.
        let work = std::env::temp_dir().join(format!("clamp-update-{}", std::process::id()));
        if source.join("Cargo.toml").exists() {
            println!("Updating clamp from {}", source.display());
        } else if cfg!(unix) {
            // Release binaries: rerun the installer, which fetches the latest release.
            println!("Updating clamp from the latest release");
            let status = Command::new("sh")
                .args([
                    "-c",
                    "curl --proto '=https' --tlsv1.2 -fsSL https://rustclamp.com/install.sh | sh",
                ])
                .status()
                .map_err(|error| format!("cannot start sh: {error}"))?;
            return Ok(status.code().unwrap_or(1).clamp(0, 255) as u8);
        } else {
            println!("Updating clamp from github.com/rustclamp (main)");
            for repo in ["core", "kernel", "rustclamp"] {
                let status = Command::new("git")
                    .args(["clone", "--quiet", "--depth", "1"])
                    .arg(format!("https://github.com/rustclamp/{repo}"))
                    .arg(work.join(repo))
                    .status()
                    .map_err(|error| format!("cannot start git: {error}"))?;
                if !status.success() {
                    let _ = fs::remove_dir_all(&work);
                    return Err(format!("cannot clone rustclamp/{repo}"));
                }
            }
            source = work.join("rustclamp/tooling");
        }
        let status = Command::new("cargo")
            .args(["install", "--force", "--locked", "--path"])
            .arg(&source)
            .status()
            .map_err(|error| format!("cannot start Cargo: {error}"));
        let _ = fs::remove_dir_all(&work);
        return Ok(status?.code().unwrap_or(1).clamp(0, 255) as u8);
    }
    if command == "dev" {
        return dev();
    }
    if ["check", "test", "build", "run"].contains(&command) {
        let status = Command::new("cargo")
            .arg(command)
            .args(&args[1..])
            .status()
            .map_err(|error| format!("cannot start Cargo: {error}"))?;
        return Ok(status.code().unwrap_or(1).clamp(0, 255) as u8);
    }
    let (mut positional, mut process_id, mut json_output) = (Vec::new(), None, false);
    let mut index = 1;
    while index < args.len() {
        match args[index].as_str() {
            "--json" => json_output = true,
            "--process" => {
                index += 1;
                process_id = Some(args.get(index).ok_or("--process requires an id")?.clone());
            }
            option if option.starts_with('-') => return Err(format!("unknown option {option}")),
            value => positional.push(value.to_owned()),
        }
        index += 1;
    }
    let path = positional.first().ok_or("missing inspection JSON path")?;
    let (human, machine, code) = inspection(
        command,
        path,
        process_id.as_deref(),
        positional.get(1).map(String::as_str),
    )?;
    if json_output {
        println!("{}", serde_json::to_string_pretty(&machine).unwrap());
    } else {
        println!("{human}");
    }
    Ok(code)
}

/// Runs one read-only inspection command on a document file and returns its
/// human text, its `--json` value, and the exit status. `clamp mcp` calls this too.
fn inspection(
    command: &str,
    path: &str,
    process_id: Option<&str>,
    module: Option<&str>,
) -> Result<(String, Value, u8), String> {
    let document: Value = serde_json::from_slice(
        &fs::read(path).map_err(|error| format!("cannot read {path}: {error}"))?,
    )
    .map_err(|error| format!("invalid JSON in {path}: {error}"))?;
    validate_document(&document)?;

    if command == "doctor" {
        let code = if document["status"] == "resolved" {
            0
        } else {
            1
        };
        return Ok((doctor_text(&document).0, document, code));
    }
    if document["status"] != "resolved" {
        return Err("document contains composition errors; run `clamp doctor FILE`".into());
    }

    let processes = document["processes"]
        .as_array()
        .ok_or("resolved document has no process list")?;
    let process = match process_id {
        Some(id) => processes
            .iter()
            .find(|process| process["id"].as_str() == Some(id))
            .ok_or_else(|| format!("process {id:?} is not in this document"))?,
        None if processes.len() == 1 => &processes[0],
        None => return Err("select a process with --process ID".into()),
    };

    let (human, machine) = match command {
        "inspect" => match document.get("config_keys") {
            Some(keys) => (
                format!(
                    "{}\nConfig keys: {}",
                    inspect_text(process),
                    keys.as_array()
                        .into_iter()
                        .flatten()
                        .filter_map(Value::as_str)
                        .collect::<Vec<_>>()
                        .join(", ")
                ),
                json!({"process": process, "config_keys": keys}),
            ),
            None => (inspect_text(process), json!({"process": process})),
        },
        "tree" => {
            let tree = tree_text(process);
            (tree.clone(), json!({"tree": tree}))
        }
        "graph" => {
            let edges = graph_text(process);
            (
                edges.clone(),
                json!({"edges": edges.lines().collect::<Vec<_>>()}),
            )
        }
        "why" => {
            let module = module.ok_or("why requires a module id")?;
            let explanation = why_text(process, module)?;
            (
                explanation.clone(),
                json!({"module": module, "explanation": explanation}),
            )
        }
        _ => return Err(format!("unknown command {command:?}")),
    };
    Ok((human, machine, 0))
}

fn usage() -> &'static str {
    "Usage:\n  clamp init <project-name> [--blank|--app|--web|--vue|--react|--tui|--package|--profile cli,service,worker]\n  clamp <inspect|tree|graph|why|doctor> FILE [MODULE] [--process ID] [--json]\n  clamp mcp\n  clamp dev\n  clamp make:migration NAME | make:seeder NAME | make:controller NAME | make:middleware NAME | make:request NAME | make:model NAME | make:command NAME\n  clamp migrate | migrate:rollback | migrate:status | db:seed\n  clamp key:generate [--force]\n  clamp env:encrypt | env:decrypt [--key=KEY] [--env=NAME] [--force]\n  clamp self-update\n  clamp --version\n  clamp <check|test|build|run> [Cargo arguments]\n\nCreate a RustClamp blank, app, web (plain, Vue or React) or TUI scaffold, a package or a combination of profiles, inspect a resolved projection (also as an MCP server over stdio), run Procfile.dev concurrently, make migrations, seeders, controllers, middleware, form requests, models and console commands, run database commands, manage APP_KEY and encrypted .env files, reinstall clamp, or run a Cargo command."
}

fn create_application(root: &std::path::Path) -> Result<(), String> {
    fs::write(
        root.join("src/main.rs"),
        "mod app;\nmod modules;\n\nuse rustclamp::prelude::*;\n\nfn main() {\n    Clamp::run(app::run);\n}\n",
    )
    .map_err(|error| format!("cannot write application entrypoint: {error}"))?;
    fs::write(
        root.join("src/app.rs"),
        "use crate::modules::health;\n\npub fn run() {\n    println!(\"Application started\");\n    println!(\"Health check: {}\", health::check());\n}\n",
    )
    .map_err(|error| format!("cannot write application module: {error}"))?;
    fs::create_dir_all(root.join("src/modules"))
        .map_err(|error| format!("cannot create application modules: {error}"))?;
    fs::write(root.join("src/modules/mod.rs"), "pub mod health;\n")
        .map_err(|error| format!("cannot write module list: {error}"))?;
    fs::write(
        root.join("src/modules/health.rs"),
        "pub fn check() -> &'static str {\n    \"ready\"\n}\n",
    )
    .map_err(|error| format!("cannot write health module: {error}"))?;
    fs::write(
        root.join("README.md"),
        "# Clamp application\n\nCreated with `clamp init --app`. The binary entrypoint delegates to `app`, which composes modules under `src/modules`.\n\nRun with `cargo run`; add application behavior in modules and keep `main.rs` as the composition root.\n",
    )
    .map_err(|error| format!("cannot write application README: {error}"))?;
    Ok(())
}

const WEB_TEMPLATE: &[(&str, &str)] = &[
    ("app/main.rs", include_str!("../templates/web/app/main.rs")),
    ("app/lib.rs", include_str!("../templates/web/app/lib.rs")),
    (
        "app/config/app.rs",
        include_str!("../templates/web/app/config/app.rs"),
    ),
    (
        "app/config/database.rs",
        include_str!("../templates/web/app/config/database.rs"),
    ),
    (
        "app/config/filesystems.rs",
        include_str!("../templates/web/app/config/filesystems.rs"),
    ),
    (
        "app/config/logging.rs",
        include_str!("../templates/web/app/config/logging.rs"),
    ),
    (
        "app/http/controllers/controller.rs",
        include_str!("../templates/web/app/http/controllers/controller.rs"),
    ),
    (
        "app/http/controllers/home.rs",
        include_str!("../templates/web/app/http/controllers/home.rs"),
    ),
    (
        "app/http/kernel.rs",
        include_str!("../templates/web/app/http/kernel.rs"),
    ),
    (
        "app/http/middleware/.gitkeep",
        include_str!("../templates/web/app/http/middleware/.gitkeep"),
    ),
    (
        "app/http/requests/.gitkeep",
        include_str!("../templates/web/app/http/requests/.gitkeep"),
    ),
    (
        "app/models/.gitkeep",
        include_str!("../templates/web/app/models/.gitkeep"),
    ),
    // Present from the start: build.rs watches these, and Cargo reruns a build
    // script on every build while a watched path is missing.
    (
        "app/database/migrations/.gitkeep",
        include_str!("../templates/web/app/database/migrations/.gitkeep"),
    ),
    (
        "app/database/seeders/.gitkeep",
        include_str!("../templates/web/app/database/seeders/.gitkeep"),
    ),
    (
        "app/database/states/.gitkeep",
        include_str!("../templates/web/app/database/states/.gitkeep"),
    ),
    (
        "app/routes/web.rs",
        include_str!("../templates/web/app/routes/web.rs"),
    ),
    ("build.rs", include_str!("../templates/web/build.rs")),
    (
        "app/routes/api.rs",
        include_str!("../templates/web/app/routes/api.rs"),
    ),
    (
        "tests/routes.rs",
        include_str!("../templates/web/tests/routes.rs"),
    ),
    (
        "app/resources/views/welcome.html",
        include_str!("../templates/web/app/resources/views/welcome.html"),
    ),
    (
        "app/resources/views/errors/4xx.html",
        include_str!("../templates/web/app/resources/views/errors/4xx.html"),
    ),
    (
        "app/resources/views/errors/5xx.html",
        include_str!("../templates/web/app/resources/views/errors/5xx.html"),
    ),
    (
        "app/resources/css/app.css",
        include_str!("../templates/web/app/resources/css/app.css"),
    ),
    (
        "app/resources/js/app.ts",
        include_str!("../templates/web/app/resources/js/app.ts"),
    ),
    (
        "public/robots.txt",
        include_str!("../templates/web/public/robots.txt"),
    ),
    (
        ".env.example",
        include_str!("../templates/web/.env.example"),
    ),
    (
        "package.json",
        include_str!("../templates/web/package.json"),
    ),
    (
        "package-lock.json",
        include_str!("../templates/web/package-lock.json"),
    ),
    (
        "vite.config.ts",
        include_str!("../templates/web/vite.config.ts"),
    ),
    (
        "Procfile.dev",
        include_str!("../templates/web/Procfile.dev"),
    ),
    ("README.md", include_str!("../templates/web/README.md")),
];

/// Where the welcome page mounts the example component; plain `--web` has none.
const COMPONENT_MOUNT: &str = "        <div class=\"mt-4 flex justify-center\" data-component=\"Counter\" data-props='{\"start\":0}'></div>\n";

/// `--vue`: written over [`WEB_TEMPLATE`].
const VUE_OVERLAY: &[(&str, &str)] = &[
    (
        "package.json",
        include_str!("../templates/web-vue/package.json"),
    ),
    (
        "package-lock.json",
        include_str!("../templates/web-vue/package-lock.json"),
    ),
    (
        "vite.config.ts",
        include_str!("../templates/web-vue/vite.config.ts"),
    ),
    (
        "app/resources/js/app.ts",
        include_str!("../templates/web-vue/app/resources/js/app.ts"),
    ),
    (
        "app/resources/js/components/Counter.vue",
        include_str!("../templates/web-vue/app/resources/js/components/Counter.vue"),
    ),
];

/// `--react`: written over [`WEB_TEMPLATE`].
const REACT_OVERLAY: &[(&str, &str)] = &[
    (
        "package.json",
        include_str!("../templates/web-react/package.json"),
    ),
    (
        "package-lock.json",
        include_str!("../templates/web-react/package-lock.json"),
    ),
    (
        "vite.config.ts",
        include_str!("../templates/web-react/vite.config.ts"),
    ),
    (
        "app/resources/js/app.ts",
        include_str!("../templates/web-react/app/resources/js/app.ts"),
    ),
    (
        "app/resources/js/components/Counter.tsx",
        include_str!("../templates/web-react/app/resources/js/components/Counter.tsx"),
    ),
];

/// Laravel-style layout: Rust and frontend sources in `app/`, web root in
/// `public/`, tests in `tests/`. `frontend` (`vue` or `react`) adds that
/// framework's components, mounted on server-rendered pages.
fn create_web(root: &std::path::Path, name: &str, frontend: Option<&str>) -> Result<(), String> {
    let crate_name = name.replace('-', "_");
    let (overlay, component): (&[(&str, &str)], &str) = match frontend {
        Some("vue") => (VUE_OVERLAY, COMPONENT_MOUNT),
        Some("react") => (REACT_OVERLAY, COMPONENT_MOUNT),
        _ => (&[], ""),
    };
    // The overlay comes last, so its files replace the plain ones.
    for (path, contents) in WEB_TEMPLATE.iter().chain(overlay) {
        let file = root.join(path);
        if let Some(parent) = file.parent() {
            fs::create_dir_all(parent)
                .map_err(|error| format!("cannot create {}: {error}", parent.display()))?;
        }
        let contents = contents
            .replace("__CRATE__", &crate_name)
            .replace("        __COMPONENT__\n", component);
        fs::write(&file, contents).map_err(|error| format!("cannot write {path}: {error}"))?;
    }
    let _ = fs::remove_dir(root.join("src"));
    let manifest = root.join("Cargo.toml");
    let mut cargo_toml =
        fs::read_to_string(&manifest).map_err(|error| format!("cannot read manifest: {error}"))?;
    cargo_toml.push_str(&format!(
        "\n[build-dependencies]\nrustclamp = {{ git = \"https://github.com/rustclamp/rustclamp\", branch = \"main\", features = [\"build\"] }}\n\n[lib]\npath = \"app/lib.rs\"\n\n[[bin]]\nname = \"{name}\"\npath = \"app/main.rs\"\n"
    ));
    fs::write(&manifest, cargo_toml).map_err(|error| format!("cannot write manifest: {error}"))?;
    let gitignore = root.join(".gitignore");
    let mut ignored = fs::read_to_string(&gitignore).unwrap_or_default();
    // Every .env is secret except the example and the encrypted ones.
    ignored.push_str("/node_modules\n/public/build\n/public/storage\n/storage\n.env\n.env.*\n!.env.example\n!.env.encrypted\n!.env.*.encrypted\n");
    fs::write(&gitignore, ignored).map_err(|error| format!("cannot update .gitignore: {error}"))?;
    Ok(())
}

const PACKAGE_TEMPLATE: &[(&str, &str)] = &[
    (
        "src/lib.rs",
        include_str!("../templates/package/src/lib.rs"),
    ),
    (
        "resources/views/index.html",
        include_str!("../templates/package/resources/views/index.html"),
    ),
    (
        "resources/css/__NAME__.css",
        include_str!("../templates/package/resources/css/__NAME__.css"),
    ),
    (
        "tests/routes.rs",
        include_str!("../templates/package/tests/routes.rs"),
    ),
    ("README.md", include_str!("../templates/package/README.md")),
];

/// A self-contained package crate: routes, views, static files and tests.
fn create_package(root: &std::path::Path, name: &str) -> Result<(), String> {
    let crate_name = name.replace('-', "_");
    let struct_name: String = name
        .split(['-', '_'])
        .flat_map(|word| {
            let mut chars = word.chars();
            chars
                .next()
                .map(|first| first.to_ascii_uppercase())
                .into_iter()
                .chain(chars)
        })
        .collect();
    let fill = |text: &str| {
        text.replace("__CRATE__", &crate_name)
            .replace("__STRUCT__", &struct_name)
            .replace("__ENV__", &crate_name.to_ascii_uppercase())
            .replace("__NAME__", name)
    };
    for (path, contents) in PACKAGE_TEMPLATE {
        let file = root.join(fill(path));
        if let Some(parent) = file.parent() {
            fs::create_dir_all(parent)
                .map_err(|error| format!("cannot create {}: {error}", parent.display()))?;
        }
        fs::write(&file, fill(contents))
            .map_err(|error| format!("cannot write {path}: {error}"))?;
    }
    Ok(())
}

fn create_tui(root: &std::path::Path) -> Result<(), String> {
    fs::write(
        root.join("src/main.rs"),
        r#"use std::io::{self, BufRead, Write};

use rustclamp::prelude::*;

fn main() {
    Clamp::run(|| {
        println!("Clamp TUI. Type a line, or `quit` to exit.");
        let mut stdout = io::stdout();
        let _ = write!(stdout, "> ");
        let _ = stdout.flush();
        for line in io::stdin().lock().lines().map_while(Result::ok) {
            if line.trim() == "quit" {
                break;
            }
            println!("you said: {line}");
            let _ = write!(stdout, "> ");
            let _ = stdout.flush();
        }
    });
}
"#,
    )
    .map_err(|error| format!("cannot write TUI entrypoint: {error}"))?;
    fs::write(
        root.join("README.md"),
        "# Clamp TUI\n\nCreated with `clamp init --tui`. Run it with `cargo run`; type `quit` to exit.\n",
    )
    .map_err(|error| format!("cannot write TUI README: {error}"))?;
    Ok(())
}

/// One `--profile` (ADR 0032): its name, the subcommand that runs it when it
/// is not the first one chosen, its facade features and its `src/` file.
type Profile = (
    &'static str,
    &'static str,
    &'static [&'static str],
    &'static str,
);

/// Every profile, in the order `main.rs` lists them; the first one chosen
/// runs when no subcommand is given.
const PROFILES: &[Profile] = &[
    ("cli", "", &[], include_str!("../templates/profiles/cli.rs")),
    (
        "service",
        "serve",
        &["web", "metrics"],
        include_str!("../templates/profiles/service.rs"),
    ),
    (
        "worker",
        "work",
        &["config", "log"],
        include_str!("../templates/profiles/worker.rs"),
    ),
];

/// The profiles named in `list`, such as `service,worker`, in [`PROFILES`] order.
fn profiles(list: &str) -> Result<Vec<&'static Profile>, String> {
    let names: Vec<&str> = list.split(',').map(str::trim).collect();
    if let Some(unknown) = names
        .iter()
        .find(|name| !PROFILES.iter().any(|profile| profile.0 == **name))
    {
        return Err(format!(
            "unknown profile {unknown:?}; choose from cli, service, worker"
        ));
    }
    Ok(PROFILES
        .iter()
        .filter(|profile| names.contains(&profile.0))
        .collect())
}

/// Writes each profile's file, a `main.rs` that picks one by its first
/// argument, a Procfile running every long-lived one, and a README.
fn create_profiles(
    root: &std::path::Path,
    name: &str,
    profiles: &[&Profile],
    list: &str,
) -> Result<(), String> {
    let (first, rest) = profiles.split_first().ok_or("--profile needs a profile")?;
    let mut main = String::new();
    let (mut procfile, mut readme) = (
        String::new(),
        format!(
            "# {name}\n\nCreated with `clamp init --profile {list}`. Each profile is one file in `src/`, and `src/main.rs` runs one:\n\n"
        ),
    );
    for (profile, _, _, contents) in profiles {
        main.push_str(&format!("mod {profile};\n"));
        fs::write(root.join(format!("src/{profile}.rs")), contents)
            .map_err(|error| format!("cannot write src/{profile}.rs: {error}"))?;
    }
    main.push_str("\nuse std::process::ExitCode;\n\nuse rustclamp::prelude::*;\n\nfn main() -> ExitCode {\n    let args: Vec<String> = std::env::args().skip(1).collect();\n");
    if rest.is_empty() {
        main.push_str(&format!(
            "    ExitCode::from(Clamp::run(|| {}::run(&args)))\n}}\n",
            first.0
        ));
    } else {
        main.push_str(
            "    ExitCode::from(Clamp::run(|| match args.first().map(String::as_str) {\n",
        );
        for (profile, command, _, _) in rest {
            main.push_str(&format!(
                "        Some(\"{command}\") => {profile}::run(&args[1..]),\n"
            ));
        }
        main.push_str(&format!(
            "        _ => {}::run(&args),\n    }}))\n}}\n",
            first.0
        ));
    }
    for (index, (profile, command, _, _)) in profiles.iter().enumerate() {
        let run = if index == 0 {
            "cargo run".to_owned()
        } else {
            format!("cargo run -- {command}")
        };
        readme.push_str(&format!(
            "- `{run}`: the {profile} profile, `src/{profile}.rs`\n"
        ));
        if *profile != "cli" {
            procfile.push_str(&format!("{profile}: {run}\n"));
        }
    }
    readme.push_str("\nWhat each profile sets up, and the runnable example behind it: https://docs.rustclamp.com/recipes\n");
    fs::write(root.join("src/main.rs"), main)
        .map_err(|error| format!("cannot write src/main.rs: {error}"))?;
    fs::write(root.join("README.md"), readme)
        .map_err(|error| format!("cannot write README.md: {error}"))?;
    if !procfile.is_empty() {
        fs::write(root.join("Procfile.dev"), procfile)
            .map_err(|error| format!("cannot write Procfile.dev: {error}"))?;
    }
    Ok(())
}

/// Runs every `name: command` line of `Procfile.dev` at once, prefixing output
/// with its name. A process that fails stops the rest.
fn dev() -> Result<u8, String> {
    // ponytail: no restart-on-crash or file watching; put `watchexec -r -- cargo run` in the Procfile for reload
    if std::path::Path::new("package.json").exists()
        && !std::path::Path::new("node_modules").exists()
    {
        println!("[clamp] installing frontend dependencies: npm install");
        let status = Command::new(if cfg!(windows) { "npm.cmd" } else { "npm" })
            .arg("install")
            .status()
            .map_err(|error| format!("cannot start npm (is Node.js installed?): {error}"))?;
        if !status.success() {
            return Ok(status.code().unwrap_or(1).clamp(0, 255) as u8);
        }
    }
    let procfile = fs::read_to_string("Procfile.dev").unwrap_or_else(|_| "app: cargo run\n".into());
    let mut children = Vec::new();
    for line in procfile.lines().map(str::trim) {
        if line.is_empty() || line.starts_with('#') {
            continue;
        }
        let (name, command) = line
            .split_once(':')
            .ok_or_else(|| format!("Procfile.dev line needs `name: command`: {line:?}"))?;
        let (name, command) = (name.trim().to_string(), command.trim());
        let mut shell = if cfg!(windows) {
            let mut shell = Command::new("cmd");
            shell.arg("/C");
            shell
        } else {
            let mut shell = Command::new("sh");
            shell.arg("-c");
            shell
        };
        let mut child = shell
            .arg(command)
            .stdout(std::process::Stdio::piped())
            .stderr(std::process::Stdio::piped())
            .spawn()
            .map_err(|error| format!("cannot start {name}: {error}"))?;
        for pipe in [
            child
                .stdout
                .take()
                .map(|out| Box::new(out) as Box<dyn io::Read + Send>),
            child
                .stderr
                .take()
                .map(|err| Box::new(err) as Box<dyn io::Read + Send>),
        ]
        .into_iter()
        .flatten()
        {
            let name = name.clone();
            std::thread::spawn(move || {
                for line in io::BufRead::lines(io::BufReader::new(pipe)).map_while(Result::ok) {
                    println!("[{name}] {line}");
                }
            });
        }
        println!("[clamp] started {name}: {command}");
        children.push((name, child));
    }
    if children.is_empty() {
        return Err("Procfile.dev has no commands".into());
    }
    while !children.is_empty() {
        let mut index = 0;
        while index < children.len() {
            let Some(status) = children[index]
                .1
                .try_wait()
                .map_err(|error| error.to_string())?
            else {
                index += 1;
                continue;
            };
            let (name, _) = children.remove(index);
            if status.success() {
                println!("[clamp] {name} finished");
                continue;
            }
            println!("[clamp] {name} failed ({status}); stopping the rest");
            for (_, child) in &mut children {
                let _ = child.kill();
                let _ = child.wait();
            }
            return Ok(status.code().unwrap_or(1).clamp(0, 255) as u8);
        }
        std::thread::sleep(std::time::Duration::from_millis(100));
    }
    Ok(0)
}

#[cfg(test)]
mod tests {
    use super::*;
    use rustclamp::config::Config;
    use rustclamp_core::{ApplicationId, ExecutionId, ModuleId, ProcessId};
    use rustclamp_kernel::ApplicationBlueprint;

    /// `clamp inspect` lists config keys but never a value (#124).
    #[test]
    fn inspect_shows_config_keys_never_values() {
        let root = ModuleId::new("app.web");
        let mut blueprint = ApplicationBlueprint::new(ApplicationId::new("sample"));
        blueprint
            .add_module(root)
            .add_execution(ExecutionId::new("web"), root)
            .add_process(ProcessId::new("web"), vec![ExecutionId::new("web")]);
        let projection = blueprint.project(ProcessId::new("web")).unwrap();
        let document = rustclamp_tooling::with_config_keys(
            rustclamp_tooling::inspection_document(&[&projection]).unwrap(),
            &Config::parse("APP_KEY=hunter2-secret\nAPP_NAME=Site\n"),
        );
        let path = std::env::temp_dir().join(format!("clamp-inspect-{}.json", std::process::id()));
        fs::write(&path, document.to_string()).unwrap();
        let (human, machine, _) =
            inspection("inspect", path.to_str().unwrap(), None, None).unwrap();
        let _ = fs::remove_file(&path);
        assert!(human.ends_with("Config keys: APP_KEY, APP_NAME"));
        assert_eq!(machine["config_keys"], json!(["APP_KEY", "APP_NAME"]));
        assert!(!human.contains("hunter2") && !machine.to_string().contains("hunter2"));
    }

    /// The welcome page quotes `app/routes/api.rs`; a stale copy sends readers
    /// to code the project does not have (#88).
    #[test]
    fn welcome_page_quotes_the_real_api_routes() {
        let page = include_str!("../templates/web/app/resources/views/welcome.html");
        let panel = page
            .split_once("app/routes/api.rs</p>")
            .and_then(|(_, rest)| rest.split_once("</pre>"))
            .map(|(panel, _)| panel)
            .expect("welcome page has an api.rs panel");
        let mut code = String::new();
        let mut in_tag = false;
        for c in panel.chars() {
            match c {
                '<' => in_tag = true,
                '>' if in_tag => in_tag = false,
                c if !in_tag => code.push(c),
                _ => {}
            }
        }
        let code = code
            .replace("&lt;", "<")
            .replace("&gt;", ">")
            .replace("&amp;", "&");
        assert_eq!(
            code.trim(),
            include_str!("../templates/web/app/routes/api.rs").trim()
        );
    }
}