-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin_paths.rs
More file actions
304 lines (280 loc) · 9.76 KB
/
plugin_paths.rs
File metadata and controls
304 lines (280 loc) · 9.76 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright © 2025-2026 OpenVCS Contributors
// SPDX-License-Identifier: GPL-3.0-or-later
//! Path resolution helpers for installed and built-in plugins.
use log::{info, warn};
use std::{
env,
path::{Path, PathBuf},
sync::{
atomic::{AtomicBool, Ordering},
OnceLock,
},
};
/// File name expected for plugin manifests.
pub const PLUGIN_MANIFEST_NAME: &str = "openvcs.plugin.json";
/// Directory name used for built-in plugin bundles.
pub const BUILT_IN_PLUGINS_DIR_NAME: &str = "built-in-plugins";
/// Directory name used for the bundled Node runtime.
pub const NODE_RUNTIME_DIR_NAME: &str = "node-runtime";
/// Known Linux package directory names owned by OpenVCS.
#[cfg(target_os = "linux")]
const LINUX_PACKAGE_APP_DIR_NAMES: [&str; 2] = ["OpenVCS", "openvcs"];
// If the Tauri runtime resolves a resource directory at startup, we store
// it here so plugin discovery can include resources embedded in the
// application bundle.
static RESOURCE_DIR: OnceLock<PathBuf> = OnceLock::new();
static NODE_RUNTIME_RESOURCE_DIR: OnceLock<PathBuf> = OnceLock::new();
static NODE_EXECUTABLE: OnceLock<PathBuf> = OnceLock::new();
static LOGGED_BUILTIN_DIRS: AtomicBool = AtomicBool::new(false);
/// Returns the user-writable plugin installation directory.
///
/// # Returns
/// - The absolute config-directory path used to store installed plugins.
pub fn plugins_dir() -> PathBuf {
if let Some(pd) = crate::app_identity::project_dirs() {
pd.config_dir().join("plugins")
} else {
PathBuf::from("plugins")
}
}
/// Creates a directory path recursively and logs failures.
///
/// # Parameters
/// - `path`: Directory path to create if missing.
///
/// # Returns
/// - `()`.
pub fn ensure_dir(path: &Path) {
if let Err(err) = std::fs::create_dir_all(path) {
warn!("plugins: failed to create {}: {}", path.display(), err);
}
}
/// Appends a path when it has not already been recorded.
///
/// # Parameters
/// - `paths`: Candidate path list.
/// - `path`: Candidate to append.
///
/// # Returns
/// - `()`.
fn push_unique_path(paths: &mut Vec<PathBuf>, path: PathBuf) {
if !paths.iter().any(|existing| existing == &path) {
paths.push(path);
}
}
/// Returns whether a Linux package app directory name belongs to OpenVCS.
///
/// # Parameters
/// - `app_dir`: Candidate packaged app directory.
///
/// # Returns
/// - `true` when the directory name matches an OpenVCS-owned package layout.
#[cfg(target_os = "linux")]
fn is_known_linux_package_app_dir(app_dir: &Path) -> bool {
app_dir
.file_name()
.and_then(|name| name.to_str())
.is_some_and(|name| LINUX_PACKAGE_APP_DIR_NAMES.contains(&name))
}
/// Adds Linux package resource roots installed under sibling `lib` directories.
///
/// Only OpenVCS-owned app directories are accepted so resource lookup does not
/// wander into unrelated package trees that happen to contain the same
/// subdirectory names.
///
/// # Parameters
/// - `paths`: Candidate base directory list.
/// - `exe_dir`: Directory containing the executable.
/// - `resource_dir_name`: Resource subdirectory to check for.
///
/// # Returns
/// - `()`.
#[cfg(target_os = "linux")]
fn push_linux_package_resource_bases(
paths: &mut Vec<PathBuf>,
exe_dir: &Path,
resource_dir_name: &str,
) {
let Some(prefix_dir) = exe_dir.parent() else {
return;
};
for lib_dir_name in ["lib", "lib64"] {
let lib_dir = prefix_dir.join(lib_dir_name);
let entries = match std::fs::read_dir(&lib_dir) {
Ok(entries) => entries,
Err(err) => {
log::trace!(
"plugins: skipping Linux package resource root {}: {}",
lib_dir.display(),
err
);
continue;
}
};
for entry in entries.flatten() {
let app_dir = entry.path();
if !app_dir.is_dir() {
continue;
}
if !is_known_linux_package_app_dir(&app_dir) {
log::trace!(
"plugins: skipping non-OpenVCS Linux package dir {}",
app_dir.display()
);
continue;
}
if app_dir.join(resource_dir_name).is_dir() {
push_unique_path(paths, app_dir);
}
}
}
}
/// Returns candidate resource base directories derived from the executable path.
///
/// # Parameters
/// - `resource_dir_name`: Resource directory that should exist under packaged roots.
///
/// # Returns
/// - Candidate base directories that may contain the requested resource.
fn bundled_resource_base_dirs(resource_dir_name: &str) -> Vec<PathBuf> {
let mut candidates: Vec<PathBuf> = Vec::new();
#[cfg(not(target_os = "linux"))]
let _ = resource_dir_name;
if let Ok(exe) = env::current_exe() {
if let Some(dir) = exe.parent() {
push_unique_path(&mut candidates, dir.join("resources"));
push_unique_path(&mut candidates, dir.to_path_buf());
if let Some(target_dir) = dir.parent() {
push_unique_path(&mut candidates, target_dir.join("openvcs"));
#[cfg(target_os = "macos")]
push_unique_path(&mut candidates, target_dir.join("Resources"));
}
#[cfg(target_os = "linux")]
push_linux_package_resource_bases(&mut candidates, dir, resource_dir_name);
push_unique_path(
&mut candidates,
dir.join("_up_").join("target").join("openvcs"),
);
}
}
if let Some(resource_dir) = RESOURCE_DIR.get() {
push_unique_path(&mut candidates, resource_dir.clone());
}
candidates
}
/// Returns discovered built-in plugin directories that currently exist.
///
/// # Returns
/// - Existing filesystem directories searched for built-in plugins.
pub fn built_in_plugin_dirs() -> Vec<PathBuf> {
let mut candidates: Vec<PathBuf> = Vec::new();
for base_dir in bundled_resource_base_dirs(BUILT_IN_PLUGINS_DIR_NAME) {
push_unique_path(&mut candidates, base_dir.join(BUILT_IN_PLUGINS_DIR_NAME));
}
// On Windows installers the per-user AppData Local folder is commonly
// used for application data. If present, include
// %LOCALAPPDATA%/OpenVCS/built-in-plugins as a candidate so built-in
// plugins shipped by the installer are discovered.
#[cfg(target_os = "windows")]
if let Ok(local_appdata) = env::var("LOCALAPPDATA") {
push_unique_path(
&mut candidates,
PathBuf::from(local_appdata)
.join("OpenVCS")
.join(BUILT_IN_PLUGINS_DIR_NAME),
);
}
let mut seen = std::collections::HashSet::new();
let result: Vec<PathBuf> = candidates
.into_iter()
.filter_map(|path| {
if !seen.insert(path.clone()) {
return None;
}
if path.is_dir() {
Some(path)
} else {
None
}
})
.collect();
if LOGGED_BUILTIN_DIRS
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
.is_ok()
{
if result.is_empty() {
info!("plugins: no built-in plugin directories found");
} else {
let joined = result
.iter()
.map(|p| p.display().to_string())
.collect::<Vec<_>>()
.join(", ");
info!("plugins: checked built-in plugin directories: {}", joined);
}
}
result
}
/// Returns candidate bundled Node executable paths.
///
/// # Returns
/// - Ordered candidate paths for the bundled Node binary.
pub fn bundled_node_candidate_paths() -> Vec<PathBuf> {
let node_name = if cfg!(windows) { "node.exe" } else { "node" };
let mut candidates = Vec::new();
if let Some(node_runtime_dir) = NODE_RUNTIME_RESOURCE_DIR.get() {
push_unique_path(&mut candidates, node_runtime_dir.join(node_name));
}
for base_dir in bundled_resource_base_dirs(NODE_RUNTIME_DIR_NAME) {
push_unique_path(
&mut candidates,
base_dir.join(NODE_RUNTIME_DIR_NAME).join(node_name),
);
}
candidates
}
/// Set the resolved Tauri resource directory so the plugin discovery can
/// include resources embedded inside the application bundle. Call this from
/// the Tauri `setup` callback with `app.path().resolve("built-in-plugins", BaseDirectory::Resource)`.
///
/// # Parameters
/// - `path`: Resource directory path resolved by Tauri at runtime.
///
/// # Returns
/// - `()`.
pub fn set_resource_dir(path: PathBuf) {
// it's fine if this fails to set more than once; first set wins.
let _ = RESOURCE_DIR.set(path);
}
/// Sets the exact `node-runtime` resource directory resolved by Tauri.
///
/// This is tracked separately from the generic resource base because packaged
/// builds may resolve built-in plugin bundles and `node-runtime` from different
/// roots. Callers should provide the resolved `node-runtime` directory itself.
///
/// # Parameters
/// - `path`: Exact runtime directory path resolved by Tauri at runtime.
///
/// # Returns
/// - `()`.
pub fn set_node_runtime_resource_dir(path: PathBuf) {
let _ = NODE_RUNTIME_RESOURCE_DIR.set(path);
}
/// Sets the resolved bundled Node executable path used by plugin runtime.
///
/// # Parameters
/// - `path`: Absolute path to the bundled Node binary.
///
/// # Returns
/// - `()`.
pub fn set_node_executable_path(path: PathBuf) {
let _ = NODE_EXECUTABLE.set(path);
}
/// Returns the bundled Node executable path when configured.
///
/// # Returns
/// - `Some(PathBuf)` when a bundled runtime was resolved.
/// - `None` when no bundled runtime has been resolved yet.
pub fn node_executable_path() -> Option<PathBuf> {
NODE_EXECUTABLE.get().cloned()
}