-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
123 lines (116 loc) · 4.78 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
//! Install Rollup.js to bundle all material-web components into one JS file we can include using our `UseMaterialWebComponents` component.
use std::{
env,
fs::{File, OpenOptions},
io,
process::Command,
};
const IMPORTS_JS_FILE_NAME: &str = "imports.js";
const OUTPUT_BUNDLE_FILE_NAME: &str = "output_bundle.js";
fn main() {
// Only re-run if new features added.
println!("cargo::rerun-if-changed=Cargo.toml,build.rs");
let imports_file_path = format!("{}/{}", env::var("OUT_DIR").unwrap(), IMPORTS_JS_FILE_NAME);
println!("cargo::warning={:?}", imports_file_path);
let mut imports_file: File = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&imports_file_path)
.unwrap_or_else(|_| panic!("Unable to open {} file for writing",
imports_file_path));
let output_path = format!(
"{}/{}",
env::var("OUT_DIR").unwrap(),
OUTPUT_BUNDLE_FILE_NAME
);
let npm_is_installed = run_command("npm -v").unwrap();
assert!(npm_is_installed.success());
let _ =
run_command("npm install rollup @rollup/plugin-node-resolve --global").unwrap();
let install_mwc = run_command("npm install @material/web --save-dev").unwrap();
assert!(install_mwc.success());
// Import typography stylesheet
add_import("typography/md-typescale-styles", &mut imports_file);
// Import components
if is_feature_enabled("checkbox") {
add_import("checkbox/checkbox", &mut imports_file);
}
if is_feature_enabled("textfield") {
add_import("textfield/filled-text-field", &mut imports_file);
add_import("textfield/outlined-text-field", &mut imports_file);
}
if is_feature_enabled("icon") {
add_import("icon/icon", &mut imports_file);
}
if is_feature_enabled("button") {
add_import("button/outlined-button", &mut imports_file);
add_import("button/elevated-button", &mut imports_file);
add_import("button/filled-button", &mut imports_file);
add_import("button/filled-tonal-button", &mut imports_file);
add_import("button/text-button", &mut imports_file);
}
if is_feature_enabled("iconbutton") {
add_import("iconbutton/icon-button", &mut imports_file);
}
if is_feature_enabled("elevation") {
add_import("elevation/elevation", &mut imports_file);
}
if is_feature_enabled("progress") {
add_import("progress/circular-progress", &mut imports_file);
add_import("progress/linear-progress", &mut imports_file);
}
if is_feature_enabled("select") {
add_import("select/filled-select", &mut imports_file);
add_import("select/outlined-select", &mut imports_file);
add_import("select/select-option", &mut imports_file);
}
if is_feature_enabled("chips") {
add_import("chips/assist-chip", &mut imports_file);
add_import("chips/filter-chip", &mut imports_file);
add_import("chips/suggestion-chip", &mut imports_file);
add_import("chips/input-chip", &mut imports_file);
}
add_typescale_styles(&mut imports_file);
assert!(imports_file.sync_all().is_ok());
// Node modules are installed in the CARGO_MANIFEST_DIR instead of the OUT_DIR to prevent completely re-installing @material/web
// each time the build script is run.
let run_rollup = run_command(&format!(
r#"npx rollup -p 'node-resolve={{modulePaths: ["{}/node_modules/"]}}' {} -o {} --format iife"#,
env::var("CARGO_MANIFEST_DIR").unwrap(), imports_file_path, output_path
))
.unwrap();
assert!(run_rollup.success());
}
pub fn run_command(command: &str) -> Result<std::process::ExitStatus, io::Error> {
let terminal = if cfg!(target_os = "windows") {
"cmd"
} else {
"sh"
};
let terminal_command_arg = if cfg!(target_os = "windows") {
"/C"
} else {
"-c"
};
Command::new(terminal)
.arg(terminal_command_arg)
.arg(command)
.spawn()
.unwrap()
.wait()
}
fn is_feature_enabled(feature: &str) -> bool {
let feature = String::from(feature).to_uppercase().replace('-', "_");
env::var(format!("CARGO_FEATURE_{}", feature)).is_ok()
}
fn add_import<T: std::io::Write>(to_import: &str, file: &mut T) {
writeln!(file, "import '@material/web/{}.js';", to_import).unwrap_or_else(|_| panic!("Error adding import '{}' to imports.js",
to_import));
}
fn add_typescale_styles<T: std::io::Write>(file: &mut T) {
let _ = writeln!(
file,
"import {{styles as typescaleStyles}} from '@material/web/typography/md-typescale-styles.js';\ndocument.adoptedStyleSheets.push(typescaleStyles.styleSheet)",
);
}