cargo-dist 0.32.0

Shippable application packaging for Rust
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
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
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
use axoasset::{toml_edit, LocalAsset};
use axoproject::{WorkspaceGraph, WorkspaceInfo, WorkspaceKind};
use camino::Utf8PathBuf;
use cargo_dist_schema::TripleNameRef;
use semver::Version;
use serde::Deserialize;

use crate::{
    config::{
        self, CiStyle, Config, DistMetadata, HostingStyle, InstallPathStrategy, InstallerStyle,
        MacPkgConfig, PublishStyle,
    },
    do_generate,
    errors::{DistError, DistResult},
    platform::{triple_to_display_name, MinGlibcVersion},
    GenerateArgs, SortedMap, METADATA_DIST, PROFILE_DIST,
};

/// Arguments for `dist init` ([`do_init`][])
#[derive(Debug)]
pub struct InitArgs {
    /// Whether to auto-accept the default values for interactive prompts
    pub yes: bool,
    /// Don't automatically generate ci
    pub no_generate: bool,
    /// A path to a json file containing values to set in workspace.metadata.dist
    pub with_json_config: Option<Utf8PathBuf>,
    /// Hosts to enable
    pub host: Vec<HostingStyle>,
}

/// Input for --with-json-config
///
/// Contains a DistMetadata for the workspace.metadata.dist and
/// then optionally ones for each package.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
struct MultiDistMetadata {
    /// `[workspace.metadata.dist]`
    workspace: Option<DistMetadata>,
    /// package_name => `[package.metadata.dist]`
    #[serde(default)]
    packages: SortedMap<String, DistMetadata>,
}

fn theme() -> dialoguer::theme::ColorfulTheme {
    dialoguer::theme::ColorfulTheme {
        checked_item_prefix: console::style("  [x]".to_string()).for_stderr().green(),
        unchecked_item_prefix: console::style("  [ ]".to_string()).for_stderr().dim(),
        active_item_style: console::Style::new().for_stderr().cyan().bold(),
        ..dialoguer::theme::ColorfulTheme::default()
    }
}

/// Copy [workspace.metadata.dist] from one workspace to [dist] in another.
fn copy_cargo_workspace_metadata_dist(
    new_workspace: &mut toml_edit::DocumentMut,
    workspace_toml: toml_edit::DocumentMut,
) {
    if let Some(dist) = workspace_toml
        .get("workspace")
        .and_then(|t| t.get("metadata"))
        .and_then(|t| t.get("dist"))
    {
        new_workspace.insert("dist", dist.to_owned());
    }
}

/// Remove [workspace.metadata.dist], if it exists.
fn prune_cargo_workspace_metadata_dist(workspace: &mut toml_edit::DocumentMut) {
    workspace
        .get_mut("workspace")
        .and_then(|ws| ws.get_mut("metadata"))
        .and_then(|metadata_item| metadata_item.as_table_mut())
        .and_then(|table| table.remove("dist"));
}

/// Create a toml-edit document set up for a cargo workspace.
fn new_cargo_workspace() -> toml_edit::DocumentMut {
    let mut new_workspace = toml_edit::DocumentMut::new();

    // Write generic workspace config
    let mut table = toml_edit::table();
    if let Some(t) = table.as_table_mut() {
        let mut array = toml_edit::Array::new();
        array.push("cargo:.");
        t["members"] = toml_edit::value(array);
    }
    new_workspace.insert("workspace", table);

    new_workspace
}

/// Create a toml-edit document set up for a cargo workspace.
fn new_generic_workspace() -> toml_edit::DocumentMut {
    let mut new_workspace = toml_edit::DocumentMut::new();

    // Write generic workspace config
    let mut table = toml_edit::table();
    if let Some(t) = table.as_table_mut() {
        let mut array = toml_edit::Array::new();
        array.push("dist:.");
        t["members"] = toml_edit::value(array);
    }
    new_workspace.insert("workspace", table);

    new_workspace
}

fn do_migrate_from_rust_workspace() -> DistResult<()> {
    let workspaces = config::get_project()?;
    let root_workspace = workspaces.root_workspace();
    let initted = has_metadata_table(root_workspace);

    if root_workspace.kind != WorkspaceKind::Rust {
        // we're not using a Rust workspace, so no migration needed.
        return Ok(());
    }

    if !initted {
        // the workspace hasn't been initialized, so no migration needed.
        return Ok(());
    }

    // Load in the root workspace toml to edit and write back
    let workspace_toml = config::load_toml(&root_workspace.manifest_path)?;
    let mut original_workspace_toml = workspace_toml.clone();

    // Generate a new workspace, then populate it using config from Cargo.toml.
    let mut new_workspace_toml = new_cargo_workspace();
    copy_cargo_workspace_metadata_dist(&mut new_workspace_toml, workspace_toml);

    // Determine config file location.
    let filename = "dist-workspace.toml";
    let destination = root_workspace.workspace_dir.join(filename);

    // Write new config file.
    config::write_toml(&destination, new_workspace_toml)?;

    // We've been asked to migrate away from Cargo.toml; delete what
    // we've added after writing the new config
    prune_cargo_workspace_metadata_dist(&mut original_workspace_toml);
    config::write_toml(&root_workspace.manifest_path, original_workspace_toml)?;

    Ok(())
}

fn do_migrate_from_dist_toml() -> DistResult<()> {
    let workspaces = config::get_project()?;
    let root_workspace = workspaces.root_workspace();
    let initted = has_metadata_table(root_workspace);

    if !initted {
        return Ok(());
    }

    if root_workspace.kind != WorkspaceKind::Generic
        || root_workspace.manifest_path.file_name() != Some("dist.toml")
    {
        return Ok(());
    }

    // OK, now we know we have a root-level dist.toml. Time to fix that.
    let workspace_toml = config::load_toml(&root_workspace.manifest_path)?;

    eprintln!("Migrating tables");
    // Init a generic workspace with the appropriate members
    let mut new_workspace_toml = new_generic_workspace();
    // First copy the [package] section
    if let Some(package) = workspace_toml.get("package") {
        let mut package = package.clone();
        // Ensures we have whitespace between the end of [workspace] and
        // the start of [package]
        if let Some(table) = package.as_table_mut() {
            let decor = table.decor_mut();
            // Try to keep existing comments if we can
            if let Some(desc) = decor.prefix().and_then(|p| p.as_str()) {
                if !desc.starts_with('\n') {
                    decor.set_prefix(format!("\n{desc}"));
                }
            } else {
                decor.set_prefix("\n");
            }
        }
        new_workspace_toml.insert("package", package.to_owned());
    }
    // ...then copy the [dist] section
    if let Some(dist) = workspace_toml.get("dist") {
        new_workspace_toml.insert("dist", dist.to_owned());
    }

    // Finally, write out the new config...
    let filename = "dist-workspace.toml";
    let destination = root_workspace.workspace_dir.join(filename);
    config::write_toml(&destination, new_workspace_toml)?;
    // ...and delete the old config
    LocalAsset::remove_file(&root_workspace.manifest_path)?;

    Ok(())
}

/// Run `dist migrate`
pub fn do_migrate() -> DistResult<()> {
    do_migrate_from_rust_workspace()?;
    do_migrate_from_dist_toml()?;
    //do_migrate_from_v0()?;
    Ok(())
}

/// Run 'dist init'
pub fn do_init(cfg: &Config, args: &InitArgs) -> DistResult<()> {
    // on ctrl-c,  dialoguer/console will clean up the rest of its
    // formatting, but the cursor will remain hidden unless we
    // explicitly go in and show it again
    // See: https://github.com/console-rs/dialoguer/issues/294
    let ctrlc_handler = tokio::spawn(async move {
        tokio::signal::ctrl_c().await.unwrap();

        let term = console::Term::stdout();
        // Ignore the error here if there is any, this is best effort
        let _ = term.show_cursor();

        // Immediately re-exit the process with the same
        // exit code the unhandled ctrl-c would have used
        let exitstatus = if cfg!(windows) {
            0xc000013a_u32 as i32
        } else {
            130
        };
        std::process::exit(exitstatus);
    });

    let workspaces = config::get_project()?;
    let root_workspace = workspaces.root_workspace();
    let check = console::style("".to_string()).for_stderr().green();

    eprintln!("let's setup your dist config...");
    eprintln!();

    // For each [workspace] Cargo.toml in the workspaces, initialize [profile]
    let mut did_add_profile = false;
    for workspace_idx in workspaces.all_workspace_indices() {
        let workspace = workspaces.workspace(workspace_idx);
        if workspace.kind == WorkspaceKind::Rust {
            let mut workspace_toml = config::load_toml(&workspace.manifest_path)?;
            did_add_profile |= init_dist_profile(cfg, &mut workspace_toml)?;
            config::write_toml(&workspace.manifest_path, workspace_toml)?;
        }
    }

    if did_add_profile {
        eprintln!("{check} added [profile.dist] to your workspace Cargo.toml");
    }

    // Load in the root workspace toml to edit and write back
    let workspace_toml = config::load_toml(&root_workspace.manifest_path)?;
    let initted = has_metadata_table(root_workspace);

    if root_workspace.kind == WorkspaceKind::Generic
        && initted
        && root_workspace.manifest_path.file_name() == Some("dist.toml")
    {
        do_migrate()?;
        return do_init(cfg, args);
    }

    // Already-initted users should be asked whether to migrate.
    if root_workspace.kind == WorkspaceKind::Rust && initted && !args.yes {
        let prompt = r#"Would you like to opt in to the new configuration format?
    Future versions of dist will feature major changes to the
    configuration format, including a new dist-specific configuration file."#;
        let is_migrating = dialoguer::Confirm::with_theme(&theme())
            .with_prompt(prompt)
            .default(false)
            .interact()?;

        if is_migrating {
            do_migrate()?;
            return do_init(cfg, args);
        }
    }

    // If this is a Cargo.toml, offer to either write their config to
    // a dist-workspace.toml, or migrate existing config there
    let mut newly_initted_generic = false;
    // Users who haven't initted yet should be opted into the
    // new config format by default.
    let desired_workspace_kind = if root_workspace.kind == WorkspaceKind::Rust && !initted {
        newly_initted_generic = true;
        WorkspaceKind::Generic
    } else {
        root_workspace.kind
    };

    let multi_meta = if let Some(json_path) = &args.with_json_config {
        // json update path, read from a file and apply all requested updates verbatim
        let src = axoasset::SourceFile::load_local(json_path)?;
        let multi_meta: MultiDistMetadata = src.deserialize_json()?;
        multi_meta
    } else {
        // run (potentially interactive) init logic
        let meta = get_new_dist_metadata(cfg, args, &workspaces)?;
        MultiDistMetadata {
            workspace: Some(meta),
            packages: SortedMap::new(),
        }
    };

    // We're past the final dialoguer call; we can remove the
    // ctrl-c handler.
    ctrlc_handler.abort();

    // If we're migrating, the configuration will be missing the
    // generic workspace specification, and will have some
    // extraneous cargo-specific stuff that we don't want.
    let mut workspace_toml = if newly_initted_generic {
        new_cargo_workspace()
    } else {
        workspace_toml
    };

    if let Some(meta) = &multi_meta.workspace {
        apply_dist_to_workspace_toml(&mut workspace_toml, desired_workspace_kind, meta);
    }

    eprintln!();

    let filename;
    let destination;
    if newly_initted_generic {
        // Migrations and newly-initted setups always use dist-workspace.toml.
        filename = "dist-workspace.toml";
        destination = root_workspace.workspace_dir.join(filename);
    } else {
        filename = root_workspace
            .manifest_path
            .file_name()
            .expect("no filename!?");
        destination = root_workspace.manifest_path.to_owned();
    };

    // Save the workspace toml (potentially an effective no-op if we made no edits)
    config::write_toml(&destination, workspace_toml)?;
    let key = if desired_workspace_kind == WorkspaceKind::Rust {
        "[workspace.metadata.dist]"
    } else {
        "[dist]"
    };
    eprintln!("{check} added {key} to your root {filename}");

    // Now that we've done the stuff that's definitely part of the root Cargo.toml,
    // Optionally apply updates to packages
    for (_idx, package) in workspaces.all_packages() {
        // Gather up all the things we'd like to be written to this file
        let meta = multi_meta.packages.get(&package.name);
        let needs_edit = meta.is_some();

        if needs_edit {
            // Ok we have changes to make, let's load the toml
            let mut package_toml = config::load_toml(&package.manifest_path)?;
            let metadata = config::get_toml_metadata(&mut package_toml, false);

            // Apply [package.metadata.dist]
            let mut writing_metadata = false;
            if let Some(meta) = meta {
                apply_dist_to_metadata(metadata, meta);
                writing_metadata = true;
            }

            // Save the result
            config::write_toml(&package.manifest_path, package_toml)?;
            if writing_metadata {
                eprintln!(
                    "{check} added [package.metadata.dist] to {}'s Cargo.toml",
                    package.name
                );
            }
        }
    }

    eprintln!("{check} dist is setup!");
    eprintln!();

    // regenerate anything that needs to be
    if !args.no_generate {
        eprintln!("running 'dist generate' to apply any changes");
        eprintln!();

        let ci_args = GenerateArgs {
            check: false,
            modes: vec![],
        };
        do_generate(cfg, &ci_args)?;
    }
    Ok(())
}

fn init_dist_profile(
    _cfg: &Config,
    workspace_toml: &mut toml_edit::DocumentMut,
) -> DistResult<bool> {
    let profiles = workspace_toml["profile"].or_insert(toml_edit::table());
    if let Some(t) = profiles.as_table_mut() {
        t.set_implicit(true)
    }
    let dist_profile = &mut profiles[PROFILE_DIST];
    if !dist_profile.is_none() {
        return Ok(false);
    }
    let mut new_profile = toml_edit::table();
    {
        // For some detailed discussion, see: https://github.com/axodotdev/cargo-dist/issues/118
        let new_profile = new_profile.as_table_mut().unwrap();
        // We're building for release, so this is a good base!
        new_profile.insert("inherits", toml_edit::value("release"));
        // We're building for SUPER DUPER release, so lto is a good idea to enable!
        //
        // There's a decent argument for lto=true (aka "fat") here but the cost-benefit
        // is a bit complex. Fat LTO can be way more expensive to compute (to the extent
        // that enormous applications like chromium can become unbuildable), but definitely
        // eeks out a bit more from your binaries.
        //
        // In principle dist is targeting True Shippable Binaries and so it's
        // worth it to go nuts getting every last drop out of your binaries... but a lot
        // of people are going to build binaries that might never even be used, so really
        // we're just burning a bunch of CI time for nothing.
        //
        // The user has the freedom to crank this up higher (and/or set codegen-units=1)
        // if they think it's worth it, but we otherwise probably shouldn't set the planet
        // on fire just because Number Theoretically Go Up.
        new_profile.insert("lto", toml_edit::value("thin"));
        new_profile
            .decor_mut()
            .set_prefix("\n# The profile that 'dist' will build with\n")
    }
    dist_profile.or_insert(new_profile);

    Ok(true)
}

fn has_metadata_table(workspace_info: &WorkspaceInfo) -> bool {
    if workspace_info.kind == WorkspaceKind::Rust {
        // Setup [workspace.metadata.dist]
        workspace_info
            .cargo_metadata_table
            .as_ref()
            .and_then(|t| t.as_object())
            .map(|t| t.contains_key(METADATA_DIST))
            .unwrap_or(false)
    } else {
        config::parse_metadata_table_or_manifest(
            &workspace_info.manifest_path,
            workspace_info.dist_manifest_path.as_deref(),
            workspace_info.cargo_metadata_table.as_ref(),
        )
        .is_ok()
    }
}

/// Initialize [workspace.metadata.dist] with default values based on what was passed on the CLI
///
/// Returns whether the initialization was actually done
/// and whether ci was set
fn get_new_dist_metadata(
    cfg: &Config,
    args: &InitArgs,
    workspaces: &WorkspaceGraph,
) -> DistResult<DistMetadata> {
    use dialoguer::{Confirm, Input, MultiSelect};
    let root_workspace = workspaces.root_workspace();
    let has_config = has_metadata_table(root_workspace);

    let mut meta = if has_config {
        config::parse_metadata_table_or_manifest(
            &root_workspace.manifest_path,
            root_workspace.dist_manifest_path.as_deref(),
            root_workspace.cargo_metadata_table.as_ref(),
        )?
    } else {
        DistMetadata {
            // If they init with this version we're gonna try to stick to it!
            cargo_dist_version: Some(std::env!("CARGO_PKG_VERSION").parse().unwrap()),
            cargo_dist_url_override: None,
            // deprecated, default to not emitting it
            rust_toolchain_version: None,
            ci: None,
            installers: None,
            install_success_msg: None,
            tap: None,
            formula: None,
            system_dependencies: None,
            targets: None,
            dist: None,
            include: None,
            auto_includes: None,
            windows_archive: None,
            unix_archive: None,
            npm_scope: None,
            npm_package: None,
            npm_shrinkwrap: None,
            checksum: None,
            precise_builds: None,
            merge_tasks: None,
            fail_fast: None,
            cache_builds: None,
            build_local_artifacts: None,
            dispatch_releases: None,
            release_branch: None,
            install_path: None,
            features: None,
            default_features: None,
            all_features: None,
            plan_jobs: None,
            local_artifacts_jobs: None,
            global_artifacts_jobs: None,
            source_tarball: None,
            recursive_tarball: None,
            host_jobs: None,
            publish_jobs: None,
            post_announce_jobs: None,
            publish_prereleases: None,
            force_latest: None,
            create_release: None,
            github_releases_repo: None,
            github_releases_submodule_path: None,
            github_action_commits: None,
            github_release: None,
            pr_run_mode: None,
            allow_dirty: None,
            ssldotcom_windows_sign: None,
            macos_sign: None,
            github_attestations: None,
            github_attestations_filters: None,
            github_attestations_phase: None,
            msvc_crt_static: None,
            hosting: None,
            extra_artifacts: None,
            github_custom_runners: None,
            github_custom_job_permissions: None,
            bin_aliases: None,
            tag_namespace: None,
            install_updater: None,
            always_use_latest_updater: None,
            display: None,
            display_name: None,
            package_libraries: None,
            install_libraries: None,
            github_build_setup: None,
            mac_pkg_config: None,
            min_glibc_version: None,
            binaries: None,
            cargo_auditable: None,
            cargo_cyclonedx: None,
            omnibor: None,
            simple_download_url: None,
        }
    };

    // Clone this to simplify checking for settings changes
    let orig_meta = meta.clone();

    // Now prompt the user interactively to initialize these...

    // Tune the theming a bit
    let theme = theme();
    // Some indicators we'll use in a few places
    let check = console::style("".to_string()).for_stderr().green();
    let notice = console::style("⚠️".to_string()).for_stderr().yellow();

    if !args.host.is_empty() {
        meta.hosting = Some(args.host.clone());
    }

    // Set cargo-dist-version
    let current_version: Version = std::env!("CARGO_PKG_VERSION").parse().unwrap();
    if let Some(desired_version) = &meta.cargo_dist_version {
        if desired_version != &current_version && !desired_version.pre.starts_with("github-") {
            let default = true;
            let prompt = format!(
                r#"update your project to this version of dist?
    {} => {}"#,
                desired_version, current_version
            );
            let response = if args.yes {
                default
            } else {
                let res = Confirm::with_theme(&theme)
                    .with_prompt(prompt)
                    .default(default)
                    .interact()?;
                eprintln!();
                res
            };

            if response {
                meta.cargo_dist_version = Some(current_version);
            } else {
                Err(DistError::NoUpdateVersion {
                    project_version: desired_version.clone(),
                    running_version: current_version,
                })?;
            }
        }
    } else {
        // Really not allowed, so just force them onto the current version
        meta.cargo_dist_version = Some(current_version);
    }

    {
        // Start with builtin targets
        let default_platforms = crate::default_desktop_targets();
        let mut known = crate::known_desktop_targets();
        // If the config doesn't have targets at all, generate them
        let config_vals = meta.targets.as_deref().unwrap_or(&default_platforms);
        let cli_vals = cfg.targets.as_slice();
        // Add anything custom they did to the list (this will do some reordering if they hand-edited)
        for val in config_vals.iter().chain(cli_vals) {
            if !known.contains(val) {
                known.push(val.clone());
            }
        }

        // Prettify/sort things
        let desc = move |triple: &TripleNameRef| -> String {
            let pretty = triple_to_display_name(triple).unwrap_or("[unknown]");
            format!("{pretty} ({triple})")
        };
        known.sort_by_cached_key(|k| desc(k).to_uppercase());

        let mut defaults = vec![];
        let mut keys = vec![];
        for item in &known {
            // If this target is in their config, keep it
            // If they passed it on the CLI, flip it on
            let config_had_it = config_vals.contains(item);
            let cli_had_it = cli_vals.contains(item);

            let default = config_had_it || cli_had_it;
            defaults.push(default);

            keys.push(desc(item));
        }

        // Prompt the user
        let prompt = r#"what platforms do you want to build for?
    (select with arrow keys and space, submit with enter)"#;
        let selected = if args.yes {
            defaults
                .iter()
                .enumerate()
                .filter_map(|(idx, enabled)| enabled.then_some(idx))
                .collect()
        } else {
            let res = MultiSelect::with_theme(&theme)
                .items(&keys)
                .defaults(&defaults)
                .with_prompt(prompt)
                .interact()?;
            eprintln!();
            res
        };

        // Apply the results
        meta.targets = Some(selected.into_iter().map(|i| known[i].clone()).collect());
    }

    // Enable CI backends
    // FIXME: when there is more than one option we maybe shouldn't hide this
    // once the user has any one enabled, right now it's just annoying to always
    // prompt for Github CI support.
    if meta.ci.as_deref().unwrap_or_default().is_empty() {
        // FIXME: when there is more than one option this should be a proper
        // multiselect like the installer selector is! For now we do
        // most of the multi-select logic and then just give a prompt.
        let known = &[CiStyle::Github];
        let mut defaults = vec![];
        let mut keys = vec![];
        let mut github_key = 0;
        for item in known {
            // If this CI style is in their config, keep it
            // If they passed it on the CLI, flip it on
            let mut default = meta
                .ci
                .as_ref()
                .map(|ci| ci.contains(item))
                .unwrap_or(false)
                || cfg.ci.contains(item);

            // Currently default to enabling github CI because we don't
            // support anything else and we can give a good error later
            #[allow(irrefutable_let_patterns)]
            if let CiStyle::Github = item {
                github_key = 0;
                default = true;
            }
            defaults.push(default);
            // This match is here to remind you to add new CiStyles
            // to `known` above!
            keys.push(match item {
                CiStyle::Github => "github",
            });
        }

        // Prompt the user
        let prompt = r#"enable Github CI and Releases?"#;
        let default = defaults[github_key];

        let github_selected = if args.yes {
            default
        } else {
            let res = Confirm::with_theme(&theme)
                .with_prompt(prompt)
                .default(default)
                .interact()?;
            eprintln!();
            res
        };

        let selected = if github_selected {
            vec![github_key]
        } else {
            vec![]
        };

        // Apply the results
        let ci: Vec<_> = selected.into_iter().map(|i| known[i]).collect();
        meta.ci = if ci.is_empty() { None } else { Some(ci) };
    }

    // Enable installer backends (if they have a CI backend that can provide URLs)
    // FIXME: "vendored" installers like msi could be enabled without any CI...
    let has_ci = meta.ci.as_ref().map(|ci| !ci.is_empty()).unwrap_or(false);
    {
        // If they have CI, then they can use fetching installers,
        // otherwise they can only do vendored installers.
        let known: &[InstallerStyle] = if has_ci {
            &[
                InstallerStyle::Shell,
                InstallerStyle::Powershell,
                InstallerStyle::Npm,
                InstallerStyle::Homebrew,
                InstallerStyle::Msi,
            ]
        } else {
            eprintln!("{notice} no CI backends enabled, most installers have been hidden");
            &[InstallerStyle::Msi]
        };
        let mut defaults = vec![];
        let mut keys = vec![];
        for item in known {
            // If this CI style is in their config, keep it
            // If they passed it on the CLI, flip it on
            let config_had_it = meta
                .installers
                .as_deref()
                .unwrap_or_default()
                .contains(item);
            let cli_had_it = cfg.installers.contains(item);

            let default = config_had_it || cli_had_it;
            defaults.push(default);

            // This match is here to remind you to add new InstallerStyles
            // to `known` above!
            keys.push(match item {
                InstallerStyle::Shell => "shell",
                InstallerStyle::Powershell => "powershell",
                InstallerStyle::Npm => "npm",
                InstallerStyle::Homebrew => "homebrew",
                InstallerStyle::Msi => "msi",
                InstallerStyle::Pkg => "pkg",
            });
        }

        // Prompt the user
        let prompt = r#"what installers do you want to build?
    (select with arrow keys and space, submit with enter)"#;
        let selected = if args.yes {
            defaults
                .iter()
                .enumerate()
                .filter_map(|(idx, enabled)| enabled.then_some(idx))
                .collect()
        } else {
            let res = MultiSelect::with_theme(&theme)
                .items(&keys)
                .defaults(&defaults)
                .with_prompt(prompt)
                .interact()?;
            eprintln!();
            res
        };

        // Apply the results
        meta.installers = Some(selected.into_iter().map(|i| known[i]).collect());
    }

    let mut publish_jobs = orig_meta.publish_jobs.clone().unwrap_or(vec![]);

    // Special handling of the Homebrew installer
    if meta
        .installers
        .as_deref()
        .unwrap_or_default()
        .contains(&InstallerStyle::Homebrew)
    {
        let homebrew_is_new = !orig_meta
            .installers
            .as_deref()
            .unwrap_or_default()
            .contains(&InstallerStyle::Homebrew);

        if homebrew_is_new {
            let prompt = r#"you've enabled Homebrew support; if you want dist
    to automatically push package updates to a tap (repository) for you,
    please enter the tap name (in GitHub owner/name format)"#;
            let default = "".to_string();

            let tap: String = if args.yes {
                default
            } else {
                let res = Input::with_theme(&theme)
                    .with_prompt(prompt)
                    .allow_empty(true)
                    .interact_text()?;
                eprintln!();
                res
            };
            let tap = tap.trim();
            if tap.is_empty() {
                eprintln!("Homebrew packages will not be automatically published");
                meta.tap = None;
            } else {
                meta.tap = Some(tap.to_owned());
                publish_jobs.push(PublishStyle::Homebrew);

                eprintln!("{check} Homebrew package will be published to {tap}");

                eprintln!(
                    r#"{check} You must provision a GitHub token and expose it as a secret named
    HOMEBREW_TAP_TOKEN in GitHub Actions. For more information,
    see the documentation:
    https://axodotdev.github.io/cargo-dist/book/installers/homebrew.html"#
                );
            }
        }
    } else {
        let homebrew_toggled_off = orig_meta
            .installers
            .as_deref()
            .unwrap_or_default()
            .contains(&InstallerStyle::Homebrew);
        if homebrew_toggled_off {
            meta.tap = None;
            publish_jobs.retain(|job| job != &PublishStyle::Homebrew);
        }
    }

    // Special handling of the npm installer
    if meta
        .installers
        .as_deref()
        .unwrap_or_default()
        .contains(&InstallerStyle::Npm)
    {
        // If npm is being newly enabled here, prompt for a @scope
        let npm_is_new = !orig_meta
            .installers
            .as_deref()
            .unwrap_or_default()
            .contains(&InstallerStyle::Npm);
        if npm_is_new {
            let prompt = r#"you've enabled npm support, please enter the @scope you want to use
    this is the "namespace" the package will be published under
    (leave blank to publish globally)"#;
            let default = "".to_string();

            let scope: String = if args.yes {
                default
            } else {
                let res = Input::with_theme(&theme)
                    .with_prompt(prompt)
                    .allow_empty(true)
                    .validate_with(|v: &String| {
                        let v = v.trim();
                        if v.is_empty() {
                            Ok(())
                        } else if v != v.to_ascii_lowercase() {
                            Err("npm scopes must be lowercase")
                        } else if let Some(v) = v.strip_prefix('@') {
                            if v.is_empty() {
                                Err("@ must be followed by something")
                            } else {
                                Ok(())
                            }
                        } else {
                            Err("npm scopes must start with @")
                        }
                    })
                    .interact_text()?;
                eprintln!();
                res
            };
            let scope = scope.trim();
            if scope.is_empty() {
                eprintln!("{check} npm packages will be published globally");
                meta.npm_scope = None;
            } else {
                meta.npm_scope = Some(scope.to_owned());
                eprintln!("{check} npm packages will be published under {scope}");
            }
            eprintln!();
        }
    } else {
        let npm_toggled_off = orig_meta
            .installers
            .as_deref()
            .unwrap_or_default()
            .contains(&InstallerStyle::Npm);
        if npm_toggled_off {
            meta.npm_scope = None;
            publish_jobs.retain(|job| job != &PublishStyle::Npm);
        }
    }

    meta.publish_jobs = if publish_jobs.is_empty() {
        None
    } else {
        Some(publish_jobs)
    };

    if orig_meta.install_updater.is_none()
        && meta
            .installers
            .as_deref()
            .unwrap_or_default()
            .iter()
            .any(|installer| {
                installer == &InstallerStyle::Shell || installer == &InstallerStyle::Powershell
            })
    {
        let default = false;
        let install_updater = if args.yes {
            default
        } else {
            let prompt = r#"Would you like to include an updater program with your binaries?"#;
            let res = Confirm::with_theme(&theme)
                .with_prompt(prompt)
                .default(default)
                .interact()?;
            eprintln!();

            res
        };

        meta.install_updater = Some(install_updater);
    }

    Ok(meta)
}

/// Update a workspace toml-edit document with the current DistMetadata value
pub(crate) fn apply_dist_to_workspace_toml(
    workspace_toml: &mut toml_edit::DocumentMut,
    workspace_kind: WorkspaceKind,
    meta: &DistMetadata,
) {
    let metadata = if workspace_kind == WorkspaceKind::Rust {
        // Write to metadata table
        config::get_toml_metadata(workspace_toml, true)
    } else {
        // Write to document root
        workspace_toml.as_item_mut()
    };
    apply_dist_to_metadata(metadata, meta);
}

/// Ensure [*.metadata.dist] has the given values
fn apply_dist_to_metadata(metadata: &mut toml_edit::Item, meta: &DistMetadata) {
    let dist_metadata = &mut metadata[METADATA_DIST];

    // If there's no table, make one
    if !dist_metadata.is_table() {
        *dist_metadata = toml_edit::table();
    }

    // Apply formatted/commented values
    let table = dist_metadata.as_table_mut().unwrap();

    // This is intentionally written awkwardly to make you update this
    let DistMetadata {
        cargo_dist_version,
        cargo_dist_url_override,
        rust_toolchain_version,
        dist,
        ci,
        installers,
        install_success_msg,
        tap,
        formula,
        targets,
        include,
        auto_includes,
        windows_archive,
        unix_archive,
        npm_scope,
        npm_package,
        npm_shrinkwrap,
        checksum,
        precise_builds,
        merge_tasks,
        fail_fast,
        cache_builds,
        build_local_artifacts,
        dispatch_releases,
        release_branch,
        install_path,
        features,
        all_features,
        default_features,
        plan_jobs,
        local_artifacts_jobs,
        global_artifacts_jobs,
        source_tarball,
        recursive_tarball,
        host_jobs,
        publish_jobs,
        post_announce_jobs,
        publish_prereleases,
        force_latest,
        create_release,
        github_releases_repo,
        github_releases_submodule_path,
        pr_run_mode,
        allow_dirty,
        ssldotcom_windows_sign,
        macos_sign,
        github_attestations,
        github_attestations_filters,
        github_attestations_phase,
        msvc_crt_static,
        hosting,
        tag_namespace,
        install_updater,
        always_use_latest_updater,
        display,
        display_name,
        github_release,
        package_libraries,
        install_libraries,
        mac_pkg_config,
        min_glibc_version,
        cargo_auditable,
        cargo_cyclonedx,
        simple_download_url,
        omnibor,
        // These settings are complex enough that we don't support editing them in init
        github_action_commits: _,
        extra_artifacts: _,
        github_custom_runners: _,
        github_custom_job_permissions: _,
        bin_aliases: _,
        system_dependencies: _,
        github_build_setup: _,
        binaries: _,
    } = &meta;

    // Forcibly inline the default install_path if not specified,
    // and if we've specified a shell or powershell installer
    let install_path = if install_path.is_none()
        && installers
            .as_ref()
            .map(|i| {
                i.iter()
                    .any(|el| matches!(el, InstallerStyle::Shell | InstallerStyle::Powershell))
            })
            .unwrap_or(false)
    {
        Some(InstallPathStrategy::default_list())
    } else {
        install_path.clone()
    };

    apply_optional_value(
        table,
        "cargo-dist-version",
        "# The preferred dist version to use in CI (Cargo.toml SemVer syntax)\n",
        cargo_dist_version.as_ref().map(|v| v.to_string()),
    );

    apply_optional_value(
        table,
        "cargo-dist-url-override",
        "# A URL to use to install `cargo-dist` (with the installer script)\n",
        cargo_dist_url_override.as_ref().map(|v| v.to_string()),
    );

    apply_optional_value(
        table,
        "rust-toolchain-version",
        "# The preferred Rust toolchain to use in CI (rustup toolchain syntax)\n",
        rust_toolchain_version.as_deref(),
    );

    apply_string_or_list(table, "ci", "# CI backends to support\n", ci.as_ref());

    apply_string_list(
        table,
        "installers",
        "# The installers to generate for each app\n",
        installers.as_ref(),
    );

    apply_optional_mac_pkg(
        table,
        "mac-pkg-config",
        "\n# Configuration for the Mac .pkg installer\n",
        mac_pkg_config.as_ref(),
    );

    apply_optional_value(
        table,
        "tap",
        "# A GitHub repo to push Homebrew formulas to\n",
        tap.clone(),
    );

    apply_optional_value(
        table,
        "formula",
        "# Customize the Homebrew formula name\n",
        formula.clone(),
    );

    apply_string_list(
        table,
        "targets",
        "# Target platforms to build apps for (Rust target-triple syntax)\n",
        targets.as_ref(),
    );

    apply_optional_value(
        table,
        "dist",
        "# Whether to consider the binaries in a package for distribution (defaults true)\n",
        *dist,
    );

    apply_string_list(
        table,
        "include",
        "# Extra static files to include in each App (path relative to this Cargo.toml's dir)\n",
        include.as_ref(),
    );

    apply_optional_value(
        table,
        "auto-includes",
        "# Whether to auto-include files like READMEs, LICENSEs, and CHANGELOGs (default true)\n",
        *auto_includes,
    );

    apply_optional_value(
        table,
        "windows-archive",
        "# The archive format to use for windows builds (defaults .zip)\n",
        windows_archive.map(|a| a.ext()),
    );

    apply_optional_value(
        table,
        "unix-archive",
        "# The archive format to use for non-windows builds (defaults .tar.xz)\n",
        unix_archive.map(|a| a.ext()),
    );

    apply_optional_value(
        table,
        "npm-package",
        "# The npm package should have this name\n",
        npm_package.as_deref(),
    );

    apply_optional_value(
        table,
        "npm-shrinkwrap",
        "# Whether to produce an npm lockfile\n",
        *npm_shrinkwrap,
    );

    apply_optional_value(
        table,
        "install-success-msg",
        "# Custom message to display on successful install\n",
        install_success_msg.as_deref(),
    );

    apply_optional_value(
        table,
        "npm-scope",
        "# A namespace to use when publishing this package to the npm registry\n",
        npm_scope.as_deref(),
    );

    apply_optional_value(
        table,
        "checksum",
        "# Checksums to generate for each App\n",
        checksum.map(|c| c.ext().as_str()),
    );

    apply_optional_value(
        table,
        "precise-builds",
        "# Build only the required packages, and individually\n",
        *precise_builds,
    );

    apply_optional_value(
        table,
        "merge-tasks",
        "# Whether to run otherwise-parallelizable tasks on the same machine\n",
        *merge_tasks,
    );

    apply_optional_value(
        table,
        "fail-fast",
        "# Whether failing tasks should make us give up on all other tasks\n",
        *fail_fast,
    );

    apply_optional_value(
        table,
        "cache-builds",
        "# Whether builds should try to be cached in CI\n",
        *cache_builds,
    );

    apply_optional_value(
        table,
        "build-local-artifacts",
        "# Whether CI should include auto-generated code to build local artifacts\n",
        *build_local_artifacts,
    );

    apply_optional_value(
        table,
        "dispatch-releases",
        "# Whether CI should trigger releases with dispatches instead of tag pushes\n",
        *dispatch_releases,
    );

    apply_optional_value(
        table,
        "release-branch",
        "# Trigger releases on pushes to this branch instead of tag pushes\n",
        release_branch.as_ref(),
    );

    apply_optional_value(
        table,
        "create-release",
        "# Whether dist should create a Github Release or use an existing draft\n",
        *create_release,
    );

    apply_optional_value(
        table,
        "github-release",
        "# Which phase dist should use to create the GitHub release\n",
        github_release.as_ref().map(|a| a.to_string()),
    );

    apply_optional_value(
        table,
        "github-releases-repo",
        "# Publish GitHub Releases to this repo instead\n",
        github_releases_repo.as_ref().map(|a| a.to_string()),
    );

    apply_optional_value(
        table,
        "github-releases-submodule-path",
        "# Read the commit to be tagged from the submodule at this path\n",
        github_releases_submodule_path
            .as_ref()
            .map(|a| a.to_string()),
    );

    apply_string_or_list(
        table,
        "install-path",
        "# Path that installers should place binaries in\n",
        install_path.as_ref(),
    );

    apply_string_list(
        table,
        "features",
        "# Features to pass to cargo build\n",
        features.as_ref(),
    );

    apply_optional_value(
        table,
        "default-features",
        "# Whether default-features should be enabled with cargo build\n",
        *default_features,
    );

    apply_optional_value(
        table,
        "all-features",
        "# Whether to pass --all-features to cargo build\n",
        *all_features,
    );

    apply_string_list(
        table,
        "plan-jobs",
        "# Plan jobs to run in CI\n",
        plan_jobs.as_ref(),
    );

    apply_string_list(
        table,
        "local-artifacts-jobs",
        "# Local artifacts jobs to run in CI\n",
        local_artifacts_jobs.as_ref(),
    );

    apply_string_list(
        table,
        "global-artifacts-jobs",
        "# Global artifacts jobs to run in CI\n",
        global_artifacts_jobs.as_ref(),
    );

    apply_optional_value(
        table,
        "source-tarball",
        "# Generate and dist a source tarball\n",
        *source_tarball,
    );

    apply_optional_value(
        table,
        "recursive-tarball",
        "# Whether source tarballs should include submodules\n",
        *recursive_tarball,
    );

    apply_string_list(
        table,
        "host-jobs",
        "# Host jobs to run in CI\n",
        host_jobs.as_ref(),
    );

    apply_string_list(
        table,
        "publish-jobs",
        "# Publish jobs to run in CI\n",
        publish_jobs.as_ref(),
    );

    apply_string_list(
        table,
        "post-announce-jobs",
        "# Post-announce jobs to run in CI\n",
        post_announce_jobs.as_ref(),
    );

    apply_optional_value(
        table,
        "publish-prereleases",
        "# Whether to publish prereleases to package managers\n",
        *publish_prereleases,
    );

    apply_optional_value(
        table,
        "force-latest",
        "# Always mark releases as latest, ignoring semver semantics\n",
        *force_latest,
    );

    apply_optional_value(
        table,
        "pr-run-mode",
        "# Which actions to run on pull requests\n",
        pr_run_mode.as_ref().map(|m| m.to_string()),
    );

    apply_string_list(
        table,
        "allow-dirty",
        "# Skip checking whether the specified configuration files are up to date\n",
        allow_dirty.as_ref(),
    );

    apply_optional_value(
        table,
        "msvc-crt-static",
        "# Whether +crt-static should be used on msvc\n",
        *msvc_crt_static,
    );

    apply_optional_value(
        table,
        "ssldotcom-windows-sign",
        "",
        ssldotcom_windows_sign.as_ref().map(|p| p.to_string()),
    );

    apply_optional_value(
        table,
        "macos-sign",
        "# Whether to sign macOS executables\n",
        *macos_sign,
    );

    apply_optional_value(
        table,
        "github-attestations",
        "# Whether to enable GitHub Attestations\n",
        *github_attestations,
    );

    apply_string_or_list(
        table,
        "github-attestations-filters",
        "# Patterns to attest when creating attestations for release artifacts\n",
        github_attestations_filters.as_ref(),
    );

    apply_optional_value(
        table,
        "github-attestations-phase",
        "# When to generate GitHub Attestations\n",
        github_attestations_phase.as_ref().map(|p| p.to_string()),
    );

    apply_string_or_list(
        table,
        "hosting",
        "# Where to host releases\n",
        hosting.as_ref(),
    );

    apply_optional_value(
        table,
        "tag-namespace",
        "# A prefix git tags must include for dist to care about them\n",
        tag_namespace.as_ref(),
    );

    apply_optional_value(
        table,
        "install-updater",
        "# Whether to install an updater program\n",
        *install_updater,
    );

    apply_optional_value(
        table,
        "always-use-latest-updater",
        "# Whether to always use the latest updater instead of a specific known-good version\n",
        *always_use_latest_updater,
    );

    apply_optional_value(
        table,
        "display",
        "# Whether to display this app's installers/artifacts in release bodies\n",
        *display,
    );

    apply_optional_value(
        table,
        "display-name",
        "# Custom display name to use for this app in release bodies\n",
        display_name.as_ref(),
    );

    apply_string_or_list(
        table,
        "package-libraries",
        "# Which kinds of built libraries to include in the final archives\n",
        package_libraries.as_ref(),
    );

    apply_string_or_list(
        table,
        "install-libraries",
        "# Which kinds of packaged libraries to install\n",
        install_libraries.as_ref(),
    );

    apply_optional_min_glibc_version(
        table,
        "min-glibc-version",
        "# The minimum glibc version supported by the package (overrides auto-detection)\n",
        min_glibc_version.as_ref(),
    );

    apply_optional_value(
        table,
        "cargo-auditable",
        "# Whether to embed dependency information using cargo-auditable\n",
        *cargo_auditable,
    );

    apply_optional_value(
        table,
        "cargo-cyclonedx",
        "# Whether to use cargo-cyclonedx to generate an SBOM\n",
        *cargo_cyclonedx,
    );

    apply_optional_value(
        table,
        "omnibor",
        "# Whether to use omnibor-cli to generate OmniBOR Artifact IDs\n",
        *omnibor,
    );

    apply_optional_value(
        table,
        "simple-download-url",
        "# A URL to download artifacts from\n",
        simple_download_url.clone(),
    );

    // Finalize the table
    table.decor_mut().set_prefix("\n# Config for 'dist'\n");
}

/// Update the toml table to add/remove this value
///
/// If the value is Some we will set the value and hang a description comment off of it.
/// If the given key already existed in the table, this will update it in place and overwrite
/// whatever comment was above it. If the given key is new, it will appear at the end of the
/// table.
///
/// If the value is None, we delete it (and any comment above it).
fn apply_optional_value<I>(table: &mut toml_edit::Table, key: &str, desc: &str, val: Option<I>)
where
    I: Into<toml_edit::Value>,
{
    if let Some(val) = val {
        table.insert(key, toml_edit::value(val));
        if let Some(mut key) = table.key_mut(key) {
            key.leaf_decor_mut().set_prefix(desc)
        }
    } else {
        table.remove(key);
    }
}

/// Same as [`apply_optional_value`][] but with a list of items to `.to_string()`
fn apply_string_list<I>(table: &mut toml_edit::Table, key: &str, desc: &str, list: Option<I>)
where
    I: IntoIterator,
    I::Item: std::fmt::Display,
{
    if let Some(list) = list {
        let items = list.into_iter().map(|i| i.to_string()).collect::<Vec<_>>();
        let array: toml_edit::Array = items.into_iter().collect();
        // FIXME: Break the array up into multiple lines with pretty formatting
        // if the list is "too long". Alternatively, more precisely toml-edit
        // the existing value so that we can preserve the user's formatting and comments.
        table.insert(key, toml_edit::Item::Value(toml_edit::Value::Array(array)));
        if let Some(mut key) = table.key_mut(key) {
            key.leaf_decor_mut().set_prefix(desc)
        }
    } else {
        table.remove(key);
    }
}

/// Same as [`apply_string_list`][] but when the list can be shorthanded as a string
fn apply_string_or_list<I>(table: &mut toml_edit::Table, key: &str, desc: &str, list: Option<I>)
where
    I: IntoIterator,
    I::Item: std::fmt::Display,
{
    if let Some(list) = list {
        let items = list.into_iter().map(|i| i.to_string()).collect::<Vec<_>>();
        if items.len() == 1 {
            apply_optional_value(table, key, desc, items.into_iter().next())
        } else {
            apply_string_list(table, key, desc, Some(items))
        }
    } else {
        table.remove(key);
    }
}

/// Similar to [`apply_optional_value`][] but specialized to `MacPkgConfig`, since we're not able to work with structs dynamically
fn apply_optional_mac_pkg(
    table: &mut toml_edit::Table,
    key: &str,
    desc: &str,
    val: Option<&MacPkgConfig>,
) {
    if let Some(mac_pkg_config) = val {
        let MacPkgConfig {
            identifier,
            install_location,
        } = mac_pkg_config;

        let new_item = &mut table[key];
        let mut new_table = toml_edit::table();
        if let Some(new_table) = new_table.as_table_mut() {
            apply_optional_value(
                new_table,
                "identifier",
                "# A unique identifier, in tld.domain.package format\n",
                identifier.as_ref(),
            );
            apply_optional_value(
                new_table,
                "install-location",
                "# The location to which the software should be installed\n",
                install_location.as_ref(),
            );
            new_table.decor_mut().set_prefix(desc);
        }
        new_item.or_insert(new_table);
    } else {
        table.remove(key);
    }
}

/// Similar to [`apply_optional_value`][] but specialized to `MinGlibcVersion`, since we're not able to work with structs dynamically
fn apply_optional_min_glibc_version(
    table: &mut toml_edit::Table,
    key: &str,
    desc: &str,
    val: Option<&MinGlibcVersion>,
) {
    if let Some(min_glibc_version) = val {
        let new_item = &mut table[key];
        let mut new_table = toml_edit::table();
        if let Some(new_table) = new_table.as_table_mut() {
            for (target, version) in min_glibc_version {
                new_table.insert(target, toml_edit::Item::Value(version.to_string().into()));
            }
            new_table.decor_mut().set_prefix(desc);
        }
        new_item.or_insert(new_table);
    } else {
        table.remove(key);
    }
}