-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
76 lines (69 loc) · 2.13 KB
/
Copy pathbuild.rs
File metadata and controls
76 lines (69 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use std::process::Command;
// Dev only build
fn main() -> std::io::Result<()> {
// build client as static files
//test
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=client/src/**");
println!("cargo:rerun-if-changed=client/static/**");
println!("cargo:rerun-if-changed=client/svelte.config.js");
println!("cargo:rerun-if-changed=.env");
if !check_program_installed("npm") {
panic!("npm is not installed! install it first.");
}
// check diesel_cli installed
if !check_program_installed("diesel") {
panic!("diesel_cli is not installed! installing it: 'cargo install diesel_cli --no-default-features --features sqlite'");
}
let env_file = std::path::Path::new(".env");
if !env_file.exists() {
let current_dir = std::env::current_dir()?;
let database_url = current_dir.join("db.sqlite3");
std::fs::write(
".env",
format!(
"DATABASE_URL = {}\nSTATIC_FILE_PATH = {}",
database_url.display(),
current_dir.join("client/build").display()
),
)?;
}
#[cfg(not(debug_assertions))]
{
return build_client();
}
Ok(())
}
#[cfg(not(debug_assertions))]
fn build_client() -> std::io::Result<()> {
let node_modules = std::path::Path::new("client/node_modules");
if !node_modules.exists() {
let _exit_status = Command::new("npm")
.current_dir("client")
.arg("install")
.status()?;
}
// run npm build
let _exit_status = Command::new("npm")
.current_dir("client")
.arg("run")
.arg("build")
.status()?;
Ok(())
}
#[cfg(windows)]
fn check_program_installed(program: &str) -> bool {
let output = Command::new("where")
.arg(program)
.output()
.expect("failed to execute process");
output.status.success()
}
#[cfg(unix)]
fn check_program_installed(program: &str) -> bool {
let output = Command::new("which")
.arg(program)
.output()
.expect("failed to execute process");
output.status.success()
}