nsh-cli 0.7.1

The nsh command line tool: build, package and run apps.
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
//! nshm (NSH modules): packages that extend Native Shell apps, as crates
//! extend Rust programs. An app lists them in `nsh.json` `"dependencies"`
//! by package name; `nsh add`, `nsh remove` and `nsh upgrade` edit that
//! list:
//!
//! ```json
//! { "dependencies": {
//!   "nsh-omarchy": "^0.3",
//!   "nsh-extra": { "version": "^0.6", "features": ["camera"] },
//!   "acme-ui": { "github": "acme/ui", "version": "^1.4" },
//!   "my-gauge": { "path": "../my-gauge" }
//! } }
//! ```
//!
//! Two kinds, one model ([`manifest`]):
//!
//! - A **Rust nshm** is a Cargo library with a `#[native_module]` at its
//!   root, published on crates.io (or Git). It is linked into the app's
//!   hosts like the app's own crates ([`crate::rust_app`]), so the app then
//!   builds with Rust.
//! - A **TSX nshm** is TSX in a Git repository (versions are its tags,
//!   `v1.4.0`), described by its `nsh.json` `"exports"`. The bundler
//!   compiles it into the app's bundle (`nsh_bundler::nshm`): no Rust, and
//!   it ships in the app's packages and over-the-air updates.
//!
//! An nshm may be both, depend on other nshm (its own `"dependencies"`) and
//! provide a target (`"platform"`, [`platform_program`]).
//!
//! [`resolve`] settles one version of each package, transitively, and
//! `nsh.lock` beside `nsh.json` pins it (the app commits both); a build
//! takes only what the lock pins. Each version is installed once in
//! `~/.nsh/nshms/` ([`fetch`]) and shared by every app on the machine.
//!
//! nsh's own nshm (`nsh-extra`, [`rust_app::OWN_NSHM`]) are released with
//! nsh: one asked for by version is the one of the nsh the app builds with
//! ([`rust_app::Nsh`]), from where that nsh's crates come: crates.io at its
//! version, its nightly's commit, or its checkout.

mod cargo_toml;
mod dep;
mod fetch;
mod manifest;
mod resolve;

use std::{
    fs,
    path::{Path, PathBuf},
    process::Command,
};

use anyhow::{Context as _, bail};
pub(crate) use cargo_toml::relative;
pub(crate) use dep::Added;
use dep::{Dep, Origin};
use resolve::{Lock, Resolved};

use crate::rust_app::{self, AppCrate, Nsh};

/// Gives the nsh the app builds with ([`Nsh::for_app`]), asked only when one
/// of nsh's own nshm needs it.
pub(crate) type NshOf<'a> = dyn Fn() -> anyhow::Result<Nsh> + 'a;

/// The lock file beside `nsh.json`.
pub(crate) const LOCK: &str = nsh_bundler::nshm::LOCK;

/// A Cargo-style status line on stderr: `    Updating crates.io index`.
pub(crate) fn status(verb: &str, what: impl std::fmt::Display) {
    crate::ui::plain(format!(
        "{} {what}",
        crate::ui::green(format!("{verb:>12}"))
    ));
}

/// A status line to heed, in the same column: `        Note …`.
fn notice(verb: &str, what: impl std::fmt::Display) {
    crate::ui::plain(format!(
        "{} {what}",
        crate::ui::yellow(format!("{verb:>12}"))
    ));
}

/// Where nshm are installed.
fn root() -> anyhow::Result<PathBuf> {
    nsh_bundler::nshm::installed_dir().context("no home directory to install nshm in: set NSH_HOME")
}

/// The app's nshm, resolved where the lock no longer fits, installed and
/// locked (`nsh.lock` written when it changed). `config` is the `nsh.json`
/// in `dir`; `nsh` the nsh it builds with.
fn ensure(dir: &Path, config: &serde_json::Value, nsh: &NshOf) -> anyhow::Result<Vec<Resolved>> {
    let deps = dep::read_all(config)?;
    let lock = Lock::read(dir)?;
    if deps.is_empty() && lock.packages.is_empty() {
        return Ok(Vec::new());
    }
    let mut net = fetch::Net::new(root()?);
    let resolved = resolve::resolve(dir, &deps, &lock, &|_| false, nsh, &mut net)?;
    write_lock(dir, &lock, &resolved)?;
    Ok(resolved)
}

/// Writes the lock of `resolved` when it differs from `old`.
fn write_lock(dir: &Path, old: &Lock, resolved: &[Resolved]) -> anyhow::Result<()> {
    let new = Lock::of(resolved);
    if new != *old || !dir.join(LOCK).is_file() {
        if new.packages.is_empty() && !dir.join(LOCK).is_file() {
            return Ok(());
        }
        new.write(dir)?;
    }
    Ok(())
}

/// The id the generated workspace patches a Rust nshm by
/// ([`patch_table`]): its URL, its crate's name or its directory.
fn patch_id(r: &Resolved) -> String {
    match &r.origin {
        Origin::Registry => r.locked.name.clone(),
        Origin::Git { url, .. } => url.clone(),
        Origin::Path(p) => p.clone(),
    }
}

/// The Rust nshm of the app in `dir` (`config` its `nsh.json`, `nsh` the
/// nsh it builds with), each installed at its locked version, with how the
/// workspace patches it: its TSX nshm are the bundler's. Resolved and
/// locked first where needed.
pub(crate) fn crates(
    dir: &Path,
    config: &serde_json::Value,
    nsh: &NshOf,
) -> anyhow::Result<Vec<(String, AppCrate)>> {
    let deps = dep::read_all(config)?;
    let resolved = ensure(dir, config, nsh)?;
    rust_crates(&resolved, &deps, Some(nsh))
}

/// The crates of the Rust nshm in `resolved`; nsh's own linked as nsh's
/// crates are ([`AppCrate::linked_with`]) when `nsh` says where from.
fn rust_crates(
    resolved: &[Resolved],
    deps: &[Dep],
    nsh: Option<&NshOf>,
) -> anyhow::Result<Vec<(String, AppCrate)>> {
    let mut own = None;
    resolved
        .iter()
        .filter(|r| r.manifest.module.is_some())
        .map(|r| {
            let features = deps
                .iter()
                .find(|d| d.name == r.locked.name)
                .map(|d| d.features.clone())
                .unwrap_or_default();
            let mut krate =
                AppCrate::new(r.locked.name.clone(), r.dir.clone()).with_features(features);
            if r.own
                && let Some(nsh) = nsh
            {
                let nsh: &Nsh = match &own {
                    Some(n) => n,
                    None => own.insert(nsh()?),
                };
                krate = krate.linked_with(nsh);
            }
            Ok((patch_id(r), krate))
        })
        .collect()
}

/// The nsh the app `app` builds with: where its own crates take nsh's
/// crates from, or this command line's.
fn nsh_of(app: &App) -> anyhow::Result<Nsh> {
    let crates: Vec<AppCrate> = app.own_crates()?.into_iter().map(|(_, c)| c).collect();
    Nsh::for_app(&crates)
}

/// The `[patch]` table that sends the app's dependency on the nshm
/// named `id` (as [`crates`] names it) to its installed copy:
/// `Some(Some(url))` for Git, `Some(None)` for crates.io, `None` for a
/// directory, which the app's crate takes as it is.
pub(crate) fn patch_table(id: &str) -> Option<Option<&str>> {
    if id.contains("://") || id.starts_with("git@") || id.ends_with(".git") {
        Some(Some(id))
    } else if id.contains(['/', '\\', '.']) {
        None
    } else {
        Some(None)
    }
}

/// The app `nsh add` run in `dir` adds to: the nearest directory, `dir`
/// or one above it, with `nsh.json` (as `cargo add` finds `Cargo.toml`).
fn app_dir(dir: &Path) -> anyhow::Result<PathBuf> {
    dir.ancestors()
        .find(|d| d.join("nsh.json").is_file())
        .map(Path::to_path_buf)
        .ok_or_else(|| {
            anyhow::anyhow!(
                "this command works on an NSH app, but neither {} nor a directory above it \
                 has nsh.json; run it in the app's directory, or create an app first with \
                 `nsh init`",
                dir.display()
            )
        })
}

/// The app's files a command changes, put back when it fails.
struct Backup {
    files: Vec<(PathBuf, Option<String>)>,
}

impl Backup {
    fn of(paths: &[PathBuf]) -> Self {
        Backup {
            files: paths
                .iter()
                .map(|p| (p.clone(), fs::read_to_string(p).ok()))
                .collect(),
        }
    }

    fn add(&mut self, path: &Path) {
        if !self.files.iter().any(|(p, _)| p == path) {
            self.files
                .push((path.to_owned(), fs::read_to_string(path).ok()));
        }
    }

    fn restore(&self) -> anyhow::Result<()> {
        for (path, text) in &self.files {
            match text {
                Some(text) => fs::write(path, text)?,
                None => {
                    let _ = fs::remove_file(path);
                }
            }
        }
        Ok(())
    }
}

/// An app: its directory and `nsh.json`.
struct App {
    dir: PathBuf,
    config: serde_json::Value,
}

impl App {
    fn open(cwd: &Path) -> anyhow::Result<Self> {
        let dir = app_dir(cwd)?;
        let path = dir.join("nsh.json");
        let text = nsh_bundler::app_dir::read(&path)
            .with_context(|| format!("can't read {}", path.display()))?;
        let config = serde_json::from_str(&text).context("nsh.json is not JSON")?;
        Ok(App { dir, config })
    }

    /// Writes `deps` as its `"dependencies"`, keeping the order of those
    /// it had.
    fn write_dependencies(&mut self, deps: &[Dep]) -> anyhow::Result<()> {
        let mut map = serde_json::Map::new();
        for d in deps {
            if map.insert(d.name.clone(), d.value()).is_some() {
                bail!("nsh.json: dependencies: {} twice", d.name);
            }
        }
        let object = self
            .config
            .as_object_mut()
            .context("nsh.json is not an object")?;
        if map.is_empty() {
            object.remove("dependencies");
        } else {
            object.insert("dependencies".into(), map.into());
        }
        fs::write(
            self.dir.join("nsh.json"),
            serde_json::to_string_pretty(&self.config)? + "\n",
        )?;
        Ok(())
    }

    /// The directory of its own crate (the first of `"rust"`), if any.
    fn own_crate(&self) -> anyhow::Result<Option<PathBuf>> {
        let first = match self.config.get("rust") {
            Some(serde_json::Value::String(p)) => Some(p.clone()),
            Some(serde_json::Value::Array(ps)) => {
                ps.first().and_then(|p| p.as_str()).map(str::to_owned)
            }
            _ => None,
        };
        first
            .map(|p| {
                dunce::canonicalize(self.dir.join(&p))
                    .with_context(|| format!("nsh.json: \"rust\": no crate at {p}"))
            })
            .transpose()
    }

    /// Its own crates, named by their paths.
    fn own_crates(&self) -> anyhow::Result<Vec<(String, AppCrate)>> {
        let paths: Vec<String> = match self.config.get("rust") {
            Some(serde_json::Value::String(p)) => vec![p.clone()],
            Some(serde_json::Value::Array(ps)) => ps
                .iter()
                .filter_map(|p| p.as_str().map(str::to_owned))
                .collect(),
            _ => Vec::new(),
        };
        paths
            .into_iter()
            .map(|path| {
                let dir = dunce::canonicalize(self.dir.join(&path))
                    .with_context(|| format!("nsh.json: \"rust\": no crate at {path}"))?;
                let package = rust_app::package_name(&dir.join("Cargo.toml"))?;
                Ok((path, AppCrate::new(package, dir)))
            })
            .collect()
    }
}

/// Makes the app's own crate depend on the Rust nshm `r` (`use acme_ui::Gauge`)
/// with `features`, as `cargo add` would; nothing when the app has no crate.
fn depend_from_own_crate(
    app: &App,
    backup: &mut Backup,
    r: &Resolved,
    features: &[String],
) -> anyhow::Result<()> {
    let Some(crate_dir) = app.own_crate()? else {
        return Ok(());
    };
    let manifest = crate_dir.join("Cargo.toml");
    let place = match (&r.origin, &r.locked.commit) {
        (Origin::Git { url, .. }, Some(commit)) => cargo_toml::Place::Git {
            url: url.clone(),
            commit: commit.clone(),
        },
        (Origin::Registry, _) => cargo_toml::Place::Crate {
            version: r.locked.version.clone(),
        },
        _ => cargo_toml::Place::Path {
            path: relative(&crate_dir, &r.dir),
        },
    };
    let dependency = cargo_toml::Dependency {
        place,
        features: features.to_vec(),
    };
    let text = fs::read_to_string(&manifest)?;
    backup.add(&manifest);
    if let Some(new) = cargo_toml::with_dependency(&text, &r.locked.name, &dependency)? {
        fs::write(&manifest, new)?;
    }
    Ok(())
}

/// Says the app needs Rust now, when its nshm bring Rust in, it has no
/// crate of its own and `lock` (before the change) had no Rust nshm.
fn say_rust_needed(app: &App, lock: &Lock, resolved: &[Resolved]) {
    if app.config.get("rust").is_some() || lock.packages.iter().any(|p| p.module.is_some()) {
        return;
    }
    let rust: Vec<&str> = resolved
        .iter()
        .filter(|r| r.manifest.module.is_some())
        .map(|r| r.locked.name.as_str())
        .collect();
    if !rust.is_empty() {
        notice(
            "Note",
            format!(
                "{} {} Rust, so this app now builds with the Rust toolchain (https://rustup.rs)",
                rust.join(", "),
                if rust.len() == 1 { "has" } else { "have" },
            ),
        );
    }
}

/// Checks the app's crates and its Rust nshm together: a clash of
/// packages or modules is reported here, before a build.
fn check_rust(app: &App, resolved: &[Resolved], deps: &[Dep]) -> anyhow::Result<()> {
    let mut all = app.own_crates()?;
    all.extend(rust_crates(resolved, deps, None)?);
    if all.len() > 1 {
        rust_app::check_crates(&all)?;
    }
    Ok(())
}

/// `nsh add`: lists the nshm `added` in the `nsh.json` of the app in or
/// above `cwd` (replacing an entry of the same package, whose features it
/// keeps), resolves and locks it anew, and makes the app's own crate
/// depend on it when it has Rust. On an error the app's files are left as
/// they were.
pub(crate) fn add(cwd: &Path, added: Added) -> anyhow::Result<()> {
    let mut app = App::open(cwd)?;
    let mut backup = Backup::of(&[app.dir.join("nsh.json"), app.dir.join(LOCK)]);
    let result = add_to(&mut app, &mut backup, added);
    if result.is_err() {
        backup.restore()?;
    }
    result.map_err(|e| e.context(format!("nsh add: nsh.json and {LOCK} are unchanged")))
}

fn add_to(app: &mut App, backup: &mut Backup, added: Added) -> anyhow::Result<()> {
    let mut net = fetch::Net::new(root()?);
    let mut req = added.req.clone();
    // Its package's name: crates.io's, else what the nshm says it is.
    let name = match (&added.name, &added.origin) {
        (Some(name), _) => name.clone(),
        (None, Origin::Path(p)) => {
            let dir = dunce::canonicalize(app.dir.join(p))
                .with_context(|| format!("no directory {p}"))?;
            manifest::Manifest::read(&dir, p)?.name
        }
        (None, origin) => {
            use fetch::Fetch as _;
            let candidates = net.candidates("", origin)?;
            let wanted = semver::VersionReq::parse(req.as_deref().unwrap_or("*"))?;
            let candidate = candidates
                .iter()
                .filter(|c| c.version.as_ref().is_none_or(|v| wanted.matches(v)))
                .max_by(|a, b| a.version.cmp(&b.version))
                .with_context(|| format!("no version of {} matches {wanted}", origin.describe()))?
                .clone();
            let rel = net.install("", origin, &candidate)?;
            let m = manifest::Manifest::read(&net.root().join(rel), &origin.describe())?;
            m.name
        }
    };
    let mut deps = dep::read_all(&app.config)?;
    let features = match deps.iter().find(|d| d.name == name) {
        Some(listed) => {
            let mut f = listed.features.clone();
            for x in &added.features {
                if !f.contains(x) {
                    f.push(x.clone());
                }
            }
            f
        }
        None => added.features.clone(),
    };
    let dep = Dep {
        name: name.clone(),
        origin: added.origin.clone(),
        req: req.clone(),
        features: features.clone(),
    }
    .with_features(Vec::new())?;
    match deps.iter_mut().find(|d| d.name == name) {
        Some(listed) => *listed = dep,
        None => deps.push(dep),
    }
    let lock = Lock::read(&app.dir)?;
    // `nsh add` again: resolved anew, the newest its requirement accepts.
    let nsh = || nsh_of(app);
    let resolved = resolve::resolve(&app.dir, &deps, &lock, &|n| n == name, &nsh, &mut net)?;
    let this = resolved
        .iter()
        .find(|r| r.locked.name == name)
        .context("the nshm is not resolved")?
        .clone();
    // Listed without a version: what it resolved to, as `cargo add`
    // writes `^0.3.2`. A commit has no version to require; nsh's own nshm
    // has its nsh's.
    let versioned = match &this.origin {
        _ if this.own => true,
        Origin::Registry => true,
        Origin::Git { .. } => this
            .dir
            .file_name()
            .is_some_and(|n| n.to_string_lossy() == this.locked.version),
        Origin::Path(_) => false,
    };
    if req.is_none() && versioned {
        req = Some(format!("^{}", this.locked.version));
        if let Some(listed) = deps.iter_mut().find(|d| d.name == name) {
            listed.req = req.clone();
        }
    }
    if !added.features.is_empty() {
        if this.manifest.module.is_none() {
            bail!("{name} has no Rust, so no Cargo features to turn on");
        }
        let known = cargo_toml::crate_features(&this.dir)?;
        if let Some(unknown) = added.features.iter().find(|f| !known.contains(f)) {
            bail!(
                "`{unknown}` is not a feature of {name} (its features: {})",
                if known.is_empty() {
                    "none".to_owned()
                } else {
                    known.join(", ")
                }
            );
        }
    }
    check_rust(app, &resolved, &deps)?;
    app.write_dependencies(&deps)?;
    write_lock(&app.dir, &lock, &resolved)?;
    if this.manifest.module.is_some() {
        depend_from_own_crate(app, backup, &this, &features)?;
    }
    let from = match &this.origin {
        Origin::Path(p) => p.clone(),
        other => other.describe(),
    };
    status(
        "Added",
        format!("{name} v{} ({from}) to dependencies", this.locked.version),
    );
    let specifiers: Vec<String> = this
        .manifest
        .module
        .iter()
        .chain(this.manifest.exports.keys())
        .map(|s| format!("\"{s}\""))
        .collect();
    if !specifiers.is_empty() {
        status("Import", format!("from {}", specifiers.join(", ")));
    }
    say_rust_needed(app, &lock, &resolved);
    Ok(())
}

/// `nsh remove`: takes the nshm `names` out of the app's `nsh.json` (and
/// its own crate's `[dependencies]`), and locks what is left.
pub(crate) fn remove(cwd: &Path, names: &[String]) -> anyhow::Result<()> {
    let mut app = App::open(cwd)?;
    let mut backup = Backup::of(&[app.dir.join("nsh.json"), app.dir.join(LOCK)]);
    let result = (|| {
        let mut deps = dep::read_all(&app.config)?;
        for name in names {
            if !deps.iter().any(|d| &d.name == name) {
                let listed: Vec<&str> = deps.iter().map(|d| d.name.as_str()).collect();
                bail!(
                    "{name} is not a dependency of the app (its dependencies: {})",
                    if listed.is_empty() {
                        "none".to_owned()
                    } else {
                        listed.join(", ")
                    }
                );
            }
        }
        let lock = Lock::read(&app.dir)?;
        deps.retain(|d| !names.contains(&d.name));
        let mut net = fetch::Net::new(root()?);
        let nsh = || nsh_of(&app);
        let resolved = resolve::resolve(&app.dir, &deps, &lock, &|_| false, &nsh, &mut net)?;
        app.write_dependencies(&deps)?;
        write_lock(&app.dir, &lock, &resolved)?;
        if let Some(crate_dir) = app.own_crate()? {
            let manifest = crate_dir.join("Cargo.toml");
            for name in names {
                let rust = lock
                    .packages
                    .iter()
                    .any(|p| &p.name == name && p.module.is_some());
                // Unless another nshm still brings it in.
                let still = resolved.iter().any(|r| &r.locked.name == name);
                if rust && !still {
                    let text = fs::read_to_string(&manifest)?;
                    if let Some(new) = cargo_toml::without_dependency(&text, name) {
                        backup.add(&manifest);
                        fs::write(&manifest, new)?;
                    }
                }
            }
        }
        for name in names {
            status("Removed", name);
        }
        anyhow::Ok(())
    })();
    if result.is_err() {
        backup.restore()?;
    }
    result
}

/// `nsh upgrade`: resolves the nshm `names` (every one when empty) anew,
/// to the newest versions the app's and the nshm's requirements accept,
/// and locks them.
pub(crate) fn upgrade(cwd: &Path, names: &[String]) -> anyhow::Result<()> {
    let app = App::open(cwd)?;
    let mut backup = Backup::of(&[app.dir.join(LOCK)]);
    let result = (|| {
        let deps = dep::read_all(&app.config)?;
        let lock = Lock::read(&app.dir)?;
        for name in names {
            if !lock.packages.iter().any(|p| &p.name == name)
                && !deps.iter().any(|d| &d.name == name)
            {
                bail!("{name} is not an nshm of the app");
            }
        }
        let mut net = fetch::Net::new(root()?);
        let resolved = resolve::resolve(
            &app.dir,
            &deps,
            &lock,
            &|n| names.is_empty() || names.iter().any(|x| x == n),
            &|| nsh_of(&app),
            &mut net,
        )?;
        check_rust(&app, &resolved, &deps)?;
        write_lock(&app.dir, &lock, &resolved)?;
        let mut changed = false;
        for r in &resolved {
            let old = lock.packages.iter().find(|p| p.name == r.locked.name);
            match old {
                Some(old) if old.version == r.locked.version && old.commit == r.locked.commit => {}
                Some(old) => {
                    changed = true;
                    status(
                        "Upgrading",
                        format!(
                            "{} v{} -> v{}",
                            r.locked.name, old.version, r.locked.version
                        ),
                    );
                }
                None => {
                    changed = true;
                    status("Adding", format!("{} v{}", r.locked.name, r.locked.version));
                }
            }
            // The app's crate takes the new version too.
            if r.manifest.module.is_some()
                && deps.iter().any(|d| d.name == r.locked.name)
                && old.is_some_and(|o| o.version != r.locked.version || o.commit != r.locked.commit)
            {
                let features = deps
                    .iter()
                    .find(|d| d.name == r.locked.name)
                    .map(|d| d.features.clone())
                    .unwrap_or_default();
                depend_from_own_crate(&app, &mut backup, r, &features)?;
            }
        }
        for old in &lock.packages {
            if !resolved.iter().any(|r| r.locked.name == old.name) {
                changed = true;
                status("Removing", format!("{} v{}", old.name, old.version));
            }
        }
        if !changed {
            status(
                "Locked",
                "every nshm is at the newest version its requirements accept",
            );
        }
        anyhow::Ok(())
    })();
    if result.is_err() {
        backup.restore()?;
    }
    result
}

/// The executable of the platform `name` that a platform nshm of the app
/// in or above `cwd` provides, built when it is not: `None` when no nshm of
/// the app provides it.
///
/// The nshm is built from a copy in the app's `.nsh/platforms/<name>`, so
/// its build (its `Cargo.lock` and `target/`) stays the app's and the
/// installed copy every app shares stays as it was.
pub(crate) fn platform_program(cwd: &Path, name: &str) -> anyhow::Result<Option<PathBuf>> {
    let Ok(app) = App::open(cwd) else {
        return Ok(None);
    };
    if app.config.get("dependencies").is_none() {
        return Ok(None);
    }
    let resolved = ensure(&app.dir, &app.config, &|| nsh_of(&app))?;
    let Some(r) = resolved
        .iter()
        .find(|r| r.manifest.platform.as_deref() == Some(name))
    else {
        return Ok(None);
    };
    let build = app.dir.join(".nsh/platforms").join(name);
    let stamp = build.join(".nshm");
    let at = format!("{}@{}", r.locked.name, r.dir.display());
    if fs::read_to_string(&stamp).ok().as_deref() != Some(at.as_str()) {
        let _ = fs::remove_dir_all(&build);
        copy_dir(&r.dir, &build)?;
        fs::write(&stamp, &at)?;
    }
    own_workspace(&build.join("Cargo.toml"))?;
    let bin = format!("{}{name}", crate::platforms::PREFIX);
    status(
        "Building",
        format!("platform {name} ({} v{})", r.locked.name, r.locked.version),
    );
    let ok = Command::new(rust_app::cargo())
        .args([
            "build",
            "--release",
            "--quiet",
            "--bin",
            &bin,
            "--manifest-path",
        ])
        .arg(build.join("Cargo.toml"))
        .status()
        .context("running cargo (is Rust installed?)")?
        .success();
    if !ok {
        bail!(
            "{} could not build the platform {name} (its binary `{bin}`)",
            r.locked.name
        );
    }
    let program = build
        .join("target/release")
        .join(format!("{bin}{}", std::env::consts::EXE_SUFFIX));
    if !program.is_file() {
        bail!(
            "{}: no {} after the build",
            r.locked.name,
            program.display()
        );
    }
    Ok(Some(program))
}

/// Makes the manifest at `path` the root of its own workspace: the copy in
/// `.nsh/platforms/<name>` lies inside the app's directory, and a Rust app's
/// `Cargo.toml` is a workspace root, which Cargo would take the copy to be
/// an undeclared member of. A platform from Git or a directory often has its
/// own `[workspace]`; one from crates.io never does, as `cargo package` drops
/// it from the manifest it publishes.
fn own_workspace(path: &Path) -> anyhow::Result<()> {
    let manifest =
        fs::read_to_string(path).with_context(|| format!("reading {}", path.display()))?;
    let has_workspace = manifest.lines().any(|line| {
        let line = line.trim_start();
        line.starts_with("[workspace]") || line.starts_with("[workspace.")
    });
    if !has_workspace {
        fs::write(path, format!("{manifest}\n[workspace]\n"))
            .with_context(|| format!("writing {}", path.display()))?;
    }
    Ok(())
}

/// Copies the directory `from` to `to`, without its `target/`, `.git/` and
/// `.nsh/`: a platform nshm in a directory above the app (an example app in
/// the platform's repository, `{ "path": "../.." }`) holds the copy itself in
/// the app's `.nsh/platforms`.
fn copy_dir(from: &Path, to: &Path) -> anyhow::Result<()> {
    fs::create_dir_all(to)?;
    for entry in fs::read_dir(from)? {
        let entry = entry?;
        let path = entry.path();
        let dest = to.join(entry.file_name());
        let kind = entry.file_type()?;
        if kind.is_dir() {
            if ["target", ".git", ".nsh"]
                .iter()
                .any(|n| entry.file_name() == *n)
            {
                continue;
            }
            copy_dir(&path, &dest)?;
        } else if kind.is_file() {
            fs::copy(&path, &dest).with_context(|| format!("copying {}", path.display()))?;
        }
    }
    Ok(())
}

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

    #[test]
    fn copies_leave_builds_out() {
        let root = std::env::temp_dir().join(format!("nsh-copy-dir-{}", std::process::id()));
        let _ = fs::remove_dir_all(&root);
        let from = root.join("platform");
        for dir in [
            "src",
            "target/release",
            ".git",
            "examples/app/.nsh/platforms/x",
        ] {
            fs::create_dir_all(from.join(dir)).unwrap();
        }
        fs::write(from.join("src/main.rs"), "fn main() {}").unwrap();
        fs::write(from.join("examples/app/nsh.json"), "{}").unwrap();
        // The app's copy lies inside the platform it copies.
        copy_dir(&from, &from.join("examples/app/.nsh/platforms/x")).unwrap();
        let to = from.join("examples/app/.nsh/platforms/x");
        assert!(to.join("src/main.rs").is_file());
        assert!(to.join("examples/app/nsh.json").is_file());
        assert!(!to.join("target").exists());
        assert!(!to.join(".git").exists());
        assert!(!to.join("examples/app/.nsh").exists());
        fs::remove_dir_all(&root).unwrap();
    }

    /// A platform from crates.io, built inside a Rust app's workspace,
    /// builds on its own (Cargo's own check, not only the manifest's text).
    #[test]
    fn a_platform_copy_is_its_own_workspace() {
        let root = std::env::temp_dir().join(format!("nsh-own-workspace-{}", std::process::id()));
        let _ = fs::remove_dir_all(&root);
        let app = root.join("app");
        let build = app.join(".nsh/platforms/x");
        fs::create_dir_all(app.join("src")).unwrap();
        fs::create_dir_all(build.join("src")).unwrap();
        fs::write(
            app.join("Cargo.toml"),
            "[package]\nname = \"app\"\nversion = \"0.1.0\"\nedition = \"2024\"\n\n[workspace]\n",
        )
        .unwrap();
        fs::write(app.join("src/lib.rs"), "").unwrap();
        let platform =
            "[package]\nname = \"nsh-platform-x\"\nversion = \"0.1.0\"\nedition = \"2024\"\n";
        fs::write(build.join("Cargo.toml"), platform).unwrap();
        fs::write(build.join("src/main.rs"), "fn main() {}").unwrap();
        let metadata = || {
            Command::new(rust_app::cargo())
                .args([
                    "metadata",
                    "--no-deps",
                    "--offline",
                    "--format-version",
                    "1",
                    "--manifest-path",
                ])
                .arg(build.join("Cargo.toml"))
                .output()
                .unwrap()
                .status
                .success()
        };
        assert!(!metadata(), "Cargo takes the copy into the app's workspace");
        own_workspace(&build.join("Cargo.toml")).unwrap();
        assert!(metadata());
        // Once is enough: a second build leaves the manifest as it is.
        let once = fs::read_to_string(build.join("Cargo.toml")).unwrap();
        own_workspace(&build.join("Cargo.toml")).unwrap();
        assert_eq!(fs::read_to_string(build.join("Cargo.toml")).unwrap(), once);
        // A platform's own workspace stays as it is.
        let ws = format!("{platform}\n[workspace]\nmembers = [\"crates/*\"]\n");
        fs::write(build.join("Cargo.toml"), &ws).unwrap();
        own_workspace(&build.join("Cargo.toml")).unwrap();
        assert_eq!(fs::read_to_string(build.join("Cargo.toml")).unwrap(), ws);
        fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn add_finds_the_app_above() {
        let root = std::env::temp_dir().join(format!("nsh-app-dir-{}", std::process::id()));
        let src = root.join("app/src/ui");
        fs::create_dir_all(&src).unwrap();
        fs::write(root.join("app/nsh.json"), "{}").unwrap();
        assert_eq!(app_dir(&src).unwrap(), root.join("app"));
        let err = app_dir(&root).unwrap_err().to_string();
        assert!(err.contains("nsh init"), "{err}");
        assert!(!err.contains("os error"), "{err}");
        fs::remove_dir_all(&root).unwrap();
    }

    #[test]
    fn workspace_patches_follow_the_origin() {
        assert_eq!(
            patch_table("https://github.com/acme/ui"),
            Some(Some("https://github.com/acme/ui"))
        );
        assert_eq!(patch_table("nsh-omarchy"), Some(None));
        assert_eq!(patch_table("../ui"), None);
    }

    /// A TSX nshm from a Git repository, end to end: added by URL, listed
    /// with the version its newest tag has, installed once under
    /// `NSH_HOME`, locked, upgraded, removed.
    #[test]
    fn a_tsx_nshm_from_git_is_added_locked_and_removed() {
        if fetch::git(None, &["--version"]).is_err() {
            return;
        }
        let base = std::env::temp_dir().join(format!("nsh-nshm-e2e-{}", std::process::id()));
        let _ = fs::remove_dir_all(&base);
        let repo = base.join("acme-ui");
        let app = base.join("app");
        fs::create_dir_all(repo.join("src")).unwrap();
        fs::create_dir_all(&app).unwrap();
        fs::write(app.join("nsh.json"), "{ \"name\": \"Demo\" }\n").unwrap();
        let git = |args: &[&str]| fetch::git(Some(&repo), args).unwrap();
        let commit = |version: &str| {
            fs::write(
                repo.join("src/index.tsx"),
                format!("export const version = {version:?};\n"),
            )
            .unwrap();
            fs::write(
                repo.join("nsh.json"),
                format!(
                    r#"{{ "name": "acme-ui", "version": "{version}", "exports": {{ "acme-ui": "src/index.tsx" }} }}"#
                ),
            )
            .unwrap();
            git(&["add", "."]);
            git(&[
                "-c",
                "user.name=t",
                "-c",
                "user.email=t@t",
                "commit",
                "-qm",
                version,
            ]);
            git(&["tag", &format!("v{version}")]);
        };
        git(&["init", "-q", "-b", "main"]);
        commit("1.0.0");
        // SAFETY: tests of this crate that read NSH_HOME run in this one test.
        unsafe { std::env::set_var("NSH_HOME", base.join("home")) };
        let url = format!("file://{}", repo.display());
        add(
            &app,
            Added::parse(Some(url.clone()), None, None, None, Vec::new()).unwrap(),
        )
        .unwrap();
        let config: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(app.join("nsh.json")).unwrap()).unwrap();
        assert_eq!(config["dependencies"]["acme-ui"]["git"], url);
        assert_eq!(config["dependencies"]["acme-ui"]["version"], "^1.0.0");
        let lock = Lock::read(&app).unwrap();
        assert_eq!(lock.packages[0].version, "1.0.0");
        let installed = base
            .join("home/nshms")
            .join(lock.packages[0].dir.as_ref().unwrap());
        assert!(
            installed.join("src/index.tsx").is_file(),
            "{}",
            installed.display()
        );
        // The bundler finds its TSX from the lock.
        let exports = nsh_bundler::nshm::exports(&app).unwrap();
        assert_eq!(exports[0].0, "acme-ui");
        assert!(exports[0].1.starts_with(&installed));
        // A build with the lock in place reads nothing new: unchanged.
        let again = crates(&app, &config, &|| Ok(Nsh::Registry)).unwrap();
        assert!(again.is_empty(), "a TSX nshm is no crate");
        assert_eq!(Lock::read(&app).unwrap(), lock);
        // A new version within the requirement: `nsh upgrade` takes it.
        commit("1.1.0");
        upgrade(&app, &[]).unwrap();
        assert_eq!(Lock::read(&app).unwrap().packages[0].version, "1.1.0");
        assert!(
            base.join("home/nshms")
                .join(repo_rel(&url))
                .join("1.0.0")
                .is_dir()
        );
        remove(&app, &["acme-ui".into()]).unwrap();
        let config: serde_json::Value =
            serde_json::from_str(&fs::read_to_string(app.join("nsh.json")).unwrap()).unwrap();
        assert!(config.get("dependencies").is_none());
        assert!(Lock::read(&app).unwrap().packages.is_empty());
        assert!(remove(&app, &["acme-ui".into()]).is_err());
        // SAFETY: as above.
        unsafe { std::env::remove_var("NSH_HOME") };
        fs::remove_dir_all(&base).unwrap();
    }

    fn repo_rel(url: &str) -> String {
        fetch::repo_dir(url)
    }
}