-
Notifications
You must be signed in to change notification settings - Fork 8
/
build.rs
85 lines (76 loc) · 2.68 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
use std::env;
use std::path::PathBuf;
use std::process::Command;
use chrono::prelude::*;
use crowbook_intl::{Extractor, Localizer};
fn gen_commit_file() {
let git_log_output = Command::new("git")
.arg("--no-pager")
.args(["--work-tree", "."])
.args(["--git-dir", "./.git"])
.arg("log")
.args(["--oneline", r##"--format=%h"##])
.output()
.ok()
.and_then(|output| String::from_utf8(output.stdout).ok());
if let Some(git_log) = git_log_output {
let count = git_log.lines().count().to_string();
if let Some(last_commit) = git_log.lines().next() {
println!(
"cargo:rustc-env=GIT_DESCRIPTION=commit {} ({})",
count.trim(),
last_commit.to_string().trim()
);
} else {
println!(r#"cargo:rustc-env=GIT_DESCRIPTION="no git description""#);
}
} else {
println!(r#"cargo:rustc-env=GIT_DESCRIPTION="no git description""#);
}
}
fn generate_localization() {
// Generate a `lang/default.pot` containing strings used to call `lformat!`
let mut extractor = Extractor::new();
extractor
.add_messages_from_dir(concat!(env!("CARGO_MANIFEST_DIR"), "/src"))
.unwrap();
extractor
.write_pot_file(concat!(env!("CARGO_MANIFEST_DIR"), "/lang/default.pot"))
.unwrap();
// Generate the `localize_macros.rs` file
let mut localizer = Localizer::new(&extractor);
localizer
.add_lang("de", include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/lang/de.po")))
.unwrap();
// Use env::var instead of env! to avoid problems when cross-compiling
let dest_path = PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("localize_macros.rs");
localizer.write_macro_file(dest_path).unwrap();
}
fn build_webapp() {
Command::new("yarn")
.current_dir("./webapp")
.output()
.unwrap_or_else(|e| panic!("yarn install step failed {e}"));
Command::new("yarn")
.current_dir("./webapp")
.arg("build")
.output()
.unwrap_or_else(|e| panic!("yarn build step failed {e}"));
}
fn main() {
// passing variables to rustc
println!(
"cargo:rustc-env=PROFILE={}",
env::var("PROFILE").unwrap_or_else(|_| "unknown profile".into())
);
println!("cargo:rustc-env=BUILD_DATE={}", Utc::now().format("%+"));
if env::var("CARGO_FEATURE_WEBAPP") == Ok(String::from("1")) {
build_webapp();
}
if env::var("CARGO_FEATURE_LOCALIZE") == Ok(String::from("1")) {
generate_localization();
}
if env::var("CARGO_FEATURE_VERSION_STRING") == Ok(String::from("1")) {
gen_commit_file();
}
}