-
Notifications
You must be signed in to change notification settings - Fork 19
/
build.rs
45 lines (39 loc) · 1.25 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
use std::env;
use std::fs::File;
use std::io::{self, BufRead, BufReader, BufWriter, Write};
use std::path::Path;
fn main() -> Result<(), Box<dyn std::error::Error>> {
update_version_if_nightly();
let out_dir = env::var("OUT_DIR")?;
let out_dir = Path::new(&out_dir);
let src_dir = Path::new("data");
generate(
src_dir.join("adjectives.txt"),
out_dir.join("adjectives.rs"),
)?;
generate(src_dir.join("nouns.txt"), out_dir.join("nouns.rs"))?;
Ok(())
}
fn generate(src_path: impl AsRef<Path>, dst_path: impl AsRef<Path>) -> io::Result<()> {
let src = BufReader::new(File::open(src_path.as_ref())?);
let mut dst = BufWriter::new(File::create(dst_path.as_ref())?);
writeln!(dst, "[")?;
for word in src.lines() {
writeln!(dst, "\"{}\",", &word.unwrap())?;
}
writeln!(dst, "]")
}
fn update_version_if_nightly() {
println!("cargo:rerun-if-env-changed=NIGHTLY_BUILD");
if let Ok(date) = std::env::var("NIGHTLY_BUILD") {
println!(
"cargo:rustc-env=CARGO_PKG_VERSION={}-nightly.{}",
std::env::var("CARGO_PKG_VERSION")
.unwrap()
.split('-')
.next()
.unwrap(),
date,
);
}
}