conjure-build 0.3.1

A modern build tool and dependency manager for C and C++.
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
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
//! Command-line interface: argument parsing and the `handle_*` implementations
//! for every subcommand.
//!
//! The clap types at the top are the sole source of the CLI surface; each
//! subcommand's `handle_*` below does the work. Most subcommands operate on the
//! manifest in the current directory (`.conjure.kdl`), while `conjure add`/`rm`
//! edit it in place through `proj_write`'s helpers and `conjure as` re-enters the
//! CLI under an active profile.

/***********************************************************************/

use super::{
  build, compile, git, lock, proj,
  proj_parse::{
    self, Arch, Compile, Flags, Linkage, Profile, Project, ProjectType, slugify,
  },
  proj_write::fix_braces,
  ui::{StepStatus, Ui},
};
use clap::{
  Args, CommandFactory, FromArgMatches, Parser, Subcommand, ValueEnum,
  builder::{Styles, styling::AnsiColor},
};
use kdl::{FormatConfigBuilder, KdlDocument, KdlNode};
use miette::{IntoDiagnostic, Result};
use owo_colors::OwoColorize;
use std::{
  collections::{HashMap, HashSet},
  env, fs,
  path::Path,
};

/***********************************************************************/

const LONG_ABOUT: &str = r#"
Conjure, the modern build-tool for C and C++.
Made by h4rl, for everyone.
"#;

const HELP_TEMPLATE: &str = "{about}\n{usage-heading} {usage}\n\n{all-args}";

#[derive(Clone, ValueEnum, Default)]
enum Language {
  #[value(alias = "c")]
  #[default]
  C,
  #[value(alias = "c++")]
  Cpp,
}

impl std::fmt::Display for Language {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      Language::C => write!(f, "C"),
      Language::Cpp => write!(f, "C++"),
    }
  }
}

#[derive(Clone, ValueEnum, Default)]
enum TypeType {
  #[value(alias = "binary")]
  #[default]
  Binary,
  #[value(alias = "library")]
  Library,
}

impl std::fmt::Display for TypeType {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      TypeType::Binary => write!(f, "binary"),
      TypeType::Library => write!(f, "library"),
    }
  }
}

#[derive(Clone, ValueEnum, Default)]
enum LinkType {
  #[value(alias = "static")]
  Static,
  #[value(alias = "dynamic")]
  #[default]
  Dynamic,
}

impl std::fmt::Display for LinkType {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      LinkType::Static => write!(f, "static"),
      LinkType::Dynamic => write!(f, "dynamic"),
    }
  }
}

#[derive(Clone, ValueEnum, Default)]
enum ArchType {
  #[value(alias = "native")]
  #[default]
  Native,
  #[value(alias = "x86")]
  X86,
  #[value(alias = "x64")]
  X64,
  #[value(alias = "arm64")]
  Arm64,
}

impl std::fmt::Display for ArchType {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      ArchType::Native => write!(f, "native"),
      ArchType::X86 => write!(f, "x86"),
      ArchType::X64 => write!(f, "x64"),
      ArchType::Arm64 => write!(f, "arm64"),
    }
  }
}

#[derive(Clone, ValueEnum, Debug)]
enum Transport {
  #[value(alias = "ssh")]
  Ssh,
  #[value(alias = "https")]
  Https,
}

#[derive(Clone, ValueEnum)]
enum BuildSystem {
  #[value(alias = "make")]
  Make,
  #[value(alias = "cmake")]
  Cmake,
  #[value(alias = "autotools")]
  Autotools,
  #[value(alias = "meson")]
  Meson,
  #[value(alias = "ninja")]
  Ninja,
  #[value(alias = "xmake")]
  Xmake,
  #[value(alias = "just")]
  Just,
}

#[derive(Clone, ValueEnum)]
enum LocalOrRemote {
  #[value(alias = "local")]
  Local,
  #[value(alias = "codeberg")]
  Codeberg,
  #[value(alias = "github")]
  Github,
  #[value(alias = "bitbucket")]
  Bitbucket,
  #[value(alias = "git")]
  Git,
}

#[derive(Args)]
struct NewArgs {
  /// Name of the project
  name: String,
  /// Language of the project
  #[arg(long, short = 'l', value_enum)]
  language: Option<Language>,
  /// Which C/C++ standard to use
  #[arg(long, short = 's')]
  standard: Option<String>,
  /// Compile type of the project
  #[arg(long = "type", value_enum)]
  ty: Option<TypeType>,
  /// Link type of the project
  #[arg(long, value_enum)]
  link: Option<LinkType>,
  /// Force the creation of the project
  #[arg(long)]
  force: bool,
  /// Target architecture
  #[arg(long, short = 'a', value_enum)]
  arch: Option<ArchType>,
}

#[derive(Args)]
struct InitArgs {
  /// Name of the project, if not provided, the current directory's name will be used
  #[arg(long, short = 'n')]
  name: Option<String>,
  /// Language of the project
  #[arg(long, short = 'l', value_enum)]
  language: Option<Language>,
  /// Which C/C++ standard to use
  #[arg(long, short = 's')]
  standard: Option<String>,
  /// Compile type of the project
  #[arg(long = "type", value_enum)]
  ty: Option<TypeType>,
  /// Link type of the project
  #[arg(long, value_enum)]
  link: Option<LinkType>,
  /// Force the creation of the project
  #[arg(long)]
  force: bool,
  /// Target architecture
  #[arg(long, short = 'a', value_enum)]
  arch: Option<ArchType>,
}

#[derive(Args)]
struct AddArgs {
  /// The type of dependency
  local_or_remote: LocalOrRemote,
  /// Path or URL of the dependency
  path_or_url: String,
  /// Build system to use for the dependency
  build: Option<BuildSystem>,
  /// Transport to use for getting the dependency
  #[arg(long, value_enum)]
  transport: Option<Transport>,
  /// Tag, branch, or commit to pin
  #[arg(long, short = 'r')]
  r#ref: Option<String>,
  /// Force the addition of the dependency
  #[arg(long)]
  force: bool,
}

#[derive(Args)]
struct RemoveArgs {
  /// Dependency to remove
  name: String,
}

#[derive(Args)]
struct UpdateArgs {
  /// Dependency to update
  name: Option<String>,
}

#[derive(Args)]
struct BuildArgs {
  /// Build profile to use
  #[arg(long, short = 'p')]
  profile: Option<String>,
  /// Force a build even if nothing has changed
  #[arg(long, short = 'f')]
  force: bool,
  #[arg(long, short = 's')]
  no_siblings: bool,
}

#[derive(Args)]
struct CompileCommandsArgs {
  /// Profile to use for making the compile_commands.json
  #[arg(long, short = 'p')]
  profile: Option<String>,
  /// Don't build siblings
  #[arg(long, short = 's')]
  no_siblings: bool,
}

#[derive(Args)]
struct AsArgs {
  /// Profile to use for the subcommand
  profile: String,
  /// Subcommand to run (all --help commands are supported)
  #[arg(trailing_var_arg = true, allow_hyphen_values = true)]
  subcommand: Vec<String>,
}

#[derive(Args)]
struct TestArgs {
  /// Names of the tests to build (If empty, build all)
  names: Vec<String>,
  /// Profile to use for building the tests
  #[arg(long, short = 'p')]
  profile: Option<String>,
}

#[derive(Subcommand)]
enum Commands {
  /// Create a new Conjure project
  New(NewArgs),
  /// Initialize a Conjure project in the current directory
  Init(InitArgs),
  /// Add a dependency to the project
  Add(AddArgs),
  /// Remove a dependency from the project
  #[command(alias = "remove")]
  Rm(RemoveArgs),
  /// Lock the project's dependencies
  Lock,
  /// Update the project's dependencies, and recache them.
  Update(UpdateArgs),
  /// Build the project
  Build(BuildArgs),
  /// Generate a compile_commands.json for clangd
  #[command(name = "compile-commands")]
  Compile(CompileCommandsArgs),
  /// Run a subcommand as a specific profile
  As(AsArgs),
  /// Build the project's tests
  Test(TestArgs),
}

fn styles() -> Styles {
  Styles::styled()
    .header(AnsiColor::Green.on_default().bold())
    .usage(AnsiColor::Green.on_default().bold())
    .literal(AnsiColor::BrightBlue.on_default())
    .placeholder(AnsiColor::BrightCyan.on_default())
}

#[derive(Parser)]
#[command(version, about, long_about = Some(LONG_ABOUT), help_template = HELP_TEMPLATE)]
#[command(propagate_version = true)]
#[command(styles = styles())]
struct Cli {
  #[command(subcommand)]
  command: Commands,
}

fn project_arch(arch: ArchType) -> Arch {
  match arch {
    ArchType::X86 => Arch::X86,
    ArchType::X64 => Arch::X86_64,
    ArchType::Arm64 => Arch::Arm64,
    ArchType::Native => Arch::Native,
  }
}

fn project_type(ty: Option<TypeType>) -> ProjectType {
  match ty {
    Some(TypeType::Binary) => ProjectType::Binary,
    Some(TypeType::Library) => ProjectType::Library,
    None => ProjectType::default(),
  }
}

fn project_link(link: Option<LinkType>) -> Linkage {
  match link {
    Some(LinkType::Static) => Linkage::Static,
    Some(LinkType::Dynamic) => Linkage::Dynamic,
    None => Linkage::default(),
  }
}

fn default_standard(language: Option<Language>) -> &'static str {
  match language {
    Some(Language::Cpp) => "c++17",
    _ => "c11",
  }
}

/// The one-line success message shared by `new` and `init`, showing the values
/// the project was actually created with (defaults filled in).
fn created_summary(
  name: &str,
  language: Option<Language>,
  standard: Option<String>,
  ty: Option<TypeType>,
  link: Option<LinkType>,
  arch: Option<ArchType>,
) -> String {
  format!(
    "Created project {} with language {}, standard {}, type {}, link {}, and arch {}",
    name.bold().green(),
    language.clone().unwrap_or_default().bold().blue(),
    standard
      .unwrap_or_else(|| default_standard(language).to_string())
      .bold()
      .yellow(),
    ty.unwrap_or_default().bold().purple(),
    link.unwrap_or_default().bold().purple(),
    arch.unwrap_or_default().bold().cyan()
  )
}

/// Assemble the manifest for a freshly created project, including the default
/// `debug`/`release` profiles.
fn build_project(
  name: String,
  language: Option<Language>,
  standard: Option<String>,
  ty: Option<TypeType>,
  link: Option<LinkType>,
  arch: Option<ArchType>,
) -> Project {
  Project {
    name,
    language: match language {
      Some(Language::Cpp) => crate::conjure::proj_parse::Language::Cpp,
      _ => crate::conjure::proj_parse::Language::C,
    },
    compile: Some(Compile {
      standard: Some(
        standard.unwrap_or_else(|| default_standard(language).to_string()),
      ),
      arch: arch.map(project_arch),
      ..Default::default()
    }),
    ty: project_type(ty),
    link: project_link(link),
    siblings: Some(HashMap::new()),
    profiles: Some(HashMap::from([
      (
        "debug".to_string(),
        Profile {
          c_flags: Some(Flags::Append(vec![
            "-g".to_string(),
            "-O0".to_string(),
          ])),
          ld_flags: Some(Flags::Append(vec![
            "-g".to_string(),
            "-O0".to_string(),
          ])),
          ..Default::default()
        },
      ),
      (
        "release".to_string(),
        Profile {
          c_flags: Some(Flags::Append(vec!["-O2".to_string()])),
          ld_flags: Some(Flags::Append(vec![
            "-O2".to_string(),
            "-flto".to_string(),
          ])),
          ..Default::default()
        },
      ),
    ])),
    dependencies: Some(HashMap::new()),
    tests: Some(HashMap::new()),
    ..Default::default()
  }
}

/// Best-effort build-system detection for an added dependency, by marker file.
fn guess_build_system(dir: &Path) -> Option<BuildSystem> {
  for (marker, system) in [
    ("CMakeLists.txt", BuildSystem::Cmake),
    ("meson.build", BuildSystem::Meson),
    ("configure.ac", BuildSystem::Autotools),
    ("configure", BuildSystem::Autotools),
    ("GNUmakefile", BuildSystem::Make),
    ("makefile", BuildSystem::Make),
    ("justfile", BuildSystem::Just),
    ("Justfile", BuildSystem::Just),
    ("Makefile", BuildSystem::Make),
    ("xmake.lua", BuildSystem::Xmake),
    ("build.ninja", BuildSystem::Ninja),
  ] {
    if dir.join(marker).exists() {
      return Some(system);
    }
  }
  None
}

/// Re-apply the quoting KDL parsing discarded.
///
/// `kdl` does not preserve value quoting, so a rewritten manifest would turn a
/// deliberately-quoted `"c"` into bare `c`. `original` is the pre-edit text;
/// every word that was quoted there is re-quoted in `out`.
fn restore_quotes(original: &str, out: &str) -> String {
  let quoted: HashSet<&str> = original.split('"').skip(1).step_by(2).collect();
  if quoted.is_empty() {
    return out.to_string();
  }

  let mut res = String::with_capacity(out.len() + 8);
  for line in out.lines() {
    let lead = line.len() - line.trim_start().len();
    res.push_str(&line[..lead]);
    let mut first = true;
    for (i, seg) in line[lead..].split('"').enumerate() {
      if i % 2 == 1 {
        res.push('"');
        res.push_str(seg);
        res.push('"');
        first = false;
        continue;
      }
      for tok in seg.split_whitespace() {
        if !first && quoted.contains(tok) {
          res.push('"');
          res.push_str(tok);
          res.push('"');
        } else {
          res.push_str(tok);
        }
        res.push(' ');
        first = false;
      }
    }
    if res.ends_with(' ') {
      res.pop();
    }
    res.push('\n');
  }
  res
}

/// Autoformat an edited `conjure.kdl`, restore its quoting, and write it back.
fn save_edit(text: &str, doc: &mut kdl::KdlDocument) -> Result<()> {
  let cfg = FormatConfigBuilder::new().indent("  ").build();
  doc.autoformat_config(&cfg);
  let out = fix_braces(&doc.to_string());
  fs::write("conjure.kdl", restore_quotes(text, &out)).into_diagnostic()?;
  Ok(())
}

fn dependency_exists(name: &str) -> Result<bool> {
  let text = std::fs::read_to_string("conjure.kdl").into_diagnostic()?;
  let doc: KdlDocument = text.parse().into_diagnostic()?;
  Ok(
    doc
      .get("project")
      .and_then(|p| p.children())
      .and_then(|c| c.get("dependencies"))
      .and_then(|d| d.children())
      .is_some_and(|deps| deps.get(name).is_some()),
  )
}

/// Insert or replace a dependency node in `conjure.kdl`, building the node from
/// the CLI args and round-tripping the file through [`save_edit`].
fn edit_dependencies(
  args: &AddArgs,
  name: &str,
  build: Option<String>,
  transport: Option<String>,
) -> Result<()> {
  let text = std::fs::read_to_string("conjure.kdl").into_diagnostic()?;
  let mut doc: kdl::KdlDocument = text.parse().into_diagnostic()?;

  let project = doc.get_mut("project").ok_or_else(|| {
    miette::miette!(
      help = "Generate a conjure.kdl using `conjure init`",
      "No project node in conjure.kdl"
    )
  })?;
  if project.children_mut().is_none() {
    *project.children_mut() = Some(kdl::KdlDocument::new());
  }
  let project_children = project.children_mut().as_mut().unwrap();

  if project_children.get_mut("dependencies").is_none() {
    let mut n = kdl::KdlNode::new("dependencies");
    n.set_children(kdl::KdlDocument::new());
    project_children.nodes_mut().push(n);
  }
  let deps = project_children.get_mut("dependencies").unwrap();
  if deps.children_mut().is_none() {
    *deps.children_mut() = Some(kdl::KdlDocument::new());
  }
  let children = deps.children_mut().as_mut().unwrap();

  miette::ensure!(
    args.force || children.get(name).is_none(),
    miette::miette!(
      help = "Use --force to replace",
      "Dependency `{name}` already exists"
    )
  );

  if children.get(name).is_none() {
    children.nodes_mut().retain(|n| n.name().value() != name);
  }

  let mut frag = format!("{name} {{");
  match &args.local_or_remote {
    LocalOrRemote::Local => {
      frag.push_str(&format!("\n  local \"{}\"", args.path_or_url))
    }
    host => frag.push_str(&format!(
      "\n  remote {} \"{}\"",
      host.to_possible_value().unwrap().get_name(),
      args.path_or_url,
    )),
  }

  if let Some(r) = &args.r#ref {
    frag.push_str(&format!("\n  ref {r}"));
  }

  if let Some(t) = &transport {
    frag.push_str(&format!("\n  transport {t}"));
  }

  if let Some(b) = &build {
    frag.push_str(&format!("\n  build {b}"));
  }

  frag.push_str("\n}");
  let dep_node: KdlNode = frag.parse().into_diagnostic()?;
  children.nodes_mut().push(dep_node);

  save_edit(&text, &mut doc)?;
  Ok(())
}

/// Clone (or reuse) a remote dependency and record its resolved commit in the
/// lockfile. `fetch` selects `conjure update`'s always-refetch behavior.
fn lock_remote(
  ui: &Ui,
  lock: &mut lock::LockFile,
  name: &str,
  dep: &proj_parse::Dependency,
  fetch: bool,
) -> Result<()> {
  let (host, path) = match dep.remote.as_ref() {
    Some(proj_parse::Remote::Codeberg(p)) => ("codeberg", p),
    Some(proj_parse::Remote::GitHub(p)) => ("github", p),
    Some(proj_parse::Remote::BitBucket(p)) => ("bitbucket", p),
    Some(proj_parse::Remote::Git(p)) => ("git", p),
    None => unreachable!(),
  };

  let t = match dep.transport {
    Some(proj_parse::Transport::Ssh) | None => "ssh",
    Some(proj_parse::Transport::Https) => "https",
  };

  let dir = if fetch {
    git::clone_remote(Some(ui), host, path, t, name)?
  } else {
    git::ensure_cloned(Some(ui), &git::remote_url(host, path, t), name)?
  };

  if let Some(r) = &dep.r#ref {
    git::checkout(&dir, r)?;
  }

  let commit = git::resolve_head(&dir)?;
  let r#ref = git::head_ref(&dir)?;
  git::checkout(&dir, &commit)?;

  lock.lock(
    name,
    Some(dep.remote.clone().unwrap()),
    dep.transport,
    r#ref,
    commit,
  );
  Ok(())
}

/// Whether the current manifest declares any profiles (used to hide the `as`
/// subcommand when it would be useless).
fn has_profiles() -> bool {
  Project::from_file("conjure.kdl")
    .ok()
    .and_then(|p| p.profiles)
    .is_some_and(|p| !p.is_empty())
}

fn handle_new(args: &NewArgs) -> Result<()> {
  let proj_path = Path::new(".").join(slugify(&args.name));
  let ui = Ui::new();
  if proj_path.is_dir() {
    miette::ensure!(
      args.force,
      miette::miette!(
        help = "Use --force to overwrite the project",
        "Project `{}` already exists",
        args.name
      )
    );
  }

  let format = created_summary(
    &args.name,
    args.language.clone(),
    args.standard.clone(),
    args.ty.clone(),
    args.link.clone(),
    args.arch.clone(),
  );

  proj::make_proj(
    proj_path,
    build_project(
      args.name.clone(),
      args.language.clone(),
      args.standard.clone(),
      args.ty.clone(),
      args.link.clone(),
      args.arch.clone(),
    ),
  )?;

  ui.println(Some(&StepStatus::Success), format)?;
  Ok(())
}

fn handle_init(args: &InitArgs) -> Result<()> {
  let proj_path = Path::new(".");
  let ui = Ui::new();
  let proj_name = args.name.clone().unwrap_or_else(|| {
    env::current_dir()
      .ok()
      .and_then(|p| p.file_name().map(|s| s.to_string_lossy().to_string()))
      .unwrap_or_default()
  });

  if proj_path.join("project.kdl").exists() {
    miette::ensure!(
      args.force,
      miette::miette!(
        help = "Use --force to overwrite the project",
        "Project `{proj_name}` already exists"
      )
    );
  }

  let format = created_summary(
    &proj_name,
    args.language.clone(),
    args.standard.clone(),
    args.ty.clone(),
    args.link.clone(),
    args.arch.clone(),
  );

  proj::make_proj(
    proj_path,
    build_project(
      proj_name,
      args.language.clone(),
      args.standard.clone(),
      args.ty.clone(),
      args.link.clone(),
      args.arch.clone(),
    ),
  )?;

  ui.println(Some(&StepStatus::Success), format)?;
  Ok(())
}

fn handle_add(args: &AddArgs) -> Result<()> {
  let name = args
    .path_or_url
    .rsplit('/')
    .next()
    .unwrap_or(&args.path_or_url)
    .to_string();

  miette::ensure!(
    args.force || !dependency_exists(&name)?,
    miette::miette!(
      help = "Use --force to replace the dependency",
      "Dependency `{name}` already exists"
    )
  );

  let transport = args.transport.clone().unwrap_or(Transport::Ssh);
  let ui = Ui::new();

  let (dir, build, transport_name) = match &args.local_or_remote {
    LocalOrRemote::Local => (None, None, None),
    host => {
      let host_val = &host.to_possible_value().unwrap();
      let host_name = host_val.get_name();
      let t = transport
        .to_possible_value()
        .unwrap()
        .get_name()
        .to_string();
      let dir =
        git::clone_remote(Some(&ui), host_name, &args.path_or_url, &t, &name)?;
      let build = args
        .build
        .as_ref()
        .map(|b| b.to_possible_value().unwrap().get_name().to_string())
        .or_else(|| {
          guess_build_system(&dir)
            .map(|b| b.to_possible_value().unwrap().get_name().to_string())
        });
      (Some(dir), build, Some(t))
    }
  };

  edit_dependencies(args, &name, build, transport_name.clone())?;

  if let Some(dir) = dir {
    if let Some(r) = &args.r#ref {
      git::checkout(&dir, r)?;
    }
    let commit = git::resolve_head(&dir)?;
    let r#ref = git::head_ref(&dir)?.or_else(|| args.r#ref.clone());
    let remote = Some(match &args.local_or_remote {
      LocalOrRemote::Codeberg => {
        proj_parse::Remote::Codeberg(args.path_or_url.clone())
      }
      LocalOrRemote::Github => {
        proj_parse::Remote::GitHub(args.path_or_url.clone())
      }
      LocalOrRemote::Bitbucket => {
        proj_parse::Remote::BitBucket(args.path_or_url.clone())
      }
      LocalOrRemote::Git => proj_parse::Remote::Git(args.path_or_url.clone()),
      LocalOrRemote::Local => unreachable!(),
    });

    let transport = transport_name
      .map(|s| {
        proj_parse::Transport::try_from(s).map_err(|e| miette::miette!(e))
      })
      .transpose()?;

    let mut lock = lock::LockFile::load("conjure.lock")?;
    lock.lock(&name, remote, transport, r#ref, commit);
    lock.save("conjure.lock")?;
  }

  ui.println(
    Some(&StepStatus::Success),
    format!("Added dependency `{name}`"),
  )?;
  Ok(())
}

fn handle_remove(args: &RemoveArgs) -> Result<()> {
  let text = fs::read_to_string("conjure.kdl").into_diagnostic()?;
  let mut doc: KdlDocument = text.parse().into_diagnostic()?;
  let ui = Ui::new();

  let project = doc.get_mut("project").ok_or_else(|| {
    miette::miette!(
      help = "Use `conjure init` to generate a new conjure.kdl",
      "No project node in conjure.kdl"
    )
  })?;

  let project_children = project
    .children_mut()
    .as_mut()
    .ok_or_else(|| miette::miette!(
      help = "Verify the conjure.kdl to make sure nothing is wrong, try to generate a new one using `conjure init`", 
      "No children in project node, is your project malformed?"
    ))?;

  let deps = project_children.get_mut("dependencies").ok_or_else(|| {
    miette::miette!("No dependency node in project, nothing to remove")
  })?;

  let deps_children = deps.children_mut().as_mut().ok_or_else(|| {
    miette::miette!("No children in dependencies node, nothing to remove")
  })?;

  miette::ensure!(
    deps_children.get(&args.name).is_some(),
    miette::miette!(
      help = "Double check if the dependency exists, if not, use `conjure add` to add it",
      "Dependency `{}` not found",
      args.name
    )
  );

  deps_children
    .nodes_mut()
    .retain(|n| n.name().value() != args.name);
  if deps_children.nodes().is_empty() {
    project_children
      .nodes_mut()
      .retain(|n| n.name().value() != "dependencies");
  }

  save_edit(&text, &mut doc)?;

  if Path::new("conjure.lock").exists() {
    let mut lock = lock::LockFile::load("conjure.lock")?;
    lock.unlock(&args.name);
    lock.save("conjure.lock")?;
  }

  let dir = git::cache_dir().join(&args.name);
  if dir.is_dir() {
    fs::remove_dir_all(dir).into_diagnostic()?;
  }

  ui.println(
    Some(&StepStatus::Success),
    format!("Removed dependency `{}`", args.name),
  )?;
  Ok(())
}

fn handle_lock() -> Result<()> {
  let project = proj_parse::Project::from_file("conjure.kdl")?;
  let mut lock = lock::LockFile::load("conjure.lock")?;
  let ui = Ui::new();
  let mut deps_list: Vec<String> = vec![];

  lock.retain(|name| {
    project
      .dependencies
      .as_ref()
      .is_some_and(|deps| deps.contains_key(name))
  });

  if let Some(deps) = &project.dependencies {
    for (name, dep) in deps.iter().filter(|(_, d)| d.remote.is_some()) {
      deps_list.push(name.clone());
      ui.wrap(format!("Locking {name}"), || {
        lock_remote(&ui, &mut lock, name, dep, false)
      })?;
    }
  } else {
    ui.println(Some(&StepStatus::Failure), "No dependencies to lock")?;
    return Ok(());
  }

  lock.save("conjure.lock")?;
  ui.println(
    Some(&StepStatus::Success),
    format!("Locked dependencies {}", deps_list.join(", ")),
  )?;
  Ok(())
}

fn handle_update(args: &UpdateArgs) -> Result<()> {
  let project = proj_parse::Project::from_file("conjure.kdl")?;
  let mut lock = lock::LockFile::load("conjure.lock")?;
  let ui = Ui::new();

  let names: Vec<String> = lock
    .entries()
    .keys()
    .filter(|name| args.name.as_ref().is_none_or(|want| want == *name))
    .cloned()
    .collect();

  miette::ensure!(
    !names.is_empty(),
    miette::miette!(
      help = "Try to run `conjure lock` to lock the dependencies",
      "Dependencies are not locked"
    )
  );

  for name in names {
    let dep = project
      .dependencies
      .as_ref()
      .and_then(|d| d.get(&name))
      .ok_or_else(|| {
        miette::miette!("dependency `{name}` not found in conjure.kdl")
      })?;
    ui.wrap(format!("updating {name}"), || {
      lock_remote(&ui, &mut lock, &name, dep, true)
    })?;
  }
  lock.save("conjure.lock")?;
  ui.println(Some(&StepStatus::Success), "lockfile updated")?;
  Ok(())
}

fn handle_build(args: &BuildArgs) -> Result<()> {
  let project = Project::from_file("conjure.kdl")?;
  let profile = args
    .profile
    .clone()
    .or_else(|| std::env::var("CONJURE_PROFILE").ok());
  if let Some(name) = profile.as_deref() {
    miette::ensure!(
      project
        .profiles
        .as_ref()
        .is_some_and(|m| m.contains_key(name)),
      "profile `{name}` not found"
    );
  }

  build::build(&project, profile.as_deref(), args.force, !args.no_siblings)
}

fn handle_test(args: &TestArgs) -> Result<()> {
  let project = Project::from_file("conjure.kdl")?;
  let profile = args
    .profile
    .clone()
    .or_else(|| std::env::var("CONJURE_PROFILE").ok());
  if let Some(name) = profile.as_deref() {
    miette::ensure!(
      project
        .profiles
        .as_ref()
        .is_some_and(|m| m.contains_key(name)),
      "profile `{name}` not found"
    );
  }
  build::test(&project, profile.as_deref(), &args.names)
}

fn handle_compile_commands(args: &CompileCommandsArgs) -> Result<()> {
  let project = Project::from_file("conjure.kdl")?;
  let ui = Ui::new();

  let profile = args
    .profile
    .clone()
    .or_else(|| std::env::var("CONJURE_PROFILE").ok());
  if let Some(name) = profile.as_deref() {
    miette::ensure!(
      project
        .profiles
        .as_ref()
        .is_some_and(|m| m.contains_key(name)),
      "profile `{name}` not found"
    );
  }

  compile::compile_commands(&project, profile.as_deref(), !args.no_siblings)?;
  ui.println(
    Some(&StepStatus::Success),
    "Generated compile_commands.json",
  )?;
  Ok(())
}

/// Re-enter the CLI with `CONJURE_PROFILE` set, so the wrapped subcommand runs
/// under `args.profile`. The profile must exist; `CONJURE_PROFILE` is how the
/// subcommand's `handle_*` will see it.
fn handle_as(args: &AsArgs) -> Result<()> {
  let project = Project::from_file("conjure.kdl")?;
  let profiles = project
    .profiles
    .as_ref()
    .filter(|p| !p.is_empty())
    .ok_or_else(|| miette::miette!("no profiles in conjure.kdl"))?;
  miette::ensure!(
    profiles.contains_key(&args.profile),
    "profile `{}` not found",
    args.profile
  );

  unsafe {
    std::env::set_var("CONJURE_PROFILE", args.profile.clone());
  }

  let argv0 = std::env::args().next().unwrap_or_else(|| "conjure".into());
  let inner =
    Cli::parse_from([argv0].into_iter().chain(args.subcommand.iter().cloned()));
  run_command(&inner.command)
}

fn run_command(cmd: &Commands) -> Result<()> {
  match cmd {
    Commands::New(args) => handle_new(args)?,
    Commands::Init(args) => handle_init(args)?,
    Commands::Add(args) => handle_add(args)?,
    Commands::Rm(args) => handle_remove(args)?,
    Commands::Lock => handle_lock()?,
    Commands::Update(args) => handle_update(args)?,
    Commands::Build(args) => handle_build(args)?,
    Commands::Compile(args) => handle_compile_commands(args)?,
    Commands::Test(args) => handle_test(args)?,
    Commands::As(_) => unreachable!(),
  }
  Ok(())
}

fn parse_cli() -> Cli {
  let mut cmd = Cli::command();
  if !has_profiles() {
    cmd = cmd.mut_subcommand("as", |c| c.hide(true));
  }
  let matches = cmd.get_matches();
  Cli::from_arg_matches(&matches).unwrap_or_else(|e| e.exit())
}

/// CLI entry point.
pub fn run() -> Result<(), miette::Error> {
  let cli = parse_cli();

  if let Commands::As(args) = &cli.command {
    return handle_as(args);
  }

  run_command(&cli.command)
}

#[cfg(test)]
mod tests {
  use super::restore_quotes;

  #[test]
  fn restore_quotes_keeps_quoted_words_and_bare_ones() {
    let original = "project {\n  name \"meow2\"\n  language \"c\"\n  compile {\n    cc \"\"\n  }\n  dependencies {\n    cheese {\n      transport ssh\n    }\n  }\n}";
    let out = "project {\n  name meow2\n  language c\n  compile {\n    cc \"\"\n  }\n  dependencies {\n    cheese {\n      transport ssh\n    }\n  }\n}";
    let expected = "project {\n  name \"meow2\"\n  language \"c\"\n  compile {\n    cc \"\"\n  }\n  dependencies {\n    cheese {\n      transport ssh\n    }\n  }\n}\n";
    assert_eq!(restore_quotes(original, out), expected);
  }
}