1182 lines · 41.9 KB · Rust
RawDownload
use std::{    collections::{HashMap, HashSet},    env, fs,    path::{Path, PathBuf},    process::Command,}; use anyhow::{Context, Result, bail};use clap::Parser;use ranim_cli::cli::Cli;use serde::{Deserialize, Serialize};use sha1::{Digest, Sha1};use toml::Table;use walkdir::WalkDir; mod shadow; /// File extensions synced from render outputs into the website.const WEBSITE_OUTPUT_EXTENSIONS: &[&str] = &["png", "jpg", "mp4", "webm", /*"mov",*/ "gif"];/// File extensions that count as preview images in `website/data/*.toml`.const PREVIEW_EXTENSIONS: &[&str] = &["png", "jpg"]; fn copy_file(source: &Path, target_dir: &Path) -> Result<String> {    let file_name = source        .file_name()        .and_then(|name| name.to_str())        .ok_or(anyhow::anyhow!("无法获取文件名"))?        .to_string();     let target_path = target_dir.join(&file_name);    std::fs::copy(source, &target_path)?;     Ok(file_name)} /// The package and wasm names of the shared wasm scene bundle.const EXAMPLES_BUNDLE_PACKAGE: &str = "ranim-examples";/// Root directory of generated Zola example pages, relative to the website root.const WEBSITE_EXAMPLES_CONTENT_DIR: &str = "content/examples";/// Top-level title used when `content/examples/_index.md` does not exist yet.const WEBSITE_EXAMPLES_TITLE: &str = "例子";const EXAMPLES_BUNDLE_CRATE: &str = "ranim_examples";const EXAMPLES_BUNDLE_WEB_DIR: &str = "ranim-examples"; fn clean_examples_wasm(root_dir: &Path) -> Result<()> {    let output_dir = root_dir        .join("website")        .join("static")        .join(EXAMPLES_BUNDLE_WEB_DIR)        .join("pkg");    if std::fs::exists(&output_dir)? {        std::fs::remove_dir_all(&output_dir)            .with_context(|| format!("failed to clean {}", output_dir.display()))?;    }    Ok(())} /// Build the shared wasm bundle containing every example scene.////// The bundle is built once with the full feature set (`preview`, `anims`,/// `items` and `typst`) and exposed to the website as/// `website/static/ranim-examples/pkg/ranim_examples.js`.pub fn build_examples_wasm(root_dir: impl AsRef<Path>) -> Result<()> {    let root_dir = root_dir.as_ref();    let website_root = root_dir.join("website");    let output_dir = website_root.join("static").join(EXAMPLES_BUNDLE_WEB_DIR);    std::fs::create_dir_all(&output_dir)?;     clean_examples_wasm(root_dir)?;     println!("[1/3] compiling {EXAMPLES_BUNDLE_PACKAGE} for wasm32-unknown-unknown");    let status = Command::new("cargo")        .current_dir(root_dir)        .args([            "build",            "-p",            EXAMPLES_BUNDLE_PACKAGE,            "--target",            "wasm32-unknown-unknown",            "--release",        ])        .stdout(std::process::Stdio::null())        .status()        .context("failed to run cargo build for the wasm example bundle")?;    if !status.success() {        bail!("failed to build the wasm example bundle");    }    println!("[2/3] running wasm-bindgen");    let wasm_path = root_dir        .join("target")        .join("wasm32-unknown-unknown")        .join("release")        .join(format!("{EXAMPLES_BUNDLE_CRATE}.wasm"));    let pkg_dir = output_dir.join("pkg");    let status = Command::new("wasm-bindgen")        .current_dir(root_dir)        .args([            "--out-dir",            pkg_dir.as_os_str().to_str().unwrap(),            "--target",            "web",            wasm_path.as_os_str().to_str().unwrap(),        ])        .stdout(std::process::Stdio::null())        .status()        .context("failed to run wasm-bindgen; is it installed?")?;    if !status.success() {        bail!("wasm-bindgen failed for the wasm example bundle");    }     println!("[3/3] optimizing wasm with wasm-opt");    let bg_wasm = pkg_dir.join(format!("{EXAMPLES_BUNDLE_CRATE}_bg.wasm"));    optimize_wasm(&bg_wasm).context("failed to optimize the wasm example bundle")?;     clean_legacy_example_wasm_packages(root_dir)?;    Ok(())} /// Remove per-example wasm packages left over from website builds that/// predate the shared `ranim-examples` bundle.fn clean_legacy_example_wasm_packages(root_dir: &Path) -> Result<()> {    let examples_root = root_dir.join("website").join("static").join("examples");    if !std::fs::exists(&examples_root)? {        return Ok(());    }     for entry in std::fs::read_dir(&examples_root)? {        let entry = entry?;        if !entry.file_type()?.is_dir() {            continue;        }        let pkg_dir = entry.path().join("pkg");        if std::fs::exists(&pkg_dir)? {            std::fs::remove_dir_all(&pkg_dir)                .with_context(|| format!("failed to clean {}", pkg_dir.display()))?;        }    }    Ok(())} /// Optimize a wasm-bindgen output module for website delivery.////// `wasm-bindgen` may rewrite the module it receives, so this must run after/// bindgen has produced the final `*_bg.wasm` file in the package directory.fn optimize_wasm(wasm_path: &Path) -> Result<()> {    let optimized_path = wasm_path.with_extension("wasm-opt.wasm");    if optimized_path.exists() {        std::fs::remove_file(&optimized_path)            .with_context(|| format!("failed to remove {}", optimized_path.display()))?;    }     let status = Command::new("wasm-opt")        .args(["-Oz", "--output"])        .arg(&optimized_path)        .arg(wasm_path)        .stdout(std::process::Stdio::null())        .status()        .context(            "failed to run wasm-opt; install Binaryen and ensure `wasm-opt` is available on PATH",        )?;    if !status.success() {        bail!("wasm-opt failed for {}", wasm_path.display());    }     std::fs::copy(&optimized_path, wasm_path).with_context(|| {        format!(            "failed to replace {} with wasm-opt output",            wasm_path.display()        )    })?;    std::fs::remove_file(&optimized_path)        .with_context(|| format!("failed to remove {}", optimized_path.display()))?;    Ok(())} /// What happened when [`Example::run`] was invoked.#[derive(Debug, Clone, Copy, PartialEq, Eq)]pub enum RunStatus {    /// The example was rendered and its website outputs were refreshed.    Rendered,    /// `--lazy-run` was enabled and the existing outputs were already current.    UpToDate,} /// Removed stale website artifacts, split by artifact kind.#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]pub struct CleanStats {    /// Removed generated Zola example pages.    pub pages: usize,    /// Removed generated `website/data/*.toml` files.    pub data: usize,    /// Removed generated `website/static/examples/*` directories.    pub outputs: usize,} impl CleanStats {    pub fn is_empty(&self) -> bool {        self.pages == 0 && self.data == 0 && self.outputs == 0    }} #[derive(Serialize, Deserialize, Clone, Debug)]pub struct Example {    pub path: PathBuf,    /// The name of the example, corresponding to the string passed to `--example`    pub name: String,    /// Path of the Zola section that owns this example, relative to    /// `website/content/examples/`. Empty means the examples root section.    pub group: PathBuf,    pub code: String,    pub hash: String,    pub required_features: Vec<String>,     pub meta: ExampleMeta,} impl Example {    pub fn clean_output(&self, root_dir: impl AsRef<Path>) -> Result<()> {        let root_dir = root_dir.as_ref();         let output_dir = root_dir            .join("website")            .join("static")            .join("examples")            .join(&self.name);        if std::fs::exists(&output_dir)? {            std::fs::remove_dir_all(&output_dir)                .with_context(|| format!("failed to clean {}", output_dir.display()))?;        }        std::fs::create_dir_all(&output_dir)?;        Ok(())    }     /// Render the scene and refresh `website/static/examples/<example-name>/`,    /// `website/data/<example-name>.toml`, and the generated Zola page under    /// `website/content/examples/<group>/<example-name>.md`.    ///    /// The render runs **before** any existing website output is cleaned, so a    /// failed render leaves the previous outputs untouched.    pub fn run(&self, root_dir: impl AsRef<Path>, lazy: bool) -> Result<RunStatus> {        #[derive(Serialize, Deserialize)]        struct OutputData {            name: String,            code: String,            hash: String,             preview_imgs: Vec<String>,            output_files: Vec<OutputFile>,            wasm: bool,        }         let root_dir = root_dir.as_ref();        let website_root = root_dir.join("website");        let output_dir = website_root            .join("static")            .join("examples")            .join(&self.name);        std::fs::create_dir_all(&output_dir)?;        let data_dir = website_root.join("data");         let data_path = data_dir.join(format!("{}.toml", self.name));        // Missing or outdated data is not an error — it just means "regenerate".        let up_to_date = std::fs::read_to_string(&data_path)            .ok()            .and_then(|data| toml::from_str::<OutputData>(&data).ok())            .is_some_and(|data| data.hash == self.hash);        if lazy && up_to_date {            // The data is current, but the generated page may still be missing            // after a hierarchy change or an interrupted previous run.            if !self.meta.hide {                self.create_example_page(root_dir)?;            }            return Ok(RunStatus::UpToDate);        }         // `ranim output` renders every `#[output(...)]` declared by the        // example (and processes `TimeMark::Capture`), which is exactly what        // the website artifacts need. `ranim render` only exists for quick        // single-scene smoke renders and requires an explicit scene name.        let mut args = vec!["ranim", "output", "--example", &self.name];        let features = self.required_features.join(",");        if !features.is_empty() {            args.extend(["--features", &features]);        }         // Output dirs in `#[output(...)]` are relative to the directory from        // which `ranim output` is invoked, so run it from the workspace root.        let previous_cwd = env::current_dir().context("failed to read current directory")?;        if previous_cwd != root_dir {            env::set_current_dir(root_dir).with_context(|| {                format!("failed to set current directory to {}", root_dir.display())            })?;        }        let render_result = Cli::parse_from(args)            .run()            .with_context(|| format!("failed to render example <{}>", self.name));        if previous_cwd != root_dir {            let _ = env::set_current_dir(previous_cwd);        }        render_result?;         self.clean_output(root_dir)?;         let mut files = vec![];        for render_dir in self.render_output_dirs(root_dir)? {            if !fs::exists(&render_dir)? {                continue;            }            for entry in WalkDir::new(&render_dir) {                let entry = entry.with_context(|| {                    format!("failed to walk render output {}", render_dir.display())                })?;                let path = entry.into_path();                if !path.is_file() {                    continue;                }                let Some(ext) = path.extension().and_then(|e| e.to_str()) else {                    continue;                };                if WEBSITE_OUTPUT_EXTENSIONS.contains(&ext) {                    files.push(copy_file(&path, &output_dir)?);                }            }        }         // README-referenced images join the published set ahead of `shadow        // publish` so their links resolve to shadow URLs like the renders.        self.sync_readme_images(root_dir)?;        let shadow = shadow::Shadow::load(root_dir)?;        shadow.publish()?;         write_example_data(root_dir, self, files, &shadow)?;        if !self.meta.hide {            self.create_example_page(root_dir)?;        }        Ok(RunStatus::Rendered)    }     /// Generate (or refresh) the Zola page for this example.    ///    /// The page is written below `website/content/examples/<group>/` so the    /// generated content tree mirrors the layout of the `examples/` directory.    pub fn create_example_page(&self, root_dir: impl AsRef<Path>) -> Result<PathBuf> {        let root_dir = root_dir.as_ref();        let website_root = root_dir.join("website");        let content_examples = website_root.join(WEBSITE_EXAMPLES_CONTENT_DIR);         self.ensure_section_indexes(&content_examples)?;         let page_dir = if self.group.as_os_str().is_empty() {            content_examples.clone()        } else {            content_examples.join(&self.group)        };        fs::create_dir_all(&page_dir)            .with_context(|| format!("failed to create {}", page_dir.display()))?;         let output_page_path = page_dir.join(format!("{}.md", self.name));         // If the example moved between groups, remove the stale page at its        // previous location instead of leaving two pages with the same name.        remove_stale_pages_for_example(&content_examples, &self.name, &output_page_path)?;         let example_dir = self.path.parent().with_context(|| {            format!(                "example path {} has no parent directory",                self.path.display()            )        })?;        let readme_path = example_dir.join("README.md");         // 创建markdown内容        let mut md_content = format!(            r#"+++title = "{}"template = "examples-page.html"+++ "#,            self.name        );         // 如果README.md存在,则复制其内容        if readme_path.exists() {            let readme_content = fs::read_to_string(readme_path)?;            let static_example_dir = website_root                .join("static")                .join("examples")                .join(&self.name);            let readme_content = rewrite_readme_images(                &readme_content,                example_dir,                &static_example_dir,                &|file: &str| {                    shadow::Shadow::try_object_url(root_dir, static_example_dir.join(file))                        .unwrap_or_else(|| format!("/examples/{}/{}", self.name, file))                },            )?;            md_content.push_str(&readme_content);             // 确保README内容后有换行            if !readme_content.ends_with('\n') {                md_content.push('\n');            }             // 如果最后一行不是空行,添加一个空行            if !md_content.ends_with("\n\n") {                md_content.push('\n');            }        }         // 添加示例标记        md_content.push_str(&format!("!example-{}\n", self.name));         // 写入markdown文件        fs::write(&output_page_path, &md_content)            .with_context(|| format!("failed to write {}", output_page_path.display()))?;        Ok(output_page_path)    }     /// Copy README-referenced local images into the static example directory    /// ahead of `shadow publish`, so their links resolve to shadow URLs just    /// like the rendered outputs.    fn sync_readme_images(&self, root_dir: &Path) -> Result<()> {        let example_dir = self.path.parent().with_context(|| {            format!(                "example path {} has no parent directory",                self.path.display()            )        })?;        let readme_path = example_dir.join("README.md");        if !readme_path.exists() {            return Ok(());        }        let static_example_dir = root_dir            .join("website")            .join("static")            .join("examples")            .join(&self.name);        let readme = fs::read_to_string(&readme_path)?;        copy_readme_images(&readme, example_dir, &static_example_dir)    }     /// Directories that `ranim output` writes for this example.    ///    /// The directories are parsed from the example's `#[output(dir = "...")]`    /// attributes, so multi-dir examples such as `perlin_terrain` work without    /// duplicating their directory layout in the xtask.    fn render_output_dirs(&self, root_dir: &Path) -> Result<Vec<PathBuf>> {        let source = fs::read_to_string(&self.path)            .with_context(|| format!("failed to read example source {}", self.path.display()))?;        let mut dirs = parse_output_dirs(&source)            .into_iter()            .map(|dir| {                let dir = dir.strip_prefix("./").unwrap_or(&dir);                root_dir.join(dir)            })            .collect::<Vec<_>>();         if dirs.is_empty() {            dirs.push(root_dir.join("output").join(&self.name));        }         let mut seen = HashSet::new();        dirs.retain(|dir| seen.insert(dir.clone()));        Ok(dirs)    }     fn ensure_section_indexes(&self, content_examples: &Path) -> Result<()> {        // Keep the root examples section available even on fresh checkouts.        ensure_section_index(content_examples, WEBSITE_EXAMPLES_TITLE)?;         let mut current = content_examples.to_path_buf();        for component in self.group.components() {            current.push(component);            let title = component.as_os_str().to_string_lossy();            ensure_section_index(&current, &title)?;        }        Ok(())    }} fn parse_output_dirs(source: &str) -> Vec<String> {    let mut dirs = vec![];    let mut rest = source;    while let Some(attr_start) = rest.find("#[output(") {        let attr_start = attr_start + "#[output(".len();        let Some(attr_end) = rest[attr_start..].find(")]") else {            break;        };        let attr_end = attr_start + attr_end;        let attr = &rest[attr_start..attr_end];         let mut cursor = 0;        while let Some(dir_start) = attr[cursor..].find("dir") {            let dir_start = cursor + dir_start;            let after_dir = &attr[dir_start + "dir".len()..];            let Some(eq) = after_dir.find('=') else {                break;            };

Lines 1–500 of 1182.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t