forked from tauri-apps/wry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
85 lines (73 loc) · 2.7 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
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
fn main() {
let is_macos = std::env::var("TARGET")
.map(|t| t.ends_with("-darwin"))
.unwrap_or_default();
if is_macos {
println!("cargo:rustc-link-lib=framework=WebKit");
}
let is_android = std::env::var("CARGO_CFG_TARGET_OS")
.map(|t| t == "android")
.unwrap_or_default();
if is_android {
use std::{fs, path::PathBuf};
fn env_var(var: &str) -> String {
std::env::var(var).expect(&format!(
" `{}` is not set, which is needed to generate the kotlin files for android.",
var
))
}
println!("cargo:rerun-if-env-changed=WRY_ANDROID_REVERSED_DOMAIN");
println!("cargo:rerun-if-env-changed=WRY_ANDROID_APP_NAME_SNAKE_CASE");
println!("cargo:rerun-if-env-changed=WRY_ANDROID_KOTLIN_FILES_OUT_DIR");
let reversed_domain = env_var("WRY_ANDROID_REVERSED_DOMAIN");
let app_name_snake_case = env_var("WRY_ANDROID_APP_NAME_SNAKE_CASE");
let kotlin_out_dir = env_var("WRY_ANDROID_KOTLIN_FILES_OUT_DIR");
let kotlin_out_dir = PathBuf::from(kotlin_out_dir)
.canonicalize()
.expect("Failed to canonicalize path");
let kotlin_files =
fs::read_dir(PathBuf::from(env_var("CARGO_MANIFEST_DIR")).join("src/webview/android/kotlin"))
.expect("failed to read kotlin directory");
for file in kotlin_files {
let file = file.unwrap();
let class_extension_env = format!(
"WRY_{}_CLASS_EXTENSION",
file
.path()
.file_stem()
.unwrap()
.to_string_lossy()
.to_uppercase()
);
let class_init_env = format!(
"WRY_{}_CLASS_INIT",
file
.path()
.file_stem()
.unwrap()
.to_string_lossy()
.to_uppercase()
);
println!("cargo:rerun-if-env-changed={}", class_extension_env);
println!("cargo:rerun-if-env-changed={}", class_init_env);
let content = fs::read_to_string(file.path())
.expect("failed to read kotlin file as string")
.replace("{{app-domain-reversed}}", &reversed_domain)
.replace("{{app-name-snake-case}}", &app_name_snake_case)
.replace(
"{{class-extension}}",
&std::env::var(&class_extension_env).unwrap_or_default(),
)
.replace(
"{{class-init}}",
&std::env::var(&class_init_env).unwrap_or_default(),
);
let mut out = String::from("/* THIS FILE IS AUTO-GENERATED. DO NOT MODIFY!! */\n\n");
out.push_str(&content);
fs::write(kotlin_out_dir.join(file.file_name()), out).expect("Failed to write kotlin file");
}
}
}