Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/layout.feature
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ Feature: One project layout: compositions, components, controllers, assets, arti
When a project's composition imports "@shared/components/desk"
Then the engine's loader resolves it to the workspace's shared/components/desk.tsx (walking up to .raea/workspace),
as it resolves a relative import, with the same extensions and index files
And a render, `raea check`, `raea context` and the app's Compositions page all read it the same way
And a render, `raea check`, `raea context` and the app's Compositions page all read it the same way: the bundler
that copies an edit (runtime/bundle outside.rs) copies what it imports from shared/ beside it, and the one that
bundles a script where it lives (engine/bundle script.rs) resolves it from the importer (raea_io_home::shared).
0.1.7's render left @shared/ to the bundler, which took it for a package: "Can't find variable:
_shared_components_badge" before the first frame, though tsc passed and raea's skill says to import this way
And TypeScript resolves it too: the project's tsconfig (made by `raea project new`, kept by `raea doctor --fix`)
maps @shared/* to ../../shared/*, so an editor and tsc follow the import
And @shared/ outside a workspace is refused with what to do (UnknownLibrary's message names shared/)
Expand Down
5 changes: 5 additions & 0 deletions engine/bundle/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ impl Plugin for ScriptResolve {
"@raea/shell" => return Ok(path_id(&s.shell)),
_ => {}
}
// the workspace's shared code (docs/layout.feature LY2), from where the importer is: not a package
if spec.starts_with(raea_io_home::shared::PREFIX) {
let dir = args.importer.map(Path::new).and_then(Path::parent).unwrap_or(Path::new("."));
return raea_io_home::shared::file(dir, spec).map(|f| path_id(&f)).map_err(|e| anyhow::anyhow!("{e} (imported by {importer})"));
}
// the SDK by its names (docs/sdk.feature S1, S1d): "raea" is its prelude, "raea/<module>" a module, "@raea/<package>"
// a package, each where sdk/tsconfig.base.json's paths put it (sdk/ is the folder above sdk/raea/src). An
// `@raea/` name the list doesn't have is left to the rest (@raea/ui's own folders)
Expand Down
11 changes: 11 additions & 0 deletions io/home/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//! models, the fonts), and where its per-user caches and logs go on each platform.

pub mod sdk;
pub mod shared;

/// What a folder takes on disk, as `du -sh` says it (`links`: through a link to its files), or "?".
pub fn disk_use(dir: &std::path::Path, links: bool) -> String {
Expand Down Expand Up @@ -139,6 +140,16 @@ pub fn src_on(s: &str, windows: bool) -> String {
if s.as_bytes().get(1) == Some(&b':') { format!("file:///{}", s.replace('\\', "/")) } else { s.to_string() }
}

/// The import path from a file in folder `from` to the file `to` (both relative to one folder): ./x, ../y/z.
pub fn import_path(from: &std::path::Path, to: &std::path::Path) -> String {
let (a, b): (Vec<_>, Vec<_>) = (from.components().collect(), to.components().collect());
let common = a.iter().zip(&b).take_while(|(x, y)| x == y).count();
let mut parts = vec![".."; a.len() - common].into_iter().map(String::from).collect::<Vec<_>>();
parts.extend(b[common..].iter().map(|c| c.as_os_str().to_string_lossy().into_owned()));
let p = parts.join("/");
if p.starts_with("..") { p } else { format!("./{p}") }
}

/// A local file as a page names it in a URL's path: its path on Unix; on Windows `/C:/x/y` (the path of the file URL
/// file:///C:/x/y, `src`'s, which a page's file:/// base joins it to). Written `C:\x`, a URL reads `c` as its scheme
/// and CSS reads the backslashes as escapes (#40, #41, #42).
Expand Down
71 changes: 71 additions & 0 deletions io/home/src/shared.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! A workspace's shared/ (docs/layout.feature LY2): what every project in it imports as `@shared/<kind>/<name>`. The
//! engine's loader (engine/modules) and both bundlers (engine/bundle's script resolution, runtime/bundle's copy of an
//! edit) find it the same way. A render once left `@shared/...` to the bundler, which read it as a package and threw
//! "Can't find variable: _shared_components_badge" before the first frame, though tsc passed (0.1.7).
use std::path::{Component, Path, PathBuf};

/// The prefix a project imports its workspace's shared files by: `@shared/components/desk`.
pub const PREFIX: &str = "@shared/";

/// The shared/ of the workspace `dir` is in: under the nearest folder above it holding .raea/workspace, or that folder
/// itself when it has no shared/ yet (a workspace from before shared/ keeps its kinds at its root until `raea workspace
/// tidy`, as raea_app_registry::shared_dir reads it).
pub fn of(dir: &Path) -> Option<PathBuf> {
let ws = dir.ancestors().find(|d| d.join(".raea/workspace").is_file())?;
Some(if ws.join("shared").is_dir() { ws.join("shared") } else { ws.to_path_buf() })
}

/// The script a path names as an import does: the file itself, with a script or JSON extension, or a folder's index.
pub fn script(want: &Path) -> Option<PathBuf> {
let with = |ext: &str| PathBuf::from(format!("{}{ext}", want.display()));
let tries = [want.to_path_buf(), with(".tsx"), with(".ts"), with(".jsx"), with(".js"), with(".mjs"), with(".json"), want.join("index.tsx"), want.join("index.ts"), want.join("index.js")];
tries.into_iter().find(|t| t.is_file())
}

/// The file `@shared/<rest>` names for a source in `dir`, or why there's none (for a message naming its importer).
pub fn file(dir: &Path, spec: &str) -> Result<PathBuf, String> {
let rest = spec.strip_prefix(PREFIX).ok_or_else(|| format!("`{spec}` isn't an {PREFIX} import"))?;
let Some(shared) = of(dir) else {
return Err(format!("`{spec}` names the workspace's shared/, and {} is in no workspace (no .raea/workspace above it): import the file by a relative path, or copy it into the project", dir.display()));
};
for part in Path::new(rest).components() {
if !matches!(part, Component::Normal(_)) {
return Err(format!("`{spec}` climbs out of the workspace's shared/"));
}
}
let want = shared.join(rest);
script(&want).ok_or_else(|| format!("`{spec}` isn't there: no {} (tried .tsx, .ts, .jsx, .js, .mjs, .json and a folder's index)", want.display()))
}

/// The file an import names for a source in `dir`: a relative one beside it (None when it isn't there), an `@shared/`
/// one in the workspace's shared/ (Err when it isn't: a bundler would take the name for a package's).
pub fn import(dir: &Path, spec: &str) -> Result<Option<PathBuf>, String> {
if spec.starts_with(PREFIX) { file(dir, spec).map(Some) } else { Ok(script(&dir.join(spec))) }
}

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

#[test]
fn an_import_of_shared_finds_the_workspace_s_file_from_any_depth() {
let t = std::env::temp_dir().join(format!("raea-home-shared-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&t);
for (p, s) in [(".raea/workspace", "id = \"ws\"\n"), ("shared/components/badge.tsx", ""), ("shared/components/kit/index.ts", ""), ("projects/p/compositions/edit.tsx", "")] {
std::fs::create_dir_all(t.join(p).parent().unwrap()).unwrap();
std::fs::write(t.join(p), s).unwrap();
}
let from = t.join("projects/p/compositions");
assert_eq!(file(&from, "@shared/components/badge").unwrap(), t.join("shared/components/badge.tsx"));
assert_eq!(file(&from, "@shared/components/badge.tsx").unwrap(), t.join("shared/components/badge.tsx"));
assert_eq!(file(&from, "@shared/components/kit").unwrap(), t.join("shared/components/kit/index.ts"));
assert!(file(&from, "@shared/components/none").unwrap_err().contains("isn't there"));
assert!(file(&from, "@shared/../projects/p/compositions/edit").unwrap_err().contains("climbs out"));
assert!(file(&std::env::temp_dir(), "@shared/components/badge").unwrap_err().contains("in no workspace"));
// a workspace from before shared/: its kinds at its root
std::fs::rename(t.join("shared/components"), t.join("components")).unwrap();
std::fs::remove_dir_all(t.join("shared")).unwrap();
assert_eq!(file(&from, "@shared/components/badge").unwrap(), t.join("components/badge.tsx"));
std::fs::remove_dir_all(&t).unwrap();
}
}
40 changes: 15 additions & 25 deletions runtime/bundle/src/outside.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
use super::*;
use std::collections::BTreeSet;

/// A source the edit reaches outside its own folder by a relative import (content/anything/paint/shots.tsx's
/// ../../../examples/objects/wake): `file` where it is, `copy` where bundle_fresh puts it, relative to the work folder
/// (ext/raea/<its path from raea's home>, or ext/project/<its path from the project's git root>).
/// A source the edit reaches outside its folder by a relative import (../../../examples/objects/wake) or as shared code
/// (@shared/components/badge, docs/layout.feature LY2): `file` where it is, `copy` where bundle_fresh puts it under the
/// work folder (ext/raea/<from raea's home>, ext/project/<from the project's git root>, ext/shared/<from shared/>).
#[derive(Debug, Clone, PartialEq)]
pub struct Outside {
pub file: PathBuf,
pub copy: PathBuf,
}

fn specs() -> regex::Regex {
regex::Regex::new(r#"(\bfrom\s+)(["'])(\.[^"']*)(["'])"#).unwrap()
regex::Regex::new(r#"(\bfrom\s+)(["'])(\.[^"']*|@shared/[^"']*)(["'])"#).unwrap()
}

fn absolute(p: &Path) -> PathBuf {
normalize(&std::path::absolute(p).unwrap_or_else(|_| p.to_path_buf()))
}

/// The file a relative `spec` names from `base`, as the bundler would find it: itself, with a script or JSON extension,
/// or a folder's index
/// The file a relative or @shared/ `spec` names from `base`, as the bundler would find it: itself, with a script or JSON
/// extension, or a folder's index
fn resolve(base: &Path, spec: &str) -> Option<PathBuf> {
let p = normalize(&base.join(spec));
let with = |ext: &str| PathBuf::from(format!("{}{ext}", p.display()));
let tries = [p.clone(), with(".tsx"), with(".ts"), with(".jsx"), with(".js"), with(".json"), p.join("index.tsx"), p.join("index.ts"), p.join("index.js")];
tries.into_iter().find(|t| t.is_file())
raea_io_home::shared::import(base, spec).ok().flatten().map(|p| normalize(&p))
}

/// Where an edit may import from outside its folder, each with its name under ext/: raea's home, and the project's
/// root (the nearest folder above the edit holding .git) when that is somewhere else
/// Where an edit may import from outside its folder, each with its name under ext/: raea's home, the project's root
/// (the nearest folder above the edit holding .git) when elsewhere, and its workspace's shared/ (LY2)
fn roots(dir: &Path) -> Vec<(&'static str, PathBuf)> {
let home = absolute(&raea_io_home::home());
let mut out = vec![("raea", home.clone())];
Expand All @@ -37,6 +34,9 @@ fn roots(dir: &Path) -> Vec<(&'static str, PathBuf)> {
out.push(("project", git.to_path_buf()));
}
}
if let Some(shared) = raea_io_home::shared::of(dir) {
out.push(("shared", absolute(&shared)));
}
out
}

Expand Down Expand Up @@ -64,8 +64,8 @@ pub fn outside_in(dir: &Path, roots: &[(&'static str, PathBuf)]) -> R<Vec<Outsid
if MEDIA_EXTS.contains(&ext.as_str()) {
continue;
}
// one the bundler can't find either: left for it to name
let Some(t) = resolve(&base, spec) else { continue };
// one the bundler can't find either: left for it to name (but an @shared/ one, which it takes for a package)
let Some(t) = raea_io_home::shared::import(&base, spec).map_err(|e| format!("{}: {e}", f.display()))?.map(|p| normalize(&p)) else { continue };
if t.starts_with(dir) || sdk.iter().any(|s| t.starts_with(s)) || t.components().any(|c| c.as_os_str() == "node_modules") || !seen.insert(t.clone()) {
continue;
}
Expand Down Expand Up @@ -95,19 +95,9 @@ pub fn point_outside(src: &str, from_dir: &Path, at: &Path, dir: &Path, outs: &[
None => None,
});
match target {
Some(t) => format!("{}{}{}{}", &c[1], &c[2], relative(at, &t), &c[4]),
Some(t) => format!("{}{}{}{}", &c[1], &c[2], raea_io_home::import_path(at, &t), &c[4]),
None => c[0].to_string(),
}
})
.into_owned()
}

/// The import path from a file in `from` to `to`, both under the work folder
fn relative(from: &Path, to: &Path) -> String {
let (a, b): (Vec<_>, Vec<_>) = (from.components().collect(), to.components().collect());
let common = a.iter().zip(&b).take_while(|(x, y)| x == y).count();
let mut parts = vec![".."; a.len() - common].into_iter().map(String::from).collect::<Vec<_>>();
parts.extend(b[common..].iter().map(|c| c.as_os_str().to_string_lossy().into_owned()));
let p = parts.join("/");
if p.starts_with("..") { p } else { format!("./{p}") }
}
11 changes: 11 additions & 0 deletions runtime/bundle/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ fn outside_copies_what_the_edit_imports_beyond_its_folder() {
w("y.ts", "export const y = 0;\n");
let e = outside_in(&dir, &roots).unwrap_err();
assert!(e.contains("lib/c.ts") && e.contains("outside raea's home"), "{e}");
// @shared/ (LY2), as raea's skill says to import: 0.1.7 left it to the bundler, which took it for a package
w(".raea/workspace", "");
w("shared/components/badge.tsx", "export { TONE } from \"./tone\";\n");
w("shared/components/tone.ts", "export const TONE = 1;\n");
w("proj/edit/a.tsx", "import { TONE } from \"@shared/components/badge\";\n");
let roots = vec![("project", root.clone()), ("shared", t.join("shared"))];
let outs = outside_in(&dir, &roots).unwrap();
assert_eq!(outs.iter().map(|o| o.copy.to_str().unwrap()).collect::<Vec<_>>(), ["ext/shared/components/badge.tsx", "ext/shared/components/tone.ts"]);
assert!(point_outside("import { TONE } from \"@shared/components/badge\";", &dir, Path::new("src"), &dir, &outs).contains("\"../ext/shared/components/badge.tsx\""));
w("proj/edit/a.tsx", "import { N } from \"@shared/components/nope\";\n");
assert!(outside_in(&dir, &roots).unwrap_err().contains("a.tsx: `@shared/components/nope` isn't there"));
std::fs::remove_dir_all(&t).unwrap();
}

Expand Down
Loading