forked from Choochmeque/tauri-plugin-notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
177 lines (153 loc) · 5.11 KB
/
build.rs
File metadata and controls
177 lines (153 loc) · 5.11 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
#[cfg(target_os = "macos")]
use std::{path::PathBuf, process::Command};
const COMMANDS: &[&str] = &[
"register_listener",
"remove_listener",
"notify",
"request_permission",
"is_permission_granted",
"register_for_push_notifications",
"unregister_for_push_notifications",
"register_for_unified_push",
"unregister_from_unified_push",
"get_unified_push_distributors",
"save_unified_push_distributor",
"get_unified_push_distributor",
"register_action_types",
"cancel",
"cancel_all",
"get_pending",
"remove_active",
"get_active",
"check_permissions",
"show",
"batch",
"list_channels",
"delete_channel",
"create_channel",
"permission_state",
"set_click_listener_active",
];
fn main() {
let enable_push = cfg!(feature = "push-notifications");
let enable_unified_push = cfg!(feature = "unified-push");
if std::env::var("TARGET")
.unwrap_or_default()
.contains("android")
{
let properties_content = format!(
"enablePushNotifications={}\nenableUnifiedPush={}",
enable_push, enable_unified_push
);
std::fs::write("android/build.properties", properties_content)
.expect("Failed to write build.properties");
}
let ios_marker_path = std::path::Path::new("ios/.push-notifications-enabled");
let macos_marker_path = std::path::Path::new("macos/.push-notifications-enabled");
if enable_push {
std::fs::write(ios_marker_path, "").expect("Failed to write iOS push marker file");
std::fs::write(macos_marker_path, "").expect("Failed to write macOS push marker file");
} else {
if ios_marker_path.exists() {
std::fs::remove_file(ios_marker_path).ok();
}
if macos_marker_path.exists() {
std::fs::remove_file(macos_marker_path).ok();
}
}
let result = tauri_plugin::Builder::new(COMMANDS)
.android_path("android")
.ios_path("ios")
.try_build();
// when building documentation for Android the plugin build result is always Err() and is irrelevant to the crate documentation build
if !(cfg!(docsrs)
&& std::env::var("TARGET")
.expect("Failed to get TARGET environment variable")
.contains("android"))
{
result.expect("Failed to build Tauri plugin");
}
#[cfg(target_os = "macos")]
{
// Only run macOS-specific build steps when building for macOS
if std::env::var("CARGO_CFG_TARGET_OS").unwrap_or_default() == "macos" {
let bridges = vec!["src/macos.rs"];
for path in &bridges {
println!("cargo:rerun-if-changed={path}");
}
println!("cargo:rerun-if-changed=macos/Sources/NotificationPlugin.swift");
swift_bridge_build::parse_bridges(bridges)
.write_all_concatenated(swift_bridge_out_dir(), env!("CARGO_PKG_NAME"));
compile_swift();
println!("cargo:rustc-link-lib=static=tauri-plugin-notifications");
println!(
"cargo:rustc-link-search={}",
swift_library_static_lib_dir()
.to_str()
.expect("Swift library path must be valid UTF-8")
);
}
}
}
#[cfg(target_os = "macos")]
fn compile_swift() {
let swift_package_dir = manifest_dir().join("macos");
let mut cmd = Command::new("swift");
cmd.current_dir(swift_package_dir).arg("build").args([
"-Xswiftc",
"-import-objc-header",
"-Xswiftc",
swift_source_dir()
.join("bridging-header.h")
.to_str()
.expect("Bridging header path must be valid UTF-8"),
]);
if is_release_build() {
cmd.args(["-c", "release"]);
}
let exit_status = cmd
.spawn()
.expect("Failed to spawn swift build command")
.wait_with_output()
.expect("Failed to wait for swift build output");
if !exit_status.status.success() {
panic!(
r#"
Stderr: {}
Stdout: {}
"#,
String::from_utf8(exit_status.stderr).expect("Stderr must be valid UTF-8"),
String::from_utf8(exit_status.stdout).expect("Stdout must be valid UTF-8"),
)
}
}
#[cfg(target_os = "macos")]
fn swift_bridge_out_dir() -> PathBuf {
generated_code_dir()
}
#[cfg(target_os = "macos")]
fn manifest_dir() -> PathBuf {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR must be set");
PathBuf::from(manifest_dir)
}
#[cfg(target_os = "macos")]
fn is_release_build() -> bool {
std::env::var("PROFILE").expect("PROFILE must be set") == "release"
}
#[cfg(target_os = "macos")]
fn swift_source_dir() -> PathBuf {
manifest_dir().join("macos/Sources")
}
#[cfg(target_os = "macos")]
fn generated_code_dir() -> PathBuf {
swift_source_dir().join("generated")
}
#[cfg(target_os = "macos")]
fn swift_library_static_lib_dir() -> PathBuf {
let debug_or_release = if is_release_build() {
"release"
} else {
"debug"
};
manifest_dir().join(format!("macos/.build/{debug_or_release}"))
}