mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-10-07 11:38:12 +08:00
Make the data model use Item and List types universally, with nodes authored as rank-polymorphic kernels (#4335)
* Add rank polymorphism node audit classifying all 271 nodes
* Implement StaticType for Item<T>
* Generate Item and mapped List wire variants for nodes declaring an Item<T> primary input
* Migrate nine nodes to Item element-wise kernels, dissolving the blending trait boilerplate
* Document the Item kernel implementation and staging plan
* Route Item<Vector> through TaggedValue::TypeDefault
* Add executor integration tests covering the Item and List wire variants
* Collapse element-wise Item/List wire pairs to the List form for conversion insertion
* Migrate sixteen vector modifier nodes to Item element-wise kernels
* Migrate Sample Image, Extend Image to Bounds, and Dehaze to Item element-wise kernels
* Fix bevel_with_transform test to actually exercise the transform attribute
* Implement From<T> for Item<T>
* Register PromoteNode rank adapters wrapping bare values into Item wires
* Insert PromoteNode adapters for Item/List wire pair fields in the preprocessor
* Define a real promote node backing the PromoteNode registry identifiers
* Zip ranked Item connectors by frame slot in the mapped element-wise variant
* Register ItemToListNode singleton raise adapters
* Resolve Item wires against List connectors by inserting promotion adapters at construction
* Rank the Offset Points distance connector and prove mixed-rank resolution end-to-end
* Implement Clampable for Item and List wires with per-variant clamp bounds
* Rank the Round Corners radius connector, exercising hard bounds on a ranked wire
* Implement ApplyTransform for Item
* Add Item wire implementations to the Transform node, keeping rank-0 chains rank 0
* Detect element-wise nodes by lazy primary connectors declaring Output = Item
* Convert Transform to an Item kernel with ranked parameters, delivering the broadcast milestone
* Rename Apply Transform to Bake Transform, baking item transforms on Vector, DAffine2, and DVec2
* Promote bare wires onto Item connectors at resolution via WrapItemNode adapters
* Rank the numeric, vector, and boolean parameters across the migrated element-wise nodes
* Rank the enum, integer, and seed parameters, registering their rank adapters via a consolidated macro
* Amend the audit with the DashPattern value type resolution
* Migrate the string family to Item element-wise kernels
* Unwrap Item wires into bare legacy connectors at resolution via UnwrapItemNode adapters
* Shadow owned node parameters in bodies instead of mut in signatures
* Migrate the math family and string measure nodes to Item element-wise kernels
* Convert the comparison and clamp nodes to Item kernels, dropping unreachable &str rows
* Flat-map expander kernels returning List under the mapped variant's frame
* Migrate the expander nodes to Item kernels flat-mapping under the frame
* Remove the unused peel_list helper
* Rank the raster adjustment and blending kernels, recontextualizing shader nodes onto an Item stand-in
Migrate the 16 adjustment nodes, Mix, Color Overlay, and Gradient Map from whole-List kernels to rank-0 Item kernels, letting the macro derive the List-mapped (zip) variants. Move the Adjust and Blend per-element seams off List onto the element types (add the Raster<CPU> impls, drop the now-dead List impls).
Shader nodes keep their bodies verbatim: PerPixelAdjust re-emits the identical kernel against a transparent no_std Item stand-in, so every Item<T> connector and .element() call resolves to a zero-cost identity on the GPU while the uniform buffer stays bare repr(C). The macro peels Item off ranked uniform params, wraps the fetched texel and uniforms at the entry point, and unwraps the result. This drops the shader_node/Item incompatibility guard. Register rank adapters for the adjustment enums.
* Update the rank polymorphism roadmap for the landed shader-node and adjustments chunk
* Rename the GPU Item stand-in to ShaderItem, aliased as Item at its shader-node import sites
* Flip the vector shape generators to emit rank-0 Item<Vector>
The shape generators (Rectangle, Circle, Ellipse, Arc, Spiral, Polygon, Star, Arrow, Line, Grid, QR Code) each produced exactly one shape wrapped in a singleton List<Vector>. Emit Item<Vector> directly so they connect to the rank-0 content connector of the migrated Transform node. Downstream List consumers receive the value through the existing Item to List promotion.
Relax the element-wise validation so a `()` (generator) primary may return Item<T> without being element-wise. Adapt the Repeat on Points test, which still takes a List content connector, by raising the generator's Item output through a singleton wrapper node.
* Parse ranked Item<T> parameter defaults against the bare element type
A ranked `Item<T>` parameter's default value is a bare, unranked `T` (promoted to the wire at resolution), but the preprocessor was handed the wrapped `Item<T>` type and could not parse the literal, flooding the console with warnings and dropping the defaults. Key the field's default_type metadata off the peeled element type for concrete ranked parameters, leaving generic `Item<T>` primaries and skip_impl nodes untouched.
* Parse an element-wise primary's scalar default against the bare element type
An element-wise node's primary reports its default_type as the List wire form so an unconnected primary defaults to an empty list. But when the primary carries a scalar `#[default]` (such as Root's radicand), that literal must parse as a bare element, not a List. Key the primary's default_type off the bare element type when it has a Default value source, keeping the List form otherwise.
* Add the DashPattern value type for stroke dash sequences
Introduce a rank-0 DashPattern value type (a Vec<f64> of alternating dash and gap lengths) so a stroke's dash pattern is a single frameable value rather than a rank-1 List<f64>. Register it as an auto-generated TaggedValue variant, parse its default from a comma or space separated string, and register its rank adapters. Not yet wired into the Stroke node.
* Rank the Fill and Stroke nodes element-wise and give Stroke a DashPattern connector
Migrate Fill and Stroke to element-wise Item<V> primaries (over Vector and Graphic element types) via a new element-level VectorItemMut trait, so styling one shape yields one shape and rank is preserved instead of promoting the input to a singleton List and emitting a List. The macro derives the List-mapped variant for genuine collections.
Wire the Stroke dash sequence to the new rank-0 DashPattern value type, collapsing the old content x paint x dash cartesian and dropping the IntoF64Vec trait. Update the stroke properties dash widget, the drawing tool, and graph-operation plumbing to read and write DashPattern, and migrate legacy F64Array, F64, and String dash inputs on document open.
Assign Colors stays a whole-collection node: each element's gradient position depends on its index among all siblings, which the element frame does not expose, so it keeps its List primary and the VectorListIterMut trait.
* Register rank adapters for the ranked Stroke enum parameters
The element-wise Stroke node ranks its align, cap, and paint order parameters as Item<StrokeAlign>, Item<StrokeCap>, and Item<PaintOrder>, but those enums lacked promotion adapters, so a bare default enum value could not be promoted to its Item wire and no Stroke variant resolved ("No construct found for node"). Register their rank adapters alongside StrokeJoin.
* Display Item wires in the Data panel without a List's ID column
Add a TableItemLayout impl for Item<T> and recognize Item wire types when introspecting graph data. An Item holds a single element, so it renders as a one-row table of the element plus its attributes with no leading index column, and it labels as its element type T rather than a List's T[]. Add ItemAttributeValues::get_any for the attribute widget dispatch.
* Register MonitorNode for Item wire types so the Data panel introspects them directly
Graph introspection wraps the inspected output in a generic MonitorNode typed to the wire. Without Item<T> monitor registrations, an Item<Vector> output could only be monitored after an Item to List promotion, so the Data panel captured and displayed a List<Vector> despite the connector being Item<Vector>. Register monitors for the Item types the element-wise nodes emit, and add the matching Data panel downcast entries.
* Color and double Item/List wires and cleave layer-stack connectors in the node graph
* Route wire color and rank through hidden nodes and refresh them on type changes
* Rework the DashPattern connector conversions with element-wise promotion and an explicit reducer node
* Rank the remaining value, context, aggregation, and transform nodes onto Item<T> wires
* Back DashPattern with a List<f64> so the Data panel can introspect its lengths
* Carry a single Item<T> through varargs so the Read context nodes emit Item<T> not List<T>
* Relax rank validation for aggregation shapes, add element adapters, and match variants by fewest promotions
* Rank the remaining bare and unnecessarily-List connectors across the node catalog
* Add Graphic::None and the FillChoice paint value, making colors and gradients plain values
* Rename GradientStops to Gradient and the legacy Gradient/Fill structs to LegacyGradient/LegacyFill
* Restore generator frame-from-params ranking to the roadmap as a planned stage
* Rename the ranked-field adapter identifier from PromoteNode to FieldAdapterNode to reflect its full contract
* Unload only the wires whose displayed style changed when types update
* Peel wire rank in the editor's semantic type checks so rank-0 layers are recognized
* Restore the whole-List Transform variant so rank-1 content wires resolve again
* Register the Item wire forms for the Memoize and Context Modification infrastructure nodes
* Give every ranked connector a field adapter and add numeric cast variants for legacy wires
* Key a ranked param's type default off its Item wire form when no literal default exists
* Inherit the layer's content value when splicing a node into an empty chain
* Migrate stale List-form TypeDefault inputs to the definition's current default
* Generate the mapped wire variant only when the element-wise node has a frame source
* Let a bare wire feed a List connector via a wrap-raise adapter, costed as two rank steps
* Add a zip companion to the whole-List Transform so ranked List parameters pair per slot
* Add the Sum, Average, Minimum, Maximum, Any, and All list reducers
* Convert the measure family to element-wise Item kernels per the audit classification
* Prefer the bare element value over the Item type default so ranked params keep their widgets
* Rename GradientStopsUI to GradientUI
* Split Fill's optional transform into a _has_transform bool and a ranked _transform matrix
* Rename the migration-only OptionalDAffine2 TaggedValue to LegacyOptionalDAffine2
* Flow byte buffers as Item<Resource> instead of List<u8> across the byte nodes
* Macro-generate the list-content wire variant, retiring the hand-written Transform-zip, Area, and Centroid companions
* Let ()-primary generators take ranked params and frame over them via the mapped variant, ranking Circle's radius
* Rank the vector shape generators' params to Item, adding a rank-aware input grab to the introspection harness
* Rank the value, color, and text generator params to Item
* Rank the raster, web-request, and context-reader generator params to Item
* Fix the repeat and brush test wirings left behind by the param-ranking sweeps
* Delete the vestigial Some, Unwrap Option, and Size Of debug nodes
* Delete the Attach Attribute node, folding its role into Write Attribute
* Add the Filter and Sort list companion nodes
* Guard the removed-definition migration swap target with a test
* Add the Box Corners value type in place of the rectangle corner radius list
* Split Text to Vector's per-glyph mode into a Text to Vector Glyphs node
* Rank the Combine Channels node's channel connectors to Item
* Make Map Points an element-wise node
* Delete the deprecated Upload Texture node
* Update the implementation roadmap to reflect the landed stages
* Let monitor introspection read rank-0 wires, locking in the layer coercion promotion path
* Prefer the rank-0 default when disconnecting a rank-capable input
* Make Path Modify an element-wise node
* Wrap node paths in a NodeIdPath newtype so they flow as a single Item
* Give Item<Raster<CPU>> a default so an unconnected Brush background resolves
* Stop the Brush node from setting layer attributes its paint operation doesn't produce
* Present-gate Flatten Path's adopted layer path like its fill and stroke
* Gate carried layer attributes on static column presence, not runtime values
* Give the remaining graphic Item<T> types a default so unconnected primaries resolve
* Dispatch a ranked param's Properties widget from its rank-0 element type
* Make Extract Transform an element-wise node, restoring the Origins to Polyline body
* Rename Flatten Path to Combine Paths
* Stamp Legacy Layer Extend's adopted layer path as a readable NodeIdPath
* Drop the dead List<u8> and List<NodeId> wire rows
* Rank Flatten Graphic's Fully Flatten toggle to Item
* Update the implementation roadmap with the endgame scope
* Make Combine Paths a reducer that collapses the whole frame into one path
* Stop type-converter nodes from carrying the source's unrelated attributes
* Format the Origins to Polyline regression test
* Wrap the Brush node's trace in a BrushTrace newtype so it flows as one value
* Make Switch a framed element-wise select, bundling whole collections
* Widen and align element-type coverage across the list and graphic nodes
* Register the compiler's cache chain pair for every ranked enum and newtype wire
* Fix wire colors for Passthrough outputs, bundled lists, and bools, and widen list wires
* Represent List wire types structurally with Type::List, replacing name-parsed rank promotion
* Treat scope and data fields as environment, rank scope wires as Item, and feed the render boundary through a context vararg
* Delete the vestigial Clone debug node
* Reinstate Upload Texture as an element-wise node and fix the GPU variants' scope executor and rank adapters
* Rename Combine Paths back to Flatten Path, deferring that rename to its own PR
* Deduplicate the promotion adapter registrations into the field adapter macro
* Rank Write Attribute's value connector to Item<AttributeValueDyn>, retiring the UnwrapItem bridge
* Vertical wire styling
* Store the editor layer path attribute as a bare NodeIdPath, not an Item<NodeIdPath>
* Rank Context Modification's features connector to Item<ContextFeatures>, dropping the dead memoize row
* Rank Path Modify's modification parameter to Item<Box<VectorModification>>
* Rename the field adapter node family to input adapter
* Drop the dead bare scalar rows from Context Modification's implementations list
* Move the dynamic executor's test module into its own file
* Drop the registry's unreachable bare rows for Memoize, the cache chain, and ConvertNode
* Materialize stored TaggedValues as ranked Item wires at the source
* Remove the bare-wire promotion and adapter machinery made dead by ranked value materialization
* Plant the input adapter for List-only inputs, composing position conversion from standard rows
* Consolidate Into/Convert conversions into the input adapter umbrella and rename the rank adapter identifiers
* Fix grouped layers gaining a phantom None stack element from the FillChoice default hijacking every List<Graphic> disconnect
* Enforce ranked node inputs in the macro, rejecting bare wire declarations
* Remove the unit Context => () machinery rows, leaving () purely as the no-primary sentinel
* Add a --signatures rank-audit mode to node-docs for the ranked-wire migration
* Remove the node-docs --signatures rank-audit mode now that ranked wires are enforced
* Migrate legacy no-color values on the Black & White, Color Overlay, and Empty Image color inputs
* Rewrite the element-wise accessor wire type at the primary input, not raw index 0
* Register the cache chain for Resource wires, replacing the lone hand-written Monitor row
* Gate the remaining Raster<GPU> registry rows behind the gpu feature
* Let List<DVec2> wires erase to ListDyn for the attribute reader and element counter
* Rename Extract Element to Item at Index, Count Elements to List Length, and Omit Element to Remove at Index
* Store paint picks as plain color/gradient values, removing the FillChoice value type
* Code review restructuring
* Sort by the consumed sort_key attribute or natural element order, adding the Sort Key node
* Remove the new list-combinator and reducer nodes to defer them to a follow-up PR
* Parse Fill and Stroke color defaults through the paint wire's Graphic element
* Emit ranked implementation-row default types structurally so their element TypeIds survive to default-literal parsing
* Exempt the deliberate no-paint choice from the stale List-form TypeDefault migration
* Migrate the legacy 4-input Fill directly to the split has-transform shape
* Upgrade the demo artwork
* Fix the valid AI review findings: Item eq/hash contract, table-era no-paint migration, quantize List rows, and other smaller issues
* Remove the rank polymorphism working documents
* Hash Item attribute values directly instead of debug-formatting them, speeding up cached evaluation
* Replace the data panel's dead bare-wire downcast arms with full coverage of the ranked monitor row types
* Derive PartialEq for Item now that attributes participate in equality
* Extend the data panel's attribute dispatchers with the newly supported scalar and choice enum types
* Add List monitor rows for the framed numeric conversion outputs so inspecting them resolves, with matching data panel arms
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "cargo-run"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
default-run = "cargo-run"
|
||||
|
||||
[dependencies]
|
||||
thiserror = { workspace = true }
|
||||
semver = "1"
|
||||
duct = "1"
|
||||
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "cargo-run-internal-download"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
cargo-run = { path = "../.." }
|
||||
ureq = { version = "3", default-features = false, features = ["rustls"] }
|
||||
sha2 = { version = "0.10" }
|
||||
flate2 = { version = "1", default-features = false, features = ["rust_backend"] }
|
||||
tar = { version = "0.4", default-features = false }
|
||||
@@ -0,0 +1,162 @@
|
||||
use cargo_run::Error;
|
||||
use flate2::read::GzDecoder;
|
||||
use sha2::{Digest, Sha256};
|
||||
use std::io::{self, Read};
|
||||
use std::path::{Component, Path, PathBuf};
|
||||
use std::process::ExitCode;
|
||||
|
||||
pub fn usage() {
|
||||
eprintln!();
|
||||
eprintln!("USAGE:");
|
||||
eprintln!(" cargo run -p cargo-run-internal-download -- <URL> <SHA256> <OUT> \\");
|
||||
eprintln!(" [--extract] [--strip <N>] [--include <PREFIX>]...");
|
||||
eprintln!();
|
||||
eprintln!("Args:");
|
||||
eprintln!(" <URL> HTTPS source to download");
|
||||
eprintln!(" <SHA256> Expected SHA-256 of the response body (64 hex digits)");
|
||||
eprintln!(" <OUT> Destination file (default) or directory (with --extract)");
|
||||
eprintln!(" --extract Decompress the body as tar.gz and extract into <OUT>");
|
||||
eprintln!(" --strip <N> Strip N leading path components from each entry (requires --extract)");
|
||||
eprintln!(" --include <PREFIX> Only extract entries whose stripped path starts with PREFIX, repeatable (requires --extract)");
|
||||
eprintln!();
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
if let Err(e) = parse_args().inspect_err(|_| usage()).and_then(run) {
|
||||
eprintln!("Error: {e}");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
|
||||
struct Args {
|
||||
url: String,
|
||||
sha256: String,
|
||||
out: PathBuf,
|
||||
mode: Mode,
|
||||
}
|
||||
|
||||
enum Mode {
|
||||
File,
|
||||
ExtractTarGz { strip: usize, include: Vec<String> },
|
||||
}
|
||||
|
||||
fn run(Args { url, sha256, out, mode }: Args) -> Result<(), Error> {
|
||||
eprintln!("Downloading {url}");
|
||||
let mut body = Vec::new();
|
||||
ureq::get(&url)
|
||||
.call()
|
||||
.map_err(|e| Error::Io(io::Error::other(e), format!("HTTP GET {url}")))?
|
||||
.into_body()
|
||||
.into_with_config()
|
||||
.reader()
|
||||
.read_to_end(&mut body)
|
||||
.map_err(|e| Error::Io(io::Error::other(e), "reading response body".into()))?;
|
||||
|
||||
let actual = Sha256::digest(&body).iter().map(|b| format!("{b:02x}")).collect::<String>();
|
||||
if !actual.eq_ignore_ascii_case(&sha256) {
|
||||
eprintln!("SHA-256 mismatch:");
|
||||
eprintln!(" expected: {sha256}");
|
||||
eprintln!(" actual: {actual}");
|
||||
return Err(Error::Io(io::Error::other("SHA-256 mismatch"), "verifying download".into()));
|
||||
}
|
||||
|
||||
match mode {
|
||||
Mode::File => write_to_file(&body, &out),
|
||||
Mode::ExtractTarGz { strip, include } => extract_tar_gz(&body, &out, strip, &include),
|
||||
}
|
||||
}
|
||||
|
||||
fn write_to_file(body: &[u8], path: &Path) -> Result<(), Error> {
|
||||
if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) {
|
||||
std::fs::create_dir_all(parent).map_err(|e| Error::Io(e, format!("creating '{}'", parent.display())))?;
|
||||
}
|
||||
std::fs::write(path, body).map_err(|e| Error::Io(e, format!("writing '{}'", path.display())))?;
|
||||
eprintln!("Wrote {}", path.display());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// TODO: support other compression/archive formats
|
||||
fn extract_tar_gz(body: &[u8], dir: &Path, strip: usize, include: &[String]) -> Result<(), Error> {
|
||||
std::fs::create_dir_all(dir).map_err(|e| Error::Io(e, format!("creating '{}'", dir.display())))?;
|
||||
let mut archive = tar::Archive::new(GzDecoder::new(body));
|
||||
archive.set_preserve_permissions(true);
|
||||
for entry in archive.entries().map_err(|e| Error::Io(e, "reading tar archive".into()))? {
|
||||
let mut entry = entry.map_err(|e| Error::Io(e, "reading tar entry".into()))?;
|
||||
let entry_path = entry.path().map_err(|e| Error::Io(e, "reading tar entry path".into()))?.into_owned();
|
||||
let Some(stripped) = strip_components(&entry_path, strip) else {
|
||||
continue;
|
||||
};
|
||||
if stripped.as_os_str().is_empty() || stripped.components().any(|c| matches!(c, Component::ParentDir | Component::Prefix(_) | Component::RootDir)) {
|
||||
continue;
|
||||
}
|
||||
if !include.is_empty() {
|
||||
let s = stripped.to_string_lossy().replace('\\', "/");
|
||||
if !include.iter().any(|p| s.starts_with(p.as_str())) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
let entry_type = entry.header().entry_type();
|
||||
if entry_type.is_symlink() || entry_type.is_hard_link() {
|
||||
continue;
|
||||
}
|
||||
let target = dir.join(&stripped);
|
||||
if let Some(parent) = target.parent() {
|
||||
std::fs::create_dir_all(parent).map_err(|e| Error::Io(e, format!("creating '{}'", parent.display())))?;
|
||||
}
|
||||
entry.unpack(&target).map_err(|e| Error::Io(e, format!("unpacking '{}'", target.display())))?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn strip_components(path: &Path, n: usize) -> Option<PathBuf> {
|
||||
let mut comps = path.components();
|
||||
for _ in 0..n {
|
||||
comps.next()?;
|
||||
}
|
||||
Some(comps.as_path().to_path_buf())
|
||||
}
|
||||
|
||||
fn parse_args() -> Result<Args, Error> {
|
||||
fn arg_err(msg: impl Into<String>) -> Error {
|
||||
Error::Io(io::Error::new(io::ErrorKind::InvalidInput, msg.into()), "invalid arguments".into())
|
||||
}
|
||||
|
||||
let mut args = std::env::args().skip(1);
|
||||
let url = args.next().ok_or_else(|| arg_err("URL is required (first positional argument)"))?;
|
||||
let sha256 = args.next().ok_or_else(|| arg_err("SHA-256 is required (second positional argument)"))?;
|
||||
if sha256.len() != 64 || !sha256.chars().all(|c| c.is_ascii_hexdigit()) {
|
||||
return Err(arg_err("SHA-256 must be 64 hex digits"));
|
||||
}
|
||||
let out = PathBuf::from(args.next().ok_or_else(|| arg_err("OUT is required (third positional argument)"))?);
|
||||
|
||||
let mut extract = false;
|
||||
let mut strip: usize = 0;
|
||||
let mut include: Vec<String> = Vec::new();
|
||||
|
||||
while let Some(arg) = args.next() {
|
||||
match arg.as_str() {
|
||||
"--extract" => extract = true,
|
||||
"--strip" => {
|
||||
let v = args.next().ok_or_else(|| arg_err("'--strip' requires a value"))?;
|
||||
strip = v.parse().map_err(|_| arg_err(format!("--strip must be a non-negative integer, got '{v}'")))?;
|
||||
}
|
||||
"--include" => include.push(args.next().ok_or_else(|| arg_err("'--include' requires a value"))?),
|
||||
other => return Err(arg_err(format!("unknown flag '{other}'"))),
|
||||
}
|
||||
}
|
||||
|
||||
let mode = if extract {
|
||||
Mode::ExtractTarGz { strip, include }
|
||||
} else {
|
||||
if strip != 0 {
|
||||
return Err(arg_err("--strip is only valid with --extract"));
|
||||
}
|
||||
if !include.is_empty() {
|
||||
return Err(arg_err("--include is only valid with --extract"));
|
||||
}
|
||||
Mode::File
|
||||
};
|
||||
|
||||
Ok(Args { url, sha256, out, mode })
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "cargo-run-internal-watch"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
cargo-run = { path = "../.." }
|
||||
notify = "8.2.0"
|
||||
notify-debouncer-full = "0.7.0"
|
||||
@@ -0,0 +1,100 @@
|
||||
use cargo_run::Error;
|
||||
use cargo_run::cmd::prelude::*;
|
||||
use notify::RecursiveMode;
|
||||
use notify::event::{EventKind, ModifyKind};
|
||||
use notify_debouncer_full::{Debouncer, RecommendedCache, new_debouncer, notify::RecommendedWatcher};
|
||||
use std::collections::HashSet;
|
||||
use std::path::Path;
|
||||
use std::process::ExitCode;
|
||||
use std::time::Duration;
|
||||
|
||||
const EXCLUDED_DIRECTORIES: &[&str] = &[
|
||||
"target",
|
||||
".git",
|
||||
"frontend/node_modules",
|
||||
"frontend/dist",
|
||||
"frontend/wrapper/pkg",
|
||||
"frontend/wrapper/pkg-native",
|
||||
"tools",
|
||||
];
|
||||
const INCLUDED_EXTENSIONS: &[&str] = &["rs"];
|
||||
|
||||
const DEBOUNCE: Duration = Duration::from_millis(500);
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let release = std::env::args().nth(1).as_deref() == Some("release");
|
||||
|
||||
let _guard = match watch(cargo_run::frontend::build_wasm_steps(release, false)) {
|
||||
Ok(guard) => guard,
|
||||
Err(e) => {
|
||||
eprintln!("Error setting up file watcher: {e}");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
};
|
||||
loop {
|
||||
std::thread::park();
|
||||
}
|
||||
}
|
||||
|
||||
struct WatchGuard {
|
||||
_debouncer: Debouncer<RecommendedWatcher, RecommendedCache>,
|
||||
}
|
||||
|
||||
fn watch(steps: impl IntoIterator<Item = Expression>) -> Result<WatchGuard, Error> {
|
||||
let steps: Vec<Expression> = steps.into_iter().collect();
|
||||
let root = std::env::current_dir()
|
||||
.and_then(|p| p.canonicalize())
|
||||
.map_err(|e| Error::Io(e, "Failed to resolve root for file watcher".into()))?;
|
||||
let root_clone = root.clone();
|
||||
|
||||
let mut current: Option<Sequence> = None;
|
||||
|
||||
let mut debouncer = new_debouncer(DEBOUNCE, None, move |result: notify_debouncer_full::DebounceEventResult| match result {
|
||||
Ok(events) => {
|
||||
let mut seen = HashSet::new();
|
||||
let mut triggered = false;
|
||||
for ev in events {
|
||||
if !matches!(
|
||||
&ev.event.kind,
|
||||
EventKind::Create(_) | EventKind::Remove(_) | EventKind::Modify(ModifyKind::Data(_) | ModifyKind::Name(_) | ModifyKind::Any)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
for path in &ev.event.paths {
|
||||
if is_excluded(path, &root) {
|
||||
continue;
|
||||
}
|
||||
if seen.insert(path.clone()) {
|
||||
triggered = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if triggered {
|
||||
if let Some(c) = current.take() {
|
||||
c.kill();
|
||||
}
|
||||
current = Some(sequence(steps.clone()));
|
||||
}
|
||||
}
|
||||
Err(errors) => {
|
||||
for e in errors {
|
||||
eprintln!("watch: {e}");
|
||||
}
|
||||
}
|
||||
})
|
||||
.map_err(|e| Error::Io(std::io::Error::other(e.to_string()), "file watcher".into()))?;
|
||||
|
||||
debouncer
|
||||
.watch(&root_clone, RecursiveMode::Recursive)
|
||||
.map_err(|e| Error::Io(std::io::Error::other(e.to_string()), "file watcher".into()))?;
|
||||
|
||||
Ok(WatchGuard { _debouncer: debouncer })
|
||||
}
|
||||
|
||||
fn is_excluded(path: &Path, root: &Path) -> bool {
|
||||
let rel = path.strip_prefix(root).unwrap_or(path);
|
||||
if EXCLUDED_DIRECTORIES.iter().any(|d| rel.starts_with(d)) {
|
||||
return true;
|
||||
}
|
||||
!path.extension().and_then(|e| e.to_str()).is_some_and(|e| INCLUDED_EXTENSIONS.contains(&e))
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
use crate::cmd::prelude::*;
|
||||
use crate::{Error, workspace_dir};
|
||||
|
||||
const INFO_FILE: &str = ".branding";
|
||||
const DIR: &str = "branding";
|
||||
|
||||
pub fn setup() -> Result<(), Error> {
|
||||
let workspace = workspace_dir();
|
||||
let info_path = workspace.join(INFO_FILE);
|
||||
let dir_path = workspace.join(DIR);
|
||||
let marker_path = dir_path.join(INFO_FILE);
|
||||
|
||||
let info = std::fs::read_to_string(&info_path).map_err(|e| Error::Io(e, format!("reading '{}'", info_path.display())))?;
|
||||
|
||||
if let Ok(marker) = std::fs::read_to_string(&marker_path)
|
||||
&& marker == info
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let mut lines = info.lines().map(str::trim).filter(|l| !l.is_empty());
|
||||
let url = lines
|
||||
.next()
|
||||
.ok_or_else(|| Error::Io(std::io::Error::other("missing URL"), format!("parsing '{}'", info_path.display())))?;
|
||||
let sha256 = lines
|
||||
.next()
|
||||
.ok_or_else(|| Error::Io(std::io::Error::other("missing SHA-256"), format!("parsing '{}'", info_path.display())))?;
|
||||
|
||||
eprintln!("Downloading branding assets from <{url}>...");
|
||||
|
||||
if dir_path.exists() {
|
||||
std::fs::remove_dir_all(&dir_path).map_err(|e| Error::Io(e, format!("removing '{}'", dir_path.display())))?;
|
||||
}
|
||||
|
||||
utils::internal("download").args([url, sha256, DIR, "--extract", "--strip", "1"]).dir(&workspace).run()?;
|
||||
|
||||
std::fs::copy(&info_path, &marker_path).map_err(|e| Error::Io(e, format!("writing '{}'", marker_path.display())))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,350 @@
|
||||
use crate::Error;
|
||||
use duct::{Handle, ReaderHandle};
|
||||
use std::ffi::OsString;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread::JoinHandle;
|
||||
|
||||
pub use duct::{Expression, cmd};
|
||||
|
||||
pub mod prelude {
|
||||
pub use super::{Expression, ExpressionExt, Sequence, TerminalColor, cmd, sequence, supervise, utils};
|
||||
}
|
||||
|
||||
pub trait ExpressionExt {
|
||||
fn arg(self, arg: impl Into<OsString>) -> Self;
|
||||
fn args<I, S>(self, args: I) -> Self
|
||||
where
|
||||
I: IntoIterator<Item = S>,
|
||||
S: Into<OsString>;
|
||||
fn arg_if(self, cond: bool, arg: impl Into<OsString>) -> Self;
|
||||
fn args_if<I, S>(self, cond: bool, args: I) -> Self
|
||||
where
|
||||
I: IntoIterator<Item = S>,
|
||||
S: Into<OsString>;
|
||||
fn run(self) -> Result<(), Error>;
|
||||
fn read(self) -> Result<String, Error>;
|
||||
fn output_unchecked(self) -> Result<std::process::Output, Error>;
|
||||
}
|
||||
|
||||
impl ExpressionExt for Expression {
|
||||
fn arg(self, arg: impl Into<OsString>) -> Self {
|
||||
let arg = arg.into();
|
||||
self.before_spawn(move |c| {
|
||||
c.arg(&arg);
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
fn args<I, S>(self, args: I) -> Self
|
||||
where
|
||||
I: IntoIterator<Item = S>,
|
||||
S: Into<OsString>,
|
||||
{
|
||||
let args: Vec<OsString> = args.into_iter().map(Into::into).collect();
|
||||
self.before_spawn(move |c| {
|
||||
c.args(&args);
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
fn arg_if(self, cond: bool, arg: impl Into<OsString>) -> Self {
|
||||
if cond { self.arg(arg) } else { self }
|
||||
}
|
||||
|
||||
fn args_if<I, S>(self, cond: bool, args: I) -> Self
|
||||
where
|
||||
I: IntoIterator<Item = S>,
|
||||
S: Into<OsString>,
|
||||
{
|
||||
if cond { self.args(args) } else { self }
|
||||
}
|
||||
|
||||
fn run(self) -> Result<(), Error> {
|
||||
Expression::run(&self).map_err(Error::Command)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read(self) -> Result<String, Error> {
|
||||
Expression::read(&self).map_err(Error::Command)
|
||||
}
|
||||
|
||||
fn output_unchecked(self) -> Result<std::process::Output, Error> {
|
||||
let e = self.unchecked().stdout_capture().stderr_capture();
|
||||
Expression::run(&e).map_err(Error::Command)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum TerminalColor {
|
||||
Black,
|
||||
Red,
|
||||
Green,
|
||||
Yellow,
|
||||
Blue,
|
||||
Magenta,
|
||||
Cyan,
|
||||
White,
|
||||
BrightBlack,
|
||||
BrightRed,
|
||||
BrightGreen,
|
||||
BrightYellow,
|
||||
BrightBlue,
|
||||
BrightMagenta,
|
||||
BrightCyan,
|
||||
BrightWhite,
|
||||
Reset,
|
||||
}
|
||||
impl TerminalColor {
|
||||
fn as_str(&self) -> &'static str {
|
||||
match self {
|
||||
Self::Black => "\x1b[30m",
|
||||
Self::Red => "\x1b[31m",
|
||||
Self::Green => "\x1b[32m",
|
||||
Self::Yellow => "\x1b[33m",
|
||||
Self::Blue => "\x1b[34m",
|
||||
Self::Magenta => "\x1b[35m",
|
||||
Self::Cyan => "\x1b[36m",
|
||||
Self::White => "\x1b[37m",
|
||||
Self::BrightBlack => "\x1b[90m",
|
||||
Self::BrightRed => "\x1b[91m",
|
||||
Self::BrightGreen => "\x1b[92m",
|
||||
Self::BrightYellow => "\x1b[93m",
|
||||
Self::BrightBlue => "\x1b[94m",
|
||||
Self::BrightMagenta => "\x1b[95m",
|
||||
Self::BrightCyan => "\x1b[96m",
|
||||
Self::BrightWhite => "\x1b[97m",
|
||||
Self::Reset => "\x1b[0m",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn sequence<I: IntoIterator<Item = Expression>>(expressions: I) -> Sequence {
|
||||
let expressions: Vec<Expression> = expressions.into_iter().collect();
|
||||
let current: Arc<Mutex<Option<Arc<Handle>>>> = Arc::new(Mutex::new(None));
|
||||
let killed = Arc::new(AtomicBool::new(false));
|
||||
|
||||
let worker_current = Arc::clone(¤t);
|
||||
let worker_killed = Arc::clone(&killed);
|
||||
let worker = std::thread::spawn(move || {
|
||||
for expr in expressions {
|
||||
if worker_killed.load(Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
let handle = match expr.start() {
|
||||
Ok(h) => Arc::new(h),
|
||||
Err(e) => {
|
||||
eprintln!("sequence: failed to start step: {e}");
|
||||
return;
|
||||
}
|
||||
};
|
||||
{
|
||||
let mut slot = worker_current.lock().unwrap();
|
||||
if worker_killed.load(Ordering::SeqCst) {
|
||||
let _ = handle.kill();
|
||||
return;
|
||||
}
|
||||
*slot = Some(Arc::clone(&handle));
|
||||
}
|
||||
let result = handle.wait().map(|_| ());
|
||||
worker_current.lock().unwrap().take();
|
||||
if worker_killed.load(Ordering::SeqCst) {
|
||||
return;
|
||||
}
|
||||
if let Err(e) = result {
|
||||
eprintln!("sequence: step failed: {e}");
|
||||
return;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Sequence {
|
||||
current,
|
||||
killed,
|
||||
worker: Some(worker),
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Sequence {
|
||||
current: Arc<Mutex<Option<Arc<Handle>>>>,
|
||||
killed: Arc<AtomicBool>,
|
||||
worker: Option<JoinHandle<()>>,
|
||||
}
|
||||
impl Sequence {
|
||||
pub fn kill(&self) {
|
||||
let slot = self.current.lock().unwrap();
|
||||
self.killed.store(true, Ordering::SeqCst);
|
||||
if let Some(handle) = slot.as_ref() {
|
||||
let _ = handle.kill();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wait(&mut self) {
|
||||
if let Some(w) = self.worker.take() {
|
||||
let _ = w.join();
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Drop for Sequence {
|
||||
fn drop(&mut self) {
|
||||
self.kill();
|
||||
self.wait();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn supervise<I, S>(children: I) -> Result<(), Error>
|
||||
where
|
||||
I: IntoIterator<Item = (S, TerminalColor, Expression)>,
|
||||
S: Into<String>,
|
||||
{
|
||||
use std::io::{BufRead, BufReader, IsTerminal, Write};
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
windows_ctrl_c::install();
|
||||
|
||||
let mut handles: Vec<(String, TerminalColor, Arc<ReaderHandle>)> = Vec::new();
|
||||
for (label, color, expr) in children {
|
||||
#[cfg(target_os = "windows")]
|
||||
let expr = expr.before_spawn(|cmd| {
|
||||
use std::os::windows::process::CommandExt;
|
||||
const CREATE_NEW_PROCESS_GROUP: u32 = 0x0000_0200;
|
||||
cmd.creation_flags(CREATE_NEW_PROCESS_GROUP);
|
||||
Ok(())
|
||||
});
|
||||
|
||||
let handle = expr.stderr_to_stdout().reader().map_err(Error::Command)?;
|
||||
handles.push((label.into(), color, Arc::new(handle)));
|
||||
}
|
||||
|
||||
let mut io_threads = Vec::new();
|
||||
for (label, color, handle) in handles.iter() {
|
||||
let prefix = if std::io::stdout().is_terminal() {
|
||||
format!("{color}[{label}]{reset} ", color = color.as_str(), reset = TerminalColor::Reset.as_str())
|
||||
} else {
|
||||
format!("[{label}] ")
|
||||
};
|
||||
let handle = handle.clone();
|
||||
io_threads.push(thread::spawn(move || {
|
||||
let reader = BufReader::new(&*handle);
|
||||
for line in reader.lines().map_while(Result::ok) {
|
||||
let out = std::io::stdout();
|
||||
let mut out = out.lock();
|
||||
let _ = writeln!(out, "{prefix}{line}");
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
let mut reason: Option<(String, std::process::ExitStatus)> = None;
|
||||
|
||||
loop {
|
||||
#[cfg(target_os = "windows")]
|
||||
if windows_ctrl_c::interrupted() {
|
||||
break;
|
||||
}
|
||||
|
||||
for (label, _color, handle) in handles.iter() {
|
||||
if let Ok(Some(output)) = handle.try_wait() {
|
||||
reason = Some((label.clone(), output.status));
|
||||
break;
|
||||
}
|
||||
}
|
||||
if reason.is_some() {
|
||||
break;
|
||||
}
|
||||
|
||||
thread::sleep(Duration::from_millis(200));
|
||||
}
|
||||
|
||||
for (_, _color, handle) in handles.iter() {
|
||||
if matches!(handle.try_wait(), Ok(Some(_))) {
|
||||
continue;
|
||||
}
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
for pid in handle.pids() {
|
||||
let _ = std::process::Command::new("taskkill")
|
||||
.args(["/T", "/F", "/PID", &pid.to_string()])
|
||||
.stdout(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::null())
|
||||
.status();
|
||||
}
|
||||
}
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
{
|
||||
let _ = handle.kill();
|
||||
}
|
||||
}
|
||||
|
||||
for t in io_threads {
|
||||
let _ = t.join();
|
||||
}
|
||||
|
||||
match reason {
|
||||
Some((label, status)) if !status.success() => Err(Error::Command(std::io::Error::other(format!("supervised child '{label}' exited with status {status}")))),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
pub mod utils {
|
||||
use super::*;
|
||||
|
||||
pub fn internal(name: &str) -> Expression {
|
||||
let package = format!("cargo-run-internal-{name}");
|
||||
cmd!("cargo", "run", "-p", package, "--")
|
||||
}
|
||||
|
||||
pub fn npm<I, S>(args: I) -> Expression
|
||||
where
|
||||
I: IntoIterator<Item = S>,
|
||||
S: Into<OsString>,
|
||||
{
|
||||
let prog = if cfg!(target_os = "windows") { "npm.cmd" } else { "npm" };
|
||||
cmd(prog, args)
|
||||
}
|
||||
|
||||
pub fn node_bin(rel: &str) -> Expression {
|
||||
cmd!("node", format!("node_modules/{rel}"))
|
||||
}
|
||||
|
||||
pub fn open_url(url: &str) -> Result<(), Error> {
|
||||
#[cfg(target_os = "windows")]
|
||||
let expr = cmd!("cmd", "/c", "start", url);
|
||||
#[cfg(target_os = "macos")]
|
||||
let expr = cmd!("open", url);
|
||||
#[cfg(not(any(target_os = "windows", target_os = "macos")))]
|
||||
let expr = cmd!("xdg-open", url);
|
||||
expr.run()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "windows")]
|
||||
mod windows_ctrl_c {
|
||||
use std::sync::Once;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
|
||||
static INTERRUPTED: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
#[link(name = "kernel32")]
|
||||
unsafe extern "system" {
|
||||
fn SetConsoleCtrlHandler(handler: Option<unsafe extern "system" fn(u32) -> i32>, add: i32) -> i32;
|
||||
}
|
||||
|
||||
unsafe extern "system" fn handler(_ctrl_type: u32) -> i32 {
|
||||
INTERRUPTED.store(true, Ordering::SeqCst);
|
||||
1 // Report the event as handled
|
||||
}
|
||||
|
||||
pub fn install() {
|
||||
INTERRUPTED.store(false, Ordering::SeqCst);
|
||||
static REGISTER: Once = Once::new();
|
||||
REGISTER.call_once(|| unsafe {
|
||||
SetConsoleCtrlHandler(Some(handler), 1);
|
||||
});
|
||||
}
|
||||
|
||||
pub fn interrupted() -> bool {
|
||||
INTERRUPTED.load(Ordering::SeqCst)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
use crate::cmd::prelude::*;
|
||||
use crate::*;
|
||||
use std::path::PathBuf;
|
||||
|
||||
const WRAPPER_CRATE: &str = "graphite-wasm-wrapper";
|
||||
const WASM_TARGET: &str = "wasm32-unknown-unknown";
|
||||
const OUT_NAME: &str = "graphite_wasm_wrapper";
|
||||
|
||||
pub fn frontend_dir() -> PathBuf {
|
||||
workspace_dir().join("frontend")
|
||||
}
|
||||
|
||||
fn pkg_dir(native: bool) -> PathBuf {
|
||||
frontend_dir().join("wrapper").join(if native { "pkg-native" } else { "pkg" })
|
||||
}
|
||||
|
||||
fn wasm_glue_path(native: bool) -> PathBuf {
|
||||
pkg_dir(native).join(format!("{OUT_NAME}.js"))
|
||||
}
|
||||
|
||||
pub fn setup() -> Result<(), Error> {
|
||||
let frontend = frontend_dir();
|
||||
let node_modules = frontend.join("node_modules");
|
||||
let timestamp_path = node_modules.join(".install-timestamp");
|
||||
|
||||
let mtime = |p: PathBuf| std::fs::metadata(p).and_then(|m| m.modified()).ok();
|
||||
|
||||
if let Some(install_time) = mtime(timestamp_path.clone())
|
||||
&& let Some(package_json_time) = mtime(frontend.join("package.json"))
|
||||
&& let Some(package_lock_json_time) = mtime(frontend.join("package-lock.json"))
|
||||
&& install_time >= package_json_time
|
||||
&& install_time >= package_lock_json_time
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
eprintln!("Installing npm packages...");
|
||||
let install = || utils::npm(["ci", "--include=dev", "--prefer-offline", "--no-audit", "--no-fund"]).dir(&frontend).run();
|
||||
if install().is_err() {
|
||||
eprintln!("Failed to install npm packages. Wiping `frontend/node_modules` and retrying...");
|
||||
let _ = std::fs::remove_dir_all(&node_modules);
|
||||
install()?;
|
||||
}
|
||||
|
||||
std::fs::write(×tamp_path, "").map_err(|e| Error::Io(e, format!("writing '{}'", timestamp_path.display())))?;
|
||||
eprintln!("Finished installing npm packages.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn build_wasm(release: bool, native: bool) -> Result<(), Error> {
|
||||
sequence(build_wasm_steps(release, native)).wait();
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn build_wasm_steps(release: bool, native: bool) -> Vec<Expression> {
|
||||
let wasm_artifact = target_dir().join(WASM_TARGET).join(if release { "release" } else { "debug" }).join(format!("{OUT_NAME}.wasm"));
|
||||
let pkg_dir = pkg_dir(native);
|
||||
|
||||
let mut steps = vec![
|
||||
cmd!("cargo", "build", "--lib", "--package", WRAPPER_CRATE, "--target", WASM_TARGET)
|
||||
.arg_if(release, "--release")
|
||||
.args_if(native, ["--no-default-features", "--features", "native"])
|
||||
.dir(workspace_dir())
|
||||
.before_spawn(move |_| {
|
||||
if is_build_corrupted(wasm_glue_path(native)) {
|
||||
clean_wasm();
|
||||
}
|
||||
Ok(())
|
||||
}),
|
||||
cmd!("wasm-bindgen", "--target", "web", "--out-name", OUT_NAME, "--out-dir", &pkg_dir, &wasm_artifact)
|
||||
.arg_if(release, "--no-demangle")
|
||||
.arg_if(!release, "--debug"),
|
||||
];
|
||||
|
||||
if release {
|
||||
let wasm_file = pkg_dir.join(format!("{OUT_NAME}_bg.wasm"));
|
||||
// `-O3` favors runtime speed over binary size and `-g` preserves the name section,
|
||||
// which the panic hook reads at runtime to spot node-graph panics (see wrapper `lib.rs`).
|
||||
steps.push(cmd!("wasm-opt", "-O3", "-g", &wasm_file, "-o", &wasm_file));
|
||||
}
|
||||
|
||||
steps
|
||||
}
|
||||
|
||||
pub fn clean_wasm() -> bool {
|
||||
let wasm_target_dir = target_dir().join(WASM_TARGET);
|
||||
eprintln!("The Wasm build emitted undefined `env` imports, a sign of corrupt incremental artifacts (typically from an interrupted build).");
|
||||
eprintln!("Fixing by wiping `{}` and rebuilding...", wasm_target_dir.display());
|
||||
match std::fs::remove_dir_all(&wasm_target_dir) {
|
||||
Ok(()) => {}
|
||||
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {}
|
||||
Err(e) => eprintln!("warning: could not fully clean `{}`: {e}", wasm_target_dir.display()),
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
pub fn is_build_corrupted(path: PathBuf) -> bool {
|
||||
let Ok(js) = std::fs::read_to_string(&path) else {
|
||||
return false;
|
||||
};
|
||||
js.contains("from \"env\"") || js.contains("from 'env'")
|
||||
}
|
||||
|
||||
pub fn vite() -> Expression {
|
||||
utils::node_bin("vite/bin/vite.js").dir(frontend_dir()).env("CARGO_TARGET_DIR", target_dir())
|
||||
}
|
||||
|
||||
pub fn watch(release: bool) -> Result<(), Error> {
|
||||
use crate::cmd::prelude::*;
|
||||
|
||||
setup()?;
|
||||
build_wasm(release, false)?;
|
||||
|
||||
let vite = vite().env("FORCE_COLOR", "1").env("CARGO_TERM_COLOR", "always");
|
||||
let rust = utils::internal("watch")
|
||||
.arg_if(release, "release")
|
||||
.dir(workspace_dir())
|
||||
.env("CARGO_TARGET_DIR", target_dir())
|
||||
.env("CARGO_TERM_COLOR", "always");
|
||||
|
||||
supervise([("VITE", TerminalColor::Magenta, vite), ("RUST", TerminalColor::Blue, rust)])
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
use std::path::PathBuf;
|
||||
|
||||
pub mod branding;
|
||||
pub mod cmd;
|
||||
pub mod frontend;
|
||||
pub mod requirements;
|
||||
|
||||
pub enum Action {
|
||||
Run,
|
||||
Build,
|
||||
Explore(Option<String>),
|
||||
}
|
||||
|
||||
pub enum Target {
|
||||
Web,
|
||||
Desktop,
|
||||
Cli,
|
||||
}
|
||||
|
||||
pub enum Profile {
|
||||
Default,
|
||||
Release,
|
||||
Debug,
|
||||
}
|
||||
|
||||
pub struct Task {
|
||||
pub action: Action,
|
||||
pub target: Target,
|
||||
pub profile: Profile,
|
||||
pub args: Vec<String>,
|
||||
}
|
||||
|
||||
impl Task {
|
||||
pub fn parse(args: &[&str]) -> Option<Self> {
|
||||
let split = args.iter().position(|a| *a == "--").unwrap_or(args.len());
|
||||
let passthru_args = args[split..].iter().skip(1).map(|s| s.to_string()).collect();
|
||||
let args = &args[..split];
|
||||
|
||||
let (action, args) = match args.first() {
|
||||
Some(&"build") => (Action::Build, &args[1..]),
|
||||
Some(&"run") => (Action::Run, &args[1..]),
|
||||
Some(&"explore") => (Action::Explore(args.get(1).map(|s| s.to_string())), &[] as &[&str]),
|
||||
Some(&"help") => return None,
|
||||
_ => (Action::Run, args),
|
||||
};
|
||||
|
||||
let (target, args) = match args.first() {
|
||||
Some(&"desktop") => (Target::Desktop, &args[1..]),
|
||||
Some(&"web") => (Target::Web, &args[1..]),
|
||||
Some(&"cli") => (Target::Cli, &args[1..]),
|
||||
_ => (Target::Web, args),
|
||||
};
|
||||
|
||||
let profile = match args.first() {
|
||||
Some(&"release") => Profile::Release,
|
||||
Some(&"debug") => Profile::Debug,
|
||||
None => Profile::Default,
|
||||
_ => return None,
|
||||
};
|
||||
|
||||
Some(Task {
|
||||
target,
|
||||
action,
|
||||
profile,
|
||||
args: passthru_args,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn workspace_dir() -> PathBuf {
|
||||
PathBuf::from(env!("CARGO_WORKSPACE_DIR"))
|
||||
}
|
||||
|
||||
pub fn target_dir() -> PathBuf {
|
||||
match std::env::var_os("CARGO_TARGET_DIR") {
|
||||
Some(custom_dir) => workspace_dir().join(custom_dir),
|
||||
None => workspace_dir().join("target"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn install_dir() -> PathBuf {
|
||||
target_dir().join("cargo-run")
|
||||
}
|
||||
|
||||
pub fn bin_dir() -> PathBuf {
|
||||
install_dir().join("bin")
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("One or more requirements were not met")]
|
||||
RequirementsNotMet,
|
||||
|
||||
#[error("{1}: {0}")]
|
||||
Io(#[source] std::io::Error, String),
|
||||
/// Used by the duct-based `cmd` module; folds in `Command` once call sites are migrated.
|
||||
#[error("{0}")]
|
||||
Command(#[source] std::io::Error),
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
use std::process::ExitCode;
|
||||
|
||||
use cargo_run::cmd::prelude::*;
|
||||
use cargo_run::*;
|
||||
|
||||
fn usage() {
|
||||
println!();
|
||||
println!("USAGE:");
|
||||
println!(" cargo run [<command>] [<target>] [<profile>] [-- [args]...]");
|
||||
println!();
|
||||
println!("COMMON USAGE:");
|
||||
println!(" cargo run Run the web app");
|
||||
println!(" cargo run desktop Run the desktop app");
|
||||
println!();
|
||||
println!("OPTIONS:");
|
||||
println!("<command>:");
|
||||
println!(" [run] Run the selected target (default)");
|
||||
println!(" build Build the selected target");
|
||||
println!(" explore Open an assortment of tools for exploring the codebase");
|
||||
println!(" help Show this message");
|
||||
println!("<target>:");
|
||||
println!(" [web] Web app (default)");
|
||||
println!(" desktop Desktop app");
|
||||
println!(" cli Graphene CLI");
|
||||
println!("<profile>:");
|
||||
println!(" [debug] Optimizations disabled (default for run)");
|
||||
println!(" [release] Optimizations enabled (default for build)");
|
||||
println!();
|
||||
println!("MORE EXAMPLES:");
|
||||
println!(" cargo run build desktop");
|
||||
println!(" cargo run desktop release");
|
||||
println!(" cargo run cli -- --help");
|
||||
println!()
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
prepend_path();
|
||||
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
let args: Vec<&str> = args.iter().skip(1).map(String::as_str).collect();
|
||||
|
||||
let task = match Task::parse(&args) {
|
||||
Some(run) => run,
|
||||
None => {
|
||||
usage();
|
||||
return ExitCode::SUCCESS;
|
||||
}
|
||||
};
|
||||
|
||||
if let Err(e) = run_task(&task) {
|
||||
eprintln!("Error: {e}");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
|
||||
fn explore_usage() {
|
||||
println!();
|
||||
println!("USAGE:");
|
||||
println!(" cargo run explore <tool>");
|
||||
println!();
|
||||
println!("OPTIONS:");
|
||||
println!("<tool>:");
|
||||
println!(" bisect Binary search through recent commits to find which introduced a bug or feature");
|
||||
println!(" deps View the crate dependency graph for the workspace");
|
||||
println!(" editor View an interactive outline of the editor's message system architecture");
|
||||
println!();
|
||||
}
|
||||
|
||||
fn run_task(task: &Task) -> Result<(), Error> {
|
||||
if let Action::Explore(tool) = &task.action {
|
||||
match tool.as_deref() {
|
||||
Some("bisect") => return utils::open_url("https://graphite.art/volunteer/guide/codebase-overview/debugging-tips/#build-bisect-tool"),
|
||||
Some("deps") => return utils::open_url("https://graphite.art/volunteer/guide/codebase-overview/#crate-dependency-graph"),
|
||||
Some("editor") => return utils::open_url("https://graphite.art/volunteer/guide/codebase-overview/editor-structure/#editor-outline"),
|
||||
None | Some("--help") => {
|
||||
explore_usage();
|
||||
return Ok(());
|
||||
}
|
||||
Some(other) => {
|
||||
eprintln!("Unknown explore tool: '{other}'");
|
||||
explore_usage();
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
requirements::check(task)?;
|
||||
|
||||
if !matches!(task.target, Target::Cli) {
|
||||
branding::setup()?;
|
||||
}
|
||||
|
||||
match (&task.action, &task.target, &task.profile) {
|
||||
(Action::Run, Target::Web, Profile::Debug | Profile::Default) => frontend::watch(false)?,
|
||||
(Action::Run, Target::Web, Profile::Release) => frontend::watch(true)?,
|
||||
|
||||
(Action::Build, Target::Web, Profile::Debug) => {
|
||||
frontend::setup()?;
|
||||
frontend::build_wasm(false, false)?;
|
||||
frontend::vite().args(["build", "--mode", "dev"]).run()?;
|
||||
}
|
||||
(Action::Build, Target::Web, Profile::Release | Profile::Default) => {
|
||||
frontend::setup()?;
|
||||
frontend::build_wasm(true, false)?;
|
||||
frontend::vite().args(["build"]).run()?;
|
||||
}
|
||||
|
||||
(action, Target::Desktop, mut profile) => {
|
||||
if matches!(profile, Profile::Default) {
|
||||
profile = match action {
|
||||
Action::Run => &Profile::Debug,
|
||||
Action::Build => &Profile::Release,
|
||||
Action::Explore(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
// Build the editor's Wasm module with the `native` feature, then bundle the frontend with Vite
|
||||
frontend::setup()?;
|
||||
frontend::build_wasm(matches!(profile, Profile::Release), true)?;
|
||||
frontend::vite().args(["build", "--mode", "native"]).run()?;
|
||||
|
||||
cmd!("cargo", "run", "-p", "third-party-licenses", "--features", "desktop").run()?;
|
||||
|
||||
let cargo_profile = match profile {
|
||||
Profile::Debug => "dev",
|
||||
Profile::Release => "release",
|
||||
Profile::Default => unreachable!(),
|
||||
};
|
||||
cmd!("cargo", "run", "--profile", cargo_profile, "-p", "graphite-desktop-bundle")
|
||||
.args_if(matches!(action, Action::Run), ["--", "open"].into_iter().chain(task.args.iter().map(String::as_str)))
|
||||
.run()?;
|
||||
}
|
||||
|
||||
(Action::Run, Target::Cli, Profile::Debug | Profile::Default) => cmd!("cargo", "run", "-p", "graphene-cli", "--").args(&task.args).run()?,
|
||||
(Action::Run, Target::Cli, Profile::Release) => cmd!("cargo", "run", "-r", "-p", "graphene-cli", "--").args(&task.args).run()?,
|
||||
|
||||
(Action::Build, Target::Cli, Profile::Debug) => cmd!("cargo", "build", "-p", "graphene-cli").run()?,
|
||||
(Action::Build, Target::Cli, Profile::Release | Profile::Default) => cmd!("cargo", "build", "-r", "-p", "graphene-cli").run()?,
|
||||
|
||||
(Action::Explore(_), _, _) => unreachable!(),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn prepend_path() {
|
||||
let mut paths = vec![bin_dir()];
|
||||
if let Some(path) = std::env::var_os("PATH") {
|
||||
paths.extend(std::env::split_paths(&path));
|
||||
}
|
||||
if let Ok(joined) = std::env::join_paths(paths) {
|
||||
// Safety: this runs before any other threads are spawned
|
||||
unsafe { std::env::set_var("PATH", joined) };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,330 @@
|
||||
use semver::{Version, VersionReq};
|
||||
use std::io::IsTerminal;
|
||||
|
||||
use crate::cmd::prelude::*;
|
||||
use crate::*;
|
||||
|
||||
mod wasm_opt;
|
||||
|
||||
#[derive(Default, Clone)]
|
||||
pub struct Requirement {
|
||||
command: &'static str,
|
||||
args: &'static [&'static str],
|
||||
name: &'static str,
|
||||
check: Check,
|
||||
version: Option<&'static str>,
|
||||
install: InstallAction,
|
||||
skip: Option<&'static dyn Fn(&Task) -> bool>,
|
||||
}
|
||||
|
||||
fn requirements(task: &Task) -> Vec<Requirement> {
|
||||
[
|
||||
Requirement {
|
||||
command: "rustc",
|
||||
args: &["--version"],
|
||||
name: "Rust",
|
||||
..Default::default()
|
||||
},
|
||||
Requirement {
|
||||
command: "rustc",
|
||||
args: &["--print", "target-libdir", "--target", "wasm32-unknown-unknown"],
|
||||
check: Check::Matches(&|out| std::path::Path::new(out.trim()).is_dir()),
|
||||
name: "Rust - Wasm Target",
|
||||
install: "rustup target add wasm32-unknown-unknown".into(),
|
||||
skip: Some(&|task| matches!(task.target, Target::Cli)),
|
||||
..Default::default()
|
||||
},
|
||||
Requirement {
|
||||
command: "wasm-opt",
|
||||
args: &["--version"],
|
||||
name: "Wasm Opt",
|
||||
version: Some(">=130"),
|
||||
install: wasm_opt::install_action(),
|
||||
skip: Some(&|task| {
|
||||
matches!(task.target, Target::Cli)
|
||||
|| match task.profile {
|
||||
Profile::Debug => true,
|
||||
Profile::Release => false,
|
||||
Profile::Default => matches!(task.action, Action::Run),
|
||||
}
|
||||
}),
|
||||
..Default::default()
|
||||
},
|
||||
Requirement {
|
||||
command: "wasm-bindgen",
|
||||
args: &["--version"],
|
||||
name: "Wasm Bindgen",
|
||||
version: Some("=0.2.121"),
|
||||
install: "cargo install -f wasm-bindgen-cli@0.2.121".into(),
|
||||
skip: Some(&|task| matches!(task.target, Target::Cli)),
|
||||
..Default::default()
|
||||
},
|
||||
Requirement {
|
||||
command: "cargo-about",
|
||||
args: &["--version"],
|
||||
name: "Cargo About",
|
||||
install: "cargo install cargo-about".into(),
|
||||
skip: Some(&|task| matches!(task.target, Target::Cli)),
|
||||
..Default::default()
|
||||
},
|
||||
Requirement {
|
||||
command: "node",
|
||||
args: &["--version"],
|
||||
name: "Node.js",
|
||||
skip: Some(&|task| matches!(task.target, Target::Cli)),
|
||||
..Default::default()
|
||||
},
|
||||
Requirement {
|
||||
command: "cmake",
|
||||
args: &["--version"],
|
||||
name: "CMake",
|
||||
install: InstallAction::ManualInstructions("https://cmake.org/download/ or find it in your system's package manager"),
|
||||
skip: Some(&|task| !matches!(task.target, Target::Desktop) || cfg!(target_os = "linux")),
|
||||
..Default::default()
|
||||
},
|
||||
Requirement {
|
||||
command: "ninja",
|
||||
args: &["--version"],
|
||||
name: "Ninja",
|
||||
install: InstallAction::ManualInstructions("https://github.com/ninja-build/ninja/releases or find it in your system's package manager"),
|
||||
skip: Some(&|task| !matches!(task.target, Target::Desktop) || cfg!(target_os = "linux")),
|
||||
..Default::default()
|
||||
},
|
||||
]
|
||||
.iter()
|
||||
.filter(|d| if let Some(skip) = d.skip { !skip(task) } else { true })
|
||||
.cloned()
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn check(task: &Task) -> Result<(), Error> {
|
||||
eprintln!();
|
||||
eprintln!("Checking Requirements:");
|
||||
|
||||
let mut installable: Vec<Requirement> = Vec::new();
|
||||
let mut manual: Vec<(Requirement, String)> = Vec::new();
|
||||
|
||||
for dep in requirements(task) {
|
||||
match cmd(dep.command, dep.args.iter().copied()).output_unchecked() {
|
||||
Ok(output) if output.status.success() => {
|
||||
let stdout = String::from_utf8_lossy(&output.stdout);
|
||||
match dep.check {
|
||||
Check::PrintsVersion => {
|
||||
let line = stdout.lines().next().unwrap_or_default().trim();
|
||||
match dep.version {
|
||||
None => eprintln!(" ✓ {} ({line})", dep.name),
|
||||
Some(req_str) => {
|
||||
let req = VersionReq::parse(req_str).expect("invalid semver requirement");
|
||||
match extract_version(line) {
|
||||
Some(version) if req.matches(&version) => eprintln!(" ✓ {} ({version})", dep.name),
|
||||
Some(version) => {
|
||||
eprintln!(" ✗ {} (found {version}, requires {req_str})", dep.name);
|
||||
if dep.install.is_auto_installable() {
|
||||
installable.push(dep);
|
||||
} else {
|
||||
manual.push((dep, format!("version mismatch (found {version}, requires {req_str})")));
|
||||
}
|
||||
}
|
||||
None => {
|
||||
eprintln!(" ✗ {} (could not parse version from '{line}')", dep.name);
|
||||
manual.push((dep, format!("could not parse version from '{line}'")));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Check::Matches(check) => {
|
||||
if !check(stdout.to_string()) {
|
||||
eprintln!(" ✗ {} - check failed", dep.name);
|
||||
if dep.install.is_auto_installable() {
|
||||
installable.push(dep);
|
||||
} else {
|
||||
manual.push((dep, "check failed".into()));
|
||||
}
|
||||
} else {
|
||||
eprintln!(" ✓ {}", dep.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(output) => {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
eprintln!(" ✗ {} - command failed: {}", dep.name, stderr.trim());
|
||||
if dep.install.is_auto_installable() {
|
||||
installable.push(dep);
|
||||
} else {
|
||||
manual.push((dep.clone(), format!("`{}` not installed or not working", dep.command)));
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
eprintln!(" ✗ {} - not found", dep.name);
|
||||
if dep.install.is_auto_installable() {
|
||||
installable.push(dep);
|
||||
} else {
|
||||
manual.push((dep.clone(), format!("`{}` not found in PATH", dep.command)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
eprintln!();
|
||||
|
||||
if installable.is_empty() && manual.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let total = installable.len() + manual.len();
|
||||
eprintln!("{total} requirement{} not met:", if total > 1 { "s" } else { "" });
|
||||
for dep in &installable {
|
||||
eprintln!(" - {}", dep.name);
|
||||
}
|
||||
for (dep, msg) in &manual {
|
||||
eprintln!(" - {}: {msg}", dep.name);
|
||||
}
|
||||
|
||||
if !manual.is_empty() {
|
||||
eprintln!();
|
||||
eprintln!("See: https://graphite.art/volunteer/guide/project-setup/");
|
||||
}
|
||||
|
||||
let is_interactive = std::io::stdout().is_terminal() && std::io::stderr().is_terminal() && std::io::stdin().is_terminal();
|
||||
|
||||
// Don't prompt for automatic installation if we're not interactive session
|
||||
if !is_interactive {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if installable.is_empty() && manual.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !installable.is_empty() {
|
||||
eprintln!();
|
||||
eprintln!("The following can be installed automatically:");
|
||||
for dep in &installable {
|
||||
match &dep.install {
|
||||
InstallAction::Command(cmd) => eprintln!(" - {}: {}", dep.name, cmd),
|
||||
InstallAction::Expression { description, .. } => eprintln!(" - {description}"),
|
||||
InstallAction::None | InstallAction::ManualInstructions(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
eprintln!();
|
||||
if installable.len() == 1 {
|
||||
eprint!("Install it now? [y/N] ");
|
||||
} else {
|
||||
eprint!("Install them now? [y/N] ");
|
||||
}
|
||||
|
||||
let mut input = String::new();
|
||||
std::io::stdin().read_line(&mut input).map_err(|e| Error::Io(e, "Failed to read from stdin".into()))?;
|
||||
let input = input.trim();
|
||||
|
||||
if input.eq_ignore_ascii_case("y") || input.eq_ignore_ascii_case("yes") {
|
||||
for dep in installable.into_iter() {
|
||||
eprintln!("Installing {}...", dep.name);
|
||||
match &dep.install {
|
||||
InstallAction::Command(install_cmd) => {
|
||||
let parts: Vec<&str> = install_cmd.split_whitespace().collect();
|
||||
let expr = cmd(parts[0], parts[1..].iter().copied()).unchecked();
|
||||
match Expression::run(&expr) {
|
||||
Ok(output) if !output.status.success() => {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
manual.push((dep, format!("installation command failed: {}", stderr.trim())));
|
||||
}
|
||||
Err(e) => return Err(Error::Command(e)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
InstallAction::Expression { expression, .. } => {
|
||||
if let Err(e) = expression.clone().run() {
|
||||
eprintln!("{e}");
|
||||
eprintln!("Failed to install {}", dep.name);
|
||||
manual.push((dep, format!("failed to install ({e})")));
|
||||
}
|
||||
}
|
||||
InstallAction::None | InstallAction::ManualInstructions(_) => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !manual.is_empty() {
|
||||
eprintln!();
|
||||
eprintln!("Please install the following dependenc{}:", if manual.len() == 1 { "y" } else { "ies" });
|
||||
for (dep, msg) in &manual {
|
||||
match dep.install {
|
||||
InstallAction::ManualInstructions(instructions) => eprintln!(" - {}: {}", dep.name, instructions),
|
||||
_ => eprintln!(" - {}: {}", dep.name, msg),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!manual.is_empty()) && is_interactive {
|
||||
eprintln!();
|
||||
eprintln!("Attempt to continue regardless of unmet requirements? [y/N]");
|
||||
|
||||
let mut input = String::new();
|
||||
std::io::stdin().read_line(&mut input).map_err(|e| Error::Io(e, "Failed to read from stdin".into()))?;
|
||||
let input = input.trim();
|
||||
|
||||
if !input.eq_ignore_ascii_case("y") && !input.eq_ignore_ascii_case("yes") {
|
||||
return Err(Error::RequirementsNotMet);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn extract_version(line: &str) -> Option<Version> {
|
||||
line.split_whitespace().find_map(|token| {
|
||||
let token = token.trim_start_matches('v').trim_end_matches(|c: char| !c.is_ascii_alphanumeric());
|
||||
if token.is_empty() {
|
||||
return None;
|
||||
}
|
||||
if let Ok(version) = Version::parse(token) {
|
||||
return Some(version);
|
||||
}
|
||||
let (core, suffix) = match token.find(['-', '+']) {
|
||||
Some(i) => token.split_at(i),
|
||||
None => (token, ""),
|
||||
};
|
||||
let parts: Vec<&str> = core.split('.').collect();
|
||||
if parts.iter().any(|p| p.is_empty() || !p.chars().all(|c| c.is_ascii_digit())) {
|
||||
return None;
|
||||
}
|
||||
let major = parts[0];
|
||||
let minor = parts.get(1).copied().unwrap_or("0");
|
||||
let patch = parts.get(2).copied().unwrap_or("0");
|
||||
Version::parse(&format!("{major}.{minor}.{patch}{suffix}")).ok()
|
||||
})
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
enum Check {
|
||||
#[default]
|
||||
PrintsVersion,
|
||||
Matches(&'static dyn Fn(String) -> bool),
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
enum InstallAction {
|
||||
#[default]
|
||||
None,
|
||||
Command(&'static str),
|
||||
Expression {
|
||||
description: String,
|
||||
expression: Expression,
|
||||
},
|
||||
ManualInstructions(&'static str),
|
||||
}
|
||||
|
||||
impl InstallAction {
|
||||
fn is_auto_installable(&self) -> bool {
|
||||
!matches!(self, InstallAction::None | InstallAction::ManualInstructions(_))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'static str> for InstallAction {
|
||||
fn from(value: &'static str) -> Self {
|
||||
InstallAction::Command(value)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
use super::InstallAction;
|
||||
use crate::cmd::prelude::*;
|
||||
use crate::{install_dir, workspace_dir};
|
||||
|
||||
/// Pinned Binaryen release used by [`install_action`].
|
||||
/// NOTICE: keep in sync with the `BINARYEN_VERSION` pinned across the CI workflows, and update [`SHA256`] below.
|
||||
const VERSION: &str = "130";
|
||||
const SHA256: &[(&str, &str)] = &[
|
||||
("x86_64-windows", "cc09c874f4332d00aa32ab72745a9b98c9a172f795762f21d03e70638a3f7f4c"),
|
||||
("arm64-windows", "b18c9cbe000562b1ee5d9cb60146616a949aca504903ad63f27fd9fd679898a7"),
|
||||
("arm64-macos", "79d3ab9f417d9e215f15f598f523d001a7d9ac1e59367e5c869fbdabd1cba72e"),
|
||||
("x86_64-macos", "d3e2d1235b70c93c54b52eabc1625ea960965152218754f1f4eeb0f873c48e03"),
|
||||
("x86_64-linux", "0a18362361ad05465118cd8eeb72edaeec89de6894bc283576ef4e07aa3babcc"),
|
||||
("aarch64-linux", "e6ae6e09ac40f4e14bc5be6f687c58e2995c84170013975fa641809dd3b480a0"),
|
||||
];
|
||||
|
||||
fn url_for(platform: &str) -> String {
|
||||
format!("https://github.com/WebAssembly/binaryen/releases/download/version_{VERSION}/binaryen-version_{VERSION}-{platform}.tar.gz")
|
||||
}
|
||||
|
||||
pub fn install_action() -> InstallAction {
|
||||
let platform = match (std::env::consts::OS, std::env::consts::ARCH) {
|
||||
("windows", "x86_64") => "x86_64-windows",
|
||||
("windows", "aarch64") => "arm64-windows",
|
||||
("macos", "aarch64") => "arm64-macos",
|
||||
("macos", "x86_64") => "x86_64-macos",
|
||||
("linux", "x86_64") => "x86_64-linux",
|
||||
("linux", "aarch64") => "aarch64-linux",
|
||||
_ => return InstallAction::None,
|
||||
};
|
||||
let url = url_for(platform);
|
||||
let Some(sha256) = SHA256.iter().find_map(|(p, s)| (*p == platform).then_some(*s)) else {
|
||||
return InstallAction::None;
|
||||
};
|
||||
let out = install_dir().to_string_lossy().into_owned();
|
||||
let description = format!("Download wasm-opt {VERSION} from {url} (sha256 {sha256})");
|
||||
|
||||
let args = [&url, sha256, &out, "--extract", "--strip", "1", "--include", "bin/wasm-opt"];
|
||||
|
||||
#[cfg(target_os = "macos")]
|
||||
let args = args.into_iter().chain(["--include", "lib/libbinaryen.dylib"]).collect::<Vec<_>>();
|
||||
|
||||
let expression = utils::internal("download").args(args).dir(workspace_dir());
|
||||
|
||||
InstallAction::Expression { description, expression }
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
*.dot
|
||||
*.png
|
||||
*.svg
|
||||
@@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "crate-hierarchy-viz"
|
||||
description = "Tool to visualize the crate hierarchy in the Graphite workspace"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
serde = { workspace = true }
|
||||
toml = "0.8"
|
||||
anyhow = { workspace = true }
|
||||
glob = "0.3"
|
||||
@@ -0,0 +1,316 @@
|
||||
use anyhow::{Context, Result};
|
||||
use serde::Deserialize;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct WorkspaceToml {
|
||||
workspace: WorkspaceConfig,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct WorkspaceConfig {
|
||||
members: Vec<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct CrateToml {
|
||||
package: PackageConfig,
|
||||
dependencies: Option<HashMap<String, CrateDependency>>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct PackageConfig {
|
||||
name: String,
|
||||
}
|
||||
|
||||
/// Represents a crate-level dependency in Cargo.toml
|
||||
/// The Simple variant's String is needed for serde deserialization but never read directly
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[serde(untagged)]
|
||||
#[allow(dead_code)]
|
||||
enum CrateDependency {
|
||||
Simple(String),
|
||||
Detailed {
|
||||
path: Option<String>,
|
||||
#[serde(flatten)]
|
||||
other: HashMap<String, toml::Value>,
|
||||
},
|
||||
}
|
||||
|
||||
struct CrateInfo {
|
||||
name: String,
|
||||
path: PathBuf,
|
||||
dependencies: Vec<String>,
|
||||
}
|
||||
|
||||
/// Remove transitive dependencies from the crate list.
|
||||
/// If A depends on B and C, and B depends on C, then A->C is removed.
|
||||
fn remove_transitive_dependencies(crates: &mut [CrateInfo]) {
|
||||
// Build a map from crate name to its dependencies for quick lookup
|
||||
let dep_map: HashMap<String, HashSet<String>> = crates.iter().map(|c| (c.name.clone(), c.dependencies.iter().cloned().collect())).collect();
|
||||
|
||||
// For each crate, compute which dependencies are reachable through other dependencies
|
||||
for crate_info in crates.iter_mut() {
|
||||
let mut transitive_deps = HashSet::new();
|
||||
|
||||
// For each direct dependency, find all its transitive dependencies
|
||||
for direct_dep in &crate_info.dependencies {
|
||||
// Recursively collect all transitive dependencies of this direct dependency
|
||||
let mut visited = HashSet::new();
|
||||
collect_all_dependencies(direct_dep, &dep_map, &mut visited);
|
||||
// Remove the direct dependency itself from the visited set
|
||||
visited.remove(direct_dep);
|
||||
transitive_deps.extend(visited);
|
||||
}
|
||||
|
||||
// Remove dependencies that are transitive
|
||||
crate_info.dependencies.retain(|dep| !transitive_deps.contains(dep));
|
||||
}
|
||||
}
|
||||
|
||||
/// Recursively collect all dependencies of a crate
|
||||
fn collect_all_dependencies(crate_name: &str, dep_map: &HashMap<String, HashSet<String>>, visited: &mut HashSet<String>) {
|
||||
if !visited.insert(crate_name.to_string()) {
|
||||
return; // Already visited, avoid cycles
|
||||
}
|
||||
if let Some(deps) = dep_map.get(crate_name) {
|
||||
for dep in deps {
|
||||
collect_all_dependencies(dep, dep_map, visited);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let output_dir = std::env::args_os()
|
||||
.nth(1)
|
||||
.map(PathBuf::from)
|
||||
.ok_or_else(|| anyhow::anyhow!("Usage: crate-hierarchy-viz <output-directory>"))?;
|
||||
let output_path = output_dir.join("crate-hierarchy.svg");
|
||||
|
||||
let workspace_root = std::env::current_dir()?;
|
||||
let workspace_toml_path = workspace_root.join("Cargo.toml");
|
||||
|
||||
// Parse workspace Cargo.toml
|
||||
let workspace_content = fs::read_to_string(&workspace_toml_path).with_context(|| format!("Failed to read {:?}", workspace_toml_path))?;
|
||||
let workspace_toml: WorkspaceToml = toml::from_str(&workspace_content).with_context(|| "Failed to parse workspace Cargo.toml")?;
|
||||
|
||||
// Expand glob patterns in workspace members (e.g., "node-graph/libraries/*")
|
||||
let mut resolved_members = Vec::new();
|
||||
let mut seen_members = HashSet::new();
|
||||
let abs_root = workspace_root.canonicalize().unwrap_or_else(|_| workspace_root.clone());
|
||||
for member in &workspace_toml.workspace.members {
|
||||
if member.contains('*') {
|
||||
let pattern = abs_root.join(member).to_string_lossy().to_string();
|
||||
let matched: Vec<_> = glob::glob(&pattern)
|
||||
.with_context(|| format!("Failed to expand glob pattern: {member}"))?
|
||||
.filter_map(|entry| entry.ok())
|
||||
.filter_map(|path| path.strip_prefix(&abs_root).ok().map(|p| p.to_string_lossy().to_string()))
|
||||
.collect();
|
||||
if matched.is_empty() {
|
||||
eprintln!("Warning: No matches for glob pattern: {member}");
|
||||
}
|
||||
for m in matched {
|
||||
let normalized = m.replace('\\', "/");
|
||||
if seen_members.insert(normalized.clone()) {
|
||||
resolved_members.push(normalized);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let normalized = member.replace('\\', "/");
|
||||
if seen_members.insert(normalized.clone()) {
|
||||
resolved_members.push(normalized);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parse each member crate's Cargo.toml
|
||||
let mut parsed_crates = Vec::new();
|
||||
for member in &resolved_members {
|
||||
let crate_path = workspace_root.join(member);
|
||||
let cargo_toml_path = crate_path.join("Cargo.toml");
|
||||
|
||||
if !cargo_toml_path.exists() {
|
||||
eprintln!("Warning: Cargo.toml not found for member: {}", member);
|
||||
continue;
|
||||
}
|
||||
|
||||
let crate_content = fs::read_to_string(&cargo_toml_path).with_context(|| format!("Failed to read {:?}", cargo_toml_path))?;
|
||||
let crate_toml: CrateToml = toml::from_str(&crate_content).with_context(|| format!("Failed to parse Cargo.toml for {}", member))?;
|
||||
|
||||
parsed_crates.push((crate_path, crate_toml));
|
||||
}
|
||||
|
||||
// Collect all workspace crate names
|
||||
let workspace_crate_names: HashSet<String> = parsed_crates.iter().map(|(_, toml)| toml.package.name.clone()).collect();
|
||||
|
||||
// Build dependency graph, keeping only workspace-internal dependencies
|
||||
let mut crates: Vec<CrateInfo> = parsed_crates
|
||||
.into_iter()
|
||||
.map(|(path, crate_toml)| {
|
||||
let dependencies = crate_toml
|
||||
.dependencies
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.filter_map(|(dep_name, dep_config)| {
|
||||
// Resolve the actual package name (handles renamed dependencies)
|
||||
let actual_name = match &dep_config {
|
||||
CrateDependency::Detailed { other, .. } => other.get("package").and_then(|v| v.as_str()).map(|s| s.to_string()).unwrap_or(dep_name),
|
||||
CrateDependency::Simple(_) => dep_name,
|
||||
};
|
||||
// Only keep dependencies that are workspace crates
|
||||
workspace_crate_names.contains(&actual_name).then_some(actual_name)
|
||||
})
|
||||
.collect();
|
||||
|
||||
CrateInfo {
|
||||
name: crate_toml.package.name,
|
||||
path,
|
||||
dependencies,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
remove_transitive_dependencies(&mut crates);
|
||||
|
||||
// Generate DOT format, convert to SVG, and write to output file
|
||||
let dot_content = generate_dot(&crates);
|
||||
let svg_content = dot_to_svg(&dot_content)?;
|
||||
|
||||
fs::create_dir_all(&output_dir).with_context(|| format!("Failed to create directory {:?}", output_dir))?;
|
||||
fs::write(&output_path, &svg_content).with_context(|| format!("Failed to write to {:?}", output_path))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Convert a DOT graph string to SVG by shelling out to @viz-js/viz via Node.js
|
||||
fn dot_to_svg(dot: &str) -> Result<String> {
|
||||
let temp_dir = std::env::temp_dir().join("crate-hierarchy-viz");
|
||||
fs::create_dir_all(&temp_dir).with_context(|| "Failed to create temp directory")?;
|
||||
|
||||
// Install @viz-js/viz into the temp directory if not already present
|
||||
let viz_package = temp_dir.join("node_modules").join("@viz-js").join("viz");
|
||||
if !viz_package.exists() {
|
||||
let npm = if cfg!(target_os = "windows") { "npm.cmd" } else { "npm" };
|
||||
let status = Command::new(npm)
|
||||
.args(["install", "--prefix", &temp_dir.to_string_lossy(), "@viz-js/viz"])
|
||||
.stdout(std::process::Stdio::null())
|
||||
.stderr(std::process::Stdio::piped())
|
||||
.status()
|
||||
.with_context(|| "Failed to run `npm install`. Is Node.js installed?")?;
|
||||
if !status.success() {
|
||||
anyhow::bail!("Executing `npm install @viz-js/viz` failed");
|
||||
}
|
||||
}
|
||||
|
||||
// Write a small script that reads DOT from stdin and outputs SVG
|
||||
let script_path = temp_dir.join("convert.mjs");
|
||||
fs::write(
|
||||
&script_path,
|
||||
r#"
|
||||
import { instance } from "@viz-js/viz";
|
||||
let dot = "";
|
||||
for await (const chunk of process.stdin) dot += chunk;
|
||||
const viz = await instance();
|
||||
process.stdout.write(viz.renderString(dot, { format: "svg" }));
|
||||
"#
|
||||
.trim(),
|
||||
)?;
|
||||
|
||||
let mut child = Command::new("node")
|
||||
.arg(&script_path)
|
||||
.stdin(std::process::Stdio::piped())
|
||||
.stdout(std::process::Stdio::piped())
|
||||
.stderr(std::process::Stdio::piped())
|
||||
.spawn()
|
||||
.with_context(|| "Failed to spawn `node`. Is Node.js installed?")?;
|
||||
|
||||
// Write DOT content to stdin then close the pipe
|
||||
child
|
||||
.stdin
|
||||
.take()
|
||||
.context("Failed to get stdin handle for node process")?
|
||||
.write_all(dot.as_bytes())
|
||||
.with_context(|| "Failed to write DOT content to stdin")?;
|
||||
|
||||
let output = child.wait_with_output().with_context(|| "Failed to wait for `node` process")?;
|
||||
|
||||
// Clean up the temp script (node_modules is intentionally kept as a cache)
|
||||
let _ = fs::remove_file(&script_path);
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
anyhow::bail!("DOT to SVG conversion failed (exit code {:?}):\n{}", output.status.code(), stderr);
|
||||
}
|
||||
|
||||
String::from_utf8(output.stdout).with_context(|| "SVG output was not valid UTF-8")
|
||||
}
|
||||
|
||||
fn generate_dot(crates: &[CrateInfo]) -> String {
|
||||
let mut out = String::new();
|
||||
out.push_str("digraph CrateHierarchy {\n");
|
||||
out.push_str(" rankdir=LR;\n");
|
||||
out.push_str(" node [shape=box, style=\"rounded,filled\", fillcolor=lightblue];\n");
|
||||
out.push_str(" edge [color=gray];\n\n");
|
||||
|
||||
// Define subgraph clusters
|
||||
let clusters: &[(&str, &str, &str, Box<dyn Fn(&CrateInfo) -> bool>)] = &[
|
||||
(
|
||||
"cluster_core",
|
||||
"Core Components",
|
||||
"lightgray",
|
||||
Box::new(|c| (c.name.starts_with("graphite-") || c.name == "editor" || c.name == "graphene-cli") && !c.name.contains("desktop")),
|
||||
),
|
||||
(
|
||||
"cluster_nodegraph",
|
||||
"Node Graph System",
|
||||
"lightyellow",
|
||||
Box::new(|c| c.name == "graph-craft" || c.name == "interpreted-executor" || c.name == "node-macro" || c.name == "preprocessor" || c.name == "graphene-cli"),
|
||||
),
|
||||
(
|
||||
"cluster_node_libraries",
|
||||
"Node Graph Libraries",
|
||||
"lightcyan",
|
||||
Box::new(|c| c.path.to_string_lossy().replace('\\', "/").contains("node-graph/libraries")),
|
||||
),
|
||||
(
|
||||
"cluster_nodes",
|
||||
"Nodes",
|
||||
"lightblue",
|
||||
Box::new(|c| c.path.to_string_lossy().replace('\\', "/").contains("node-graph/nodes")),
|
||||
),
|
||||
(
|
||||
"cluster_desktop",
|
||||
"Desktop",
|
||||
"lightgreen",
|
||||
Box::new(|c| c.path.to_string_lossy().replace('\\', "/").contains("desktop")),
|
||||
),
|
||||
];
|
||||
|
||||
for (id, label, color, filter) in clusters {
|
||||
out.push_str(&format!(" subgraph {id} {{\n"));
|
||||
out.push_str(&format!(" label=\"{label}\";\n"));
|
||||
out.push_str(" style=filled;\n");
|
||||
out.push_str(&format!(" fillcolor={color};\n"));
|
||||
for c in crates.iter().filter(|c| filter(c)) {
|
||||
out.push_str(&format!(" \"{}\";\n", c.name));
|
||||
}
|
||||
out.push_str(" }\n\n");
|
||||
}
|
||||
|
||||
// Add dependency edges
|
||||
for crate_info in crates {
|
||||
for dep in &crate_info.dependencies {
|
||||
if dep == "dyn-any" || dep == "node-macro" {
|
||||
continue;
|
||||
}
|
||||
out.push_str(&format!(" \"{}\" -> \"{}\";\n", crate_info.name, dep));
|
||||
}
|
||||
}
|
||||
|
||||
out.push_str("}\n");
|
||||
out
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "editor-message-tree"
|
||||
description = "Tool to generate developer documentation for the editor message system structure"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
# Local dependencies
|
||||
editor = { path = "../../editor", package = "graphite-editor" }
|
||||
@@ -0,0 +1,284 @@
|
||||
use editor::messages::message::Message;
|
||||
use editor::utility_types::DebugMessageTree;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
const FRONTEND_MESSAGE_STR: &str = "FrontendMessage";
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let output_dir = std::env::args_os().nth(1).map(PathBuf::from).ok_or("Usage: editor-message-tree <output-directory>")?;
|
||||
std::fs::create_dir_all(&output_dir)?;
|
||||
|
||||
let tree = Message::message_tree();
|
||||
|
||||
// Write the .txt file (plain text tree outline, served as a static download)
|
||||
let static_dir = output_dir.join("../static/volunteer/guide/codebase-overview");
|
||||
std::fs::create_dir_all(&static_dir)?;
|
||||
let mut txt_file = std::fs::File::create(static_dir.join("hierarchical-message-system-tree.txt"))?;
|
||||
write_tree_txt(&tree, &mut txt_file)?;
|
||||
|
||||
// Write the .html file (structured HTML embedded in the website page)
|
||||
let mut html = String::new();
|
||||
write_tree_html(&tree, &mut html);
|
||||
std::fs::write(output_dir.join("hierarchical-message-system-tree.html"), &html)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// =================
|
||||
// PLAIN TEXT OUTPUT
|
||||
// =================
|
||||
|
||||
fn write_tree_txt(tree: &DebugMessageTree, file: &mut std::fs::File) -> std::io::Result<()> {
|
||||
if tree.path().is_empty() {
|
||||
file.write_all(format!("{}\n", tree.name()).as_bytes())?;
|
||||
} else {
|
||||
file.write_all(format!("{} `{}#L{}`\n", tree.name(), tree.path(), tree.line_number()).as_bytes())?;
|
||||
}
|
||||
|
||||
if let Some(variants) = tree.variants() {
|
||||
for (i, variant) in variants.iter().enumerate() {
|
||||
let is_last = i == variants.len() - 1;
|
||||
write_tree_txt_node(variant, "", is_last, file)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_tree_txt_node(tree: &DebugMessageTree, prefix: &str, is_last: bool, file: &mut std::fs::File) -> std::io::Result<()> {
|
||||
let (branch, child_prefix) = if tree.message_handler_data_fields().is_some() || tree.message_handler_fields().is_some() {
|
||||
("├── ", format!("{prefix}│ "))
|
||||
} else if is_last {
|
||||
("└── ", format!("{prefix} "))
|
||||
} else {
|
||||
("├── ", format!("{prefix}│ "))
|
||||
};
|
||||
|
||||
if tree.path().is_empty() {
|
||||
file.write_all(format!("{}{}{}\n", prefix, branch, tree.name()).as_bytes())?;
|
||||
} else {
|
||||
file.write_all(format!("{}{}{} `{}#L{}`\n", prefix, branch, tree.name(), tree.path(), tree.line_number()).as_bytes())?;
|
||||
}
|
||||
|
||||
if let Some(variants) = tree.variants() {
|
||||
let len = variants.len();
|
||||
for (i, variant) in variants.iter().enumerate() {
|
||||
let is_last_child = i == len - 1;
|
||||
write_tree_txt_node(variant, &child_prefix, is_last_child, file)?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(fields) = tree.fields() {
|
||||
let len = fields.len();
|
||||
for (i, field) in fields.iter().enumerate() {
|
||||
let is_last_field = i == len - 1;
|
||||
let branch = if is_last_field { "└── " } else { "├── " };
|
||||
file.write_all(format!("{child_prefix}{branch}{field}\n").as_bytes())?;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(data) = tree.message_handler_fields() {
|
||||
let len = data.fields().len();
|
||||
let (branch, child_prefix) = if tree.message_handler_data_fields().is_some() {
|
||||
("├── ", format!("{prefix}│ "))
|
||||
} else {
|
||||
("└── ", format!("{prefix} "))
|
||||
};
|
||||
|
||||
if data.name().is_empty() && tree.name() != FRONTEND_MESSAGE_STR {
|
||||
panic!("{}'s MessageHandler is missing #[message_handler_data]", tree.name());
|
||||
} else if tree.name() != FRONTEND_MESSAGE_STR {
|
||||
file.write_all(format!("{}{}{} `{}#L{}`\n", prefix, branch, data.name(), data.path(), data.line_number()).as_bytes())?;
|
||||
|
||||
for (i, field) in data.fields().iter().enumerate() {
|
||||
let is_last_field = i == len - 1;
|
||||
let branch = if is_last_field { "└── " } else { "├── " };
|
||||
file.write_all(format!("{}{}{}\n", child_prefix, branch, field.0).as_bytes())?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(data) = tree.message_handler_data_fields() {
|
||||
let len = data.fields().len();
|
||||
if data.path().is_empty() {
|
||||
file.write_all(format!("{}{}{}\n", prefix, "└── ", data.name()).as_bytes())?;
|
||||
} else {
|
||||
file.write_all(format!("{}{}{} `{}#L{}`\n", prefix, "└── ", data.name(), data.path(), data.line_number()).as_bytes())?;
|
||||
}
|
||||
for (i, field) in data.fields().iter().enumerate() {
|
||||
let is_last_field = i == len - 1;
|
||||
let branch = if is_last_field { "└── " } else { "├── " };
|
||||
let field = &field.0;
|
||||
file.write_all(format!("{prefix} {branch}{field}\n").as_bytes())?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// ===========
|
||||
// HTML OUTPUT
|
||||
// ===========
|
||||
|
||||
const GITHUB_BASE: &str = "https://github.com/GraphiteEditor/Graphite/blob/master/";
|
||||
const NAMING_SUFFIXES: &[&str] = &["Message", "MessageHandler", "MessageContext"];
|
||||
|
||||
fn escape_html(s: &str) -> String {
|
||||
s.replace('&', "&").replace('<', "<").replace('>', ">")
|
||||
}
|
||||
|
||||
fn github_link(path: &str, line: usize) -> String {
|
||||
let path = path.replace('\\', "/");
|
||||
let filename = path.rsplit('/').next().unwrap_or(&path);
|
||||
format!(r#"<a href="{GITHUB_BASE}{path}#L{line}" target="_blank">{filename}:{line}</a>"#)
|
||||
}
|
||||
|
||||
fn naming_convention_warning(name: &str) -> &'static str {
|
||||
// Strip generic parameters for the check (e.g. `Foo<Bar>` -> `Foo`)
|
||||
let base_name = name.split('<').next().unwrap_or(name);
|
||||
if NAMING_SUFFIXES.iter().any(|suffix| base_name.ends_with(suffix)) {
|
||||
""
|
||||
} else {
|
||||
r#"<span class="warn">(violates naming convention — should end with 'Message', 'MessageHandler', or 'MessageContext')</span>"#
|
||||
}
|
||||
}
|
||||
|
||||
fn write_tree_html(tree: &DebugMessageTree, out: &mut String) {
|
||||
// Root node
|
||||
let link = if !tree.path().is_empty() { github_link(tree.path(), tree.line_number()) } else { String::new() };
|
||||
let escaped_name = escape_html(tree.name());
|
||||
|
||||
out.push_str("<ul>\n");
|
||||
out.push_str(&format!(r#"<li><span class="tree-node"><span class="subsystem">{escaped_name}</span>{link}</span>"#));
|
||||
|
||||
if let Some(variants) = tree.variants() {
|
||||
out.push_str(r#"<div class="nested">"#);
|
||||
write_tree_html_children(variants, out);
|
||||
out.push_str("</div>");
|
||||
}
|
||||
|
||||
out.push_str("</li>\n</ul>\n");
|
||||
}
|
||||
|
||||
fn write_tree_html_children(variants: &[DebugMessageTree], out: &mut String) {
|
||||
out.push_str("<ul>\n");
|
||||
for variant in variants {
|
||||
write_tree_html_node(variant, out);
|
||||
}
|
||||
out.push_str("</ul>\n");
|
||||
}
|
||||
|
||||
fn write_tree_html_node(tree: &DebugMessageTree, out: &mut String) {
|
||||
let has_link = !tree.path().is_empty();
|
||||
let link = if has_link { github_link(tree.path(), tree.line_number()) } else { String::new() };
|
||||
let escaped_name = escape_html(tree.name());
|
||||
|
||||
enum HtmlChild<'a> {
|
||||
Subtree(&'a DebugMessageTree),
|
||||
Field(String),
|
||||
HandlerFields(String, String, usize, Vec<String>),
|
||||
DataFields(String, String, usize, Vec<String>),
|
||||
}
|
||||
|
||||
// Collect all child entries for this node
|
||||
let mut children: Vec<HtmlChild> = Vec::new();
|
||||
|
||||
if let Some(variants) = tree.variants() {
|
||||
for variant in variants {
|
||||
children.push(HtmlChild::Subtree(variant));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(fields) = tree.fields() {
|
||||
for field in fields {
|
||||
children.push(HtmlChild::Field(field.to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(data) = tree.message_handler_fields()
|
||||
&& (!data.name().is_empty() || tree.name() == FRONTEND_MESSAGE_STR)
|
||||
&& tree.name() != FRONTEND_MESSAGE_STR
|
||||
{
|
||||
children.push(HtmlChild::HandlerFields(
|
||||
data.name().to_string(),
|
||||
data.path().to_string(),
|
||||
data.line_number(),
|
||||
data.fields().iter().map(|f| f.0.clone()).collect(),
|
||||
));
|
||||
}
|
||||
|
||||
if let Some(data) = tree.message_handler_data_fields() {
|
||||
children.push(HtmlChild::DataFields(
|
||||
data.name().to_string(),
|
||||
data.path().to_string(),
|
||||
data.line_number(),
|
||||
data.fields().iter().map(|f| f.0.clone()).collect(),
|
||||
));
|
||||
}
|
||||
|
||||
let has_children = !children.is_empty();
|
||||
let has_deeper_children = children.iter().any(|child| matches!(child, HtmlChild::Subtree(t) if t.variants().is_some() || t.fields().is_some()));
|
||||
|
||||
// Determine role
|
||||
let role = if has_link {
|
||||
"subsystem"
|
||||
} else if has_deeper_children {
|
||||
"submessage"
|
||||
} else {
|
||||
"message"
|
||||
};
|
||||
|
||||
// Naming convention warning (only for linked/subsystem nodes)
|
||||
let warning = if has_link { naming_convention_warning(tree.name()) } else { "" };
|
||||
|
||||
if has_children {
|
||||
out.push_str(&format!(r#"<li><span class="tree-node"><span class="{role}">{escaped_name}</span>{link}{warning}</span>"#));
|
||||
out.push_str(r#"<div class="nested"><ul>"#);
|
||||
out.push('\n');
|
||||
|
||||
for child in &children {
|
||||
match child {
|
||||
HtmlChild::Subtree(subtree) => write_tree_html_node(subtree, out),
|
||||
HtmlChild::Field(field) => write_field_html(field, out),
|
||||
HtmlChild::HandlerFields(name, path, line, fields) => write_handler_or_data_html(name, path, *line, fields, out),
|
||||
HtmlChild::DataFields(name, path, line, fields) => write_handler_or_data_html(name, path, *line, fields, out),
|
||||
}
|
||||
}
|
||||
|
||||
out.push_str("</ul>\n</div></li>\n");
|
||||
} else {
|
||||
out.push_str(&format!(r#"<li><span class="tree-leaf {role}">{escaped_name}</span>{link}{warning}</li>"#));
|
||||
out.push('\n');
|
||||
}
|
||||
}
|
||||
|
||||
fn write_field_html(field: &str, out: &mut String) {
|
||||
if let Some((name, ty)) = field.split_once(':') {
|
||||
let name = escape_html(name.trim());
|
||||
let ty = escape_html(ty.trim());
|
||||
out.push_str(&format!(r#"<li><span class="tree-leaf field">{name}</span>: <span>{ty}</span></li>"#));
|
||||
} else {
|
||||
let escaped = escape_html(field);
|
||||
out.push_str(&format!(r#"<li><span class="tree-leaf message">{escaped}</span></li>"#));
|
||||
}
|
||||
out.push('\n');
|
||||
}
|
||||
|
||||
fn write_handler_or_data_html(name: &str, path: &str, line: usize, fields: &[String], out: &mut String) {
|
||||
let escaped_name = escape_html(name);
|
||||
let link = if !path.is_empty() { github_link(path, line) } else { String::new() };
|
||||
let warning = if !path.is_empty() { naming_convention_warning(name) } else { "" };
|
||||
|
||||
if fields.is_empty() {
|
||||
out.push_str(&format!(r#"<li><span class="tree-leaf subsystem">{escaped_name}</span>{link}{warning}</li>"#));
|
||||
} else {
|
||||
out.push_str(&format!(r#"<li><span class="tree-node"><span class="subsystem">{escaped_name}</span>{link}{warning}</span>"#));
|
||||
out.push_str(r#"<div class="nested"><ul>"#);
|
||||
out.push('\n');
|
||||
for field in fields {
|
||||
write_field_html(field, out);
|
||||
}
|
||||
out.push_str("</ul>\n</div></li>\n");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "node-docs"
|
||||
description = "Tool to generate node documentation for the node catalog on the Graphite website"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
# Local dependencies
|
||||
graphene-std = { workspace = true }
|
||||
interpreted-executor = { workspace = true }
|
||||
graph-craft = { workspace = true, features = ["loading"] }
|
||||
preprocessor = { workspace = true }
|
||||
|
||||
# Workspace dependencies
|
||||
indoc = { workspace = true }
|
||||
convert_case = { workspace = true }
|
||||
@@ -0,0 +1,51 @@
|
||||
mod page_catalog;
|
||||
mod page_category;
|
||||
mod page_node;
|
||||
mod utility;
|
||||
|
||||
use crate::utility::*;
|
||||
use convert_case::{Case, Casing};
|
||||
use std::collections::HashMap;
|
||||
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let output_path = std::env::args_os()
|
||||
.nth(1)
|
||||
.ok_or("Usage: node-docs <output-directory>")?
|
||||
.into_string()
|
||||
.map_err(|_| "Output path is not valid UTF-8")?;
|
||||
|
||||
// TODO: Also obtain document nodes, not only proto nodes
|
||||
let nodes = graphene_std::registry::NODE_METADATA.lock().unwrap();
|
||||
|
||||
// Group nodes by category
|
||||
let mut nodes_by_category: HashMap<_, Vec<_>> = HashMap::new();
|
||||
for (id, metadata) in nodes.iter() {
|
||||
nodes_by_category.entry(metadata.category.to_string()).or_default().push((id, metadata));
|
||||
}
|
||||
|
||||
// Sort the categories
|
||||
let mut categories = nodes_by_category.keys().cloned().collect::<Vec<_>>();
|
||||
categories.sort();
|
||||
|
||||
// Create _index.md for the node catalog page
|
||||
page_catalog::write_catalog_index_page(&output_path, &categories);
|
||||
|
||||
// Create node category pages and individual node pages
|
||||
for (index, category) in categories.iter().map(|c| if !OMIT_HIDDEN && c.is_empty() { "Hidden" } else { c }).filter(|c| !c.is_empty()).enumerate() {
|
||||
// Get nodes in this category
|
||||
let mut nodes = nodes_by_category.remove(if !OMIT_HIDDEN && category == "Hidden" { "" } else { category }).unwrap();
|
||||
nodes.sort_by_key(|(_, metadata)| metadata.display_name.to_string());
|
||||
|
||||
// Create _index.md file for category
|
||||
let category_path_part = sanitize_path(&category.to_case(Case::Kebab));
|
||||
let category_path = format!("{output_path}/{category_path_part}");
|
||||
page_category::write_category_index_page(index, category, &nodes, &category_path);
|
||||
|
||||
// Create individual node pages
|
||||
for (index, (id, metadata)) in nodes.into_iter().enumerate() {
|
||||
page_node::write_node_page(index, id, metadata, &category_path);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
use crate::utility::*;
|
||||
use convert_case::{Case, Casing};
|
||||
use indoc::formatdoc;
|
||||
use std::io::Write;
|
||||
|
||||
pub fn write_catalog_index_page(output_path: &str, categories: &[String]) {
|
||||
if std::path::Path::new(output_path).exists() {
|
||||
std::fs::remove_dir_all(output_path).expect("Failed to remove existing node catalog directory");
|
||||
}
|
||||
std::fs::create_dir_all(output_path).expect("Failed to create node catalog directory");
|
||||
let page_path = format!("{output_path}/_index.md");
|
||||
let mut page = std::fs::File::create(&page_path).expect("Failed to create index file");
|
||||
|
||||
write_frontmatter(&mut page);
|
||||
write_description(&mut page);
|
||||
write_categories_table_header(&mut page);
|
||||
write_categories_table_rows(&mut page, categories);
|
||||
}
|
||||
|
||||
fn write_frontmatter(page: &mut std::fs::File) {
|
||||
let content = formatdoc!(
|
||||
"
|
||||
+++
|
||||
title = \"Node catalog\"
|
||||
template = \"book.html\"
|
||||
page_template = \"book.html\"
|
||||
|
||||
[extra]
|
||||
order = 3
|
||||
css = [\"/page/user-manual/node-catalog.css\"]
|
||||
+++
|
||||
"
|
||||
);
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to index file");
|
||||
}
|
||||
|
||||
fn write_description(page: &mut std::fs::File) {
|
||||
let content = formatdoc!(
|
||||
"
|
||||
|
||||
The node catalog documents all of the nodes available in Graphite's node graph system, organized by category.
|
||||
|
||||
<p><img src=\"https://static.graphite.art/content/learn/node-catalog/node-terminology.avif\" onerror=\"this.onerror = null; this.src = this.src.replace('.avif', '.png')\" alt=\"Terminology diagram covering how the node system operates\" /></p>
|
||||
"
|
||||
);
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to index file");
|
||||
}
|
||||
|
||||
fn write_categories_table_header(page: &mut std::fs::File) {
|
||||
let content = formatdoc!(
|
||||
"
|
||||
|
||||
## Node categories
|
||||
|
||||
| Category | Details |
|
||||
|:-|:-|
|
||||
"
|
||||
);
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to index file");
|
||||
}
|
||||
|
||||
fn write_categories_table_rows(page: &mut std::fs::File, categories: &[String]) {
|
||||
let content = categories
|
||||
.iter()
|
||||
.filter_map(|c| if c.is_empty() { if OMIT_HIDDEN { None } else { Some("Hidden") } } else { Some(c) })
|
||||
.map(|category| {
|
||||
let category_path_part = sanitize_path(&category.to_case(Case::Kebab));
|
||||
let details = category_description(category).replace("\n\n", "</p><p>").replace('\n', "<br />");
|
||||
format!("| [{category}](./{category_path_part}) | <p>{details}</p> |")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to index file");
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
use crate::utility::*;
|
||||
use convert_case::{Case, Casing};
|
||||
use graph_craft::concrete;
|
||||
use graph_craft::proto::NodeMetadata;
|
||||
use graphene_std::core_types;
|
||||
use indoc::formatdoc;
|
||||
use std::collections::HashSet;
|
||||
use std::io::Write;
|
||||
|
||||
pub fn write_category_index_page(index: usize, category: &str, nodes: &[(&core_types::ProtoNodeIdentifier, &NodeMetadata)], category_path: &String) {
|
||||
std::fs::create_dir_all(category_path).expect("Failed to create category directory");
|
||||
let page_path = format!("{category_path}/_index.md");
|
||||
let mut page = std::fs::File::create(&page_path).expect("Failed to create index file");
|
||||
|
||||
write_frontmatter(&mut page, category, index + 1);
|
||||
write_description(&mut page, category);
|
||||
write_nodes_table_header(&mut page);
|
||||
write_nodes_table_rows(&mut page, nodes);
|
||||
}
|
||||
|
||||
fn write_frontmatter(page: &mut std::fs::File, category: &str, order: usize) {
|
||||
let content = formatdoc!(
|
||||
"
|
||||
+++
|
||||
title = \"{category}\"
|
||||
template = \"book.html\"
|
||||
page_template = \"book.html\"
|
||||
|
||||
[extra]
|
||||
order = {order}
|
||||
css = [\"/page/user-manual/node-category.css\"]
|
||||
+++
|
||||
"
|
||||
);
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to index file");
|
||||
}
|
||||
|
||||
fn write_description(page: &mut std::fs::File, category: &str) {
|
||||
let category_description = category_description(category);
|
||||
let content = formatdoc!(
|
||||
"
|
||||
|
||||
{category_description}
|
||||
"
|
||||
);
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to index file");
|
||||
}
|
||||
|
||||
fn write_nodes_table_header(page: &mut std::fs::File) {
|
||||
let content = formatdoc!(
|
||||
"
|
||||
|
||||
## Nodes
|
||||
|
||||
| Node | Details | Possible Types |
|
||||
|:-|:-|:-|
|
||||
"
|
||||
);
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to index file");
|
||||
}
|
||||
|
||||
fn write_nodes_table_rows(page: &mut std::fs::File, nodes: &[(&core_types::ProtoNodeIdentifier, &NodeMetadata)]) {
|
||||
let content = nodes
|
||||
.iter()
|
||||
.filter_map(|&(id, metadata)| {
|
||||
// Path to page
|
||||
let name_url_part = sanitize_path(&metadata.display_name.to_case(Case::Kebab));
|
||||
|
||||
// Name and description
|
||||
let name = metadata.display_name;
|
||||
let description = node_description(metadata);
|
||||
let details = description.split('\n').map(|line| format!("<p>{}</p>", line.trim())).collect::<Vec<_>>().join("");
|
||||
|
||||
// Possible types
|
||||
let node_registry = core_types::registry::NODE_REGISTRY.lock().unwrap();
|
||||
let implementations = node_registry.get(id)?;
|
||||
let valid_primary_inputs_to_outputs = implementations
|
||||
.iter()
|
||||
.map(|entry| {
|
||||
let input = entry
|
||||
.io
|
||||
.inputs
|
||||
.first()
|
||||
.map(|ty| ty.nested_type())
|
||||
.filter(|&ty| ty != &concrete!(()))
|
||||
.map(ToString::to_string)
|
||||
.unwrap_or_default();
|
||||
let output = entry.io.return_value.nested_type().to_string();
|
||||
format!("`{input} → {output}`")
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
let valid_primary_inputs_to_outputs = {
|
||||
// Dedupe while preserving order
|
||||
let mut found = HashSet::new();
|
||||
valid_primary_inputs_to_outputs.into_iter().filter(|s| found.insert(s.clone())).collect::<Vec<_>>()
|
||||
};
|
||||
let possible_types = valid_primary_inputs_to_outputs.join("<br />");
|
||||
|
||||
// Add table row
|
||||
Some(format!("| [{name}]({name_url_part}) | {details} | {possible_types} |"))
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to index file");
|
||||
}
|
||||
@@ -0,0 +1,249 @@
|
||||
use crate::utility::*;
|
||||
use convert_case::{Case, Casing};
|
||||
use graph_craft::concrete;
|
||||
use graph_craft::document::value;
|
||||
use graph_craft::proto::{NodeMetadata, RegistryValueSource};
|
||||
use graphene_std::{ContextDependencies, core_types};
|
||||
use indoc::formatdoc;
|
||||
use std::collections::HashSet;
|
||||
use std::io::Write;
|
||||
|
||||
pub fn write_node_page(index: usize, id: &core_types::ProtoNodeIdentifier, metadata: &NodeMetadata, category_path: &String) {
|
||||
let node_registry = core_types::registry::NODE_REGISTRY.lock().unwrap();
|
||||
let Some(implementations) = node_registry.get(id) else { return };
|
||||
|
||||
// Path to page
|
||||
let name_url_part = sanitize_path(&metadata.display_name.to_case(Case::Kebab));
|
||||
let page_path = format!("{category_path}/{name_url_part}.md");
|
||||
let mut page = std::fs::File::create(&page_path).expect("Failed to create node page file");
|
||||
|
||||
// Context features
|
||||
let context_features = &metadata.context_features;
|
||||
let context_dependencies: ContextDependencies = context_features.as_slice().into();
|
||||
|
||||
// Input types
|
||||
let mut valid_input_types = vec![Vec::new(); metadata.fields.len()];
|
||||
for entry in implementations.iter() {
|
||||
for (i, ty) in entry.io.inputs.iter().enumerate() {
|
||||
valid_input_types[i].push(ty.nested_type().clone());
|
||||
}
|
||||
}
|
||||
for item in valid_input_types.iter_mut() {
|
||||
// Dedupe while preserving order
|
||||
let mut found = HashSet::new();
|
||||
*item = item.clone().into_iter().filter(|s| found.insert(s.clone())).collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
// Primary output types
|
||||
let valid_primary_outputs = implementations.iter().map(|entry| entry.io.return_value.nested_type().clone()).collect::<Vec<_>>();
|
||||
|
||||
// Write sections to the file
|
||||
write_frontmatter(&mut page, metadata, index + 1);
|
||||
write_description(&mut page, metadata);
|
||||
write_interface_header(&mut page);
|
||||
write_context(&mut page, context_dependencies);
|
||||
write_inputs(&mut page, &valid_input_types, metadata);
|
||||
write_outputs(&mut page, &valid_primary_outputs);
|
||||
}
|
||||
|
||||
fn write_frontmatter(page: &mut std::fs::File, metadata: &NodeMetadata, order: usize) {
|
||||
let name = metadata.display_name;
|
||||
|
||||
let content = formatdoc!(
|
||||
"
|
||||
+++
|
||||
title = \"{name}\"
|
||||
|
||||
[extra]
|
||||
order = {order}
|
||||
css = [\"/page/user-manual/node.css\"]
|
||||
+++
|
||||
"
|
||||
);
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to node page file");
|
||||
}
|
||||
|
||||
fn write_description(page: &mut std::fs::File, metadata: &NodeMetadata) {
|
||||
let description = node_description(metadata);
|
||||
|
||||
let content = formatdoc!(
|
||||
"
|
||||
|
||||
{description}
|
||||
"
|
||||
);
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to node page file");
|
||||
}
|
||||
|
||||
fn write_interface_header(page: &mut std::fs::File) {
|
||||
let content = formatdoc!(
|
||||
"
|
||||
|
||||
## Interface
|
||||
"
|
||||
);
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to node page file");
|
||||
}
|
||||
|
||||
fn write_context(page: &mut std::fs::File, context_dependencies: ContextDependencies) {
|
||||
let extract = context_dependencies.extract;
|
||||
let inject = context_dependencies.inject;
|
||||
if !extract.is_empty() || !inject.is_empty() {
|
||||
let mut context_features = "| | |\n|:-|:-|".to_string();
|
||||
if !extract.is_empty() {
|
||||
let names = extract.iter().map(|ty| format!("`{}`", ty.name())).collect::<Vec<_>>().join("<br />");
|
||||
context_features.push_str(&format!("\n| **Reads** | {names} |"));
|
||||
}
|
||||
if !inject.is_empty() {
|
||||
let names = inject.iter().map(|ty| format!("`{}`", ty.name())).collect::<Vec<_>>().join("<br />");
|
||||
context_features.push_str(&format!("\n| **Sets** | {names} |"));
|
||||
}
|
||||
|
||||
let content = formatdoc!(
|
||||
"
|
||||
|
||||
### Context
|
||||
|
||||
{context_features}
|
||||
"
|
||||
);
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to node page file");
|
||||
};
|
||||
}
|
||||
|
||||
fn write_inputs(page: &mut std::fs::File, valid_input_types: &[Vec<core_types::Type>], metadata: &NodeMetadata) {
|
||||
let rows = metadata
|
||||
.fields
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter(|&(index, field)| !field.hidden || index == 0)
|
||||
.map(|(index, field)| {
|
||||
// Parameter
|
||||
let parameter = field.name;
|
||||
|
||||
// Possible types
|
||||
let possible_types_list = valid_input_types.get(index).cloned().unwrap_or_default();
|
||||
if index == 0 && possible_types_list.as_slice() == [concrete!(())] {
|
||||
return "| - | *No Primary Input* | - |".to_string();
|
||||
}
|
||||
let mut possible_types = possible_types_list.iter().map(|ty| format!("`{ty}`")).collect::<Vec<_>>();
|
||||
possible_types.sort();
|
||||
possible_types.dedup();
|
||||
let mut possible_types = possible_types.join("<br />");
|
||||
if possible_types.is_empty() {
|
||||
possible_types = "*Any Type*".to_string();
|
||||
}
|
||||
|
||||
// Details: description
|
||||
let mut details = field
|
||||
.description
|
||||
.trim()
|
||||
.split('\n')
|
||||
.filter(|line| !line.is_empty())
|
||||
.map(|line| format!("<p>{}</p>", line.trim()))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// Details: primary input
|
||||
if index == 0 {
|
||||
details.push("<p>*Primary Input*</p>".to_string());
|
||||
}
|
||||
|
||||
// Details: exposed by default
|
||||
if field.exposed {
|
||||
details.push("<p>*Exposed to the Graph by Default*</p>".to_string());
|
||||
}
|
||||
|
||||
// Details: sourced from scope
|
||||
if let RegistryValueSource::Scope(scope_name) = &field.value_source {
|
||||
details.push(format!("<p>*Sourced From Scope: `{scope_name}`*</p>"));
|
||||
}
|
||||
|
||||
// Details: default value
|
||||
let default_value = match &field.value_source {
|
||||
RegistryValueSource::Default(default_value) => Some(default_value.to_string().replace(" :: ", "::")),
|
||||
_ => field
|
||||
.default_type
|
||||
.as_ref()
|
||||
.or(match possible_types_list.as_slice() {
|
||||
[single] => Some(single),
|
||||
_ => None,
|
||||
})
|
||||
.and_then(|ty| value::TaggedValue::from_type(ty.nested_type()))
|
||||
.map(|ty| ty.to_debug_string()),
|
||||
};
|
||||
if index > 0
|
||||
&& !field.exposed
|
||||
&& let Some(default_value) = default_value
|
||||
{
|
||||
let default_value = default_value.trim_end_matches('.').trim_end_matches(".0"); // Display whole-number floats as integers
|
||||
|
||||
let render_color = |color| format!(r#"<span style="padding-right: 100px; border: 2px solid var(--color-fog); background: {color}"></span>"#);
|
||||
// Compare against the typed default's debug form so the swatch tracks the `Gradient` representation
|
||||
let black_to_white_gradient = value::TaggedValue::Gradient(Default::default()).to_debug_string();
|
||||
let default_value = match default_value {
|
||||
"Color::BLACK" => render_color("black"),
|
||||
gradient if gradient == black_to_white_gradient => render_color("linear-gradient(to right, black, white)"),
|
||||
_ => format!("`{default_value}{}`", field.unit.unwrap_or_default()),
|
||||
};
|
||||
|
||||
details.push(format!("<p>*Default:* {default_value}</p>"));
|
||||
}
|
||||
|
||||
// Construct the table row
|
||||
let details = details.join("");
|
||||
format!("| {parameter} | {details} | {possible_types} |")
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("\n");
|
||||
if !rows.is_empty() {
|
||||
let content = formatdoc!(
|
||||
"
|
||||
|
||||
### Inputs
|
||||
|
||||
| Parameter | Details | Possible Types |
|
||||
|:-|:-|:-|
|
||||
{rows}
|
||||
"
|
||||
);
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to node page file");
|
||||
}
|
||||
}
|
||||
|
||||
fn write_outputs(page: &mut std::fs::File, valid_primary_outputs: &[core_types::Type]) {
|
||||
// Product
|
||||
let product = "Result";
|
||||
|
||||
// Details: description
|
||||
let details = "The value produced by the node operation.";
|
||||
let mut details = format!("<p>{details}</p>");
|
||||
|
||||
// Details: primary output
|
||||
details.push_str("<p>*Primary Output*</p>");
|
||||
|
||||
// Possible types
|
||||
let valid_primary_outputs = valid_primary_outputs.iter().map(|ty| format!("`{ty}`")).collect::<Vec<_>>();
|
||||
let valid_primary_outputs = {
|
||||
// Dedupe while preserving order
|
||||
let mut found = HashSet::new();
|
||||
valid_primary_outputs.into_iter().filter(|s| found.insert(s.clone())).collect::<Vec<_>>()
|
||||
};
|
||||
let valid_primary_outputs = {
|
||||
// Dedupe while preserving order
|
||||
let mut found = HashSet::new();
|
||||
valid_primary_outputs.into_iter().filter(|s| found.insert(s.clone())).collect::<Vec<_>>()
|
||||
};
|
||||
let valid_primary_outputs = valid_primary_outputs.join("<br />");
|
||||
|
||||
let content = formatdoc!(
|
||||
"
|
||||
|
||||
### Outputs
|
||||
|
||||
| Product | Details | Possible Types |
|
||||
|:-|:-|:-|
|
||||
| {product} | {details} | {valid_primary_outputs} |
|
||||
"
|
||||
);
|
||||
page.write_all(content.as_bytes()).expect("Failed to write to node page file");
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
use graph_craft::proto::NodeMetadata;
|
||||
use indoc::indoc;
|
||||
|
||||
pub const OMIT_HIDDEN: bool = true;
|
||||
|
||||
pub fn category_description(category: &str) -> &str {
|
||||
match category {
|
||||
"Animation" => indoc!(
|
||||
"
|
||||
Nodes in this category enable the creation of animated, real-time, and interactive motion graphics involving paramters that change over time.
|
||||
|
||||
These nodes require that playback is activated by pressing the play button above the viewport.
|
||||
"
|
||||
),
|
||||
"Blending" => "Nodes in this category control how overlapping graphical content is composited together, considering blend modes, opacity, and clipping.",
|
||||
"Color" => "Nodes in this category deal with selecting and manipulating colors, gradients, and palettes.",
|
||||
"Debug" => indoc!(
|
||||
"
|
||||
Nodes in this category are temporarily included for debugging purposes by Graphite's developers. They may have rare potential uses for advanced users, but are not intended for general use and will be removed in future releases.
|
||||
"
|
||||
),
|
||||
"General" => "Nodes in this category deal with general data handling, such as merging and flattening graphical elements.",
|
||||
"Context" => "Nodes in this category read data from the evaluation context supplied by downstream nodes, such as the current loop iteration index.",
|
||||
"Repeat" => "Nodes in this category enable the duplication, arrangement, and looped generation of graphical elements.",
|
||||
"Math: Arithmetic" => "Nodes in this category perform common arithmetic operations on numerical values (and where applicable, `vec2` values).",
|
||||
"Math: Logic" => "Nodes in this category perform boolean logic operations such as comparisons, conditionals, logic gates, and switching.",
|
||||
"Math: Numeric" => "Nodes in this category perform discontinuous numeric operations such as rounding, clamping, mapping, and randomization.",
|
||||
"Math: Transform" => "Nodes in this category perform transformations on graphical elements and calculations involving transformation matrices.",
|
||||
"Math: Trig" => "Nodes in this category perform trigonometric operations such as sine, cosine, tangent, and their inverses.",
|
||||
"Math: Vector" => "Nodes in this category perform operations involving `vec2` values (points or arrows in 2D space) such as the dot product, normalization, and distance calculations.",
|
||||
"Raster: Adjustment" => "Nodes in this category perform per-pixel color adjustments on raster graphics, such as brightness and contrast modifications.",
|
||||
"Raster: Channels" => "Nodes in this category enable channel-specific manipulation of the RGB and alpha channels of raster graphics.",
|
||||
"Raster: Filter" => "Nodes in this category apply filtering effects to raster graphics such as blurs and sharpening.",
|
||||
"Raster: Pattern" => "Nodes in this category generate procedural raster patterns, fractals, textures, and noise.",
|
||||
"Raster" => "Nodes in this category deal with fundamental raster image operations.",
|
||||
"Text" => "Nodes in this category support the manipulation, formatting, and rendering of text strings.",
|
||||
"Text: Regex" => "Nodes in this category perform string operations involving regular expressions, such as pattern matching and replacement.",
|
||||
"Text: JSON" => "Nodes in this category perform string operations involving JSON data, such as parsing and stringifying.",
|
||||
"Value" => "Nodes in this category supply data values of common types such as numbers, colors, booleans, and strings.",
|
||||
"Vector: Measure" => "Nodes in this category perform measurements and analysis on vector graphics, such as length/area calculations, path traversal, and hit testing.",
|
||||
"Vector: Modifier" => "Nodes in this category modify the geometry of vector graphics, such as boolean operations, smoothing, and morphing.",
|
||||
"Vector: Shape" => "Nodes in this category generate parametrically-described primitive vector shapes such as rectangles, grids, stars, and spirals.",
|
||||
"Vector: Style" => "Nodes in this category apply fill and stroke styles to alter the appearance of vector graphics.",
|
||||
"Vector" => "Nodes in this category deal with fundamental vector graphics data handling and operations.",
|
||||
"Web Request" => "Nodes in this category facilitate fetching and handling resources from HTTP endpoints and sending webhook requests to external services.",
|
||||
_ => {
|
||||
eprintln!("Category '{category}' is missing a description");
|
||||
|
||||
"*Category description coming soon.*"
|
||||
}
|
||||
}.trim()
|
||||
}
|
||||
|
||||
pub fn node_description(metadata: &NodeMetadata) -> &str {
|
||||
let mut description = metadata.description.trim();
|
||||
if description.is_empty() {
|
||||
description = "*Node description coming soon.*";
|
||||
}
|
||||
description
|
||||
}
|
||||
|
||||
pub fn sanitize_path(s: &str) -> String {
|
||||
// Replace disallowed characters with a dash
|
||||
let allowed_characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~[]@!$&'()*+,;=";
|
||||
let filtered = s.chars().map(|c| if allowed_characters.contains(c) { c } else { '-' }).collect::<String>();
|
||||
|
||||
// Fix letter-number type names
|
||||
let mut filtered = format!("-{filtered}-");
|
||||
filtered = filtered.replace("-vec-2-", "-vec2-");
|
||||
filtered = filtered.replace("-f-32-", "-f32-");
|
||||
filtered = filtered.replace("-f-64-", "-f64-");
|
||||
filtered = filtered.replace("-u-32-", "-u32-");
|
||||
filtered = filtered.replace("-u-64-", "-u64-");
|
||||
filtered = filtered.replace("-i-32-", "-i32-");
|
||||
filtered = filtered.replace("-i-64-", "-i64-");
|
||||
|
||||
// Remove consecutive dashes
|
||||
while filtered.contains("--") {
|
||||
filtered = filtered.replace("--", "-");
|
||||
}
|
||||
|
||||
// Trim leading and trailing dashes
|
||||
filtered.trim_matches('-').to_string()
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
*.hash
|
||||
@@ -0,0 +1,20 @@
|
||||
[package]
|
||||
name = "third-party-licenses"
|
||||
edition.workspace = true
|
||||
version.workspace = true
|
||||
license.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[features]
|
||||
desktop = ["dep:cef-dll-sys", "dep:scraper"]
|
||||
|
||||
[dependencies]
|
||||
# Workspace dependencies
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
lzma-rust2 = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
|
||||
# Optional workspace dependencies
|
||||
cef-dll-sys = { workspace = true, optional = true }
|
||||
scraper = { workspace = true, optional = true }
|
||||
@@ -0,0 +1,15 @@
|
||||
fn main() {
|
||||
println!("cargo:rerun-if-changed=src");
|
||||
|
||||
println!("cargo:rerun-if-env-changed=DEP_CEF_DLL_WRAPPER_CEF_DIR");
|
||||
if let Ok(cef_dir) = std::env::var("DEP_CEF_DLL_WRAPPER_CEF_DIR") {
|
||||
println!("cargo:rustc-env=CEF_PATH={cef_dir}");
|
||||
}
|
||||
|
||||
let manifest_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
if std::env::var("CARGO_FEATURE_DESKTOP").is_ok() {
|
||||
let _ = std::fs::remove_file(manifest_dir.join("desktop.hash"));
|
||||
} else {
|
||||
let _ = std::fs::remove_file(manifest_dir.join("web.hash"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
use crate::{Error, LicenceSource, LicenseEntry, Package};
|
||||
use serde::Deserialize;
|
||||
use std::fs;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
pub struct CargoLicenseSource {}
|
||||
|
||||
impl CargoLicenseSource {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl LicenceSource for CargoLicenseSource {
|
||||
fn licenses(&self) -> Result<Vec<LicenseEntry>, Error> {
|
||||
Ok(parse(run()?))
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for CargoLicenseSource {
|
||||
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||
let lock_path = PathBuf::from(env!("CARGO_WORKSPACE_DIR")).join("Cargo.lock");
|
||||
fs::read_to_string(lock_path).unwrap().hash(state)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Output {
|
||||
licenses: Vec<License>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct License {
|
||||
name: Option<String>,
|
||||
text: Option<String>,
|
||||
used_by: Vec<UsedBy>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct UsedBy {
|
||||
#[serde(rename = "crate")]
|
||||
crate_info: Crate,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Crate {
|
||||
name: Option<String>,
|
||||
version: Option<String>,
|
||||
authors: Option<Vec<String>>,
|
||||
repository: Option<String>,
|
||||
}
|
||||
|
||||
fn parse(parsed: Output) -> Vec<LicenseEntry> {
|
||||
parsed
|
||||
.licenses
|
||||
.into_iter()
|
||||
.map(|license| {
|
||||
let packages = license
|
||||
.used_by
|
||||
.into_iter()
|
||||
.map(|used| {
|
||||
let name = used.crate_info.name.as_deref().unwrap_or_default();
|
||||
let version = used.crate_info.version.as_deref().unwrap_or_default();
|
||||
let display_name = if version.is_empty() { name.to_string() } else { format!("{name}@{version}") };
|
||||
|
||||
let repository = used.crate_info.repository.filter(|s| !s.is_empty());
|
||||
|
||||
Package {
|
||||
name: display_name,
|
||||
authors: used.crate_info.authors.unwrap_or_default(),
|
||||
url: repository,
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
LicenseEntry {
|
||||
name: license.name,
|
||||
text: license.text.as_deref().unwrap_or_default().to_string(),
|
||||
packages,
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn run() -> Result<Output, Error> {
|
||||
let output = Command::new("cargo")
|
||||
.args(["about", "generate", "--format", "json", "--locked"])
|
||||
.current_dir(env!("CARGO_WORKSPACE_DIR"))
|
||||
.output()
|
||||
.map_err(|e| Error::Io(e, "Failed to run cargo about generate".into()))?;
|
||||
|
||||
if !output.status.success() {
|
||||
return Err(Error::Command(format!("cargo about generate failed:\n{}", String::from_utf8_lossy(&output.stderr))));
|
||||
}
|
||||
|
||||
let stdout = String::from_utf8(output.stdout).map_err(|e| Error::Utf8(e, "cargo about generate returned invalid UTF-8".into()))?;
|
||||
|
||||
serde_json::from_str(&stdout).map_err(|e| Error::Json(e, "Failed to parse cargo about generate JSON".into()))
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
use lzma_rust2::XzReader;
|
||||
use scraper::{Html, Selector};
|
||||
use std::fs;
|
||||
use std::hash::Hash;
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{Error, LicenceSource, LicenseEntry, Package};
|
||||
|
||||
pub struct CefLicenseSource;
|
||||
|
||||
impl CefLicenseSource {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl LicenceSource for CefLicenseSource {
|
||||
fn licenses(&self) -> Result<Vec<LicenseEntry>, Error> {
|
||||
let html = read()?;
|
||||
Ok(parse(&html))
|
||||
}
|
||||
}
|
||||
|
||||
impl Hash for CefLicenseSource {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
read().unwrap().hash(state)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse(html: &str) -> Vec<LicenseEntry> {
|
||||
let document = Html::parse_document(html);
|
||||
|
||||
let product_sel = Selector::parse("div.product").unwrap();
|
||||
let title_sel = Selector::parse("span.title").unwrap();
|
||||
let homepage_sel = Selector::parse("span.homepage a").unwrap();
|
||||
let license_sel = Selector::parse("div.license pre").unwrap();
|
||||
|
||||
document
|
||||
.select(&product_sel)
|
||||
.filter_map(|product| {
|
||||
let name: String = product.select(&title_sel).next().map(|el| el.text().collect()).unwrap_or_default();
|
||||
|
||||
if name.is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let homepage = product.select(&homepage_sel).next().and_then(|el| el.value().attr("href").map(String::from));
|
||||
|
||||
let license_text: String = product.select(&license_sel).next().map(|el| el.text().collect::<String>()).unwrap_or_default().trim().to_string();
|
||||
|
||||
let pkg = Package {
|
||||
name,
|
||||
url: homepage,
|
||||
authors: Vec::new(),
|
||||
};
|
||||
|
||||
Some(LicenseEntry {
|
||||
name: None,
|
||||
text: license_text,
|
||||
packages: vec![pkg],
|
||||
})
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn read() -> Result<String, Error> {
|
||||
let cef_path = PathBuf::from(env!("CEF_PATH"));
|
||||
let cef_credits = std::fs::read_dir(&cef_path)
|
||||
.map_err(|e| Error::Io(e, format!("Failed to read CEF_PATH directory {}", cef_path.display())))?
|
||||
.filter_map(|entry| entry.ok())
|
||||
.find(|entry| {
|
||||
let name = entry.file_name();
|
||||
name.eq_ignore_ascii_case("credits.html") || name.eq_ignore_ascii_case("credits.html.xz")
|
||||
})
|
||||
.map(|entry| entry.path())
|
||||
.ok_or_else(|| Error::CefCreditsNotFound(cef_path.clone()))?;
|
||||
|
||||
let decompress_xz = cef_credits.extension().map(|ext| ext.eq_ignore_ascii_case("xz")).unwrap_or(false);
|
||||
|
||||
if decompress_xz {
|
||||
let file = fs::File::open(&cef_credits).map_err(|e| Error::Io(e, format!("Failed to open CEF credits file {}", cef_credits.display())))?;
|
||||
let mut reader = XzReader::new(file, false);
|
||||
let mut html = String::new();
|
||||
reader
|
||||
.read_to_string(&mut html)
|
||||
.map_err(|e| Error::Io(e, format!("Failed to decompress CEF credits file {}", cef_credits.display())))?;
|
||||
Ok(html)
|
||||
} else {
|
||||
fs::read_to_string(&cef_credits).map_err(|e| Error::Io(e, format!("Failed to read CEF credits file {}", cef_credits.display())))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,240 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::hash::{DefaultHasher, Hash, Hasher};
|
||||
use std::path::PathBuf;
|
||||
use std::process::ExitCode;
|
||||
|
||||
mod cargo;
|
||||
#[cfg(feature = "desktop")]
|
||||
mod cef;
|
||||
mod npm;
|
||||
|
||||
use crate::cargo::CargoLicenseSource;
|
||||
#[cfg(feature = "desktop")]
|
||||
use crate::cef::CefLicenseSource;
|
||||
use crate::npm::NpmLicenseSource;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error("{1}: {0}")]
|
||||
Io(#[source] std::io::Error, String),
|
||||
|
||||
#[error("{1}: {0}")]
|
||||
Json(#[source] serde_json::Error, String),
|
||||
|
||||
#[error("{1}: {0}")]
|
||||
Utf8(#[source] std::string::FromUtf8Error, String),
|
||||
|
||||
#[error("{0}")]
|
||||
Command(String),
|
||||
|
||||
#[cfg(feature = "desktop")]
|
||||
#[error("Could not find CREDITS.html or CREDITS.html.xz in {0}")]
|
||||
CefCreditsNotFound(PathBuf),
|
||||
}
|
||||
|
||||
pub trait LicenceSource: std::hash::Hash {
|
||||
fn licenses(&self) -> Result<Vec<LicenseEntry>, Error>;
|
||||
}
|
||||
|
||||
pub struct LicenseEntry {
|
||||
name: Option<String>,
|
||||
text: String,
|
||||
packages: Vec<Package>,
|
||||
}
|
||||
|
||||
pub struct Package {
|
||||
name: String,
|
||||
authors: Vec<String>,
|
||||
url: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Hash)]
|
||||
struct Run<'a> {
|
||||
output: &'a Vec<u8>,
|
||||
cargo: &'a CargoLicenseSource,
|
||||
npm: &'a NpmLicenseSource,
|
||||
#[cfg(feature = "desktop")]
|
||||
cef: &'a CefLicenseSource,
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
if let Err(e) = run() {
|
||||
eprintln!("Error: {e}");
|
||||
return ExitCode::FAILURE;
|
||||
}
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
|
||||
fn run() -> Result<(), Error> {
|
||||
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
|
||||
let workspace_dir = PathBuf::from(env!("CARGO_WORKSPACE_DIR"));
|
||||
|
||||
#[cfg(feature = "desktop")]
|
||||
let output_path = workspace_dir.join("desktop/third-party-licenses.txt.xz");
|
||||
#[cfg(not(feature = "desktop"))]
|
||||
let output_path = workspace_dir.join("frontend/third-party-licenses.txt");
|
||||
|
||||
#[cfg(feature = "desktop")]
|
||||
let current_hash_path = manifest_dir.join("desktop.hash");
|
||||
#[cfg(not(feature = "desktop"))]
|
||||
let current_hash_path = manifest_dir.join("web.hash");
|
||||
|
||||
let cargo_source = CargoLicenseSource::new();
|
||||
let npm_source = NpmLicenseSource::new(workspace_dir.join("frontend"));
|
||||
#[cfg(feature = "desktop")]
|
||||
let cef_source = CefLicenseSource::new();
|
||||
|
||||
let mut run = Run {
|
||||
cargo: &cargo_source,
|
||||
npm: &npm_source,
|
||||
#[cfg(feature = "desktop")]
|
||||
cef: &cef_source,
|
||||
output: &fs::read(&output_path).unwrap_or_default(),
|
||||
};
|
||||
|
||||
let mut hasher = DefaultHasher::new();
|
||||
run.hash(&mut hasher);
|
||||
let current_hash = format!("{:016x}", hasher.finish());
|
||||
|
||||
if current_hash == fs::read_to_string(¤t_hash_path).unwrap_or_default() {
|
||||
return Ok(());
|
||||
}
|
||||
eprintln!("Changes in licenses detected, generating new license file...");
|
||||
|
||||
let licenses = merge_filter_dedup_and_sort(vec![
|
||||
cargo_source.licenses()?,
|
||||
npm_source.licenses()?,
|
||||
#[cfg(feature = "desktop")]
|
||||
cef_source.licenses()?,
|
||||
]);
|
||||
let formatted = format_credits(&licenses);
|
||||
|
||||
#[cfg(feature = "desktop")]
|
||||
let output = compress(&formatted)?;
|
||||
#[cfg(not(feature = "desktop"))]
|
||||
let output = formatted.as_bytes().to_vec();
|
||||
if let Some(parent) = output_path.parent() {
|
||||
fs::create_dir_all(parent).map_err(|e| Error::Io(e, format!("Failed to create directory {}", parent.display())))?;
|
||||
}
|
||||
fs::write(&output_path, &output).map_err(|e| Error::Io(e, format!("Failed to write {}", output_path.display())))?;
|
||||
run.output = &output;
|
||||
|
||||
let hash = {
|
||||
let mut hasher = DefaultHasher::new();
|
||||
run.hash(&mut hasher);
|
||||
format!("{:016x}", hasher.finish())
|
||||
};
|
||||
|
||||
fs::write(¤t_hash_path, hash).map_err(|e| Error::Io(e, format!("Failed to write hash file {}", current_hash_path.display())))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn format_credits(licenses: &Vec<LicenseEntry>) -> String {
|
||||
let mut out = String::new();
|
||||
|
||||
out.push_str("▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐\n");
|
||||
out.push_str("▐▐ ▐▐\n");
|
||||
out.push_str("▐▐ GRAPHITE THIRD-PARTY SOFTWARE LICENSE NOTICES ▐▐\n");
|
||||
out.push_str("▐▐ ▐▐\n");
|
||||
out.push_str("▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐▐\n");
|
||||
|
||||
for license in licenses {
|
||||
let package_lines: Vec<String> = license
|
||||
.packages
|
||||
.iter()
|
||||
.map(|pkg| match &pkg {
|
||||
Package { name, authors, url: Some(url) } if !authors.is_empty() => format!("{} - [{}] - {}", name, authors.join(", "), url),
|
||||
Package { name, authors: _, url: Some(url) } => format!("{} - {}", name, url),
|
||||
Package { name, authors, url: None } if !authors.is_empty() => format!("{} - [{}]", name, authors.join(", ")),
|
||||
_ => pkg.name.clone(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
let multi = package_lines.len() > 1;
|
||||
|
||||
let header = format!(
|
||||
"The package{} listed here {} licensed under the terms of the {} printed beneath",
|
||||
if multi { "s" } else { "" },
|
||||
if multi { "are" } else { "is" },
|
||||
if let Some(license) = license.name.as_ref() { license.to_string() } else { "license".to_string() }
|
||||
);
|
||||
|
||||
let max_len = std::iter::once(header.len()).chain(package_lines.iter().map(|l| l.chars().count())).max().unwrap_or(0);
|
||||
|
||||
let padded_packages: Vec<String> = package_lines
|
||||
.iter()
|
||||
.map(|line| {
|
||||
let pad = max_len - line.chars().count();
|
||||
format!("│ {}{} │", line, " ".repeat(pad))
|
||||
})
|
||||
.collect();
|
||||
|
||||
out.push_str(&format!("\n {}\n", "_".repeat(max_len + 2)));
|
||||
out.push_str(&format!("│ {} │\n", " ".repeat(max_len)));
|
||||
out.push_str(&format!("│ {}{} │\n", header, " ".repeat(max_len - header.len())));
|
||||
out.push_str(&format!("│{}│\n", "_".repeat(max_len + 2)));
|
||||
out.push_str(&padded_packages.join("\n"));
|
||||
out.push('\n');
|
||||
out.push_str(&format!(" {}", "\u{203e}".repeat(max_len + 2)));
|
||||
for line in license.text.lines() {
|
||||
if line.is_empty() {
|
||||
out.push('\n');
|
||||
continue;
|
||||
}
|
||||
out.push('\n');
|
||||
out.push_str(" ");
|
||||
out.push_str(line);
|
||||
}
|
||||
out.truncate(out.trim_end().len());
|
||||
out.push('\n');
|
||||
}
|
||||
|
||||
out
|
||||
}
|
||||
|
||||
fn merge_filter_dedup_and_sort(sources: Vec<Vec<LicenseEntry>>) -> Vec<LicenseEntry> {
|
||||
let mut all = Vec::new();
|
||||
for source in sources {
|
||||
all.extend(source);
|
||||
}
|
||||
filter(&mut all);
|
||||
let mut all = dedup_by_licence_text(all);
|
||||
all.sort_by(|a, b| b.packages.len().cmp(&a.packages.len()).then(a.text.len().cmp(&b.text.len())));
|
||||
all
|
||||
}
|
||||
|
||||
fn filter(licenses: &mut Vec<LicenseEntry>) {
|
||||
licenses.iter_mut().for_each(|l| {
|
||||
l.packages.retain(|p| !(p.authors.len() == 1 && p.authors[0].contains("contact@graphite.art")));
|
||||
});
|
||||
licenses.retain(|l| !l.packages.is_empty());
|
||||
}
|
||||
|
||||
fn dedup_by_licence_text(vec: Vec<LicenseEntry>) -> Vec<LicenseEntry> {
|
||||
let mut map: HashMap<String, LicenseEntry> = HashMap::new();
|
||||
|
||||
for entry in vec {
|
||||
match map.entry(entry.text.clone()) {
|
||||
std::collections::hash_map::Entry::Occupied(mut e) => {
|
||||
e.get_mut().packages.extend(entry.packages);
|
||||
}
|
||||
std::collections::hash_map::Entry::Vacant(e) => {
|
||||
e.insert(entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
map.into_values().collect()
|
||||
}
|
||||
|
||||
#[cfg(feature = "desktop")]
|
||||
fn compress(content: &str) -> Result<Vec<u8>, Error> {
|
||||
use std::io::Write;
|
||||
let mut buf = Vec::new();
|
||||
let mut writer = lzma_rust2::XzWriter::new(&mut buf, lzma_rust2::XzOptions::default()).map_err(|e| Error::Io(e, "Failed to create XZ writer".into()))?;
|
||||
writer.write_all(content.as_bytes()).map_err(|e| Error::Io(e, "Failed to write compressed credits".into()))?;
|
||||
writer.finish().map_err(|e| Error::Io(e, "Failed to finish XZ compression".into()))?;
|
||||
Ok(buf)
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use std::process::Command;
|
||||
|
||||
use crate::{Error, LicenceSource, LicenseEntry, Package};
|
||||
|
||||
pub struct NpmLicenseSource {
|
||||
dir: PathBuf,
|
||||
}
|
||||
impl NpmLicenseSource {
|
||||
pub fn new(dir: PathBuf) -> Self {
|
||||
Self { dir }
|
||||
}
|
||||
}
|
||||
|
||||
impl LicenceSource for NpmLicenseSource {
|
||||
fn licenses(&self) -> Result<Vec<LicenseEntry>, Error> {
|
||||
Ok(parse(run(&self.dir)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl std::hash::Hash for NpmLicenseSource {
|
||||
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
|
||||
let lock_path = self.dir.join("package-lock.json");
|
||||
fs::read_to_string(lock_path).unwrap().hash(state)
|
||||
}
|
||||
}
|
||||
|
||||
type Output = HashMap<String, NpmEntry>;
|
||||
|
||||
#[derive(serde::Deserialize)]
|
||||
struct NpmEntry {
|
||||
licenses: Option<String>,
|
||||
repository: Option<String>,
|
||||
#[serde(rename = "licenseFile")]
|
||||
license_file: Option<String>,
|
||||
publisher: Option<String>,
|
||||
email: Option<String>,
|
||||
}
|
||||
|
||||
fn parse(parsed: Output) -> Vec<LicenseEntry> {
|
||||
parsed
|
||||
.iter()
|
||||
.map(|(name, entry)| {
|
||||
let publisher_info = entry.publisher.as_ref().map(|p| {
|
||||
let email_part = entry.email.as_ref().map(|e| format!(" <{}>", e)).unwrap_or_default();
|
||||
format!("{}{}", p, email_part)
|
||||
});
|
||||
|
||||
let pkg = Package {
|
||||
name: name.to_string(),
|
||||
url: entry.repository.clone(),
|
||||
authors: publisher_info.into_iter().collect(),
|
||||
};
|
||||
|
||||
let license_text = entry.license_file.as_ref().and_then(|p| fs::read_to_string(p).ok()).map(|s| s.to_string()).unwrap_or_default();
|
||||
|
||||
LicenseEntry {
|
||||
name: entry.licenses.clone(),
|
||||
text: license_text,
|
||||
packages: vec![pkg],
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn run(dir: &std::path::Path) -> Result<Output, Error> {
|
||||
#[cfg(not(target_os = "windows"))]
|
||||
let mut cmd = Command::new("npx");
|
||||
#[cfg(target_os = "windows")]
|
||||
let mut cmd = Command::new("npx.cmd");
|
||||
cmd.args(["license-checker-rseidelsohn", "--production", "--json"]);
|
||||
cmd.current_dir(dir);
|
||||
|
||||
let output = cmd.output().map_err(|e| Error::Io(e, "Failed to run npx license-checker-rseidelsohn".into()))?;
|
||||
|
||||
if !output.status.success() {
|
||||
return Err(Error::Command(format!("npx license-checker-rseidelsohn failed:\n{}", String::from_utf8_lossy(&output.stderr))));
|
||||
}
|
||||
|
||||
let json_str = String::from_utf8(output.stdout).map_err(|e| Error::Utf8(e, "Invalid UTF-8 from license-checker".into()))?;
|
||||
|
||||
serde_json::from_str(&json_str).map_err(|e| Error::Json(e, "Failed to parse license-checker JSON".into()))
|
||||
}
|
||||
Reference in New Issue
Block a user