Skip to content

Commit

Permalink
chore(hub-embed): auto-build & embed hub instead of pulling from rele…
Browse files Browse the repository at this point in the history
…ases
  • Loading branch information
NathanFlurry committed Jan 29, 2025
1 parent 7ad0ba7 commit 4bce14d
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 39 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ tests/basic-game/.env
# Added by cargo

/target
.env*
.yarn/cache
.yarn/install-state.gz
.turbo
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/apps/hub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
5 changes: 5 additions & 0 deletions frontend/apps/hub/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions packages/api/ui/src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ impl Router {
) -> GlobalResult<Vec<u8>> {
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())
}
Expand Down
1 change: 1 addition & 0 deletions packages/common/hub-embed/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
70 changes: 35 additions & 35 deletions packages/common/hub-embed/build.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
// 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(())
}

0 comments on commit 4bce14d

Please sign in to comment.