-
Notifications
You must be signed in to change notification settings - Fork 7
/
build.rs
132 lines (110 loc) · 3.86 KB
/
build.rs
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#[cfg(feature = "build-wasm")]
use std::{
env, fs,
path::{Path, PathBuf},
process::Command,
};
// needed for windows compatibility
#[cfg(feature = "build-wasm")]
use which::which;
#[cfg(feature = "build-wasm")]
const GIT_REPOSITORY: &str = "https://github.com/massalabs/as_abi_protobuf.git";
#[cfg(feature = "build-wasm")]
const GIT_BRANCH: &str = "feature/Improve_ABI_types_in_wasmv1";
#[cfg(feature = "build-wasm")]
fn main() {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").expect("Failed to get Cargo dir");
let cargo_dir = &Path::new(&manifest_dir);
let target_path = cargo_dir.join("as_abi_protobuf");
let target_path_exists = target_path.exists();
if target_path_exists {
let target_path = target_path
.canonicalize()
.expect("as_abi_protobuf does not exist");
let is_symlink = target_path
.symlink_metadata()
.map(|metadata| metadata.file_type().is_symlink())
.unwrap_or(false);
if !is_symlink && target_path.join(".git").exists() {
env::set_current_dir(&target_path).expect("cd failed");
Command::new("git")
.arg("pull")
.output()
.expect("git pull failed");
}
} else {
git_clone(&target_path);
}
npm_install(&target_path);
build_wasm();
copy_wasm(target_path, cargo_dir);
}
#[cfg(feature = "build-wasm")]
fn git_clone(target_path: &PathBuf) {
Command::new("git")
.arg("clone")
.arg(GIT_REPOSITORY)
.arg(target_path.display().to_string())
.output()
.expect("Failed to execute git clone command");
env::set_current_dir(target_path)
.expect("Failed to change directory to the destination folder");
Command::new("git")
.arg("checkout")
.arg(GIT_BRANCH)
.output()
.expect("Failed to execute git checkout command");
}
#[cfg(feature = "build-wasm")]
fn npm_install(target_path: &PathBuf) {
env::set_current_dir(target_path).expect(&format!("Failed to cd to {}", target_path.display()));
let npm_path = which("npm").expect("npm not found in PATH");
Command::new(npm_path)
.arg("install")
.output()
.expect("npm install failed");
}
#[cfg(feature = "build-wasm")]
fn build_wasm() {
let package_json =
fs::read_to_string("package.json").expect("Failed to read package.json file");
let package_data: serde_json::Value =
serde_json::from_str(&package_json).expect("Failed to parse package.json file");
let Some(scripts) = package_data["scripts"].as_object() else {
return;
};
let rules: Vec<String> = scripts
.into_iter()
.filter(|(s, _)| s.starts_with("all:"))
.map(|(s, _)| s.clone())
.collect();
for rule in rules.iter() {
let npm_path = which("npm").expect("npm not found in PATH");
Command::new(npm_path)
.arg("run")
.arg(rule)
.output()
.expect(&format!("Failed to execute npm run {} script", rule));
}
}
#[cfg(feature = "build-wasm")]
fn copy_wasm(target_path: PathBuf, cargo_dir: &&Path) {
let build_dir = target_path.join("build");
let wasm_dir = cargo_dir.join("wasm");
if let Ok(entries) = fs::read_dir(&build_dir) {
for entry in entries {
if let Ok(entry) = entry {
let path = entry.path();
if path.is_file() && path.extension().unwrap_or_default() == "wasm_add" {
let file_name = path.file_name().unwrap();
let destination_path = wasm_dir.join(file_name);
fs::copy(&path, &destination_path).expect("Failed to copy the WASM file");
}
}
}
}
}
#[cfg(not(feature = "build-wasm"))]
fn main() {
// Do nothing if the "build-wasm" feature is not defined
}