Skip to content

Commit 093c92d

Browse files
committed
chore(hub-embed): auto-build & embed hub instead of pulling from releases (#1966)
<!-- Please make sure there is an issue that this PR is correlated to. --> ## Changes <!-- If there are frontend changes, please include screenshots. -->
1 parent 0bf75dd commit 093c92d

File tree

7 files changed

+46
-39
lines changed

7 files changed

+46
-39
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ tests/basic-game/.env
6161
# Added by cargo
6262

6363
/target
64-
.env*
6564
.yarn/cache
6665
.yarn/install-state.gz
6766
.turbo

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

frontend/apps/hub/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"scripts": {
77
"dev": "vite",
88
"build": "vite build --mode=production --base=/",
9+
"build:embedded": "vite build --mode=production --base=/ui/",
910
"preview": "vite preview"
1011
},
1112
"dependencies": {

frontend/apps/hub/turbo.json

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
"outputs": ["dist/**"],
88
"dependsOn": ["@rivet-gg/actor-client#build"]
99
},
10+
"build:embedded": {
11+
"env": ["VITE_APP_*"],
12+
"outputs": ["dist/**"],
13+
"dependsOn": ["@rivet-gg/actor-client#build"]
14+
},
1015
"dev": {
1116
"env": ["VITE_APP_*"],
1217
"persistent": true,

packages/api/ui/src/route.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ impl Router {
1010
) -> GlobalResult<Vec<u8>> {
1111
let content_str = std::str::from_utf8(content)?;
1212

13-
let replacement_count = content_str.matches("%VITE_APP_API_URL%").count();
13+
let replacement_count = content_str.matches("__VITE_APP_API_URL__").count();
1414
ensure!(
1515
replacement_count > 0,
16-
"Expected at least one occurrence of %VITE_APP_API_URL%, found {}",
16+
"Expected at least one occurrence of __VITE_APP_API_URL__, found {}",
1717
replacement_count
1818
);
1919

2020
let public_origin =
2121
util::url::to_string_without_slash(&config.server()?.rivet.api_public.public_origin());
22-
let replaced_content = content_str.replace("%VITE_APP_API_URL%", &public_origin);
22+
let replaced_content = content_str.replace("__VITE_APP_API_URL__", &public_origin);
2323

2424
Ok(replaced_content.into_bytes())
2525
}

packages/common/hub-embed/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ license.workspace = true
66
edition.workspace = true
77

88
[build-dependencies]
9+
fs_extra = "1.3.0"
910
reqwest = { version = "0.12.8", features = ["blocking"] }
1011
zip = "2.2.0"
1112

packages/common/hub-embed/build.rs

+35-35
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,45 @@
1-
use std::{env, fs, path::Path};
2-
3-
const HUB_URL: &str = "https://releases.rivet.gg/hub/2025-01-13-06-33-44-c31561791-embed.zip";
4-
const OUT_DIR: &str = "hub_files";
1+
use std::{env, fs, path::Path, process::Command, path::PathBuf};
52

63
fn main() -> Result<(), Box<dyn std::error::Error>> {
74
// Get the output directory from the cargo environment variable
85
let target_dir = env::var("OUT_DIR")?;
9-
let out_dir = Path::new(&target_dir).join(OUT_DIR);
10-
11-
// Download the file
12-
println!("cargo:rerun-if-changed=build.rs");
13-
14-
let response = reqwest::blocking::get(HUB_URL)?;
15-
let zip_content = response.bytes()?;
16-
17-
// Extract to out dir
18-
if !out_dir.exists() {
19-
fs::create_dir_all(&out_dir)?;
20-
}
21-
22-
let mut zip_archive = zip::ZipArchive::new(std::io::Cursor::new(zip_content))?;
23-
24-
for i in 0..zip_archive.len() {
25-
let mut file = zip_archive.by_index(i)?;
26-
let outpath = out_dir.join(file.name());
27-
28-
if file.name().ends_with('/') {
29-
fs::create_dir_all(&outpath)?;
30-
} else {
31-
if let Some(p) = outpath.parent() {
32-
if !p.exists() {
33-
fs::create_dir_all(p)?;
34-
}
35-
}
36-
let mut outfile = fs::File::create(&outpath)?;
37-
std::io::copy(&mut file, &mut outfile)?;
38-
}
39-
}
6+
let manifest_dir = env::var("CARGO_MANIFEST_DIR")?;
7+
let out_dir = Path::new(&target_dir);
8+
9+
let project_root = PathBuf::from(manifest_dir.clone()).join("../../..");
10+
let hub_path = project_root.join("frontend/apps/hub");
11+
12+
// Build hub
13+
println!("Running yarn install");
14+
let status = Command::new("yarn")
15+
.arg("install")
16+
.arg("--immutable")
17+
.current_dir(&hub_path)
18+
.status()?;
19+
assert!(status.success(), "yarn install failed");
20+
21+
println!("Running yarn build");
22+
let status = Command::new("yarn")
23+
.current_dir(&hub_path)
24+
.args(["dlx", "turbo", "run", "build:embedded"])
25+
// This will be substituted at runtime
26+
.env("VITE_APP_API_URL", "__APP_API_URL__")
27+
.status()?;
28+
assert!(status.success(), "hub build failed");
29+
30+
// Copy dist directory to out_dir
31+
let dist_path = hub_path.join("dist");
32+
fs::create_dir_all(out_dir)?;
33+
fs_extra::dir::copy(
34+
dist_path,
35+
out_dir,
36+
&fs_extra::dir::CopyOptions::new().overwrite(true),
37+
)?;
4038

4139
// Set the path in the env
4240
println!("cargo:rustc-env=HUB_PATH={}", out_dir.display());
4341

42+
println!("cargo:rerun-if-changed={}", hub_path.display());
43+
4444
Ok(())
4545
}

0 commit comments

Comments
 (0)