use std::{
env, fs,
path::{Path, PathBuf},
process::Command,
};
use anyhow::{Context as _, bail};
use nsh_core::element::description::{Description, SetDescription};
use sha2::{Digest as _, Sha256};
use crate::{runtime::Platform, ui};
const ENTRY: &str = "__nsh_native_module";
const COMPONENTS_ENTRY: &str = "__nsh_native_components_";
static CURRENT: std::sync::Mutex<Option<RustApp>> = std::sync::Mutex::new(None);
static RESTARTED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
static RELEASE_HOSTS: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);
pub(crate) fn set_release_hosts() {
RELEASE_HOSTS.store(true, std::sync::atomic::Ordering::Relaxed);
}
pub(crate) fn set_current(app: RustApp) {
*CURRENT.lock().unwrap_or_else(|e| e.into_inner()) = Some(app);
}
pub(crate) fn current() -> Option<RustApp> {
CURRENT.lock().unwrap_or_else(|e| e.into_inner()).clone()
}
pub(crate) fn set_restarted() {
RESTARTED.store(true, std::sync::atomic::Ordering::Relaxed);
}
#[derive(Debug)]
pub(crate) struct Restart;
impl std::fmt::Display for Restart {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("the app's Rust code changed")
}
}
impl std::error::Error for Restart {}
#[derive(Debug, Clone)]
pub(crate) struct RustApp {
workspace: PathBuf,
nsh: Nsh,
crates: Vec<AppCrate>,
watched: Vec<PathBuf>,
root: Option<RootConfig>,
retargeted: bool,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct AppCrate {
package: String,
dir: PathBuf,
}
impl AppCrate {
pub(crate) fn package(&self) -> &str {
&self.package
}
pub(crate) fn dir(&self) -> &Path {
&self.dir
}
pub(crate) fn cargo(&self, subcommand: &str) -> Command {
let mut cmd = Command::new(cargo());
cmd.arg(subcommand)
.arg("--manifest-path")
.arg(self.dir.join("Cargo.toml"))
.current_dir(&self.dir);
cmd
}
fn name(&self) -> String {
format!("app_{}", self.package.replace('-', "_"))
}
fn dependency(&self) -> String {
format!(
"{} = {{ package = {:?}, path = {:?} }}",
self.name(),
self.package,
self.dir.to_string_lossy()
)
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
enum Nsh {
Registry,
Source(PathBuf),
Git { checkout: PathBuf, url: String },
}
const VERSION: &str = env!("CARGO_PKG_VERSION");
impl Nsh {
fn of(crates: &[AppCrate]) -> anyhow::Result<Nsh> {
let mut first: Option<(Nsh, String)> = None;
for c in crates {
for dep in nsh_dependencies(c)? {
let name = dep["name"].as_str().unwrap_or_default();
let req = dep["req"].as_str().unwrap_or("*");
let (nsh, what) = match dep["path"].as_str() {
Some(path) => {
let checkout = checkout_of(Path::new(path)).with_context(|| {
format!(
"{} depends on {name} at {path}, which is in no nsh checkout",
c.package
)
})?;
let what =
format!("{} takes {name} from {}", c.package, checkout.display());
let nsh = match dep["git"].as_str() {
Some(url) => Nsh::Git {
checkout,
url: url.into(),
},
None => Nsh::Source(checkout),
};
(nsh, what)
}
None => (
Nsh::Registry,
format!("{} takes {name} {req} from crates.io", c.package),
),
};
match &first {
None => first = Some((nsh, what)),
Some((seen, seen_what)) if *seen != nsh => bail!(
"{what}, but {seen_what}: the app's crates take nsh's crates from one place"
),
Some(_) => {}
}
}
}
let nsh = first.map_or(Nsh::Registry, |(nsh, _)| nsh);
if let Nsh::Git { checkout, .. } = &nsh
&& checkout_version(checkout)? != VERSION
{
return Ok(Nsh::Registry);
}
Ok(nsh)
}
fn source(&self) -> Option<&Path> {
match self {
Nsh::Registry => None,
Nsh::Source(dir) | Nsh::Git { checkout: dir, .. } => Some(dir),
}
}
fn dep(&self, name: &str, extra: &str) -> String {
match self {
Nsh::Registry => format!("{{ version = \"={VERSION}\"{extra} }}"),
Nsh::Source(dir) | Nsh::Git { checkout: dir, .. } => format!(
"{{ path = {:?}{extra} }}",
nsh_crate(dir, name).to_string_lossy()
),
}
}
}
pub(crate) const RUST_APP_PLATFORM: &str = ", default-features = false, features = [\"shader\", \"editor\", \"emoji-picker\", \"highlight\", \"math\", \"plot\", \"rich-editor\", \"term\"]";
const HIGHLIGHT: &str = ", features = [\"highlight\"]";
fn checkout_of(path: &Path) -> Option<PathBuf> {
let path = dunce::canonicalize(path).ok()?;
path.ancestors()
.find(|dir| dir.join("crates/nsh-core/Cargo.toml").is_file())
.map(Path::to_path_buf)
}
fn checkout_version(dir: &Path) -> anyhow::Result<String> {
let manifest = dir.join("Cargo.toml");
let text =
fs::read_to_string(&manifest).with_context(|| format!("read {}", manifest.display()))?;
let mut in_package = false;
for line in text.lines() {
let line = line.trim();
if line.starts_with('[') {
in_package = line == "[workspace.package]";
} else if in_package
&& let Some(value) = line.strip_prefix("version")
&& let Some(value) = value.trim_start().strip_prefix('=')
{
return Ok(value.trim().trim_matches('"').to_owned());
}
}
bail!("{}: no [workspace.package] version", manifest.display())
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct RootConfig {
component: String,
theme: Option<PathBuf>,
appearance: Option<String>,
app_id: Option<String>,
status_bar: Option<String>,
url_schemes: Vec<String>,
window: Option<String>,
translations: Option<PathBuf>,
}
pub(crate) fn is_root(entry: &Path) -> bool {
entry
.file_name()
.is_some_and(|name| name.eq_ignore_ascii_case("nsh.json"))
}
pub(crate) fn of(entry: &Path) -> anyhow::Result<Option<RustApp>> {
let entry = dunce::canonicalize(entry)
.with_context(|| format!("entry not found: {}", entry.display()))?;
let dir = entry.parent().context("entry has no directory")?;
let Ok(text) = fs::read_to_string(dir.join("nsh.json")) else {
return Ok(None);
};
let config: serde_json::Value = serde_json::from_str(&text).context("nsh.json is not JSON")?;
let Some(rust) = config.get("rust") else {
return Ok(None);
};
let crates = crates(dir, rust)?;
let nsh = Nsh::of(&crates)?;
let watched = watched(&crates, nsh.source());
let workspace = dir.join(".nsh/rust");
let retargeted = retargeted(&crates, &nsh)?;
let root = if is_root(&entry) {
Some(root_config(dir, &config, &workspace)?)
} else {
None
};
let app = RustApp {
workspace,
nsh,
crates,
watched,
root,
retargeted,
};
app.write_workspace()?;
Ok(Some(app))
}
fn crates(dir: &Path, rust: &serde_json::Value) -> anyhow::Result<Vec<AppCrate>> {
const WHAT: &str = "nsh.json: \"rust\" is the path of the app's crate (\".\", \"native\") or a list of them (\"native/charts\", \"native/storage\")";
let paths: Vec<&str> = match rust {
serde_json::Value::String(path) => vec![path],
serde_json::Value::Array(paths) if !paths.is_empty() => paths
.iter()
.map(|p| p.as_str().context(WHAT))
.collect::<anyhow::Result<_>>()?,
_ => bail!(WHAT),
};
let mut crates: Vec<(&str, AppCrate)> = Vec::new();
for path in paths {
let dir = dunce::canonicalize(dir.join(path))
.with_context(|| format!("nsh.json: \"rust\": no crate at {path}"))?;
let package = package_name(&dir.join("Cargo.toml"))?;
let krate = AppCrate { package, dir };
if let Some((other, _)) = crates.iter().find(|(_, c)| c.dir == krate.dir) {
bail!("nsh.json: \"rust\" lists the crate {other} twice ({path})");
}
if let Some((other, first)) = crates.iter().find(|(_, c)| c.name() == krate.name()) {
let same = if first.package == krate.package {
format!("are both the package `{}`", krate.package)
} else {
format!(
"are the packages `{}` and `{}`, one name to Cargo",
first.package, krate.package
)
};
bail!(
"nsh.json: \"rust\": {other} and {path} {same}: give each crate a [package] name of its own"
);
}
crates.push((path, krate));
}
let mut specifiers: Vec<(&str, String)> = Vec::new();
for (path, krate) in &crates {
let Some(specifier) = specifier_of(&krate.dir) else {
continue;
};
if let Some((other, _)) = specifiers.iter().find(|(_, s)| *s == specifier) {
bail!(
"nsh.json: \"rust\": {other} and {path} both have `#[native_module({specifier:?})]`: give each crate a specifier of its own, the one its TSX imports it by"
);
}
specifiers.push((path, specifier));
}
Ok(crates.into_iter().map(|(_, c)| c).collect())
}
fn specifier_of(dir: &Path) -> Option<String> {
let manifest = fs::read_to_string(dir.join("Cargo.toml")).ok()?;
let lib = toml_tables(&manifest)
.find_map(|(table, line)| {
(table == "lib")
.then(|| toml_string(line, "path"))
.flatten()
})
.unwrap_or_else(|| "src/lib.rs".to_owned());
let code = fs::read_to_string(dir.join(lib)).ok()?;
code.lines().find_map(|line| {
const ATTR: &str = "native_module";
let attr = line.trim().strip_prefix("#[")?;
let rest = attr[attr.find(ATTR)? + ATTR.len()..].trim_start();
let rest = rest.strip_prefix('(')?.trim_start().strip_prefix('"')?;
Some(rest[..rest.find('"')?].to_owned())
})
}
fn watched(crates: &[AppCrate], source: Option<&Path>) -> Vec<PathBuf> {
let source = source.map(|s| dunce::canonicalize(s).unwrap_or_else(|_| s.to_owned()));
let mut dirs: Vec<PathBuf> = crates.iter().map(|c| c.dir.clone()).collect();
let mut next = 0;
while let Some(dir) = dirs.get(next) {
next += 1;
for dep in path_dependencies(dir) {
if let Ok(dep) = dunce::canonicalize(dep)
&& !source.as_ref().is_some_and(|s| dep.starts_with(s))
&& !dirs.contains(&dep)
{
dirs.push(dep);
}
}
}
dirs
}
fn path_dependencies(dir: &Path) -> Vec<PathBuf> {
let Ok(manifest) = fs::read_to_string(dir.join("Cargo.toml")) else {
return Vec::new();
};
toml_tables(&manifest)
.filter(|(table, _)| {
table
.split('.')
.any(|part| part == "dependencies" || part == "build-dependencies")
})
.filter_map(|(_, line)| toml_string(line, "path"))
.map(|path| dir.join(path))
.collect()
}
fn toml_tables(text: &str) -> impl Iterator<Item = (&str, &str)> {
let mut table = "";
text.lines().filter_map(move |line| {
let line = line.trim();
if line.starts_with('[') {
table = line.trim_start_matches('[');
table = table.split(']').next().unwrap_or_default().trim();
return None;
}
Some((table, line))
})
}
fn toml_string(line: &str, key: &str) -> Option<String> {
let mut rest = line;
while let Some(at) = rest.find(key) {
let before = rest[..at].trim_end();
let after = rest[at + key.len()..].trim_start();
rest = &rest[at + key.len()..];
if !(before.is_empty() || before.ends_with('{') || before.ends_with(',')) {
continue;
}
let Some(value) = after.strip_prefix('=') else {
continue;
};
let value = value.trim_start();
let quote = value.chars().next().filter(|c| *c == '"' || *c == '\'')?;
let value = &value[1..];
return Some(value[..value.find(quote)?].to_owned());
}
None
}
fn root_config(
dir: &Path,
config: &serde_json::Value,
workspace: &Path,
) -> anyhow::Result<RootConfig> {
let component = config
.get("root")
.and_then(|r| r.as_str())
.context("nsh.json: \"root\" is the path of the app's root component in its crate")?
.trim()
.to_owned();
let ident = |s: &str| {
s.chars()
.next()
.is_some_and(|c| c.is_alphabetic() || c == '_')
&& s.chars().all(|c| c.is_alphanumeric() || c == '_')
};
if !component.split("::").all(ident) {
bail!("nsh.json: \"root\": {component:?} is not a Rust path (`App`, `shell::Gallery`)");
}
let theme = match config.get("theme") {
None => None,
Some(serde_json::Value::String(file)) => Some(
dunce::canonicalize(dir.join(file))
.with_context(|| format!("nsh.json: theme: no {file}"))?,
),
Some(inline) => {
let path = workspace.join("theme.json");
write(&path, &inline.to_string())?;
Some(path)
}
};
if let Some(theme) = &theme {
let json = fs::read_to_string(theme)?;
let (_, ignored) = nsh_base::Theme::from_json_lenient(&json)
.map_err(|e| anyhow::anyhow!("nsh.json: theme: {e}"))?;
for what in ignored {
crate::ui::warn(format_args!(
"nsh.json: theme: {what}, ignored (one of a newer NSH, or a typo)"
));
}
}
let appearance = match config.get("appearance").and_then(|a| a.as_str()) {
None | Some("system") => None,
Some(a @ ("light" | "dark")) => Some(a.to_owned()),
Some(other) => {
crate::ui::warn(format_args!(
"nsh.json: appearance: {other:?} is not \"light\", \"dark\" or \"system\", ignored (one of a newer NSH, or a typo)"
));
None
}
};
let status_bar = match config.get("statusBar") {
None => None,
Some(v) => {
let (_, ignored) = nsh_core::StatusBarSetting::from_json_lenient(v);
for what in ignored {
crate::ui::warn(format_args!(
"nsh.json: statusBar: {what}, ignored (one of a newer NSH, or a typo)"
));
}
Some(v.to_string())
}
};
let mut url_schemes = match config.get("schemes") {
None => Vec::new(),
Some(v) => {
let (schemes, ignored) = nsh_core::url_scheme::schemes_from_json_lenient(v);
for what in ignored {
crate::ui::warn(format_args!(
"nsh.json: {what}, ignored (one of a newer NSH, or a typo)"
));
}
schemes
}
};
if nsh_core::file_type::declares_file_types(config) {
url_schemes.push(nsh_core::file_type::FILE_SCHEME.to_owned());
}
let window = match config.get("window") {
None => None,
Some(v) => {
nsh_core::desktop::MainWindowOptions::from_json(v)
.map_err(|e| anyhow::anyhow!("nsh.json: window: {e}"))?;
Some(v.to_string())
}
};
let translations = nsh_i18n::read_translations(dir).map_err(|e| anyhow::anyhow!(e))?;
let translations = if translations.is_empty() {
None
} else {
let path = workspace.join("translations.json");
write(&path, &nsh_i18n::translations_json(&translations))?;
Some(path)
};
Ok(RootConfig {
component,
theme,
appearance,
app_id: crate::package::app_id(&dir.join("nsh.json")).ok(),
status_bar,
url_schemes,
window,
translations,
})
}
const NSH_CRATES: &[&str] = &[
"nsh-macros",
"nsh-core",
"nsh-base",
"nsh-package",
"nsh-js",
"nsh-engine",
"nsh-testing",
"nsh-bundler",
"nsh-dev",
"nsh-platform-apple",
"nsh-platform-ios",
"nsh-platform-android",
"nsh-platform-linux",
"nsh-platform-macos",
"nsh-platform-windows",
"nsh-platform-web",
"nsh-platform-gpui",
];
fn nsh_dependencies(c: &AppCrate) -> anyhow::Result<Vec<serde_json::Value>> {
let manifest = c.dir.join("Cargo.toml");
let output = Command::new(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!(
"nsh.json: \"rust\": 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 = metadata["packages"]
.as_array()
.and_then(|packages| {
packages
.iter()
.find(|p| p["name"].as_str() == Some(&c.package))
})
.with_context(|| format!("cargo metadata has no package {}", c.package))?;
let mut dependencies: Vec<_> = package["dependencies"]
.as_array()
.into_iter()
.flatten()
.filter(|dep| {
dep["name"]
.as_str()
.is_some_and(|n| NSH_CRATES.contains(&n))
})
.cloned()
.collect();
if dependencies.iter().any(|dep| {
dep["source"]
.as_str()
.is_some_and(|s| s.starts_with("git+"))
}) {
let output = Command::new(cargo())
.args(["metadata", "--format-version", "1", "--manifest-path"])
.arg(&manifest)
.output()
.context("resolve the app's nsh Git dependencies")?;
if !output.status.success() {
bail!(
"cargo cannot resolve {}:\n{}",
manifest.display(),
String::from_utf8_lossy(&output.stderr).trim()
);
}
let resolved: serde_json::Value =
serde_json::from_slice(&output.stdout).context("cargo metadata's resolved output")?;
resolve_git_dependencies(&mut dependencies, &resolved, &c.package)?;
}
Ok(dependencies)
}
fn resolve_git_dependencies(
dependencies: &mut [serde_json::Value],
metadata: &serde_json::Value,
package: &str,
) -> anyhow::Result<()> {
let packages = metadata["packages"]
.as_array()
.context("cargo metadata packages")?;
let app = packages
.iter()
.find(|p| p["name"] == package)
.context("cargo metadata app package")?;
let node = metadata["resolve"]["nodes"]
.as_array()
.and_then(|nodes| nodes.iter().find(|node| node["id"] == app["id"]))
.context("cargo metadata app dependencies")?;
for dep in dependencies {
let name = dep["rename"]
.as_str()
.or(dep["name"].as_str())
.unwrap_or_default()
.replace('-', "_");
let Some(edge) = node["deps"]
.as_array()
.and_then(|deps| deps.iter().find(|d| d["name"] == name))
else {
continue;
};
let resolved = packages
.iter()
.find(|p| p["id"] == edge["pkg"])
.context("cargo metadata resolved dependency")?;
if let Some(source) = resolved["source"]
.as_str()
.and_then(|s| s.strip_prefix("git+"))
{
let url = source
.split(['?', '#'])
.next()
.context("Git repository URL")?;
let path = Path::new(
resolved["manifest_path"]
.as_str()
.context("Git manifest path")?,
)
.parent()
.context("Git crate directory")?;
dep["path"] = path.to_string_lossy().as_ref().into();
dep["git"] = url.into();
}
}
Ok(())
}
fn package_metadata(dir: &Path) -> anyhow::Result<serde_json::Value> {
let manifest = dir.join("Cargo.toml");
let output = Command::new(cargo())
.args([
"metadata",
"--no-deps",
"--format-version",
"1",
"--manifest-path",
])
.arg(&manifest)
.output()
.context("run cargo metadata")?;
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 path = dunce::canonicalize(&manifest)?;
metadata["packages"]
.as_array()
.and_then(|packages| {
packages.iter().find(|p| {
p["manifest_path"]
.as_str()
.and_then(|m| dunce::canonicalize(m).ok())
.is_some_and(|m| m == path)
})
})
.cloned()
.with_context(|| format!("cargo metadata has no package at {}", manifest.display()))
}
fn is_nsh(dep: &serde_json::Value) -> bool {
dep["name"]
.as_str()
.is_some_and(|name| NSH_CRATES.contains(&name))
}
fn retargeted(crates: &[AppCrate], nsh: &Nsh) -> anyhow::Result<bool> {
if *nsh != Nsh::Registry {
return Ok(false);
}
let this = semver::Version::parse(VERSION).context("nsh's own version")?;
for c in crates {
let package = package_metadata(&c.dir)?;
let other = package["dependencies"]
.as_array()
.into_iter()
.flatten()
.filter(|dep| is_nsh(dep) && dep["kind"].as_str() != Some("dev"))
.any(|dep| {
dep["source"]
.as_str()
.is_some_and(|s| s.starts_with("git+"))
|| !dep["req"]
.as_str()
.and_then(|r| semver::VersionReq::parse(r).ok())
.is_some_and(|r| r.matches(&this))
});
if other {
return Ok(true);
}
}
Ok(false)
}
fn retargeted_dependency(dep: &serde_json::Value, nsh: &Nsh, local: Option<&Path>) -> String {
let name = dep["name"].as_str().unwrap_or_default();
let mut extra = format!(", package = {name:?}");
if dep["uses_default_features"].as_bool() == Some(false) {
extra.push_str(", default-features = false");
}
if dep["optional"].as_bool() == Some(true) {
extra.push_str(", optional = true");
}
if dep["features"].as_array().is_some_and(|f| !f.is_empty()) {
extra.push_str(&format!(", features = {}", dep["features"]));
}
if is_nsh(dep) {
return nsh.dep(name, &extra);
}
if let Some(dir) = local {
return format!("{{ path = {:?}{extra} }}", dir.to_string_lossy());
}
if let Some(path) = dep["path"].as_str() {
return format!("{{ path = {path:?}{extra} }}");
}
if let Some(source) = dep["source"].as_str().and_then(|s| s.strip_prefix("git+")) {
let (url, query) = source.split_once('?').unwrap_or((source, ""));
let reference = query.split('#').next().unwrap_or_default();
let reference = reference
.split_once('=')
.map(|(key, value)| format!(", {key} = {value:?}"))
.unwrap_or_default();
return format!(
"{{ git = {:?}{reference}{extra} }}",
url.split('#').next().unwrap_or(url)
);
}
let registry = dep["registry"]
.as_str()
.map(|r| format!(", registry-index = {r:?}"))
.unwrap_or_default();
format!("{{ version = {}{registry}{extra} }}", dep["req"])
}
fn write_retargeted(ws: &Path, dir: &Path, nsh: &Nsh) -> anyhow::Result<PathBuf> {
let package = package_metadata(dir)?;
let name = package["name"].as_str().context("package name")?;
let out = ws.join("app-crates").join(name);
let targets = package["targets"].as_array().context("package targets")?;
let kinds = |t: &serde_json::Value, kind: &str| {
t["kind"]
.as_array()
.is_some_and(|k| k.iter().any(|v| v == kind))
};
let lib = targets
.iter()
.find(|t| {
["lib", "rlib", "dylib", "cdylib", "staticlib", "proc-macro"]
.iter()
.any(|kind| kinds(t, kind))
})
.with_context(|| format!("{name} has no library"))?;
let mut text = format!(
"# Generated by nsh from {}: do not edit.\n[package]\nname = {name:?}\nversion = {}\nedition = {}\n",
dir.join("Cargo.toml").display(),
package["version"],
package["edition"],
);
if package["links"].is_string() {
text.push_str(&format!("links = {}\n", package["links"]));
}
match targets.iter().find(|t| kinds(t, "custom-build")) {
Some(build) => text.push_str(&format!("build = {}\n", build["src_path"])),
None => text.push_str("build = false\n"),
}
text.push_str(&format!(
"\n[lib]\nname = {}\npath = {}\ncrate-type = {}\n\n[features]\n",
lib["name"], lib["src_path"], lib["crate_types"],
));
if let Some(features) = package["features"].as_object() {
for (feature, enables) in features {
text.push_str(&format!("{feature:?} = {enables}\n"));
}
}
let mut tables = std::collections::BTreeMap::<String, String>::new();
for dep in package["dependencies"].as_array().into_iter().flatten() {
let kind = dep["kind"].as_str();
if kind == Some("dev") {
continue;
}
let local = match dep["path"].as_str() {
Some(path) if !is_nsh(dep) => {
let path = Path::new(path);
let uses_nsh = package_metadata(path)?["dependencies"]
.as_array()
.is_some_and(|deps| deps.iter().any(is_nsh));
if uses_nsh {
Some(write_retargeted(ws, path, nsh)?)
} else {
None
}
}
_ => None,
};
let table = if kind == Some("build") {
"build-dependencies"
} else {
"dependencies"
};
let table = match dep["target"].as_str() {
Some(target) => format!("target.{target:?}.{table}"),
None => table.to_owned(),
};
let key = dep["rename"]
.as_str()
.or(dep["name"].as_str())
.unwrap_or_default();
tables.entry(table).or_default().push_str(&format!(
"{key:?} = {}\n",
retargeted_dependency(dep, nsh, local.as_deref())
));
}
for (table, lines) in tables {
text.push_str(&format!("\n[{table}]\n{lines}"));
}
write(&out.join("Cargo.toml"), &text)?;
write(&out.join("nsh-source-dir"), &dir.to_string_lossy())?;
Ok(out)
}
fn package_name(manifest: &Path) -> anyhow::Result<String> {
let text = fs::read_to_string(manifest)
.with_context(|| format!("nsh.json: \"rust\": no {}", manifest.display()))?;
let mut in_package = false;
for line in text.lines() {
let line = line.trim();
if line.starts_with('[') {
in_package = line == "[package]";
} else if in_package
&& let Some(value) = line.strip_prefix("name")
&& let Some(value) = value.trim_start().strip_prefix('=')
{
return Ok(value.trim().trim_matches('"').to_owned());
}
}
bail!("{}: no [package] name", manifest.display())
}
fn bin_name(packages: &[&str], what: &str) -> String {
match packages {
[] | [_] => format!("nsh-app-{}-{what}", packages.join("-")),
[first, ..] => {
let mut sorted = packages.to_vec();
sorted.sort_unstable();
let digest = Sha256::digest(sorted.join("\n"));
let hex: String = digest.iter().take(4).map(|b| format!("{b:02x}")).collect();
format!("nsh-app-{first}-{hex}-{what}")
}
}
}
fn write(path: &Path, contents: &str) -> anyhow::Result<()> {
if fs::read_to_string(path).is_ok_and(|old| old == contents) {
return Ok(());
}
if let Some(dir) = path.parent() {
fs::create_dir_all(dir)?;
}
fs::write(path, contents).with_context(|| format!("write {}", path.display()))
}
impl RustApp {
fn bin(&self, what: &str) -> String {
let packages: Vec<&str> = self.crates.iter().map(|c| c.package.as_str()).collect();
bin_name(&packages, what)
}
fn packages(&self) -> String {
let packages: Vec<&str> = self.crates.iter().map(|c| c.package.as_str()).collect();
packages.join(", ")
}
pub(crate) fn is_rust(&self, path: &Path) -> bool {
self.is_rust_in(path, self.target_dir().ok().as_deref())
}
fn is_rust_in(&self, path: &Path, target: Option<&Path>) -> bool {
if let Some(target) = target
&& (path.starts_with(target)
|| fs::canonicalize(target).is_ok_and(|t| path.starts_with(t)))
{
return false;
}
self.watched.iter().any(|dir| {
let Ok(rest) = path.strip_prefix(dir) else {
return false;
};
!rest.starts_with("target")
&& !rest.starts_with(".nsh")
&& !rest.starts_with(".git")
&& (rest.starts_with("assets")
|| path.extension().is_some_and(|e| e == "rs")
|| path.file_name().is_some_and(|n| n == "Cargo.toml"))
})
}
pub(crate) fn crates(&self) -> &[AppCrate] {
&self.crates
}
pub(crate) fn watched(&self) -> &[PathBuf] {
&self.watched
}
fn watch_with(&self, watcher: &mut impl notify::Watcher) -> anyhow::Result<()> {
for dir in &self.watched {
watcher
.watch(dir, notify::RecursiveMode::Recursive)
.with_context(|| format!("watch {}", dir.display()))?;
}
Ok(())
}
pub(crate) fn wait_after(&self, error: &anyhow::Error) -> anyhow::Result<()> {
ui::report(error);
ui::detail(ui::dim("fix the code and save: nsh builds it again"));
let (tx, rx) = std::sync::mpsc::channel();
let mut watcher =
notify::recommended_watcher(move |event: notify::Result<notify::Event>| {
if let Ok(event) = event {
let _ = tx.send(event);
}
})?;
self.watch_with(&mut watcher)?;
loop {
if ui::stop_requested() {
return Ok(());
}
let event = match rx.recv_timeout(std::time::Duration::from_millis(200)) {
Ok(event) => event,
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => continue,
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => {
bail!("stopped watching the app's Rust code");
}
};
if !matches!(event.kind, notify::EventKind::Access(_))
&& event.paths.iter().any(|p| self.is_rust(p))
{
std::thread::sleep(std::time::Duration::from_millis(100));
while rx.try_recv().is_ok() {}
return Ok(());
}
}
}
fn crates_what(&self) -> String {
match self.crates.as_slice() {
[one] => format!("the Rust crate {}", one.package),
_ => format!("the Rust crates {}", self.packages()),
}
}
pub(crate) fn describe(&self) -> anyhow::Result<Vec<SetDescription>> {
let what = self.crates_what();
let program = self.build(
&self.workspace,
&self.bin("describe"),
None,
false,
&[],
&what,
)?;
let cache = self.workspace.join("description.json");
let stamp = stamp(&program)?;
let cached = fs::read_to_string(&cache).ok().and_then(|text| {
let cached: serde_json::Value = serde_json::from_str(&text).ok()?;
(cached["stamp"].as_str()? == stamp)
.then(|| cached["description"].as_str().map(str::to_owned))?
});
let text = match cached {
Some(text) => text,
None => {
let output = Command::new(&program)
.output()
.with_context(|| format!("run {}", program.display()))?;
if !output.status.success() {
return Err(ui::failure(
format!("describing {what} failed ({})", output.status),
String::from_utf8_lossy(&output.stderr),
));
}
let text = String::from_utf8(output.stdout)
.with_context(|| format!("the description of {what} is not UTF-8"))?;
write(
&cache,
&serde_json::json!({ "stamp": stamp, "description": text }).to_string(),
)?;
text
}
};
read_description(&text, &self.crates, &what)
}
pub(crate) fn test_runner(&self) -> anyhow::Result<PathBuf> {
self.build(
&self.workspace,
&self.bin("test"),
None,
false,
&[],
&format!("the headless runner of {}", self.packages()),
)
}
fn write_workspace(&self) -> anyhow::Result<()> {
let src = |c: &str| self.nsh.dep(c, "");
let app = self.dependencies();
let package = |name: &str| {
format!(
"[package]\nname = {:?}\nversion = \"0.0.0\"\nedition = \"2024\"\npublish = false\n",
self.bin(name)
)
};
let sets = self.sets();
let ws = &self.workspace;
let web_dev = self.web_dev_profile();
let rust = if self.root.is_some() {
", \"rust-linux\", \"rust-macos\", \"rust-windows\", \"rust-ios\", \"rust-android\", \"rust-web\", \"rust-app\""
} else {
""
};
self.write_workspace_manifest(
ws,
&format!("# Generated by nsh for the app's Rust crate (nsh.json \"rust\"): do not edit.\n\
[workspace]
resolver = \"3\"
members = [\"describe\", \"test\", \"ios-host\", \"ios-app\", \"macos-host\", \"macos-app\", \"linux-host\", \"linux-app\", \"windows-host\", \"windows-app\", \"android\", \"web\"{rust}]
# GPUI's and the terminal's hosts are workspaces of their own, written when
# they are built (`RustApp::gpui_workspace`, `RustApp::tui_workspace`): other
# builds never resolve their crates.
exclude = [\"gpui\", \"tui\"]
# The web runtime, as nsh builds its own (nsh's `Cargo.toml`): for size.
[profile.web]
inherits = \"release\"
opt-level = \"z\"
lto = \"fat\"
codegen-units = 1
strip = true
panic = \"abort\"
{web_dev}"),
false,
)?;
for old in ["cli", "trace", "rust-trace", "tui-host"] {
let _ = fs::remove_dir_all(ws.join(old));
}
let _ = fs::remove_file(ws.join("app.json"));
if self.retargeted {
for c in &self.crates {
write_retargeted(ws, &c.dir, &self.nsh)?;
}
}
self.write_describe(ws)?;
self.write_test(ws)?;
for (dir, lib, what) in [
("ios-host", "nsh-ios-host", "development host"),
("ios-app", "nsh-ios-app", "release runner"),
("linux-host", "nsh-linux-host", "development host"),
("linux-app", "nsh-linux-app", "release runner"),
("macos-host", "nsh-macos-host", "development host"),
("macos-app", "nsh-macos-app", "release runner"),
("windows-host", "nsh-windows-host", "development host"),
("windows-app", "nsh-windows-app", "release runner"),
] {
self.write_host(ws, dir, lib, what)?;
}
self.write_web(ws)?;
if let Some(root) = &self.root {
self.write_rust_runners(ws, root)?;
}
write(
&ws.join("android/Cargo.toml"),
&format!(
"{}\n[lib]\nname = {:?}\npath = \"lib.rs\"\ncrate-type = [\"cdylib\"]\n\n[features]\ndev = [\"nsh-platform-android/dev\"]\n\n\
[dependencies]\nnsh-core = {}\nnsh-platform-android = {}\nnsh-base = {}\n{app}\n",
package("android"),
self.android_lib(false),
src("nsh-core"),
src("nsh-platform-android"),
self.nsh.dep("nsh-base", HIGHLIGHT),
),
)?;
write(&ws.join("android/build.rs"), ANDROID_BUILD_RS)?;
let android = "#[cfg(target_os = \"android\")]\n";
let root = match &self.root {
Some(root) => format!(
"{android}nsh::root!(app_root());\n{}",
self.root_fn(android, root)
),
None => String::new(),
};
write(
&ws.join("android/lib.rs"),
&format!(
"//! The app's `libnsh.so`: the Android runtime with `nsh/base` and the\n//! app's Rust components.\n\n\
{android}nsh::ui_sets!({sets});\n{root}{}",
self.app_sets(android)
),
)?;
Ok(())
}
fn write_describe(&self, ws: &Path) -> anyhow::Result<()> {
write(
&ws.join("describe/Cargo.toml"),
&format!(
"[package]\nname = {:?}\nversion = \"0.0.0\"\nedition = \"2024\"\npublish = false\n\n\
[[bin]]\nname = {:?}\npath = \"main.rs\"\n\n[dependencies]\nnsh-core = {}\n{}\n",
self.bin("describe"),
self.bin("describe"),
self.nsh.dep("nsh-core", ", features = [\"declarations\"]"),
self.dependencies(),
),
)?;
let calls: Vec<String> = self
.crates
.iter()
.map(|c| format!("{}()", set_fn(c)))
.collect();
write(
&ws.join("describe/main.rs"),
&format!(
"//! Describes the app's UI sets for `nsh`: one JSON document on standard\n\
//! output (nsh-core `ui::description`).\n\n\
fn main() {{\n let sets = vec![{}];\n \
println!(\"{{}}\", nsh_core::element::description::Description::of(&sets).to_json());\n}}\n{}",
calls.join(", "),
self.app_sets("")
),
)
}
fn write_test(&self, ws: &Path) -> anyhow::Result<()> {
write(
&ws.join("test/Cargo.toml"),
&format!(
"[package]\nname = {:?}\nversion = \"0.0.0\"\nedition = \"2024\"\npublish = false\n\n\
[[bin]]\nname = {:?}\npath = \"main.rs\"\n\n[dependencies]\n\
nsh-core = {}\nnsh-testing = {}\nnsh-base = {}\n{}\n",
self.bin("test"),
self.bin("test"),
self.nsh.dep("nsh-core", ""),
self.nsh.dep("nsh-testing", ", features = [\"js\"]"),
self.nsh.dep("nsh-base", ""),
self.dependencies(),
),
)?;
let (make_root, root_fn) = match &self.root {
Some(root) => ("Some(app_root)", self.root_fn("", root)),
None => ("None", String::new()),
};
write(
&ws.join("test/main.rs"),
&format!(
"//! The app's headless runner (`nsh test`): its test files and the apps\n\
//! they open, with `nsh/base` and the app's Rust components.\n\n\
fn main() -> std::process::ExitCode {{\n \
nsh_testing::runner({}, {make_root})\n}}\n{}{root_fn}",
self.sets(),
self.app_sets(""),
),
)
}
fn write_rust_runners(&self, ws: &Path, root: &RootConfig) -> anyhow::Result<()> {
for dir in ["rust-linux", "rust-macos", "rust-windows", "rust-ios"] {
self.write_rust_runner(ws, dir, root)?;
}
let app = self.dependencies();
let sets = self.sets();
let android = "#[cfg(target_os = \"android\")]\n";
write(
&ws.join("rust-android/Cargo.toml"),
&format!(
"[package]\nname = {:?}\nversion = \"0.0.0\"\nedition = \"2024\"\npublish = false\n\n\
[lib]\nname = {:?}\npath = \"lib.rs\"\ncrate-type = [\"cdylib\"]\n\n\
[dependencies]\nnsh-core = {}\nnsh-base = {}\n{app}\n\n\
[target.'cfg(target_os = \"android\")'.dependencies]\n\
nsh-platform-android = {}\n",
self.bin("rust-android"),
self.android_lib(true),
self.nsh.dep("nsh-core", ""),
self.nsh.dep("nsh-base", HIGHLIGHT),
self.nsh.dep("nsh-platform-android", RUST_APP_PLATFORM),
),
)?;
write(
&ws.join("rust-android/lib.rs"),
&format!(
"//! The Rust app's `libnsh.so`: its root, which `AppActivity` runs, with\n\
//! `nsh/base` and the app's components; no JS runtime.\n\n\
{android}nsh::ui_sets!({sets});\n{android}nsh::main!(app_root());\n\n\
// Off Android it is empty, as nsh-platform-android is.\n\
#[cfg(not(target_os = \"android\"))]\nuse nsh_core as _;\n\
#[cfg(not(target_os = \"android\"))]\nuse nsh_base as _;\n{}{}",
self.app_sets(android),
self.root_fn(android, root),
),
)?;
write(&ws.join("rust-android/build.rs"), ANDROID_BUILD_RS)?;
let wasm = "#[cfg(target_arch = \"wasm32\")]\n";
write(
&ws.join("rust-web/Cargo.toml"),
&format!(
"[package]\nname = {:?}\nversion = \"0.0.0\"\nedition = \"2024\"\npublish = false\n\n\
[lib]\nname = {:?}\npath = \"lib.rs\"\ncrate-type = [\"cdylib\"]\n\n\
[features]\n# What `nsh package` links the app's runtime from (its relocations kept).\n\
linkable = [\"nsh-platform-web/linkable\"]\n\n\
[dependencies]\nnsh-core = {}\nnsh-base = {}\n{app}\n\n\
[target.'cfg(target_arch = \"wasm32\")'.dependencies]\n\
nsh-platform-web = {}\n",
self.bin("rust-web"),
self.rust_web_lib(),
self.nsh.dep("nsh-core", ""),
self.nsh.dep("nsh-base", HIGHLIGHT),
self.nsh
.dep("nsh-platform-web", ", default-features = false"),
),
)?;
self.write_rust_app(ws, root)?;
write(&ws.join("rust-web/build.rs"), WEB_BUILD_RS)?;
let sets = self.rust_web_sets();
write(
&ws.join("rust-web/lib.rs"),
&format!(
"//! The Rust app's web runtime: its root, which `nsh.js` runs, with the\n\
//! `nsh/base` and the app's components; no QuickJS. `nsh/base` is its\n\
//! core part: the app makes its components by type, which links\n\
//! only those it uses.\n\n\
{wasm}nsh_platform_web::rust_app!({sets}, app_root());\n\n\
// Off WebAssembly it is empty, as nsh-platform-web is.\n\
#[cfg(not(target_arch = \"wasm32\"))]\nuse nsh_core as _;\n\
#[cfg(not(target_arch = \"wasm32\"))]\nuse nsh_base as _;\n{}{}",
self.app_sets(wasm),
self.root_fn(wasm, root),
),
)
}
fn write_rust_app(&self, ws: &Path, root: &RootConfig) -> anyhow::Result<()> {
write(
&ws.join("rust-app/Cargo.toml"),
&format!(
"[package]\nname = {:?}\nversion = \"0.0.0\"\nedition = \"2024\"\npublish = false\n\n\
[lib]\nname = \"nsh_rust_app\"\npath = \"lib.rs\"\n\n\
[dependencies]\nnsh-core = {}\nnsh-base = {}\n{}\n",
self.bin("rust-app"),
self.nsh.dep("nsh-core", ""),
self.nsh.dep("nsh-base", HIGHLIGHT),
self.dependencies(),
),
)?;
write(
&ws.join("rust-app/lib.rs"),
&format!(
"//! The Rust app's UI sets and root, on no platform: what the runner of a\n\
//! platform implemented outside NSH starts (`docs/dev/platforms.md`).\n\n\
/// The UI sets the app serves: `nsh/base`, then the app's crates'.\n\
pub fn ui_sets() -> Vec<nsh_core::element::Registry> {{\n {}\n}}\n\n\
/// The app's crates' UI sets without `nsh/base`'s: what a platform that runs\n\
/// no TSX serves (their files, such as fonts), so that its module links only\n\
/// the base components the app's code uses.\n\
pub fn app_ui_sets() -> Vec<nsh_core::element::Registry> {{\n {}\n}}\n\n\
/// The app's root (`nsh.json` `\"root\"`), with its theme, appearance and status bar.\n\
pub fn root() -> nsh_core::element::Root {{\n app_root()\n}}\n{}{}",
self.sets(),
self.app_only_sets(),
self.app_sets(""),
self.root_fn("", root),
),
)
}
fn write_rust_runner(&self, ws: &Path, dir: &str, root: &RootConfig) -> anyhow::Result<()> {
let app = self.dependencies();
let sets = self.sets();
let (os, lib, platform, exit) = match dir {
"rust-linux" => (
"target_os = \"linux\"",
"nsh-linux-app",
"nsh-platform-linux",
true,
),
"rust-macos" => (
"target_os = \"macos\"",
"nsh-macos-app",
"nsh-platform-macos",
true,
),
"rust-windows" => ("windows", "nsh-windows-app", "nsh-platform-windows", true),
_ => (
"target_os = \"ios\"",
"nsh-ios-app",
"nsh-platform-ios",
false,
),
};
write(
&ws.join(dir).join("Cargo.toml"),
&format!(
"[package]\nname = {:?}\nversion = \"0.0.0\"\nedition = \"2024\"\npublish = false\n\n\
[[bin]]\nname = {:?}\npath = \"main.rs\"\n\n\
[dependencies]\n{lib} = {}\n\
nsh-core = {}\nnsh-base = {}\n{app}\n\n\
[target.'cfg({os})'.dependencies]\n{platform} = {}\n",
self.bin(dir),
self.bin(dir),
self.nsh.dep(lib, ", default-features = false"),
self.nsh.dep("nsh-core", ""),
self.nsh.dep("nsh-base", HIGHLIGHT),
self.nsh.dep(platform, RUST_APP_PLATFORM),
),
)?;
let lib = lib.replace('-', "_");
let platform = platform.replace('-', "_");
let cfg = format!("#[cfg({os})]\n");
let subsystem = if dir == "rust-windows" {
"\n#![cfg_attr(windows, windows_subsystem = \"windows\")]\n"
} else {
""
};
let (ret, tail) = if exit {
(" -> std::process::ExitCode", "")
} else {
("", ";")
};
let (set_components, components_fn) = match components_platform(dir) {
Some(key) => (
format!("\n {platform}::set_components(app_components);"),
self.app_components(&cfg, &platform, key),
),
None => (String::new(), String::new()),
};
write(
&ws.join(dir).join("main.rs"),
&format!(
"//! The Rust app's executable: its root with `nsh/base` and the app's\n\
//! components, on {lib}'s runner; no JS runtime.\n{subsystem}\n\
fn main(){ret} {{\n \
#[cfg({os})]\n {{\n \
{platform}::set_ui_sets({sets});{set_components}\n \
{lib}::run_rust(app_root()){tail}\n }}\n \
#[cfg(not({os}))]\n {lib}::main(){tail}\n}}\n{}{components_fn}{}",
self.app_sets(&cfg),
self.root_fn(&cfg, root),
),
)
}
fn rust_web_lib(&self) -> String {
self.bin("rust-web").replace('-', "_")
}
fn android_lib(&self, rust: bool) -> String {
self.bin(if rust { "rust-android" } else { "android" })
.replace('-', "_")
}
fn write_web(&self, ws: &Path) -> anyhow::Result<()> {
let src = |c: &str| self.nsh.dep(c, "");
let app = self.dependencies();
let sets = self.sets();
let wasm = "#[cfg(target_arch = \"wasm32\")]\n";
write(
&ws.join("web/Cargo.toml"),
&format!(
"[package]\nname = {:?}\nversion = \"0.0.0\"\nedition = \"2024\"\npublish = false\n\n\
[lib]\nname = {:?}\npath = \"lib.rs\"\ncrate-type = [\"cdylib\"]\n\n\
[features]\n# The runtime of published apps: TSX on the browser's JavaScript engine.\n\
browser = [\"nsh-platform-web/browser\"]\n\
# The development host's: hot updates, and what `nsh test --target web` asks about.\n\
dev = [\"nsh-platform-web/dev\"]\n # What `nsh package` links the app's runtime from (its relocations kept).\n\
linkable = [\"nsh-platform-web/linkable\"]\n\n\
[dependencies]\nnsh-core = {}\nnsh-base = {}\n{app}\n\n\
[target.'cfg(target_arch = \"wasm32\")'.dependencies]\nnsh-platform-web = {}\n",
self.bin("web"),
self.web_lib(),
src("nsh-core"),
self.nsh.dep("nsh-base", HIGHLIGHT),
self.nsh
.dep("nsh-platform-web", ", default-features = false"),
),
)?;
write(&ws.join("web/build.rs"), WEB_BUILD_RS)?;
let (register, root) = match &self.root {
Some(root) => (
format!("{wasm}nsh_platform_web::rust_app!({sets}, app_root());\n"),
self.root_fn(wasm, root),
),
None => (self.set_gates(wasm), String::new()),
};
write(
&ws.join("web/lib.rs"),
&format!(
"//! The app's web runtime: `nsh.wasm` with `nsh/base` and the app's\n//! Rust components.\n\n\
{register}{}{root}\n\
// Off WebAssembly it is empty, as nsh-platform-web is.\n\
#[cfg(not(target_arch = \"wasm32\"))]\nuse nsh_core as _;\n\
#[cfg(not(target_arch = \"wasm32\"))]\nuse nsh_base as _;\n",
self.app_sets(wasm)
),
)
}
fn web_dev_profile(&self) -> String {
let mut profile = String::from(
"
# The development web runtime (`nsh run --target web`): built again on
# every change to the app's Rust code, so incremental and without LTO; the
# app's crates at -O1, the others for size.
[profile.web-dev]
inherits = \"dev\"
opt-level = 1
debug = false
debug-assertions = false
overflow-checks = false
panic = \"abort\"
[profile.web-dev.package.\"*\"]
opt-level = \"s\"
",
);
for c in &self.crates {
profile.push_str(&format!(
"[profile.web-dev.package.{:?}]\nopt-level = 1\n",
c.package
));
}
profile
}
fn web_lib(&self) -> String {
self.bin("web").replace('-', "_")
}
pub(crate) fn web(
&self,
kind: crate::runtime::WebKind,
build: bool,
) -> anyhow::Result<Option<crate::runtime::WebRuntime>> {
self.web_runtime(kind, build, self.root.is_some())
}
pub(crate) fn test_web(&self) -> anyhow::Result<crate::runtime::WebRuntime> {
self.web_runtime(crate::runtime::WebKind::Dev, true, false)?
.context("no web runtime")
}
fn web_runtime(
&self,
kind: crate::runtime::WebKind,
build: bool,
rust: bool,
) -> anyhow::Result<Option<crate::runtime::WebRuntime>> {
const WASM: &str = "wasm32-unknown-unknown";
let out = self.workspace.join("web-runtime");
let wasm = if rust {
out.join("nsh-rust.wasm")
} else {
out.join(kind.file())
};
let stem = wasm
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("nsh")
.to_owned();
let glue = out.join(format!("{stem}-bindgen.js"));
let mut runtime = crate::runtime::WebRuntime {
glue: glue.exists().then_some(glue),
wasm,
js: match &self.nsh {
Nsh::Source(source)
| Nsh::Git {
checkout: source, ..
} => source.join("crates/nsh-platform-web/web/nsh.js"),
Nsh::Registry => out.join("nsh.js"),
},
syntaxes: crate::runtime::syntaxes_in(&out),
};
if !build {
return Ok((runtime.wasm.exists() && runtime.js.exists()).then_some(runtime));
}
let mut cmd = Command::new(cargo());
let ar = format!("AR_{}", WASM.replace('-', "_"));
if env::var_os(&ar).is_none() && env::var_os("AR").is_none() {
cmd.env(&ar, "llvm-ar");
}
let features: &[&str] = match kind {
crate::runtime::WebKind::Browser if rust => &["--features", "linkable"],
_ if rust => &[],
crate::runtime::WebKind::Browser => &["--features", "browser,linkable"],
crate::runtime::WebKind::Dev => &["--features", "dev"],
};
let (package, artifact) = if rust {
(
self.bin("rust-web"),
format!("{}.wasm", self.rust_web_lib()),
)
} else {
(self.bin("web"), format!("{}.wasm", self.web_lib()))
};
let build = Build {
workspace: &self.workspace,
package: &package,
triple: Some(WASM),
release: false,
profile: Some(web_profile(kind)),
extra: features,
artifact: &artifact,
what: &format!("the web runtime of {}", self.packages()),
};
let built = self.build_with(cmd, &build)?;
fs::create_dir_all(&out)?;
runtime.glue = crate::bindgen::install(&built, &out, &stem)?;
if self.nsh == Nsh::Registry {
let js = self.registry_crate("nsh-platform-web")?.join("web/nsh.js");
crate::runtime::copy_browser_host(
js.parent().context("web runtime has no parent")?,
&out,
)?;
fs::copy(&js, &runtime.js)
.with_context(|| format!("copy {} to {}", js.display(), runtime.js.display()))?;
}
runtime.syntaxes = Some(crate::runtime::write_syntaxes(
&self.workspace.join("Cargo.toml"),
&out,
)?);
Ok(Some(runtime))
}
fn registry_crate(&self, name: &str) -> anyhow::Result<PathBuf> {
crate::runtime::package_dir(&self.workspace.join("Cargo.toml"), name, Some(VERSION))
}
fn gpui_workspace(&self) -> anyhow::Result<PathBuf> {
let ws = self.workspace.join("gpui");
self.write_workspace_manifest(
&ws,
"# Generated by nsh for the app's Rust crate (nsh.json \"rust\"): do not edit.\n\
[workspace]\nresolver = \"3\"\nmembers = [\"gpui-host\"]\n",
true,
)?;
self.write_host(&ws, "gpui-host", "nsh-gpui-host", "development host")?;
Ok(ws)
}
fn tui_workspace(&self) -> anyhow::Result<PathBuf> {
let ws = self.workspace.join("tui");
self.write_workspace_manifest(
&ws,
"# Generated by nsh for the app's Rust crate (nsh.json \"rust\"): do not edit.\n\
[workspace]\nresolver = \"3\"\nmembers = [\"tui-host\"]\n",
false,
)?;
self.write_tui_host(&ws)?;
Ok(ws)
}
fn write_workspace_manifest(&self, ws: &Path, base: &str, gpui: bool) -> anyhow::Result<()> {
let path = ws.join("Cargo.toml");
let mut manifest = base.to_owned();
for package in ["quickjs-jit-sys", "taffy", "objc2", "objc2-encode"] {
manifest.push_str(&format!(
"\n[profile.dev.package.{package}]\nopt-level = 3\n"
));
}
if let Some(source) = self.nsh.source() {
manifest.push_str("\n[patch.crates-io]\n");
let mut patches = String::new();
for name in NSH_CRATES
.iter()
.filter(|name| gpui || **name != "nsh-platform-gpui")
.filter(|name| nsh_crate(source, name).join("Cargo.toml").is_file())
{
patches.push_str(&format!(
"{name} = {{ path = {:?} }}\n",
nsh_crate(source, name).to_string_lossy()
));
}
manifest.push_str(&patches);
if let Nsh::Git { url, .. } = &self.nsh {
manifest.push_str(&format!("\n[patch.{url:?}]\n{patches}"));
}
}
write(&path, &manifest)
}
fn write_tui_host(&self, ws: &Path) -> anyhow::Result<()> {
let dir = ws.join("tui-host");
let name = self.bin("tui-host");
write(
&dir.join("Cargo.toml"),
&format!(
"[package]\nname = {name:?}\nversion = \"0.0.0\"\nedition = \"2024\"\npublish = false\n\n[[bin]]\nname = {name:?}\npath = \"main.rs\"\n\n[dependencies]\nnsh-tui-host = {}\nnsh-platform-tui = {}\nnsh-core = {}\nnsh-base = {}\n{}",
self.nsh.dep("nsh-tui-host", ""),
self.nsh.dep("nsh-platform-tui", ""),
self.nsh.dep("nsh-core", ""),
self.nsh.dep("nsh-base", HIGHLIGHT),
self.dependencies(),
),
)?;
let (root, root_fn) = match &self.root {
Some(root) => (
" options.root = Some(app_root());\n",
self.root_fn("", root),
),
None => ("", String::new()),
};
write(
&dir.join("main.rs"),
&format!(
"//! The terminal development host with the app's UI sets.\nfn main() -> std::io::Result<()> {{\n let mut options = nsh_platform_tui::AppOptions::default();\n options.ui_sets = {};\n{root} nsh_tui_host::main_with_options(options)\n}}\n{}{root_fn}",
self.sets(),
self.app_sets(""),
),
)
}
fn write_host(&self, ws: &Path, dir: &str, lib: &str, what: &str) -> anyhow::Result<()> {
let src = |c: &str| self.nsh.dep(c, "");
let app = self.dependencies();
let sets = self.sets();
let exit = " -> std::process::ExitCode";
let (os, name, platform, exit) = if dir.starts_with("ios") {
("target_os = \"ios\"", "iOS", "nsh-platform-ios", "")
} else if dir.starts_with("macos") {
("target_os = \"macos\"", "macOS", "nsh-platform-macos", exit)
} else if dir.starts_with("windows") {
(
"target_os = \"windows\"",
"Windows",
"nsh-platform-windows",
exit,
)
} else if dir.starts_with("gpui") {
(
"any(target_os = \"linux\", target_os = \"macos\", windows)",
"GPUI",
"nsh-platform-gpui",
exit,
)
} else {
("target_os = \"linux\"", "Linux", "nsh-platform-linux", exit)
};
write(
&ws.join(dir).join("Cargo.toml"),
&format!(
"[package]\nname = {:?}\nversion = \"0.0.0\"\nedition = \"2024\"\npublish = false\n\n\
[[bin]]\nname = {:?}\npath = \"main.rs\"\n\n[dependencies]\n{lib} = {}\nnsh-core = {}\nnsh-base = {}\n{app}\n\n\
[target.'cfg({os})'.dependencies]\n{platform} = {}\n",
self.bin(dir),
self.bin(dir),
src(lib),
src("nsh-core"),
self.nsh.dep("nsh-base", HIGHLIGHT),
src(platform),
),
)?;
let lib = lib.replace('-', "_");
let platform = platform.replace('-', "_");
let subsystem = if dir == "windows-app" {
"\n#![cfg_attr(windows, windows_subsystem = \"windows\")]\n"
} else {
""
};
let cfg = format!("#[cfg({os})]\n");
let (set_root, root_fn) = match &self.root {
Some(root) if dir.ends_with("-host") => (
format!("\n #[cfg({os})]\n {platform}::set_root(app_root);"),
self.root_fn(&cfg, root),
),
_ => (String::new(), String::new()),
};
let (set_components, components_fn) = match components_platform(dir) {
Some(key) => (
format!("\n #[cfg({os})]\n {platform}::set_components(app_components);"),
self.app_components(&cfg, &platform, key),
),
None => (String::new(), String::new()),
};
write(
&ws.join(dir).join("main.rs"),
&format!(
"//! The {name} {what} with `nsh/base` and the app's Rust components.\n{subsystem}\n\
fn main(){exit} {{\n #[cfg({os})]\n {platform}::set_ui_sets({sets});{set_components}{set_root}\n {lib}::main()\n}}\n{}{components_fn}{root_fn}",
self.app_sets(&cfg)
),
)
}
fn target_dir(&self) -> anyhow::Result<PathBuf> {
target_dir(&self.workspace, env::var_os("CARGO_TARGET_DIR"))
}
fn build(
&self,
workspace: &Path,
package: &str,
triple: Option<&str>,
release: bool,
extra: &[&str],
what: &str,
) -> anyhow::Result<PathBuf> {
let artifact = format!("{package}{}", crate::runtime::exe_suffix(triple));
let build = Build {
workspace,
package,
triple,
release,
profile: None,
extra,
artifact: &artifact,
what,
};
self.build_with(Command::new(cargo()), &build)
}
fn build_with(&self, mut cmd: Command, build: &Build<'_>) -> anyhow::Result<PathBuf> {
let Build {
workspace,
package,
triple,
release,
profile,
extra,
artifact,
what,
} = *build;
crate::runtime::cross_build(&mut cmd, triple)?;
cmd.arg("build")
.arg("--manifest-path")
.arg(workspace.join("Cargo.toml"))
.args(["-p", package])
.args(extra)
.env("CARGO_TARGET_DIR", self.target_dir()?);
if let Some(triple) = triple {
cmd.args(["--target", triple]);
if triple == "wasm32-unknown-unknown" {
crate::runtime::web_build_flags(&mut cmd);
}
}
if let Some(profile) = profile {
cmd.args(["--profile", profile]);
} else if release {
cmd.arg("--release");
}
if let Err(error) = ui::cargo(&mut cmd, what) {
if RESTARTED.load(std::sync::atomic::Ordering::Relaxed) {
self.wait_after(&error)?;
return Err(Restart.into());
}
return Err(error);
}
let place = |mut path: PathBuf| {
if let Some(triple) = triple {
path.push(triple);
}
path.push(match profile {
Some(profile) => profile,
None if release => "release",
None => "debug",
});
path.push(artifact);
path
};
let path = place(self.target_dir()?);
if path.exists() {
return Ok(path);
}
Ok(cargo_target_directory(workspace)
.into_iter()
.chain([workspace.join("target")])
.map(place)
.find(|candidate| candidate.exists())
.unwrap_or(path))
}
pub(crate) fn runtime(
&self,
platform: Platform,
runner: bool,
triple: Option<&str>,
) -> anyhow::Result<PathBuf> {
if let Some(triple) = triple
&& Some(triple) != platform.triple()
{
crate::runtime::require_target(None, triple)?;
}
if self.root.is_some() {
return self.rust_runner(platform, runner, triple);
}
self.host(platform, runner, triple)
}
pub(crate) fn test_host(&self, platform: Platform) -> anyhow::Result<PathBuf> {
self.host(platform, false, platform.triple())
}
fn host(
&self,
platform: Platform,
runner: bool,
triple: Option<&str>,
) -> anyhow::Result<PathBuf> {
let name = match (platform, runner) {
(Platform::Linux, false) => "linux-host",
(Platform::Linux, true) => "linux-app",
(Platform::MacOs, false) => "macos-host",
(Platform::MacOs, true) => "macos-app",
(Platform::Windows, false) => "windows-host",
(Platform::Windows, true) => "windows-app",
(Platform::Tui, false) => "tui-host",
(Platform::Tui, true) => {
anyhow::bail!("terminal packaging needs the terminal release runner")
}
(Platform::Gpui, false) => "gpui-host",
(Platform::Gpui, true) => unreachable!("GPUI apps are embedded, never packaged"),
(_, false) => "ios-host",
(_, true) => "ios-app",
};
let what = platform.host(runner);
let (workspace, features) = match platform {
Platform::Gpui => (self.gpui_workspace()?, crate::runtime::gpui_features()),
Platform::Tui => (self.tui_workspace()?, &[][..]),
_ => (self.workspace.clone(), &[][..]),
};
let package = self.bin(name);
let release = runner || RELEASE_HOSTS.load(std::sync::atomic::Ordering::Relaxed);
let what = if release && !runner {
format!("{what} (release build)")
} else {
what
};
let what = format!("{what}{}", crate::runtime::other_triple(platform, triple));
self.build(&workspace, &package, triple, release, features, &what)
}
fn rust_runner(
&self,
platform: Platform,
release: bool,
triple: Option<&str>,
) -> anyhow::Result<PathBuf> {
let dir = match platform {
Platform::Linux => "rust-linux",
Platform::MacOs => "rust-macos",
Platform::Windows => "rust-windows",
Platform::Tui => return self.host(platform, false, triple),
Platform::Gpui => unreachable!("a Rust app does not run on GPUI's host"),
Platform::IphoneOs | Platform::IphoneSimulator | Platform::MacCatalyst => "rust-ios",
};
let what = format!(
"the {} runner of {}{}{}",
match platform {
Platform::Linux => "Linux",
Platform::MacOs => "macOS",
Platform::Windows => "Windows",
Platform::Tui | Platform::Gpui => unreachable!(),
Platform::IphoneOs => "iOS",
Platform::IphoneSimulator => "iOS simulator",
Platform::MacCatalyst => "Mac Catalyst",
},
self.packages(),
if release { "" } else { " (debug)" },
crate::runtime::other_triple(platform, triple)
);
self.build(&self.workspace, &self.bin(dir), triple, release, &[], &what)
}
pub(crate) fn watch(
&self,
mut stop: impl FnMut() -> anyhow::Result<bool>,
) -> anyhow::Result<()> {
let (tx, rx) = std::sync::mpsc::channel();
let mut watcher =
notify::recommended_watcher(move |event: notify::Result<notify::Event>| {
if let Ok(event) = event {
let _ = tx.send(event);
}
})?;
self.watch_with(&mut watcher)?;
loop {
if ui::stop_requested() || stop()? {
return Ok(());
}
let Ok(event) = rx.recv_timeout(std::time::Duration::from_millis(200)) else {
continue;
};
if !matches!(event.kind, notify::EventKind::Access(_))
&& event.paths.iter().any(|p| self.is_rust(p))
{
return Err(Restart.into());
}
}
}
pub(crate) fn android(
&self,
prebuilt: &Path,
dev: bool,
abis: &[String],
) -> anyhow::Result<PathBuf> {
self.android_runtime(prebuilt, dev, self.root.is_some(), abis)
}
pub(crate) fn test_android(&self, prebuilt: &Path) -> anyhow::Result<PathBuf> {
self.android_runtime(prebuilt, true, false, &[])
}
fn android_runtime(
&self,
prebuilt: &Path,
dev: bool,
rust: bool,
abis: &[String],
) -> anyhow::Result<PathBuf> {
let sdk = crate::android::Sdk::find()?;
let out = self.workspace.join(match (rust, dev) {
(true, true) => "android-rust-dev",
(true, false) => "android-rust-release",
(false, true) => "android-dev",
(false, false) => "android-release",
});
fs::create_dir_all(&out)?;
install(&prebuilt.join("classes.dex"), &out.join("classes.dex"))?;
for entry in fs::read_dir(prebuilt.join("lib"))? {
let abi = entry?.file_name().to_string_lossy().into_owned();
if !abis.is_empty() && !abis.contains(&abi) {
continue;
}
let triple = match abi.as_str() {
"arm64-v8a" => "aarch64-linux-android",
"armeabi-v7a" => "armv7-linux-androideabi",
"x86_64" => "x86_64-linux-android",
"x86" => "i686-linux-android",
other => bail!("unknown Android ABI {other}"),
};
let features: &[&str] = if dev && !rust {
&["--features", "dev"]
} else {
&[]
};
let package = self.bin(if rust { "rust-android" } else { "android" });
let artifact = format!("lib{}.so", self.android_lib(rust));
let build = Build {
workspace: &self.workspace,
package: &package,
triple: Some(triple),
release: !dev,
profile: None,
extra: features,
artifact: &artifact,
what: &format!("libnsh.so of {} for {abi}", self.packages()),
};
let lib = self.build_with(sdk.ndk_cargo(triple)?, &build)?;
let dir = out.join("lib").join(&abi);
fs::create_dir_all(&dir)?;
let dest = dir.join("libnsh.so");
if dev {
install(&lib, &dest)?;
} else {
let symbols = out.join("symbols").join(&abi).join("libnsh.so");
install_stripped(&lib, &dest, &symbols, |from, to| sdk.strip(from, to))?;
}
}
Ok(out)
}
}
#[derive(Clone, Copy)]
struct Build<'a> {
workspace: &'a Path,
package: &'a str,
triple: Option<&'a str>,
release: bool,
profile: Option<&'a str>,
extra: &'a [&'a str],
artifact: &'a str,
what: &'a str,
}
const ANDROID_BUILD_RS: &str = r#"//! Android's shared runtime supports both 4 KB and 16 KB page sizes.
fn main() {
println!("cargo:rerun-if-changed=build.rs");
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() == Ok("android") {
println!("cargo:rustc-link-arg-cdylib=-Wl,-z,max-page-size=16384");
println!("cargo:rustc-link-arg-cdylib=-Wl,-z,common-page-size=16384");
}
}
"#;
const WEB_BUILD_RS: &str = "//! The web runtime's stack: 8 MiB, as nsh's own (`.cargo/config.toml`).\n\n\
fn main() {\n if std::env::var(\"CARGO_CFG_TARGET_ARCH\").as_deref() == Ok(\"wasm32\") {\n \
println!(\"cargo:rustc-link-arg=-zstack-size=8388608\");\n }\n}\n";
impl RustApp {
fn dependencies(&self) -> String {
let lines: Vec<String> = self
.crates
.iter()
.map(|c| {
if self.retargeted {
format!(
"{} = {{ package = {:?}, path = {:?} }}",
c.name(),
c.package,
self.workspace
.join("app-crates")
.join(&c.package)
.to_string_lossy()
)
} else {
c.dependency()
}
})
.collect();
lines.join("\n")
}
fn sets(&self) -> String {
let mut sets = vec!["nsh_base::registry()".to_owned()];
sets.extend(self.crates.iter().map(|c| format!("{}()", set_fn(c))));
format!("vec![{}]", sets.join(", "))
}
fn rust_web_sets(&self) -> String {
let mut sets = vec![
"nsh_base::registry_of([nsh_base::parts::core as nsh_base::parts::Part])".to_owned(),
];
sets.extend(self.crates.iter().map(|c| format!("{}()", set_fn(c))));
format!("vec![{}]", sets.join(", "))
}
fn app_only_sets(&self) -> String {
let sets: Vec<String> = self
.crates
.iter()
.map(|c| format!("{}()", set_fn(c)))
.collect();
format!("vec![{}]", sets.join(", "))
}
fn set_gates(&self, cfg: &str) -> String {
self.crates
.iter()
.map(|krate| {
let specifier = specifier_of(&krate.dir).unwrap_or_else(|| krate.name());
format!(
"{cfg}nsh_platform_web::ui_set!({}, {}());\n",
crate::web::ui_set_gate(&specifier),
set_fn(krate)
)
})
.collect()
}
fn app_sets(&self, cfg: &str) -> String {
let mut code = String::new();
for krate in &self.crates {
code.push_str(&format!(
"\n/// The UI set of the crate {}: the entry `#[native_module(\"…\")]` makes in it.\n\
{cfg}fn {}() -> nsh_core::element::Registry {{\n \
#[allow(unused_imports)]\n use {}::*;\n \
{ENTRY}() // the crate {} (nsh.json \"rust\")\n}}\n",
krate.package,
set_fn(krate),
krate.name(),
krate.package,
));
}
code.push_str(&format!(
"\n// Without the entry: nsh's stand-in, which fails the build saying what to add.\n\
{cfg}#[allow(unused_imports)]\nuse nsh_core::element::__private::no_native_module as {ENTRY};\n"
));
code
}
fn app_components(&self, cfg: &str, platform: &str, key: &str) -> String {
let entry = format!("{COMPONENTS_ENTRY}{key}");
let mut calls = String::new();
for krate in &self.crates {
calls.push_str(&format!(
" {{\n #[allow(unused_imports)]\n use {}::*;\n \
{entry}(__nsh_components); // the crate {} (nsh.json \"rust\")\n }}\n",
krate.name(),
krate.package,
));
}
format!(
"\n/// The app's native components on this platform: what `#[native_components({key})]` registers in its crates.\n\
{cfg}fn app_components(__nsh_components: &{platform}::Components) {{\n{calls}}}\n\
\n// Without the entry: nsh's stand-in, which registers nothing.\n\
{cfg}#[allow(unused_imports)]\nuse nsh_core::element::__private::no_native_components as {entry};\n"
)
}
fn root_fn(&self, cfg: &str, root: &RootConfig) -> String {
let krate = self.crates.first().map(AppCrate::name).unwrap_or_default();
root_fn(cfg, &krate, root)
}
}
fn components_platform(dir: &str) -> Option<&'static str> {
let platform = dir.strip_prefix("rust-").unwrap_or(dir);
["windows", "macos", "linux", "ios"]
.into_iter()
.find(|key| {
platform
.strip_prefix(key)
.is_some_and(|rest| rest.is_empty() || rest == "-host" || rest == "-app")
})
}
fn set_fn(krate: &AppCrate) -> String {
format!("set_{}", krate.name())
}
fn root_fn(cfg: &str, krate: &str, root: &RootConfig) -> String {
let mut make = format!(
"nsh_core::element::Root::of::<{krate}::{}>()",
root.component
);
if let Some(theme) = &root.theme {
make.push_str(&format!(
"\n .tokens(\n nsh_base::Theme::from_json(include_str!({:?}))\n \
.expect(\"nsh.json: theme\")\n .into_tokens(),\n )",
theme.to_string_lossy()
));
}
if let Some(id) = &root.app_id {
make.push_str(&format!("\n .app_id({id:?})"));
}
if let Some(translations) = &root.translations {
make.push_str(&format!(
"\n .translations_json(include_str!({:?}))",
translations.to_string_lossy()
));
}
if !root.url_schemes.is_empty() {
make.push_str(&format!("\n .url_schemes({:?})", root.url_schemes));
}
if let Some(appearance) = &root.appearance {
let variant = if appearance == "dark" {
"Dark"
} else {
"Light"
};
make.push_str(&format!(
"\n .appearance(nsh_core::element::Appearance::{variant})"
));
}
if let Some(setting) = root
.status_bar
.as_deref()
.and_then(|json| serde_json::from_str(json).ok())
.map(|v| nsh_core::StatusBarSetting::from_json(&v))
{
let mut make_setting = "nsh_core::StatusBarSetting::default()".to_owned();
if let Some(style) = setting.style {
let variant = match style {
nsh_core::StatusBarStyle::Default => "Default",
nsh_core::StatusBarStyle::Light => "Light",
nsh_core::StatusBarStyle::Dark => "Dark",
};
make_setting.push_str(&format!(".foreground(nsh_core::StatusBarStyle::{variant})"));
}
if let Some(hidden) = setting.hidden {
make_setting.push_str(&format!(".hidden({hidden})"));
}
make.push_str(&format!("\n .status_bar({make_setting})"));
}
if let Some(window) = &root.window {
make.push_str(&format!("\n .main_window_json({window:?})"));
}
format!(
"\n/// The app's root (`nsh.json` `\"root\"`), with its theme, appearance and\n\
/// status bar.\n\
{cfg}fn app_root() -> nsh_core::element::Root {{\n {make}\n}}\n"
)
}
fn stamp(program: &Path) -> anyhow::Result<String> {
let metadata = fs::metadata(program).with_context(|| format!("no {}", program.display()))?;
let modified = metadata
.modified()?
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default();
Ok(format!("{}:{}", metadata.len(), modified.as_nanos()))
}
fn read_description(
text: &str,
crates: &[AppCrate],
what: &str,
) -> anyhow::Result<Vec<SetDescription>> {
let description = Description::from_json(text)
.map_err(|e| anyhow::anyhow!("the description of {what}: {e}"))?;
if description.nsh != VERSION {
bail!(
"{what} were described by nsh {} and this nsh is {VERSION}",
description.nsh
);
}
if description.sets.len() != crates.len() {
bail!(
"the description of {what} has {} UI sets for {} crates",
description.sets.len(),
crates.len()
);
}
check_specifiers(crates, &description.sets)?;
Ok(description.sets)
}
fn check_specifiers(crates: &[AppCrate], sets: &[SetDescription]) -> anyhow::Result<()> {
let nsh: Vec<&str> = nsh_core::js::NATIVE_MODULES
.iter()
.copied()
.chain(
nsh_core::runtime::registries()
.iter()
.filter_map(|r| r.specifier()),
)
.chain(crate::own_sets().iter().filter_map(|r| r.specifier()))
.collect();
for (i, (krate, set)) in crates.iter().zip(sets).enumerate() {
let specifier = set.specifier.as_str();
if nsh.contains(&specifier) {
bail!(
"the crate {} is imported as {specifier:?}, one of nsh's own modules: give its `#[native_module(\"…\")]` a specifier of its own",
krate.package
);
}
if let Some((other, _)) = crates
.iter()
.zip(sets)
.take(i)
.find(|(_, s)| s.specifier == specifier)
{
bail!(
"the crates {} and {} are both imported as {specifier:?}: give each crate of the app a `#[native_module(\"…\")]` specifier of its own",
other.package,
krate.package
);
}
}
Ok(())
}
fn install(from: &Path, to: &Path) -> anyhow::Result<()> {
if up_to_date(to, from) {
return Ok(());
}
fs::copy(from, to).with_context(|| format!("copy {} to {}", from.display(), to.display()))?;
Ok(())
}
fn up_to_date(file: &Path, source: &Path) -> bool {
let modified = |p: &Path| fs::metadata(p).and_then(|m| m.modified()).ok();
modified(file).is_some() && modified(file) >= modified(source)
}
fn install_stripped(
from: &Path,
to: &Path,
symbols: &Path,
strip: impl FnOnce(&Path, &Path) -> anyhow::Result<()>,
) -> anyhow::Result<()> {
if up_to_date(to, from) && up_to_date(symbols, from) {
return Ok(());
}
fs::create_dir_all(symbols.parent().context("symbols directory")?)?;
strip(from, to)?;
fs::copy(from, symbols)?;
Ok(())
}
pub(crate) fn cargo_target_directory(workspace: &Path) -> Option<PathBuf> {
let out = Command::new(cargo())
.args([
"metadata",
"--format-version",
"1",
"--no-deps",
"--manifest-path",
])
.arg(workspace.join("Cargo.toml"))
.current_dir(workspace)
.output()
.ok()
.filter(|out| out.status.success())?;
let json: serde_json::Value = serde_json::from_slice(&out.stdout).ok()?;
json.get("target_directory")?.as_str().map(PathBuf::from)
}
fn cargo() -> std::ffi::OsString {
env::var_os("CARGO").unwrap_or_else(|| "cargo".into())
}
fn target_dir(
workspace: &Path,
cargo_target_dir: Option<std::ffi::OsString>,
) -> anyhow::Result<PathBuf> {
if let Some(dir) = cargo_target_dir.filter(|d| !d.is_empty()) {
let dir = PathBuf::from(dir);
return Ok(if dir.is_absolute() {
dir
} else {
env::current_dir()?.join(dir)
});
}
let app = workspace
.parent()
.and_then(Path::parent)
.context("the generated workspace is not in an app's .nsh")?;
Ok(app.join("target"))
}
fn nsh_crate(source: &Path, name: &str) -> PathBuf {
match name {
"nsh-platform-gpui" | "nsh-gpui-host" => source.join("crates/gpui").join(name),
_ => source.join("crates").join(name),
}
}
fn web_profile(kind: crate::runtime::WebKind) -> &'static str {
match kind {
crate::runtime::WebKind::Browser => "web",
crate::runtime::WebKind::Dev => "web-dev",
}
}
#[cfg(test)]
mod tests {
use std::path::{Path, PathBuf};
use super::{AppCrate, Nsh, RootConfig, RustApp};
#[test]
fn release_libraries_upgrade_old_caches_and_restore_symbols() {
use std::{
fs,
time::{Duration, SystemTime},
};
let dir = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("../../.nsh")
.join(format!("strip-cache-test-{}", std::process::id()));
fs::create_dir_all(&dir).unwrap();
let source = dir.join("linked.so");
let dest = dir.join("libnsh.so");
let symbols = dir.join("symbols/libnsh.so");
fs::write(&source, b"complete library").unwrap();
let old = SystemTime::now() - Duration::from_secs(60);
fs::File::options()
.write(true)
.open(&source)
.unwrap()
.set_modified(old)
.unwrap();
fs::copy(&source, &dest).unwrap();
assert!(super::up_to_date(&dest, &source));
let strip = |_: &Path, to: &Path| -> anyhow::Result<()> {
fs::write(to, b"stripped library")?;
Ok(())
};
super::install_stripped(&source, &dest, &symbols, strip).unwrap();
assert_eq!(fs::read(&dest).unwrap(), b"stripped library");
assert_eq!(fs::read(&symbols).unwrap(), b"complete library");
super::install_stripped(&source, &dest, &symbols, |_, _| panic!("cache miss")).unwrap();
fs::remove_file(&symbols).unwrap();
super::install_stripped(&source, &dest, &symbols, strip).unwrap();
assert_eq!(fs::read(&symbols).unwrap(), b"complete library");
fs::File::options()
.write(true)
.open(&symbols)
.unwrap()
.set_modified(old - Duration::from_secs(60))
.unwrap();
super::install_stripped(&source, &dest, &symbols, strip).unwrap();
assert!(super::up_to_date(&symbols, &source));
fs::remove_dir_all(dir).unwrap();
}
fn app(dir: &Path, crates: &[(&str, PathBuf)], root: Option<RootConfig>) -> RustApp {
let crates: Vec<AppCrate> = crates
.iter()
.map(|(package, dir)| AppCrate {
package: (*package).into(),
dir: dir.clone(),
})
.collect();
RustApp {
workspace: dir.join(".nsh/rust"),
nsh: Nsh::Source(Path::new(env!("CARGO_MANIFEST_DIR")).join("../..")),
watched: crates.iter().map(|c| c.dir.clone()).collect(),
crates,
root,
retargeted: false,
}
}
fn write_crate(dir: &Path, package: &str, lib: &str, deps: &str) {
std::fs::create_dir_all(dir.join("src")).unwrap();
std::fs::write(
dir.join("Cargo.toml"),
format!(
"[package]\nname = \"{package}\"\nversion = \"0.1.0\"\n\n[dependencies]\n{deps}"
),
)
.unwrap();
std::fs::write(dir.join("src/lib.rs"), lib).unwrap();
}
#[test]
fn the_crates_assets_are_its_code() {
let mut app = app(
Path::new("/app"),
&[("app-native", "/app/native".into())],
None,
);
app.watched.push("/shared/ui".into());
for (path, rust) in [
("/app/native/src/lib.rs", true),
("/app/native/Cargo.toml", true),
("/app/native/assets/logo@2x.png", true),
("/app/native/assets/fonts/Script.ttf", true),
("/app/native/target/debug/build.rs", false),
("/app/native/.nsh/rust/test/main.rs", false),
("/app/native/.git/hooks/build.rs", false),
("/app/native/README.md", false),
("/app/App.tsx", false),
("/shared/ui/src/card.rs", true),
("/shared/other.rs", false),
] {
assert_eq!(app.is_rust(Path::new(path)), rust, "{path}");
}
}
#[test]
fn what_builds_write_in_a_crate_is_not_its_code() {
let app = app(
Path::new("/app"),
&[("app-native", "/app/native".into())],
None,
);
let target = Some(Path::new("/app/native/build-cache"));
for (path, rust) in [
("/app/native/src/lib.rs", true),
("/app/native/build-cache/debug/build/out.rs", false),
("/app/native/build-cache/Cargo.toml", false),
] {
assert_eq!(app.is_rust_in(Path::new(path), target), rust, "{path}");
}
}
#[test]
fn android_libraries_are_named_after_the_app() {
let lib = |package: &str, file: &str| {
let dir = std::env::temp_dir()
.join(format!("nsh-rust-android-{package}-{}", std::process::id()));
let root = RootConfig {
component: "App".into(),
theme: None,
appearance: None,
app_id: None,
status_bar: None,
url_schemes: Vec::new(),
window: None,
translations: None,
};
let app = app(&dir, &[(package, dir.join("native"))], Some(root));
app.write_workspace().unwrap();
let manifest = std::fs::read_to_string(app.workspace.join(file)).unwrap();
let _ = std::fs::remove_dir_all(&dir);
let at = manifest.find("[lib]\nname = ").expect("a [lib]");
manifest[at..].lines().nth(1).unwrap().to_owned()
};
for file in ["android/Cargo.toml", "rust-android/Cargo.toml"] {
let (charts, notes) = (lib("charts", file), lib("notes", file));
assert_ne!(charts, notes, "{file}");
assert!(charts.contains("charts"), "{file}: {charts}");
}
}
#[test]
fn rust_is_a_crate_or_a_list_of_them() {
let dir = std::env::temp_dir().join(format!("nsh-rust-crates-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
write_crate(
&dir.join("native/charts"),
"charts",
"#[native_module(\"app/charts\")]\nfn native(r: &mut Registry) {}\n",
"",
);
write_crate(
&dir.join("native/storage"),
"storage",
"use nsh_core::prelude::*;\n\n#[nsh_core::element::native_module( \"app/storage\" )]\nfn native(r: &mut Registry) {}\n",
"",
);
let json = |text: &str| serde_json::from_str::<serde_json::Value>(text).unwrap();
let packages = |rust: &str| -> Vec<String> {
super::crates(&dir, &json(rust))
.unwrap()
.into_iter()
.map(|c| c.package)
.collect()
};
assert_eq!(packages(r#""native/charts""#), ["charts"]);
assert_eq!(
packages(r#"["native/charts", "native/storage"]"#),
["charts", "storage"]
);
let crates = super::crates(&dir, &json(r#"["native/storage"]"#)).unwrap();
assert_eq!(
crates[0].dir,
dunce::canonicalize(dir.join("native/storage")).unwrap()
);
assert_eq!(crates[0].name(), "app_storage");
let error = |rust: &str| super::crates(&dir, &json(rust)).unwrap_err().to_string();
for bad in ["[]", "1", "[\"native/charts\", 2]", "{}"] {
assert!(error(bad).contains("a list of them"), "{bad}");
}
assert!(error(r#"["native/nothing"]"#).contains("no crate at native/nothing"));
assert!(
error(r#"["native/charts", "native/../native/charts"]"#)
.contains("lists the crate native/charts twice")
);
write_crate(&dir.join("native/charts-2"), "charts", "", "");
write_crate(&dir.join("native/my-ui"), "my-ui", "", "");
write_crate(&dir.join("native/my_ui"), "my_ui", "", "");
let same = error(r#"["native/charts", "native/charts-2"]"#);
assert!(
same.contains("native/charts and native/charts-2 are both the package `charts`"),
"{same}"
);
assert!(
error(r#"["native/my-ui", "native/my_ui"]"#)
.contains("are the packages `my-ui` and `my_ui`, one name to Cargo")
);
write_crate(
&dir.join("native/charts-copy"),
"charts-copy",
"#[native_module(\"app/charts\")]\nfn native(r: &mut Registry) {}\n",
"",
);
let same = error(r#"["native/charts", "native/charts-copy"]"#);
assert!(
same.contains(
"native/charts and native/charts-copy both have `#[native_module(\"app/charts\")]`"
),
"{same}"
);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn specifiers_and_path_dependencies_come_from_the_crates() {
let dir = std::env::temp_dir().join(format!("nsh-rust-deps-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let nsh = dir.join("nsh");
write_crate(&nsh.join("crates/nsh-core"), "nsh-core", "", "");
write_crate(&dir.join("shared/base"), "base", "", "");
write_crate(
&dir.join("shared/ui"),
"ui",
"",
"base = { version = \"1\", path = '../base' }\n",
);
write_crate(
&dir.join("app/charts"),
"charts",
"// #[native_module(\"commented/out\")]\n#[native_module(\"app/charts\")]\nfn native(r: &mut Registry) {}\n",
&format!(
"nsh-core = {{ path = {:?} }}\nui = {{ path = \"../../shared/ui\" }}\nlog = \"0.4\"\n\n\
[dev-dependencies]\ntest-only = {{ path = \"../test-only\" }}\n",
nsh.join("crates/nsh-core").to_string_lossy()
),
);
write_crate(
&dir.join("app/storage"),
"storage",
"",
"\n[target.'cfg(unix)'.dependencies.ui]\npath = \"../../shared/ui\"\n",
);
std::fs::write(
dir.join("app/storage/Cargo.toml"),
std::fs::read_to_string(dir.join("app/storage/Cargo.toml")).unwrap()
+ "\n[lib]\npath = \"lib/root.rs\"\n",
)
.unwrap();
std::fs::create_dir_all(dir.join("app/storage/lib")).unwrap();
std::fs::write(
dir.join("app/storage/lib/root.rs"),
"#[native_module(\"app/storage\")]\nfn native(r: &mut Registry) {}\n",
)
.unwrap();
assert_eq!(
super::specifier_of(&dir.join("app/charts")).as_deref(),
Some("app/charts")
);
assert_eq!(
super::specifier_of(&dir.join("app/storage")).as_deref(),
Some("app/storage")
);
assert_eq!(super::specifier_of(&dir.join("shared/ui")), None);
let crates =
super::crates(&dir, &serde_json::json!(["app/charts", "app/storage"])).unwrap();
let canonical = |p: &str| dunce::canonicalize(dir.join(p)).unwrap();
assert_eq!(
super::watched(&crates, Some(&nsh)),
[
canonical("app/charts"),
canonical("app/storage"),
canonical("shared/ui"),
canonical("shared/base"),
]
);
for (line, value) in [
("path = \"../a\"", Some("../a")),
("x = { version = \"1\", path = \"../a\" }", Some("../a")),
("x = {path='../a'}", Some("../a")),
("xpath = \"../a\"", None),
("path-x = \"../a\"", None),
("x = \"1\"", None),
] {
assert_eq!(super::toml_string(line, "path").as_deref(), value, "{line}");
}
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn package_names_come_from_the_package_table() {
let dir = std::env::temp_dir().join(format!("nsh-rust-app-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let manifest = dir.join("Cargo.toml");
std::fs::write(
&manifest,
"[workspace]\nname = \"no\"\n\n[package]\nname = \"gallery-native\"\nversion = \"0.1.0\"\n",
)
.unwrap();
assert_eq!(super::package_name(&manifest).unwrap(), "gallery-native");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_rust_app_declares_its_root_theme_and_appearance() {
let dir = std::env::temp_dir().join(format!("nsh-rust-root-{}", std::process::id()));
let ws = dir.join(".nsh/rust");
std::fs::create_dir_all(&ws).unwrap();
std::fs::write(dir.join("theme.json"), r#"{ "name": "t", "colors": {} }"#).ok();
let config = |json: &str| serde_json::from_str::<serde_json::Value>(json).unwrap();
let root = super::root_config(
&dir,
&config(r#"{ "root": "shell::Gallery", "appearance": "dark" }"#),
&ws,
)
.unwrap();
assert_eq!(root.component, "shell::Gallery");
assert_eq!(root.appearance.as_deref(), Some("dark"));
assert_eq!(root.theme, None);
let root = super::root_config(
&dir,
&config(r#"{ "root": "App", "appearance": "system" }"#),
&ws,
)
.unwrap();
assert_eq!(root.appearance, None);
for bad in [
r#"{ "root": "my app" }"#,
r#"{ "root": "App<T>" }"#,
r#"{ "root": 1 }"#,
] {
assert!(
super::root_config(&dir, &config(bad), &ws).is_err(),
"{bad}"
);
}
let root = super::root_config(
&dir,
&config(r#"{ "root": "App", "appearance": "dim" }"#),
&ws,
)
.unwrap();
assert_eq!(root.appearance, None);
let root = super::root_config(
&dir,
&config(r#"{ "root": "App", "statusBar": { "foreground": "light" } }"#),
&ws,
)
.unwrap();
assert_eq!(
root.status_bar.as_deref(),
Some(r#"{"foreground":"light"}"#)
);
for bad in [
r#""light""#,
r#"{ "foreground": "dim" }"#,
r#"{ "hidden": "yes" }"#,
] {
let json = format!(r#"{{ "root": "App", "statusBar": {bad} }}"#);
assert!(
super::root_config(&dir, &config(&json), &ws).is_ok(),
"{bad}"
);
}
let code = super::root_fn(
"#[cfg(x)]\n",
"app_native",
&super::RootConfig {
component: "App".into(),
theme: Some("/app/theme.json".into()),
appearance: Some("dark".into()),
app_id: Some("com.example.app".into()),
status_bar: Some(r#"{"foreground":"light","hidden":false}"#.into()),
url_schemes: vec!["myapp".into()],
window: Some(
r#"{"defaultSize":{"width":900,"height":600},"title":"Notes"}"#.into(),
),
translations: None,
},
);
for line in [
"#[cfg(x)]\nfn app_root() -> nsh_core::element::Root {",
"nsh_core::element::Root::of::<app_native::App>()",
"include_str!(\"/app/theme.json\")",
".appearance(nsh_core::element::Appearance::Dark)",
".app_id(\"com.example.app\")",
".url_schemes([\"myapp\"])",
".status_bar(nsh_core::StatusBarSetting::default().foreground(nsh_core::StatusBarStyle::Light).hidden(false))",
r#".main_window_json("{\"defaultSize\":{\"width\":900,\"height\":600},\"title\":\"Notes\"}")"#,
] {
assert!(code.contains(line), "no {line:?} in\n{code}");
}
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_rust_app_gets_runners_without_js() {
let dir = std::env::temp_dir().join(format!("nsh-rust-runners-{}", std::process::id()));
let app = app(
&dir,
&[("app-native", dir.join("native"))],
Some(super::RootConfig {
component: "App".into(),
theme: None,
appearance: None,
app_id: None,
status_bar: None,
url_schemes: Vec::new(),
window: None,
translations: None,
}),
);
app.write_workspace().unwrap();
let read = |file: &str| std::fs::read_to_string(app.workspace.join(file)).unwrap();
let manifest = read("Cargo.toml");
assert!(manifest.contains("\"rust-linux\", \"rust-macos\", \"rust-windows\", \"rust-ios\", \"rust-android\", \"rust-web\", \"rust-app\"]"), "{manifest}");
for (dir, lib, platform) in [
("rust-linux", "nsh-linux-app", "nsh-platform-linux"),
("rust-macos", "nsh-macos-app", "nsh-platform-macos"),
("rust-windows", "nsh-windows-app", "nsh-platform-windows"),
("rust-ios", "nsh-ios-app", "nsh-platform-ios"),
] {
let cargo = read(&format!("{dir}/Cargo.toml"));
assert!(cargo.contains(&format!("{lib} = {{ path =")), "{cargo}");
assert!(
cargo.contains(&format!("{platform} = {{ path =")),
"{cargo}"
);
assert_eq!(
cargo.matches("default-features = false").count(),
2,
"{cargo}"
);
let main = read(&format!("{dir}/main.rs"));
assert!(
main.contains(&format!("{}::run_rust(app_root())", lib.replace('-', "_"))),
"{main}"
);
assert!(
main.contains("nsh_core::element::Root::of::<app_app_native::App>()"),
"{main}"
);
}
assert!(!read("rust-linux/Cargo.toml").contains("webview"));
let android = read("rust-android/lib.rs");
assert!(android.contains("nsh::main!(app_root());"), "{android}");
assert!(read("rust-android/Cargo.toml").contains("default-features = false"));
let web = read("rust-web/lib.rs");
assert!(web.contains("nsh_platform_web::rust_app!(vec![nsh_base::registry_of([nsh_base::parts::core as nsh_base::parts::Part]), set_app_app_native()], app_root());"), "{web}");
assert!(!web.contains("nsh_base::registry()"), "{web}");
assert!(read("rust-web/Cargo.toml").contains("default-features = false"));
assert!(read("rust-web/Cargo.toml").contains("linkable = [\"nsh-platform-web/linkable\"]"));
assert!(read("rust-web/build.rs").contains("-zstack-size=8388608"));
let test = read("test/main.rs");
assert!(test.contains("nsh_testing::runner(vec![nsh_base::registry(), set_app_app_native()], Some(app_root))"), "{test}");
assert!(
test.contains("nsh_core::element::Root::of::<app_app_native::App>()"),
"{test}"
);
let rust_app = read("rust-app/lib.rs");
assert!(rust_app.contains("pub fn ui_sets() -> Vec<nsh_core::element::Registry> {\n vec![nsh_base::registry(), set_app_app_native()]\n}"), "{rust_app}");
assert!(rust_app.contains("pub fn app_ui_sets() -> Vec<nsh_core::element::Registry> {\n vec![set_app_app_native()]\n}"), "{rust_app}");
assert!(
rust_app.contains("pub fn root() -> nsh_core::element::Root {\n app_root()\n}"),
"{rust_app}"
);
assert!(
rust_app.contains("nsh_core::element::Root::of::<app_app_native::App>()"),
"{rust_app}"
);
assert!(read("rust-app/Cargo.toml").contains("name = \"nsh_rust_app\""));
app.gpui_workspace().unwrap();
assert!(!read("gpui/Cargo.toml").contains("rust-gpui"));
for (dir, platform) in [
("linux-host", "nsh_platform_linux"),
("macos-host", "nsh_platform_macos"),
("windows-host", "nsh_platform_windows"),
("ios-host", "nsh_platform_ios"),
("gpui/gpui-host", "nsh_platform_gpui"),
] {
let main = read(&format!("{dir}/main.rs"));
assert!(
main.contains(&format!("{platform}::set_root(app_root);")),
"{main}"
);
assert!(
main.contains("nsh_core::element::Root::of::<app_app_native::App>()"),
"{main}"
);
}
assert!(!read("macos-app/main.rs").contains("set_root"));
let android = read("android/lib.rs");
assert!(android.contains("nsh::root!(app_root());"), "{android}");
let web = read("web/lib.rs");
assert!(web.contains("nsh_platform_web::rust_app!(vec![nsh_base::registry(), set_app_app_native()], app_root());"), "{web}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_tsx_app_has_no_rust_runners() {
let dir = std::env::temp_dir().join(format!("nsh-rust-tsx-{}", std::process::id()));
let app = app(&dir, &[("app-native", dir.join("native"))], None);
app.write_workspace().unwrap();
let manifest = std::fs::read_to_string(app.workspace.join("Cargo.toml")).unwrap();
assert!(!manifest.contains("\"rust-"), "{manifest}");
assert!(!app.workspace.join("rust-web").exists());
let test = std::fs::read_to_string(app.workspace.join("test/main.rs")).unwrap();
assert!(
test.contains(
"nsh_testing::runner(vec![nsh_base::registry(), set_app_app_native()], None)"
) && !test.contains("app_root"),
"{test}"
);
let cargo = std::fs::read_to_string(app.workspace.join("test/Cargo.toml")).unwrap();
assert!(cargo.contains("features = [\"js\"]"), "{cargo}");
assert!(super::is_root(Path::new("/app/nsh.json")));
assert!(!super::is_root(Path::new("/app/App.tsx")));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn from_crates_io_every_crate_of_nsh_is_this_version() {
let dir = std::env::temp_dir().join(format!("nsh-rust-registry-{}", std::process::id()));
let mut app = app(&dir, &[("app-native", dir.join("native"))], None);
app.nsh = Nsh::Registry;
app.write_workspace().unwrap();
app.gpui_workspace().unwrap();
let exact = format!("{{ version = \"={}\"", super::VERSION);
for file in [
"describe/Cargo.toml",
"test/Cargo.toml",
"macos-host/Cargo.toml",
"ios-app/Cargo.toml",
"android/Cargo.toml",
"web/Cargo.toml",
"gpui/gpui-host/Cargo.toml",
] {
let manifest = std::fs::read_to_string(app.workspace.join(file)).unwrap();
assert!(manifest.contains(&exact), "{file}: {manifest}");
let by_path = manifest
.lines()
.any(|l| l.starts_with("nsh-") && l.contains("path ="));
assert!(!by_path, "{file}: {manifest}");
}
for file in ["Cargo.toml", "gpui/Cargo.toml"] {
let manifest = std::fs::read_to_string(app.workspace.join(file)).unwrap();
assert!(
!manifest.contains("[patch.crates-io]"),
"{file}: {manifest}"
);
}
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn generated_hosts_optimize_interaction_dependencies_in_debug() {
let dir =
std::env::temp_dir().join(format!("nsh-rust-debug-profile-{}", std::process::id()));
let app = app(&dir, &[("app-native", dir.join("native"))], None);
app.write_workspace().unwrap();
app.gpui_workspace().unwrap();
for file in ["Cargo.toml", "gpui/Cargo.toml"] {
let manifest = std::fs::read_to_string(app.workspace.join(file)).unwrap();
for package in ["quickjs-jit-sys", "taffy", "objc2", "objc2-encode"] {
assert!(
manifest.contains(&format!("[profile.dev.package.{package}]\nopt-level = 3")),
"{file} must keep {package} fast in debug builds"
);
}
}
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn builds_go_to_cargo_target_dir_else_the_app_s_target() {
let ws = Path::new("/apps/charts/.nsh/rust");
let app = super::target_dir(ws, None).unwrap();
assert_eq!(app, Path::new("/apps/charts/target"));
assert_eq!(super::target_dir(ws, Some("".into())).unwrap(), app);
let absolute = if cfg!(windows) {
r"C:\cache\target"
} else {
"/cache/target"
};
let shared = super::target_dir(ws, Some(absolute.into())).unwrap();
assert_eq!(shared, Path::new(absolute));
let relative = super::target_dir(ws, Some("build".into())).unwrap();
assert_eq!(relative, std::env::current_dir().unwrap().join("build"));
}
#[test]
fn the_app_s_crates_say_where_nsh_comes_from() {
let dir = std::env::temp_dir().join(format!("nsh-rust-versions-{}", std::process::id()));
let checkout =
dunce::canonicalize(Path::new(env!("CARGO_MANIFEST_DIR")).join("../..")).unwrap();
let by_path = |c: &str| {
let path = checkout.join("crates").join(c);
format!("{c} = {{ path = {:?} }}\n", path.to_string_lossy())
};
let of = |crates: &[(&str, String)]| {
let crates: Vec<AppCrate> = crates
.iter()
.map(|(package, deps)| {
let crate_dir = dir.join(package);
write_crate(&crate_dir, package, "", deps);
AppCrate {
package: (*package).into(),
dir: crate_dir,
}
})
.collect();
Nsh::of(&crates).map_err(|e| e.to_string())
};
let this = super::VERSION;
let exact = format!("nsh-core = \"={this}\"\n");
assert_eq!(of(&[("a", exact.clone())]), Ok(Nsh::Registry));
let caret = format!("nsh-core = \"{this}\"\nserde = \"1\"\n");
assert_eq!(of(&[("a", caret)]), Ok(Nsh::Registry));
let other = of(&[("a", "nsh-base = \"=0.0.1\"\n".into())]);
assert_eq!(other, Ok(Nsh::Registry));
let source = Ok(Nsh::Source(checkout.clone()));
assert_eq!(of(&[("a", by_path("nsh-core"))]), source);
let gpui = by_path("gpui/nsh-platform-gpui")
.replace("gpui/nsh-platform-gpui =", "nsh-platform-gpui =");
assert_eq!(
of(&[("a", format!("{}{gpui}", by_path("nsh-base")))]),
source
);
let error = of(&[("a", by_path("nsh-core")), ("b", exact)]).unwrap_err();
assert!(error.contains("from one place"), "{error}");
assert_eq!(of(&[("a", "serde = \"1\"\n".into())]), Ok(Nsh::Registry));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn retargeted_manifest_asks_for_this_version_and_keeps_the_rest() {
let dir = std::env::temp_dir().join(format!("nsh-retarget-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let app = dir.join("app");
write_crate(
&app,
"app-native",
"",
"nsh-core = \"=0.0.1\"\nnsh-base = { git = \"https://example.com/nsh.git\", tag = \"v0.0.1\", default-features = false }\nserde = { version = \"1\", features = [\"rc\"] }\n",
);
let crates = [AppCrate {
package: "app-native".into(),
dir: app.clone(),
}];
assert!(super::retargeted(&crates, &Nsh::Registry).unwrap());
let out = super::write_retargeted(&dir.join("ws"), &app, &Nsh::Registry).unwrap();
let text = std::fs::read_to_string(out.join("Cargo.toml")).unwrap();
let this = super::VERSION;
assert!(
text.contains(&format!("\"nsh-core\" = {{ version = \"={this}\"")),
"{text}"
);
assert!(
text.contains(&format!("\"nsh-base\" = {{ version = \"={this}\", package = \"nsh-base\", default-features = false")),
"{text}"
);
assert!(text.contains("features = [\"rc\"]"), "{text}");
assert!(!text.contains("example.com"), "{text}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn git_resolution_uses_patched_registry_dependencies_and_renamed_edges() {
let mut dependencies = vec![serde_json::json!({
"name": "nsh-core", "rename": "core_runtime", "req": "=0.1.1",
"source": "registry+https://github.com/rust-lang/crates.io-index"
})];
let metadata = serde_json::json!({
"packages": [
{"name": "app-native", "id": "app"},
{"name": "nsh-core", "id": "core",
"source": "git+https://example.com/nsh.git?branch=main#locked-revision",
"manifest_path": "/cargo/git/checkouts/nsh/locked/crates/nsh-core/Cargo.toml"}
],
"resolve": {"nodes": [
{"id": "app", "deps": [{"name": "core_runtime", "pkg": "core"}]}
]}
});
super::resolve_git_dependencies(&mut dependencies, &metadata, "app-native").unwrap();
assert_eq!(dependencies[0]["git"], "https://example.com/nsh.git");
assert_eq!(
dependencies[0]["path"],
"/cargo/git/checkouts/nsh/locked/crates/nsh-core"
);
}
#[test]
fn git_dependencies_use_the_locked_checkout_and_patch_both_sources() {
let dir = std::env::temp_dir().join(format!("nsh-rust-git-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
let repo = dir.join("repo");
write_crate(&repo.join("crates/nsh-core"), "nsh-core", "", "");
write_crate(&repo.join("crates/nsh-base"), "nsh-base", "", "");
std::fs::write(repo.join("Cargo.toml"), format!(
"[workspace]\nmembers = [\"crates/*\"]\nresolver = \"3\"\n[workspace.package]\nversion = {:?}\n", super::VERSION
)).unwrap();
for args in [
vec!["init"],
vec!["add", "."],
vec![
"-c",
"user.name=NSH test",
"-c",
"user.email=test@example.com",
"commit",
"-m",
"fixture",
],
] {
let output = std::process::Command::new("git")
.args(args)
.current_dir(&repo)
.output()
.unwrap();
assert!(
output.status.success(),
"{}",
String::from_utf8_lossy(&output.stderr)
);
}
let repo_path = dunce::canonicalize(&repo).unwrap();
let url = format!(
"{}{}",
if cfg!(windows) { "file:///" } else { "file://" },
repo_path.to_string_lossy().replace('\\', "/")
);
let native = dir.join("app/native");
write_crate(
&native,
"git-app",
"",
&format!("nsh-core = {{ git = {url:?} }}\nnsh-base = {{ git = {url:?} }}\n"),
);
let nsh = Nsh::of(&[AppCrate {
package: "git-app".into(),
dir: native.clone(),
}])
.unwrap();
let Nsh::Git {
checkout,
url: resolved_url,
} = &nsh
else {
panic!("Git dependencies must keep their resolved source: {nsh:?}");
};
assert_eq!(resolved_url, &url);
assert_ne!(checkout, &repo);
assert!(checkout.join("crates/nsh-core/Cargo.toml").is_file());
let mut app = app(&dir.join("app"), &[("git-app", native)], None);
app.nsh = nsh;
app.write_workspace().unwrap();
let manifest = std::fs::read_to_string(app.workspace.join("Cargo.toml")).unwrap();
assert!(manifest.contains("[patch.crates-io]"), "{manifest}");
assert!(manifest.contains(&format!("[patch.{url:?}]")), "{manifest}");
let host = std::fs::read_to_string(app.workspace.join("ios-host/Cargo.toml")).unwrap();
assert!(host.contains("nsh-ios-host = { path ="), "{host}");
assert!(!host.contains("version = \"="), "{host}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn web_dev_profile_builds_the_development_runtime_incrementally() {
use crate::runtime::WebKind;
let dir = std::env::temp_dir().join(format!("nsh-rust-web-dev-{}", std::process::id()));
let app = app(
&dir,
&[
("app-charts", dir.join("charts")),
("app-storage", dir.join("storage")),
],
None,
);
app.write_workspace().unwrap();
let manifest = std::fs::read_to_string(app.workspace.join("Cargo.toml")).unwrap();
assert_eq!(super::web_profile(WebKind::Dev), "web-dev");
let profile = |name: &str| -> Vec<String> {
let header = format!("[{name}]");
let mut lines = manifest.lines().skip_while(|l| *l != header).skip(1);
let mut keys = Vec::new();
for line in lines.by_ref() {
if line.starts_with('[') || line.is_empty() {
break;
}
keys.push(line.to_owned());
}
keys
};
let web_dev = profile("profile.web-dev");
for key in [
"inherits = \"dev\"",
"opt-level = 1",
"panic = \"abort\"",
"debug-assertions = false",
] {
assert!(web_dev.iter().any(|l| l == key), "{key}: {manifest}");
}
assert!(
!web_dev
.iter()
.any(|l| l.starts_with("lto") || l.starts_with("codegen-units"))
);
assert_eq!(
profile("profile.web-dev.package.\"*\""),
["opt-level = \"s\""]
);
assert!(profile("profile.web-dev.package.quickjs-jit-sys").is_empty());
for package in ["app-charts", "app-storage"] {
let name = format!("profile.web-dev.package.\"{package}\"");
assert_eq!(profile(&name), ["opt-level = 1"], "{manifest}");
}
assert_eq!(
profile("profile.dev.package.quickjs-jit-sys"),
["opt-level = 3"]
);
assert_eq!(profile("profile.dev.package.taffy"), ["opt-level = 3"]);
for table in [
"[profile.dev.package.quickjs-jit-sys]",
"[profile.dev.package.taffy]",
"[profile.web-dev]",
] {
assert_eq!(manifest.matches(table).count(), 1, "{table}: {manifest}");
}
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn web_dev_profile_leaves_the_release_runtime_s_profile_as_it_was() {
use crate::runtime::WebKind;
let dir = std::env::temp_dir().join(format!("nsh-rust-web-rel-{}", std::process::id()));
let app = app(&dir, &[("app-native", dir.join("native"))], None);
app.write_workspace().unwrap();
let manifest = std::fs::read_to_string(app.workspace.join("Cargo.toml")).unwrap();
assert_eq!(super::web_profile(WebKind::Browser), "web");
let web = "[profile.web]
inherits = \"release\"
opt-level = \"z\"
lto = \"fat\"
codegen-units = 1
strip = true
panic = \"abort\"
";
assert!(manifest.contains(web), "{manifest}");
assert_eq!(manifest.matches("[profile.web.").count(), 0, "{manifest}");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn the_generated_code_calls_the_entry_of_the_crate() {
let dir = std::env::temp_dir().join(format!("nsh-rust-app-ws-{}", std::process::id()));
let app = app(&dir, &[("app-native", dir.join("native"))], None);
app.write_workspace().unwrap();
let read = |file: &str| std::fs::read_to_string(app.workspace.join(file)).unwrap();
let manifest = read("Cargo.toml");
assert!(!manifest.contains("gpui-"), "{manifest}");
assert!(manifest.contains("[patch.crates-io]"), "{manifest}");
assert!(manifest.contains("nsh-core = { path ="), "{manifest}");
assert!(
manifest.contains("exclude = [\"gpui\", \"tui\"]"),
"{manifest}"
);
assert!(!manifest.contains("\"tui-host\""), "{manifest}");
assert!(manifest.contains("\"web\"]"), "{manifest}");
assert!(manifest.contains("[profile.web]"), "{manifest}");
let web = read("web/Cargo.toml");
assert!(web.contains("crate-type = [\"cdylib\"]"), "{web}");
assert!(web.contains("name = \"nsh_app_app_native_web\""), "{web}");
assert!(web.contains("dev = [\"nsh-platform-web/dev\"]"), "{web}");
let build = read("web/build.rs");
assert!(build.contains("-zstack-size=8388608"), "{build}");
assert!(
web.contains("linkable = [\"nsh-platform-web/linkable\"]"),
"{web}"
);
let lib = read("web/lib.rs");
assert!(
lib.contains("#[cfg(target_arch = \"wasm32\")]\nnsh_platform_web::ui_set!(nsh_ui_set_app_app_native, set_app_app_native());"),
"{lib}"
);
assert!(!lib.contains("nsh_base::registry()"), "{lib}");
assert!(!app.workspace.join("gpui").exists());
assert_eq!(app.gpui_workspace().unwrap(), app.workspace.join("gpui"));
assert!(read("gpui/Cargo.toml").contains("members = [\"gpui-host\"]"));
assert!(!app.workspace.join("tui").exists());
assert_eq!(app.tui_workspace().unwrap(), app.workspace.join("tui"));
let tui = read("tui/Cargo.toml");
assert!(tui.contains("members = [\"tui-host\"]"), "{tui}");
assert!(tui.contains("[patch.crates-io]"), "{tui}");
assert!(
read("tui/tui-host/main.rs")
.contains("vec![nsh_base::registry(), set_app_app_native()]")
);
for (file, cfg) in [
("test/main.rs", ""),
("ios-host/main.rs", "#[cfg(target_os = \"ios\")]\n"),
("ios-app/main.rs", "#[cfg(target_os = \"ios\")]\n"),
("linux-host/main.rs", "#[cfg(target_os = \"linux\")]\n"),
("linux-app/main.rs", "#[cfg(target_os = \"linux\")]\n"),
("macos-host/main.rs", "#[cfg(target_os = \"macos\")]\n"),
("macos-app/main.rs", "#[cfg(target_os = \"macos\")]\n"),
("windows-host/main.rs", "#[cfg(target_os = \"windows\")]\n"),
("windows-app/main.rs", "#[cfg(target_os = \"windows\")]\n"),
(
"gpui/gpui-host/main.rs",
"#[cfg(any(target_os = \"linux\", target_os = \"macos\", windows))]\n",
),
("android/lib.rs", "#[cfg(target_os = \"android\")]\n"),
("web/lib.rs", "#[cfg(target_arch = \"wasm32\")]\n"),
] {
let code = read(file);
let sets = if file == "web/lib.rs" {
"set_app_app_native()".to_owned()
} else {
"vec![nsh_base::registry(), set_app_app_native()]".to_owned()
};
for line in [
sets,
format!(
"{cfg}fn set_app_app_native() -> nsh_core::element::Registry {{\n #[allow(unused_imports)]\n use app_app_native::*;\n __nsh_native_module() // the crate app-native (nsh.json \"rust\")\n}}"
),
format!(
"{cfg}#[allow(unused_imports)]\nuse nsh_core::element::__private::no_native_module as __nsh_native_module;"
),
] {
assert!(code.contains(&line), "{file}: no {line:?} in\n{code}");
}
}
for dir in [
"describe",
"test",
"ios-host",
"ios-app",
"linux-host",
"linux-app",
"macos-host",
"macos-app",
"windows-host",
"windows-app",
"gpui/gpui-host",
"tui/tui-host",
"android",
"web",
] {
assert!(
read(&format!("{dir}/Cargo.toml")).contains("\nnsh-core = "),
"{dir}"
);
}
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_workspace_on_crates_io_names_no_terminal_crate_until_its_host_is_built() {
let dir = std::env::temp_dir().join(format!("nsh-rust-app-tui-{}", std::process::id()));
let mut app = app(&dir, &[("app-native", dir.join("native"))], None);
app.nsh = Nsh::Registry;
std::fs::create_dir_all(app.workspace.join("tui-host")).unwrap();
app.write_workspace().unwrap();
assert!(!app.workspace.join("tui-host").exists());
let mut main = std::fs::read_to_string(app.workspace.join("Cargo.toml")).unwrap();
for entry in std::fs::read_dir(&app.workspace).unwrap() {
let member = entry.unwrap().path().join("Cargo.toml");
if let Ok(manifest) = std::fs::read_to_string(member) {
main.push_str(&manifest);
}
}
assert!(
main.contains(&format!(
"nsh-core = {{ version = \"={}\" }}",
super::VERSION
)),
"{main}"
);
assert!(!main.contains("nsh-platform-tui"), "{main}");
assert!(!main.contains("nsh-tui-host"), "{main}");
let tui = std::fs::read_to_string(app.tui_workspace().unwrap().join("tui-host/Cargo.toml"))
.unwrap();
assert!(
tui.contains(&format!(
"nsh-platform-tui = {{ version = \"={}\" }}",
super::VERSION
)),
"{tui}"
);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn the_generated_registration_compiles_whatever_the_crates_export() {
let dir = std::env::temp_dir().join(format!("nsh-rust-components-{}", std::process::id()));
let app = app(
&dir,
&[
("charts", dir.join("native/charts")),
("app-storage", dir.join("native/storage")),
],
None,
);
let generated = app.app_components("", "nsh_platform_windows", "windows");
let source = format!(
"mod nsh_platform_windows {{\n pub struct Components(pub std::cell::Cell<u32>);\n}}\n\
mod nsh_core {{\n pub mod element {{\n pub mod __private {{\n \
pub fn no_native_components<C>(_components: C) {{}}\n }}\n }}\n}}\n\
mod app_charts {{\n use crate::nsh_platform_windows::Components;\n \
pub fn components(components: &Components) {{\n components.0.set(components.0.get() + 1);\n }}\n \
pub fn __nsh_native_components_windows(registry: &Components) {{\n components(registry)\n }}\n}}\n\
mod app_app_storage {{}}\n{generated}\n\
fn main() {{\n let components = nsh_platform_windows::Components(Default::default());\n \
app_components(&components);\n assert_eq!(components.0.get(), 1, \"charts registers once, storage nothing\");\n}}\n"
);
std::fs::create_dir_all(&dir).unwrap();
let file = dir.join("registration.rs");
let exe = dir.join(format!("registration{}", std::env::consts::EXE_SUFFIX));
std::fs::write(&file, &source).unwrap();
let rustc = std::env::var_os("RUSTC").unwrap_or_else(|| "rustc".into());
let built = std::process::Command::new(rustc)
.args(["--edition", "2024", "--crate-type", "bin", "-o"])
.arg(&exe)
.arg(&file)
.output()
.unwrap();
assert!(
built.status.success(),
"{}\n{source}",
String::from_utf8_lossy(&built.stderr)
);
let ran = std::process::Command::new(&exe).output().unwrap();
assert!(
ran.status.success(),
"{}",
String::from_utf8_lossy(&ran.stderr)
);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn the_generated_code_calls_the_entry_of_each_crate() {
let dir = std::env::temp_dir().join(format!("nsh-rust-two-{}", std::process::id()));
let app = app(
&dir,
&[
("charts", dir.join("native/charts")),
("app-storage", dir.join("native/storage")),
],
Some(super::RootConfig {
component: "shell::Home".into(),
theme: None,
appearance: None,
app_id: None,
status_bar: None,
url_schemes: Vec::new(),
window: None,
translations: None,
}),
);
app.write_workspace().unwrap();
app.gpui_workspace().unwrap();
let read = |file: &str| std::fs::read_to_string(app.workspace.join(file)).unwrap();
let sets = "vec![nsh_base::registry(), set_app_charts(), set_app_app_storage()]";
for dir in [
"test",
"ios-host",
"ios-app",
"linux-host",
"linux-app",
"macos-host",
"macos-app",
"windows-host",
"windows-app",
"gpui/gpui-host",
"android",
"web",
"rust-linux",
"rust-macos",
"rust-windows",
"rust-ios",
"rust-android",
"rust-web",
"rust-app",
] {
let cargo = read(&format!("{dir}/Cargo.toml"));
for (name, package, path) in [
("app_charts", "charts", "native/charts"),
("app_app_storage", "app-storage", "native/storage"),
] {
let line = format!(
"\n{name} = {{ package = \"{package}\", path = {:?} }}\n",
dir_of(&app, path)
);
assert!(cargo.contains(&line), "{dir}: no {line:?} in\n{cargo}");
}
let file = if dir == "android"
|| dir.ends_with("web")
|| dir == "rust-android"
|| dir == "rust-app"
{
"lib.rs"
} else {
"main.rs"
};
let code = read(&format!("{dir}/{file}"));
let sets = if dir == "rust-web" {
"vec![nsh_base::registry_of([nsh_base::parts::core as nsh_base::parts::Part]), set_app_charts(), set_app_app_storage()]"
} else {
sets
};
assert!(code.contains(sets), "{dir}: no {sets:?} in\n{code}");
for (name, package) in [("app_charts", "charts"), ("app_app_storage", "app-storage")] {
let set = format!(
"fn set_{name}() -> nsh_core::element::Registry {{\n #[allow(unused_imports)]\n use {name}::*;\n __nsh_native_module() // the crate {package} (nsh.json \"rust\")\n}}"
);
assert!(code.contains(&set), "{dir}: no {set:?} in\n{code}");
}
assert_eq!(
code.matches("no_native_module as __nsh_native_module;")
.count(),
1,
"{dir}"
);
if dir.contains("rust-") {
assert!(
code.contains("nsh_core::element::Root::of::<app_charts::shell::Home>()"),
"{dir}: {code}"
);
}
match super::components_platform(dir) {
Some(key) => {
let platform = format!("nsh_platform_{key}");
let set = format!("{platform}::set_components(app_components);");
assert!(code.contains(&set), "{dir}: no {set:?} in\n{code}");
let entry = format!("__nsh_native_components_{key}");
assert!(
code.contains(&format!(
"fn app_components(__nsh_components: &{platform}::Components) {{\n"
)),
"{dir}: {code}"
);
for (name, package) in
[("app_charts", "charts"), ("app_app_storage", "app-storage")]
{
let call = format!(
" {{\n #[allow(unused_imports)]\n use {name}::*;\n {entry}(__nsh_components); // the crate {package} (nsh.json \"rust\")\n }}\n"
);
assert!(code.contains(&call), "{dir}: no {call:?} in\n{code}");
}
assert_eq!(
code.matches(&format!("no_native_components as {entry};"))
.count(),
1,
"{dir}"
);
}
None => assert!(!code.contains("set_components"), "{dir}: {code}"),
}
}
for (dir, key) in [
("windows-host", Some("windows")),
("windows-app", Some("windows")),
("rust-windows", Some("windows")),
("macos-app", Some("macos")),
("rust-linux", Some("linux")),
("linux-host", Some("linux")),
("rust-ios", Some("ios")),
("ios-host", Some("ios")),
("ios-app", Some("ios")),
("gpui-host", None),
("android", None),
("web", None),
] {
assert_eq!(super::components_platform(dir), key, "{dir}");
}
let describe = read("describe/main.rs");
assert!(
describe.contains("let sets = vec![set_app_charts(), set_app_app_storage()];"),
"{describe}"
);
assert!(
describe.contains("Description::of(&sets).to_json()"),
"{describe}"
);
let manifest = read("describe/Cargo.toml");
let nsh: Vec<&str> = manifest
.lines()
.filter(|l| l.starts_with("nsh-"))
.map(|l| l.split(' ').next().unwrap())
.collect();
assert_eq!(nsh, ["nsh-core"], "{manifest}");
assert!(manifest.contains("name = \"nsh-app-charts-aebab085-describe\""));
assert!(read("test/Cargo.toml").contains("name = \"nsh-app-charts-aebab085-test\""));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn the_cli_reads_the_description_of_the_app_s_crates() {
use nsh_core::element::description::{Description, SetDescription};
let crates = |packages: &[&str]| -> Vec<AppCrate> {
packages
.iter()
.map(|p| AppCrate {
package: (*p).into(),
dir: PathBuf::from("/app").join(p),
})
.collect()
};
let set = |specifier: &'static str| -> SetDescription {
nsh_core::element::Registry::ui_set(specifier).into()
};
let text = |sets: Vec<SetDescription>| {
Description {
nsh: super::VERSION.into(),
sets,
}
.to_json()
};
let read = |text: &str, packages: &[&str]| {
super::read_description(text, &crates(packages), "the Rust crates")
.map_err(|e| e.to_string())
};
let two = text(vec![set("app/charts"), set("app/storage")]);
let sets = read(&two, &["charts", "storage"]).unwrap();
assert_eq!(sets[1].specifier, "app/storage");
assert_eq!(
read(&two, &["charts"]).unwrap_err(),
"the description of the Rust crates has 2 UI sets for 1 crates"
);
let mut other = Description::from_json(&two).unwrap();
other.nsh = "0.0.1".into();
let error = read(&other.to_json(), &["charts", "storage"]).unwrap_err();
assert!(
error.contains("described by nsh 0.0.1 and this nsh is"),
"{error}"
);
assert!(
read("{\"format\": 0}", &["charts"])
.unwrap_err()
.starts_with("the description of the Rust crates: the description is in format 0")
);
for nsh in ["nsh/base", "nsh", "nsh/mobile"] {
let error = read(&text(vec![set(nsh)]), &["charts"]).unwrap_err();
assert!(
error.contains(&format!(
"the crate charts is imported as {nsh:?}, one of nsh's own modules"
)),
"{error}"
);
}
let error = read(
&text(vec![set("app/charts"), set("app/charts")]),
&["charts", "storage"],
)
.unwrap_err();
assert!(
error.contains("the crates charts and storage are both imported as \"app/charts\""),
"{error}"
);
}
fn dir_of(app: &RustApp, path: &str) -> String {
app.workspace
.parent()
.and_then(Path::parent)
.unwrap()
.join(path)
.to_string_lossy()
.into_owned()
}
mod crate_names {
use super::super::bin_name;
const BRIDGES: [&str; 12] = [
"shell-bridge",
"feed-bridge",
"media-bridge",
"auth-bridge",
"storage-bridge",
"network-bridge",
"camera-bridge",
"location-bridge",
"share-bridge",
"payments-bridge",
"analytics-bridge",
"notifications-bridge",
];
const WHATS: [&str; 4] = ["describe", "test", "rust-android", "tui-host"];
#[test]
fn one_crate_keeps_its_name() {
assert_eq!(
bin_name(&["gallery-native"], "describe"),
"nsh-app-gallery-native-describe"
);
}
#[test]
fn several_crates_name_the_first_and_a_digest() {
assert_eq!(
bin_name(&["charts", "storage"], "describe"),
"nsh-app-charts-ef5aa537-describe"
);
let name = bin_name(&BRIDGES, "describe");
assert!(name.starts_with("nsh-app-shell-bridge-"), "{name}");
assert!(name.ends_with("-describe"), "{name}");
}
#[test]
fn names_stay_short_however_many_crates() {
for count in [1, 2, 12] {
for what in WHATS {
let name = bin_name(&BRIDGES[..count], what);
assert!(name.len() <= 42, "{count} crates: {name} ({})", name.len());
}
}
assert_eq!(
bin_name(&BRIDGES[..2], "describe").len(),
bin_name(&BRIDGES, "describe").len()
);
}
#[test]
fn names_are_stable() {
assert_eq!(bin_name(&BRIDGES, "test"), bin_name(&BRIDGES, "test"));
let mut reordered = BRIDGES;
reordered[1..].reverse();
assert_eq!(bin_name(&reordered, "test"), bin_name(&BRIDGES, "test"));
}
#[test]
fn names_are_distinct() {
let mut names = Vec::new();
for count in 1..=BRIDGES.len() {
for what in WHATS {
names.push(bin_name(&BRIDGES[..count], what));
}
}
names.push(bin_name(&["shell-bridge", "other-bridge"], "describe"));
names.push(bin_name(&["feed-bridge", "shell-bridge"], "describe"));
let count = names.len();
names.sort();
names.dedup();
assert_eq!(names.len(), count, "{names:#?}");
}
#[test]
fn names_keep_the_prefix() {
for count in [1, 2, 12] {
let name = bin_name(&BRIDGES[..count], "describe");
assert!(name.starts_with("nsh-app-"), "{name}");
}
}
}
}