Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate webui dist files as part of build.rs #180

Merged
merged 13 commits into from
Aug 12, 2024
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Just a regular Rust binary build process.

cargo build --release

The "webui" feature requires npm and python3 installed.

## Useful options

### -v <log-level>
Expand Down
3 changes: 3 additions & 0 deletions crates/librqbit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ lru = { version = "0.12.3", optional = true }
mime_guess = { version = "2.0.5", default-features = false }
tokio-socks = "0.5.2"

[build-dependencies]
anyhow = "1"

[dev-dependencies]
futures = { version = "0.3" }
tracing-subscriber = "0.3"
Expand Down
47 changes: 47 additions & 0 deletions crates/librqbit/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use anyhow::{bail, Context};
use std::path::Path;
use std::process::Command;

fn run_cmd(cwd: &Path, cmd: &str) -> anyhow::Result<()> {
#[cfg(target_os = "windows")]
let (shell, shell_args) = ("powershell", ["-command"].as_slice());
#[cfg(not(target_os = "windows"))]
let (shell, shell_args) = ("bash", ["-c"].as_slice());

// Run "npm install" in the webui directory
let output = Command::new(shell)
.args(shell_args)
.arg(cmd)
.current_dir(cwd)
.output()
.with_context(|| format!("Failed to execute {} in {:?}", cmd, cwd))?;

if !output.status.success() {
bail!(
"\"{}\" failed\n\nstderr: {}\n\nstdout: {}",
cmd,
String::from_utf8_lossy(&output.stderr),
String::from_utf8_lossy(&output.stdout)
);
}

// Optionally print the stdout output if you want to see the build logs
println!("{}", String::from_utf8_lossy(&output.stdout));

Ok(())
}

fn main() {
#[cfg(feature = "webui")]
{
let webui_dir = Path::new("webui");
let webui_src_dir = webui_dir.join("src");

println!("cargo:rerun-if-changed={}", webui_src_dir.to_str().unwrap());

// Run "npm install && npm run build" in the webui directory
for cmd in ["npm install", "npm run build"] {
run_cmd(webui_dir, cmd).unwrap();
}
}
}
1 change: 0 additions & 1 deletion crates/librqbit/webui/dist/assets/index.css

This file was deleted.

56 changes: 0 additions & 56 deletions crates/librqbit/webui/dist/assets/index.js

This file was deleted.

64 changes: 0 additions & 64 deletions crates/librqbit/webui/dist/assets/logo.svg

This file was deleted.

17 changes: 0 additions & 17 deletions crates/librqbit/webui/dist/index.html

This file was deleted.

18 changes: 0 additions & 18 deletions crates/librqbit/webui/dist/manifest.json

This file was deleted.

2 changes: 1 addition & 1 deletion crates/librqbit/webui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build && ./post-build",
"build": "vite build && python3 post-build",
"preview": "vite preview"
},
"dependencies": {
Expand Down