-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.rs
164 lines (140 loc) · 4.82 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
use std::fs;
use std::io::{self, Write};
use std::path::Path;
const TEMPLATES_DIR: &str = "src/new/templates";
const CHAIN_KINOSTATE_DIR: &str = "src/chain/kinostate";
const TARGET_DIR: &str = "target";
const NEW_INCLUDES: &str = "new_includes.rs";
const CHAIN_INCLUDES: &str = "chain_includes.rs";
/// create target/new_includes.rs to build templates into binary
fn make_new_includes() -> anyhow::Result<()> {
let mut output_buffer = Vec::new();
writeln!(
&mut output_buffer,
"const PATH_TO_CONTENT: &[(&str, &str)] = &["
)?;
writeln!(
output_buffer,
" (\"{}\", include_str!(\"{}\")),",
"componentize.mjs", "../src/new/componentize.mjs",
)?;
visit_dirs(Path::new(TEMPLATES_DIR), &mut output_buffer)?;
writeln!(&mut output_buffer, "];")?;
let target_dir = Path::new(TARGET_DIR);
let new_output_path = target_dir.join(NEW_INCLUDES);
// create *_includes.rs if it does not exist
if !target_dir.exists() {
fs::create_dir_all(target_dir)?;
}
if !new_output_path.exists() {
fs::write(&new_output_path, &output_buffer)?;
} else {
let existing_file = fs::read(&new_output_path)?;
if output_buffer != existing_file {
fs::write(&new_output_path, &output_buffer)?;
}
}
Ok(())
}
fn visit_dirs(dir: &Path, output_buffer: &mut Vec<u8>) -> io::Result<()> {
if !dir.is_dir() {
return Ok(());
}
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
let dir_name = path.file_name().and_then(|s| s.to_str());
if dir_name == Some("home") || dir_name == Some("target") {
continue;
}
visit_dirs(&path, output_buffer)?;
} else {
let ext = path.extension().and_then(|s| s.to_str());
if ext == Some("swp") || ext == Some("wasm") || ext == Some("zip") {
continue;
}
let file_name = path.file_name().and_then(|s| s.to_str());
if file_name == Some("Cargo.lock") {
continue;
}
let relative_path = path.strip_prefix(TEMPLATES_DIR).unwrap();
let path_str = relative_path.to_str().unwrap().replace("\\", "/");
let relative_path_from_includes = Path::new("..").join(&path);
let path_str_from_includes = relative_path_from_includes
.to_str()
.unwrap()
.replace("\\", "/");
writeln!(
output_buffer,
" (\"{}\", include_str!(\"{}\")),",
path_str, path_str_from_includes,
)?;
println!("cargo::rerun-if-changed={}", path.display());
}
}
Ok(())
}
fn make_chain_includes() -> anyhow::Result<()> {
let mut output_buffer = Vec::new();
writeln!(
&mut output_buffer,
"const FOUNDRY_COMMIT_TO_CONTENT: &[(&str, &str)] = &["
)?;
for entry in fs::read_dir(CHAIN_KINOSTATE_DIR)? {
let entry = entry?;
let path = entry.path();
let commit = path
.file_stem()
.and_then(|c| c.to_str())
.ok_or_else(|| anyhow::anyhow!("couldn't get commit from {path:?}"))?;
writeln!(
output_buffer,
" (\"{}\", include_str!(\"{}\")),",
commit,
Path::new("..").join(&path).display(),
)?;
println!("cargo::rerun-if-changed={}", path.display());
}
writeln!(&mut output_buffer, "];")?;
let target_dir = Path::new(TARGET_DIR);
let chain_output_path = target_dir.join(CHAIN_INCLUDES);
// create *_includes.rs if it does not exist
if !target_dir.exists() {
fs::create_dir_all(target_dir)?;
}
if !chain_output_path.exists() {
fs::write(&chain_output_path, &output_buffer)?;
} else {
let existing_file = fs::read(&chain_output_path)?;
if output_buffer != existing_file {
fs::write(&chain_output_path, &output_buffer)?;
}
}
Ok(())
}
fn add_commit_hash(repo: &git2::Repository) -> anyhow::Result<()> {
let sha = repo
.head()?
.target()
.ok_or(anyhow::anyhow!("couldn't get commit hash"))?;
println!("cargo:rustc-env=GIT_COMMIT_SHA={}", sha);
Ok(())
}
fn add_branch_name(repo: &git2::Repository) -> anyhow::Result<()> {
let head = repo.head()?;
let branch = head
.shorthand()
.ok_or(anyhow::anyhow!("couldn't get branch name"))?;
println!("cargo:rustc-env=GIT_BRANCH_NAME={}", branch);
Ok(())
}
fn main() -> anyhow::Result<()> {
make_new_includes()?;
make_chain_includes()?;
// write version info into binary
let repo = git2::Repository::open(".")?;
add_commit_hash(&repo)?;
add_branch_name(&repo)?;
Ok(())
}