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
//! Build script that generates the Tauri command allowlist.
const COMMANDS: &[&str] = &[
"victauri_eval_js",
"victauri_eval_callback",
"victauri_get_window_state",
"victauri_list_windows",
"victauri_get_ipc_log",
"victauri_get_registry",
"victauri_get_memory_stats",
"victauri_dom_snapshot",
"victauri_verify_state",
"victauri_detect_ghost_commands",
"victauri_check_ipc_integrity",
];
fn main() {
// The permission manifest is read by the app's `tauri-build`, so it must come from the
// `tauri-plugin` of the same major.
#[cfg(feature = "tauri-v3")]
tauri3::build();
#[cfg(not(feature = "tauri-v3"))]
tauri_plugin::Builder::new(COMMANDS).build();
}
#[cfg(feature = "tauri-v3")]
mod tauri3 {
use std::path::{Path, PathBuf};
/// Tauri 3's plugin build step rewrites `permissions/` in place: it deletes
/// `autogenerated/commands` (Tauri 3 lists commands in `OUT_DIR` instead) and rewrites
/// `schemas/schema.json` in the Tauri 3 format. In a checkout that also builds against Tauri 2
/// that dirties tracked files on every Tauri 3 build. So it runs over a copy of the
/// hand-written permission files staged in `OUT_DIR`. Its outputs are unaffected: the
/// permission file list it hands the app's `tauri-build` holds canonicalized paths, and its
/// `rerun-if-changed=permissions` is resolved against this package, i.e. the real directory.
pub fn build() {
let manifest_dir =
PathBuf::from(std::env::var_os("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
let stage =
PathBuf::from(std::env::var_os("OUT_DIR").expect("OUT_DIR")).join("tauri3-plugin-root");
let staged_permissions = stage.join("permissions");
// A stale stage could still hold a permission file deleted from the source tree.
let _ = std::fs::remove_dir_all(&staged_permissions);
copy_hand_written(&manifest_dir.join("permissions"), &staged_permissions)
.expect("stage the plugin permission files for the Tauri 3 build");
std::env::set_current_dir(&stage).expect("enter the staged plugin root");
tauri_plugin_v3::Builder::new(super::COMMANDS).build();
}
/// Copy `from` to `to`, leaving out the generated `autogenerated/` and `schemas/` folders.
fn copy_hand_written(from: &Path, to: &Path) -> std::io::Result<()> {
std::fs::create_dir_all(to)?;
for entry in std::fs::read_dir(from)? {
let entry = entry?;
let name = entry.file_name();
let target = to.join(&name);
if entry.file_type()?.is_dir() {
if name != "autogenerated" && name != "schemas" {
copy_hand_written(&entry.path(), &target)?;
}
} else {
std::fs::copy(entry.path(), target)?;
}
}
Ok(())
}
}