Move branding and package install into cargo-run (#4266)

* Move branding asset fetch into cargo-run

* Move frontend npm install into cargo-run

* Rename branding::ensure to branding::setup

* Wording nits

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Timon
2026-06-22 20:21:46 +00:00
committed by GitHub
parent b286e89746
commit f798b48b83
11 changed files with 74 additions and 261 deletions

View File

@@ -106,7 +106,6 @@ fn extract_tar_gz(body: &[u8], dir: &Path, strip: usize, include: &[String]) ->
}
entry.unpack(&target).map_err(|e| Error::Io(e, format!("unpacking '{}'", target.display())))?;
}
eprintln!("Extracted into {}", dir.display());
Ok(())
}

View File

@@ -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(())
}

View File

@@ -11,7 +11,32 @@ pub fn frontend_dir() -> PathBuf {
}
pub fn setup() -> Result<(), Error> {
utils::npm(["run", "setup"]).dir(frontend_dir()).run()
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(&timestamp_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> {

View File

@@ -1,5 +1,6 @@
use std::path::PathBuf;
pub mod branding;
pub mod cmd;
pub mod frontend;
pub mod requirements;

View File

@@ -87,6 +87,10 @@ fn run_task(task: &Task) -> Result<(), Error> {
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)?,

View File

@@ -29,7 +29,7 @@ fn requirements(task: &Task) -> Vec<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)",
name: "Rust - Wasm Target",
install: "rustup target add wasm32-unknown-unknown".into(),
skip: Some(&|task| matches!(task.target, Target::Cli)),
..Default::default()