-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
mod.rs
126 lines (101 loc) · 3.68 KB
/
mod.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
124
125
126
// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::path::{Path, PathBuf};
use anyhow::Result;
use tauri_utils::acl::{self, Error};
pub mod mobile;
use serde::de::DeserializeOwned;
use std::{env::var, io::Cursor};
pub fn plugin_config<T: DeserializeOwned>(name: &str) -> Option<T> {
let config_env_var_name = format!(
"TAURI_{}_PLUGIN_CONFIG",
name.to_uppercase().replace('-', "_")
);
if let Ok(config_str) = var(&config_env_var_name) {
println!("cargo:rerun-if-env-changed={config_env_var_name}");
serde_json::from_reader(Cursor::new(config_str))
.map(Some)
.expect("failed to parse configuration")
} else {
None
}
}
pub struct Builder<'a> {
commands: &'a [&'static str],
global_scope_schema: Option<schemars::schema::RootSchema>,
android_path: Option<PathBuf>,
ios_path: Option<PathBuf>,
}
impl<'a> Builder<'a> {
pub fn new(commands: &'a [&'static str]) -> Self {
Self {
commands,
global_scope_schema: None,
android_path: None,
ios_path: None,
}
}
/// Sets the global scope JSON schema.
pub fn global_scope_schema(mut self, schema: schemars::schema::RootSchema) -> Self {
self.global_scope_schema.replace(schema);
self
}
/// Sets the Android project path.
pub fn android_path<P: Into<PathBuf>>(mut self, android_path: P) -> Self {
self.android_path.replace(android_path.into());
self
}
/// Sets the iOS project path.
pub fn ios_path<P: Into<PathBuf>>(mut self, ios_path: P) -> Self {
self.ios_path.replace(ios_path.into());
self
}
/// [`Self::try_build`] but will exit automatically if an error is found.
pub fn build(self) {
if let Err(error) = self.try_build() {
println!("{}: {error:#}", env!("CARGO_PKG_NAME"));
std::process::exit(1);
}
}
/// Ensure this crate is properly configured to be a Tauri plugin.
///
/// # Errors
///
/// Errors will occur if environmental variables expected to be set inside of [build scripts]
/// are not found, or if the crate violates Tauri plugin conventions.
pub fn try_build(self) -> Result<()> {
// convention: plugin names should not use underscores
let name = build_var("CARGO_PKG_NAME")?;
if name.contains('_') {
anyhow::bail!("plugin names cannot contain underscores");
}
let out_dir = PathBuf::from(build_var("OUT_DIR")?);
// requirement: links MUST be set and MUST match the name
let _links = build_var("CARGO_MANIFEST_LINKS")?;
let autogenerated = Path::new("permissions").join(acl::build::AUTOGENERATED_FOLDER_NAME);
let commands_dir = autogenerated.join("commands");
std::fs::create_dir_all(&autogenerated).expect("unable to create permissions dir");
if !self.commands.is_empty() {
acl::build::autogenerate_command_permissions(&commands_dir, self.commands, "");
}
println!("cargo:rerun-if-changed=permissions");
let permissions = acl::build::define_permissions("./permissions/**/*.*", &name, &out_dir)?;
acl::build::generate_schema(&permissions, "./permissions")?;
acl::build::generate_docs(&permissions, &autogenerated)?;
if let Some(global_scope_schema) = self.global_scope_schema {
acl::build::define_global_scope_schema(global_scope_schema, &name, &out_dir)?;
}
mobile::setup(self.android_path, self.ios_path)?;
Ok(())
}
}
fn cfg_alias(alias: &str, has_feature: bool) {
if has_feature {
println!("cargo:rustc-cfg={alias}");
}
}
/// Grab an env var that is expected to be set inside of build scripts.
fn build_var(key: &'static str) -> Result<String, Error> {
std::env::var(key).map_err(|_| Error::BuildVar(key))
}