From 4bce14d426b2e7f5ca3d9d4bdf479ad840a06d28 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Wed, 29 Jan 2025 08:47:01 -0800 Subject: [PATCH] chore(hub-embed): auto-build & embed hub instead of pulling from releases --- .gitignore | 1 - Cargo.lock | 1 + frontend/apps/hub/package.json | 1 + frontend/apps/hub/turbo.json | 5 ++ packages/api/ui/src/route.rs | 6 +-- packages/common/hub-embed/Cargo.toml | 1 + packages/common/hub-embed/build.rs | 70 ++++++++++++++-------------- 7 files changed, 46 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index 35945eed19..662f9128d1 100644 --- a/.gitignore +++ b/.gitignore @@ -61,7 +61,6 @@ tests/basic-game/.env # Added by cargo /target -.env* .yarn/cache .yarn/install-state.gz .turbo diff --git a/Cargo.lock b/Cargo.lock index d60976346d..5c6a5885dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11817,6 +11817,7 @@ dependencies = [ name = "rivet-hub-embed" version = "25.1.0-rc.1" dependencies = [ + "fs_extra", "include_dir", "reqwest 0.12.9", "zip 2.2.2", diff --git a/frontend/apps/hub/package.json b/frontend/apps/hub/package.json index 11d7048498..f793e8f34c 100644 --- a/frontend/apps/hub/package.json +++ b/frontend/apps/hub/package.json @@ -6,6 +6,7 @@ "scripts": { "dev": "vite", "build": "vite build --mode=production --base=/", + "build:embedded": "vite build --mode=production --base=/ui/", "preview": "vite preview" }, "dependencies": { diff --git a/frontend/apps/hub/turbo.json b/frontend/apps/hub/turbo.json index af265a8674..e743eeb901 100644 --- a/frontend/apps/hub/turbo.json +++ b/frontend/apps/hub/turbo.json @@ -7,6 +7,11 @@ "outputs": ["dist/**"], "dependsOn": ["@rivet-gg/actor-client#build"] }, + "build:embedded": { + "env": ["VITE_APP_*"], + "outputs": ["dist/**"], + "dependsOn": ["@rivet-gg/actor-client#build"] + }, "dev": { "env": ["VITE_APP_*"], "persistent": true, diff --git a/packages/api/ui/src/route.rs b/packages/api/ui/src/route.rs index 5ed0aeedab..7cc6a214f5 100644 --- a/packages/api/ui/src/route.rs +++ b/packages/api/ui/src/route.rs @@ -10,16 +10,16 @@ impl Router { ) -> GlobalResult> { let content_str = std::str::from_utf8(content)?; - let replacement_count = content_str.matches("%VITE_APP_API_URL%").count(); + let replacement_count = content_str.matches("__VITE_APP_API_URL__").count(); ensure!( replacement_count > 0, - "Expected at least one occurrence of %VITE_APP_API_URL%, found {}", + "Expected at least one occurrence of __VITE_APP_API_URL__, found {}", replacement_count ); let public_origin = util::url::to_string_without_slash(&config.server()?.rivet.api_public.public_origin()); - let replaced_content = content_str.replace("%VITE_APP_API_URL%", &public_origin); + let replaced_content = content_str.replace("__VITE_APP_API_URL__", &public_origin); Ok(replaced_content.into_bytes()) } diff --git a/packages/common/hub-embed/Cargo.toml b/packages/common/hub-embed/Cargo.toml index 5589f52268..eeb43e01e0 100644 --- a/packages/common/hub-embed/Cargo.toml +++ b/packages/common/hub-embed/Cargo.toml @@ -6,6 +6,7 @@ license.workspace = true edition.workspace = true [build-dependencies] +fs_extra = "1.3.0" reqwest = { version = "0.12.8", features = ["blocking"] } zip = "2.2.0" diff --git a/packages/common/hub-embed/build.rs b/packages/common/hub-embed/build.rs index ffd02b2434..c780197c75 100644 --- a/packages/common/hub-embed/build.rs +++ b/packages/common/hub-embed/build.rs @@ -1,45 +1,45 @@ -use std::{env, fs, path::Path}; - -const HUB_URL: &str = "https://releases.rivet.gg/hub/2025-01-13-06-33-44-c31561791-embed.zip"; -const OUT_DIR: &str = "hub_files"; +use std::{env, fs, path::Path, process::Command, path::PathBuf}; fn main() -> Result<(), Box> { // Get the output directory from the cargo environment variable let target_dir = env::var("OUT_DIR")?; - let out_dir = Path::new(&target_dir).join(OUT_DIR); - - // Download the file - println!("cargo:rerun-if-changed=build.rs"); - - let response = reqwest::blocking::get(HUB_URL)?; - let zip_content = response.bytes()?; - - // Extract to out dir - if !out_dir.exists() { - fs::create_dir_all(&out_dir)?; - } - - let mut zip_archive = zip::ZipArchive::new(std::io::Cursor::new(zip_content))?; - - for i in 0..zip_archive.len() { - let mut file = zip_archive.by_index(i)?; - let outpath = out_dir.join(file.name()); - - if file.name().ends_with('/') { - fs::create_dir_all(&outpath)?; - } else { - if let Some(p) = outpath.parent() { - if !p.exists() { - fs::create_dir_all(p)?; - } - } - let mut outfile = fs::File::create(&outpath)?; - std::io::copy(&mut file, &mut outfile)?; - } - } + let manifest_dir = env::var("CARGO_MANIFEST_DIR")?; + let out_dir = Path::new(&target_dir); + + let project_root = PathBuf::from(manifest_dir.clone()).join("../../.."); + let hub_path = project_root.join("frontend/apps/hub"); + + // Build hub + println!("Running yarn install"); + let status = Command::new("yarn") + .arg("install") + .arg("--immutable") + .current_dir(&hub_path) + .status()?; + assert!(status.success(), "yarn install failed"); + + println!("Running yarn build"); + let status = Command::new("yarn") + .current_dir(&hub_path) + .args(["dlx", "turbo", "run", "build:embedded"]) + // This will be substituted at runtime + .env("VITE_APP_API_URL", "__APP_API_URL__") + .status()?; + assert!(status.success(), "hub build failed"); + + // Copy dist directory to out_dir + let dist_path = hub_path.join("dist"); + fs::create_dir_all(out_dir)?; + fs_extra::dir::copy( + dist_path, + out_dir, + &fs_extra::dir::CopyOptions::new().overwrite(true), + )?; // Set the path in the env println!("cargo:rustc-env=HUB_PATH={}", out_dir.display()); + println!("cargo:rerun-if-changed={}", hub_path.display()); + Ok(()) }