-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
50 lines (41 loc) · 1.41 KB
/
build.rs
File metadata and controls
50 lines (41 loc) · 1.41 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
use std::env;
use std::fs;
use std::io::Read;
use std::path::PathBuf;
const DATASTAR_URL: &str =
"https://cdn.jsdelivr.net/gh/starfederation/datastar@1.0.0-RC.7/bundles/datastar.js";
fn main() {
println!("cargo:rerun-if-env-changed=PICLR_NO_FETCH");
println!("cargo:rerun-if-changed=assets/datastar.js");
ensure_datastar();
#[cfg(feature = "tauri")]
tauri_build::build();
}
fn ensure_datastar() {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| ".".to_string());
let path = PathBuf::from(manifest_dir)
.join("assets")
.join("datastar.js");
if path.exists() {
return;
}
if env::var("PICLR_NO_FETCH").is_ok() {
panic!("assets/datastar.js is missing and PICLR_NO_FETCH is set");
}
if let Some(parent) = path.parent() {
if let Err(err) = fs::create_dir_all(parent) {
panic!("Failed to create assets directory: {err}");
}
}
let response = ureq::get(DATASTAR_URL).call();
if let Err(err) = response {
panic!("Failed to fetch datastar.js: {err}");
}
let response = response.unwrap();
let mut reader = response.into_reader();
let mut body = Vec::new();
reader
.read_to_end(&mut body)
.unwrap_or_else(|err| panic!("Failed to read datastar.js: {err}"));
fs::write(&path, body).unwrap_or_else(|err| panic!("Failed to write datastar.js: {err}"));
}