nsh-cli 0.7.0

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
//! The app's own crate depending on its Rust nshm: the `[dependencies]`
//! line `nsh add` writes (from crates.io at the resolved version, from Git
//! at the locked commit, or from a directory), and the crate's features.

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

use anyhow::{Context as _, bail};

use crate::rust_app;

/// How the app's crate depends on an nshm: from where, with which
/// features.
#[derive(Debug)]
pub(crate) struct Dependency {
    pub place: Place,
    pub features: Vec<String>,
}

/// Where the app's crate takes an nshm from.
#[derive(Debug, Clone)]
pub(crate) enum Place {
    Git { url: String, commit: String },
    Crate { version: String },
    Path { path: String },
}

impl Dependency {
    /// Its value in `[dependencies]`, without the crate's default features
    /// when `default_features` is false.
    fn value_with(&self, default_features: bool) -> String {
        let mut features = String::new();
        if !default_features {
            features.push_str(", default-features = false");
        }
        features.push_str(&rust_app::features_value(&self.features));
        match &self.place {
            Place::Git { url, commit } => {
                format!("{{ git = {url:?}, rev = {commit:?}{features} }}")
            }
            Place::Crate { version } if features.is_empty() => format!("{version:?}"),
            Place::Crate { version } => format!("{{ version = {version:?}{features} }}"),
            Place::Path { path } => format!("{{ path = {path:?}{features} }}"),
        }
    }

    /// Whether the dependency `line` takes the crate from the same place.
    fn same_place(&self, line: &str) -> bool {
        let git = rust_app::toml_string(line, "git");
        let path = rust_app::toml_string(line, "path");
        match &self.place {
            Place::Git { url, .. } => git.as_deref() == Some(url.as_str()),
            Place::Crate { .. } => git.is_none() && path.is_none(),
            Place::Path { .. } => path.is_some(),
        }
    }
}

/// The text after `key =` in the TOML `line`, `key` being a whole key
/// (`features` is not the end of `default-features`).
fn toml_value<'a>(line: &'a str, key: &str) -> Option<&'a str> {
    let mut from = 0;
    while let Some(found) = line[from..].find(key) {
        let at = from + found;
        from = at + key.len();
        let before = line[..at].trim_end();
        if !(before.is_empty() || before.ends_with('{') || before.ends_with(',')) {
            continue;
        }
        if let Some(value) = line[at + key.len()..].trim_start().strip_prefix('=') {
            let value = value.trim_start();
            // An array runs to its `]`; a scalar to the next `,` or `}`.
            return Some(if value.starts_with('[') {
                value
            } else {
                value
                    .split([',', '}'])
                    .next()
                    .unwrap_or_default()
                    .trim_end()
            });
        }
    }
    None
}

/// The features of the crate in `dir`, as Cargo reads them: its
/// `[features]` and the optional dependencies no feature names as
/// `dep:<name>`, in whichever form its manifest writes them.
pub(crate) fn crate_features(dir: &Path) -> anyhow::Result<Vec<String>> {
    let manifest = dir.join("Cargo.toml");
    let output = Command::new(rust_app::cargo())
        .args([
            "metadata",
            "--no-deps",
            "--format-version",
            "1",
            "--manifest-path",
        ])
        .arg(&manifest)
        .output()
        .context("run cargo metadata (is Rust installed?)")?;
    if !output.status.success() {
        bail!(
            "cargo cannot read {}:\n{}",
            manifest.display(),
            String::from_utf8_lossy(&output.stderr).trim()
        );
    }
    let metadata: serde_json::Value =
        serde_json::from_slice(&output.stdout).context("cargo metadata's output")?;
    let package = rust_app::package_name(&manifest)?;
    Ok(features_of(&metadata, &package))
}

/// The features of `package` in the `cargo metadata` output `metadata`
/// (a crate of a workspace lists the workspace's packages), sorted.
fn features_of(metadata: &serde_json::Value, package: &str) -> Vec<String> {
    metadata["packages"]
        .as_array()
        .into_iter()
        .flatten()
        .find(|p| p["name"].as_str() == Some(package))
        .and_then(|p| p["features"].as_object())
        .map(|features| {
            let mut names: Vec<String> = features.keys().cloned().collect();
            names.sort_unstable();
            names
        })
        .unwrap_or_default()
}

/// The strings of the inline array `key = [...]` in the TOML `line`.
fn toml_strings(line: &str, key: &str) -> Vec<String> {
    let Some(list) = toml_value(line, key).and_then(|v| v.strip_prefix('[')) else {
        return Vec::new();
    };
    let list = list.split(']').next().unwrap_or_default();
    list.split(',')
        .map(|item| item.trim().trim_matches(['"', '\'']).to_owned())
        .filter(|item| !item.is_empty())
        .collect()
}

/// `to` relative to `from`, with `/`.
pub(crate) fn relative(from: &Path, to: &Path) -> String {
    let from: Vec<_> = from.components().collect();
    let to: Vec<_> = to.components().collect();
    let common = from.iter().zip(&to).take_while(|(a, b)| a == b).count();
    let mut parts: Vec<String> = vec!["..".into(); from.len() - common];
    parts.extend(
        to[common..]
            .iter()
            .map(|c| c.as_os_str().to_string_lossy().into_owned()),
    );
    if parts.is_empty() {
        ".".into()
    } else {
        parts.join("/")
    }
}

/// The manifest `text` depending on `package` as `dependency` says, or
/// `None` when it does already; an error when it takes `package` from
/// elsewhere.
pub(crate) fn with_dependency(
    text: &str,
    package: &str,
    dependency: &Dependency,
) -> anyhow::Result<Option<String>> {
    // Features the manifest turns on already stay on, and so does turning
    // the default ones off.
    let mut features = dependency.features.clone();
    let mut default_features = true;
    let mut table = String::new();
    for l in text.lines() {
        let t = l.trim();
        if t.starts_with('[') {
            table = t.to_owned();
        }
        if table == "[dependencies]"
            && t.split(['=', ' ']).next() == Some(package)
            && dependency.same_place(t)
        {
            for feature in toml_strings(t, "features") {
                if !features.contains(&feature) {
                    features.push(feature);
                }
            }
            if toml_value(t, "default-features") == Some("false") {
                default_features = false;
            }
        }
    }
    let dependency = &Dependency {
        place: dependency.place.clone(),
        features,
    };
    let line = format!("{package} = {}", dependency.value_with(default_features));
    let mut out = Vec::new();
    let mut found = false;
    let mut table = String::new();
    for l in text.lines() {
        let t = l.trim();
        if t.starts_with('[') {
            table = t.to_owned();
        }
        if table == "[dependencies]" && t.split(['=', ' ']).next() == Some(package) {
            if dependency.same_place(t) {
                // The same place: as now, written under the table's header.
                continue;
            }
            bail!("Cargo.toml: [dependencies] already has {package} from elsewhere: `{t}`");
        }
        out.push(l.to_owned());
        if t == "[dependencies]" && !found {
            out.push(line.clone());
            found = true;
        }
    }
    if !found {
        out.push(String::new());
        out.push("[dependencies]".to_owned());
        out.push(line);
    }
    let new = out.join("\n") + "\n";
    Ok((new != text).then_some(new))
}

/// The manifest `text` without `package` in its `[dependencies]`, or
/// `None` when it has no such line.
pub(crate) fn without_dependency(text: &str, package: &str) -> Option<String> {
    let mut table = String::new();
    let mut found = false;
    let mut out = Vec::new();
    for l in text.lines() {
        let t = l.trim();
        if t.starts_with('[') {
            table = t.to_owned();
        }
        if table == "[dependencies]" && t.split(['=', ' ']).next() == Some(package) {
            found = true;
            continue;
        }
        out.push(l);
    }
    found.then(|| out.join("\n") + "\n")
}

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

    #[test]
    fn the_app_crate_depends_on_the_nshm() {
        let manifest = "[package]\nname = \"app\"\n\n[dependencies]\nnsh-core = \"0.6\"\n";
        let at = |commit: &str| Dependency {
            place: Place::Git {
                url: "https://x/ui".into(),
                commit: commit.into(),
            },
            features: Vec::new(),
        };
        let new = with_dependency(manifest, "acme-ui", &at("abc"))
            .unwrap()
            .unwrap();
        assert!(
            new.contains(
                "[dependencies]\nacme-ui = { git = \"https://x/ui\", rev = \"abc\" }\nnsh-core"
            ),
            "{new}"
        );
        // Again at another commit: the line is replaced.
        let newer = with_dependency(&new, "acme-ui", &at("def"))
            .unwrap()
            .unwrap();
        assert!(
            newer.contains("rev = \"def\"") && !newer.contains("abc"),
            "{newer}"
        );
        assert_eq!(newer.matches("acme-ui").count(), 1, "{newer}");
        assert!(
            with_dependency(&newer, "acme-ui", &at("def"))
                .unwrap()
                .is_none()
        );
        // Taken from elsewhere: an error, not a second source.
        let other = "[dependencies]\nacme-ui = \"1\"\n";
        assert!(with_dependency(other, "acme-ui", &at("abc")).is_err());
        // From crates.io and a directory.
        let krate = Dependency {
            place: Place::Crate {
                version: "1.2.0".into(),
            },
            features: Vec::new(),
        };
        let new = with_dependency(manifest, "acme-ui", &krate)
            .unwrap()
            .unwrap();
        assert!(new.contains("acme-ui = \"1.2.0\""), "{new}");
        let path = Dependency {
            place: Place::Path {
                path: "../ui".into(),
            },
            features: Vec::new(),
        };
        assert!(with_dependency(&new, "acme-ui", &path).is_err());
        let new = with_dependency(manifest, "acme-ui", &path)
            .unwrap()
            .unwrap();
        assert!(new.contains("acme-ui = { path = \"../ui\" }"), "{new}");
        // With the features the app turns on.
        let featured = Dependency {
            place: Place::Crate {
                version: "0.6.0".into(),
            },
            features: vec!["camera".into()],
        };
        let new = with_dependency(manifest, "nsh-extra", &featured)
            .unwrap()
            .unwrap();
        assert!(
            new.contains("nsh-extra = { version = \"0.6.0\", features = [\"camera\"] }"),
            "{new}"
        );
        let featured = Dependency {
            place: Place::Git {
                url: "https://x/extra".into(),
                commit: "abc".into(),
            },
            features: vec!["camera".into(), "push".into()],
        };
        // Features the manifest has already stay on.
        let manifest =
            "[dependencies]\nnsh-extra = { version = \"0.6.0\", features = [\"push\"] }\n";
        let more = Dependency {
            place: Place::Crate {
                version: "0.6.0".into(),
            },
            features: vec!["camera".into()],
        };
        let new = with_dependency(manifest, "nsh-extra", &more)
            .unwrap()
            .unwrap();
        assert!(
            new.contains("nsh-extra = { version = \"0.6.0\", features = [\"camera\", \"push\"] }"),
            "{new}"
        );
        assert_eq!(
            toml_strings(r#"x = { git = "u", features = ['a', "b"] }"#, "features"),
            ["a", "b"]
        );
        // `default-features` is not `features`, and stays off.
        let manifest = "[dependencies]\nacme-ui = { version = \"1.0.0\", default-features = false, features = [\"push\"] }\n";
        let camera = Dependency {
            place: Place::Crate {
                version: "1.0.0".into(),
            },
            features: vec!["camera".into()],
        };
        let new = with_dependency(manifest, "acme-ui", &camera)
            .unwrap()
            .unwrap();
        assert!(
            new.contains(
                "acme-ui = { version = \"1.0.0\", default-features = false, features = [\"camera\", \"push\"] }"
            ),
            "{new}"
        );
        assert!(toml_strings("x = { default-features = false }", "features").is_empty());
        assert_eq!(
            featured.value_with(true),
            "{ git = \"https://x/extra\", rev = \"abc\", features = [\"camera\", \"push\"] }"
        );
    }

    #[test]
    fn a_crates_features_are_read_from_its_metadata() {
        // `cargo metadata` lists a workspace's packages, implicit features
        // of optional dependencies included.
        let metadata = serde_json::json!({ "packages": [
            { "name": "acme-core", "features": { "fast": [] } },
            { "name": "acme-ui", "features": {
                "default": [],
                "camera": ["dep:tokio"],
                "serde": ["dep:serde"],
            } },
        ] });
        assert_eq!(
            features_of(&metadata, "acme-ui"),
            ["camera", "default", "serde"]
        );
        assert!(features_of(&metadata, "acme-gauge").is_empty());
        assert!(features_of(&serde_json::json!({}), "acme-ui").is_empty());
    }

    #[test]
    fn relative_paths() {
        assert_eq!(
            relative(Path::new("/a/app/rust"), Path::new("/a/ext/ui")),
            "../../ext/ui"
        );
        assert_eq!(relative(Path::new("/a"), Path::new("/a/b")), "b");
    }

    #[test]
    fn a_dependency_is_taken_out() {
        let text = "[package]\nname = \"app\"\n\n[dependencies]\nacme-ui = \"1.2.0\"\nnsh-core = \"0.6\"\n";
        assert_eq!(
            without_dependency(text, "acme-ui").unwrap(),
            "[package]\nname = \"app\"\n\n[dependencies]\nnsh-core = \"0.6\"\n"
        );
        assert_eq!(without_dependency(text, "acme"), None);
    }
}