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
use axoproject::WorkspaceInfo;
use axoproject::{errors::AxoprojectError, platforms::triple_to_display_name};
use camino::Utf8PathBuf;
use cargo_dist_schema::PrRunMode;
use semver::Version;
use serde::Deserialize;

use crate::{
    config::{
        self, CiStyle, CompressionImpl, Config, DistMetadata, InstallerStyle, PublishStyle,
        ZipStyle,
    },
    do_generate,
    errors::{DistError, DistResult, Result},
    GenerateArgs, SortedMap, METADATA_DIST, PROFILE_DIST,
};

/// Arguments for `cargo 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>,
}

/// 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>,
}

/// Run 'cargo dist init'
pub fn do_init(cfg: &Config, args: &InitArgs) -> Result<()> {
    let workspace = config::get_project()?;

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

    let check = console::style("✔".to_string()).for_stderr().green();

    // Init things
    let did_add_profile = init_dist_profile(cfg, &mut workspace_toml)?;

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

    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, &workspace)?;
        MultiDistMetadata {
            workspace: Some(meta),
            packages: SortedMap::new(),
        }
    };

    if let Some(meta) = &multi_meta.workspace {
        let metadata = config::get_toml_metadata(&mut workspace_toml, true);
        apply_dist_to_metadata(metadata, meta);
    }

    eprintln!();

    // Save the workspace toml (potentially an effective no-op if we made no edits)
    config::save_cargo_toml(&workspace.manifest_path, workspace_toml)?;
    if did_add_profile {
        eprintln!("{check} added [profile.dist] to your root Cargo.toml");
    }
    eprintln!("{check} added [workspace.metadata.dist] to your root Cargo.toml");

    // 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 workspace.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_cargo_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::save_cargo_toml(&package.manifest_path, package_toml)?;
            if writing_metadata {
                eprintln!(
                    "{check} added [package.metadata.dist] to {}'s Cargo.toml",
                    package.name
                );
            }
        }
    }

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

    // regenerate anything that needs to be
    if !args.no_generate {
        eprintln!("running 'cargo 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::Document) -> Result<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 cargo-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 'cargo dist' will build with\n")
    }
    dist_profile.or_insert(new_profile);

    Ok(true)
}

/// 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,
    workspace_info: &WorkspaceInfo,
) -> DistResult<DistMetadata> {
    use dialoguer::{Confirm, Input, MultiSelect, Select};
    // Setup [workspace.metadata.dist]
    let has_config = workspace_info
        .cargo_metadata_table
        .as_ref()
        .and_then(|t| t.as_object())
        .map(|t| t.contains_key(METADATA_DIST))
        .unwrap_or(false);
    let mut meta = if has_config {
        config::parse_metadata_table(
            &workspace_info.manifest_path,
            workspace_info.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()),
            // deprecated, default to not emitting it
            rust_toolchain_version: None,
            ci: None,
            installers: None,
            tap: None,
            system_dependencies: None,
            targets: None,
            dist: None,
            include: None,
            auto_includes: None,
            windows_archive: None,
            unix_archive: None,
            npm_scope: None,
            checksum: None,
            precise_builds: None,
            merge_tasks: None,
            fail_fast: None,
            install_path: None,
            features: None,
            default_features: None,
            all_features: None,
            publish_jobs: None,
            publish_prereleases: None,
            create_release: None,
            pr_run_mode: None,
            allow_dirty: None,
            ssldotcom_windows_sign: None,
            msvc_crt_static: 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 = 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()
    };
    // 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();

    // 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 cargo-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 {
        let prompt = format!(
            r#"looks like you deleted the cargo-dist-version key, add it back?
    this is the version of cargo-dist your releases should use
    (you're currently running {})"#,
            current_version
        );
        let default = true;

        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 {
            // Not recommended but technically ok...
        }
    }

    {
        // 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: &str| -> 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);

            // If they have a well-defined repo url and it's github, default enable it
            #[allow(irrefutable_let_patterns)]
            if let CiStyle::Github = item {
                github_key = 0;
                if let Some(repo_url) = &workspace_info.repository_url {
                    if repo_url.contains("github.com") {
                        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) };
    }

    // Enforce repository url right away
    let has_github_ci = meta
        .ci
        .as_ref()
        .map(|ci| ci.contains(&CiStyle::Github))
        .unwrap_or(false);
    if has_github_ci && workspace_info.repository_url.is_none() {
        // If axoproject complained about inconsistency, forward that
        // Massively jank manual implementation of "clone" here because lots of error types
        // (like std::io::Error) don't implement Clone and so axoproject errors can't either
        let conflict = workspace_info.warnings.iter().find_map(|w| {
            if let AxoprojectError::InconsistentRepositoryKey {
                file1,
                url1,
                file2,
                url2,
            } = w
            {
                Some(AxoprojectError::InconsistentRepositoryKey {
                    file1: file1.clone(),
                    url1: url1.clone(),
                    file2: file2.clone(),
                    url2: url2.clone(),
                })
            } else {
                None
            }
        });
        if let Some(inner) = conflict {
            Err(DistError::CantEnableGithubUrlInconsistent { inner })?;
        } else {
            // Otherwise assume no URL
            Err(DistError::CantEnableGithubNoUrl)?;
        }
    }

    if has_github_ci && meta.pr_run_mode.is_none() {
        let default_val = PrRunMode::default();
        let cur_val = meta.pr_run_mode.unwrap_or(default_val);

        // This is intentionally written awkwardly to make you update this!
        //
        // don't forget to add it to 'items' below!
        let desc = |val| match val {
            PrRunMode::Skip => "skip - don't check the release process in PRs",
            PrRunMode::Plan => "plan - run 'cargo dist plan' on PRs (recommended)",
            PrRunMode::Upload => "upload - build and upload an artifacts.zip to the PR (expensive)",
        };
        let items = [PrRunMode::Skip, PrRunMode::Plan, PrRunMode::Upload];

        // Get the index of the current value
        let default = items
            .iter()
            .position(|val| val == &cur_val)
            .expect("someone added a pr_run_mode but forgot to add it to 'init'");

        let prompt = r#"check your release process in pull requests?"#;
        let selection = Select::with_theme(&theme)
            .with_prompt(prompt)
            .items(&items.iter().map(|mode| desc(*mode)).collect::<Vec<_>>())
            .default(default)
            .interact()?;
        eprintln!();

        let result = items[selection];

        // Record that the user made a concrete decision so we don't prompt over and over
        meta.pr_run_mode = Some(result);
    }

    // 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",
            });
        }

        // 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 cargo-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://opensource.axo.dev/cargo-dist/book/installers/homebrew.html"#
                );
            }
        }
    }

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

    // 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 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!();
        }

        // FIXME (#226): If they have an npm installer, force on tar.gz compression
        const TAR_GZ: Option<ZipStyle> = Some(ZipStyle::Tar(CompressionImpl::Gzip));
        if meta.unix_archive != TAR_GZ || meta.windows_archive != TAR_GZ {
            let prompt = r#"the npm installer requires binaries to be distributed as .tar.gz, is that ok?
    otherwise we would distribute your binaries as .zip on windows, .tar.xz everywhere else
    (this is a hopefully temporary limitation of the npm installer's implementation)"#;
            let default = true;
            let force_targz = if args.yes {
                default
            } else {
                let res = Confirm::with_theme(&theme)
                    .with_prompt(prompt)
                    .default(default)
                    .interact()?;
                eprintln!();
                res
            };
            if force_targz {
                meta.unix_archive = TAR_GZ;
                meta.windows_archive = TAR_GZ;
            } else {
                Err(DistError::MustEnableTarGz)?;
            }
        }
    }

    Ok(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,
        rust_toolchain_version,
        dist,
        ci,
        installers,
        tap,
        system_dependencies: _,
        targets,
        include,
        auto_includes,
        windows_archive,
        unix_archive,
        npm_scope,
        checksum,
        precise_builds,
        merge_tasks,
        fail_fast,
        install_path,
        features,
        all_features,
        default_features,
        publish_jobs,
        publish_prereleases,
        create_release,
        pr_run_mode,
        allow_dirty,
        ssldotcom_windows_sign,
        msvc_crt_static,
    } = &meta;

    apply_optional_value(
        table,
        "cargo-dist-version",
        "# The preferred cargo-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,
        "rust-toolchain-version",
        "# The preferred Rust toolchain to use in CI (rustup toolchain syntax)\n",
        rust_toolchain_version.as_deref(),
    );

    apply_string_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_value(
        table,
        "tap",
        "# A GitHub repo to push Homebrew formulas to\n",
        tap.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-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()),
    );

    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,
        "create-release",
        "# Whether cargo-dist should create a Github Release or use an existing draft\n",
        *create_release,
    );

    apply_optional_value(
        table,
        "install-path",
        "# Path that installers should place binaries in\n",
        install_path.as_ref().map(|p| p.to_string()),
    );

    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,
        "publish-jobs",
        "# Publish jobs to run in CI\n",
        publish_jobs.as_ref(),
    );

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

    apply_optional_value(
        table,
        "pr-run-mode",
        "# Publish jobs to run in CI\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()),
    );

    // Finalize the table
    table
        .decor_mut()
        .set_prefix("\n# Config for 'cargo 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));
        table.key_decor_mut(key).unwrap().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<_>>();
        table.insert(key, toml_edit::Item::Value(items.into_iter().collect()));
        table.key_decor_mut(key).unwrap().set_prefix(desc);
    } else {
        table.remove(key);
    }
}