-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiscovery.rs
More file actions
213 lines (183 loc) · 6.31 KB
/
discovery.rs
File metadata and controls
213 lines (183 loc) · 6.31 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
use std::path::PathBuf;
use crate::plugin::PluginManifest;
/// Where the plugin was discovered
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PluginSource {
/// Found in the plugins config directory
PluginDir,
/// Found on PATH via `which`
Path,
}
/// A discovered plugin with optional manifest
#[derive(Debug, Clone)]
pub struct DiscoveredPlugin {
pub name: String,
pub manifest: Option<PluginManifest>,
pub source: PluginSource,
}
/// Discovers plugins from the config directory and PATH
pub struct PluginDiscovery {
plugins_dir: PathBuf,
}
impl PluginDiscovery {
#[must_use]
pub const fn new(plugins_dir: PathBuf) -> Self {
Self { plugins_dir }
}
/// Return the default plugins directory
///
/// # Errors
///
/// Returns an error if the base directories cannot be determined
pub fn default_dir() -> anyhow::Result<PathBuf> {
let base = directories::BaseDirs::new()
.ok_or_else(|| anyhow::anyhow!("cannot determine base directories"))?;
Ok(base.config_dir().join("omni").join("plugins"))
}
/// Find a plugin by name
///
/// Checks the plugin directory first, then falls back to PATH
///
/// # Errors
///
/// Returns an error if a manifest file exists but cannot be parsed
pub fn find(&self, name: &str) -> anyhow::Result<Option<DiscoveredPlugin>> {
let manifest_path = self.plugins_dir.join(name).join("plugin.toml");
if manifest_path.exists() {
let manifest = PluginManifest::from_file(&manifest_path)?;
return Ok(Some(DiscoveredPlugin {
name: name.to_string(),
manifest: Some(manifest),
source: PluginSource::PluginDir,
}));
}
// Fall back to PATH with `omni-` prefix convention (like git/cargo)
let prefixed = format!("omni-{name}");
if which::which(&prefixed).is_ok() {
return Ok(Some(DiscoveredPlugin {
name: name.to_string(),
manifest: None,
source: PluginSource::Path,
}));
}
Ok(None)
}
/// Discover all plugins in the plugins directory
///
/// # Errors
///
/// Returns an error if a manifest file exists but cannot be parsed
pub fn discover_all(&self) -> anyhow::Result<Vec<DiscoveredPlugin>> {
if !self.plugins_dir.exists() {
return Ok(Vec::new());
}
let mut plugins = Vec::new();
let entries = std::fs::read_dir(&self.plugins_dir)?;
for entry in entries {
let entry = entry?;
if !entry.file_type()?.is_dir() {
continue;
}
let name = entry.file_name().to_string_lossy().to_string();
let manifest_path = entry.path().join("plugin.toml");
if manifest_path.exists() {
let manifest = PluginManifest::from_file(&manifest_path)?;
plugins.push(DiscoveredPlugin {
name,
manifest: Some(manifest),
source: PluginSource::PluginDir,
});
}
}
plugins.sort_by(|a, b| a.name.cmp(&b.name));
Ok(plugins)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn write_manifest(dir: &std::path::Path, name: &str, content: &str) {
let plugin_dir = dir.join(name);
std::fs::create_dir_all(&plugin_dir).unwrap();
std::fs::write(plugin_dir.join("plugin.toml"), content).unwrap();
}
const EDEN_MANIFEST: &str = r#"
name = "eden"
version = "0.1.0"
description = "Developer onboarding preflight checks"
type = "bin"
bin = "eden"
"#;
const RUNA_MANIFEST: &str = r#"
name = "runa"
version = "0.1.0"
description = "Streamlined project management"
type = "api"
endpoint = "https://runa.omni.dev/api"
"#;
#[test]
fn discover_from_plugins_dir() {
let dir = tempfile::TempDir::new().unwrap();
write_manifest(dir.path(), "eden", EDEN_MANIFEST);
write_manifest(dir.path(), "runa", RUNA_MANIFEST);
let discovery = PluginDiscovery::new(dir.path().to_path_buf());
let plugins = discovery.discover_all().unwrap();
assert_eq!(plugins.len(), 2);
}
#[test]
fn find_by_name_from_dir() {
let dir = tempfile::TempDir::new().unwrap();
write_manifest(dir.path(), "eden", EDEN_MANIFEST);
let discovery = PluginDiscovery::new(dir.path().to_path_buf());
let plugin = discovery.find("eden").unwrap();
assert!(plugin.is_some());
let p = plugin.unwrap();
assert_eq!(p.name, "eden");
assert!(p.manifest.is_some());
assert_eq!(p.source, PluginSource::PluginDir);
}
#[test]
fn find_not_installed_returns_none() {
let dir = tempfile::TempDir::new().unwrap();
let discovery = PluginDiscovery::new(dir.path().to_path_buf());
let plugin = discovery.find("nonexistent").unwrap();
assert!(plugin.is_none());
}
#[test]
fn find_does_not_fall_back_to_unprefixed_path() {
let dir = tempfile::TempDir::new().unwrap();
let discovery = PluginDiscovery::new(dir.path().to_path_buf());
// "ls" exists on PATH but "omni-ls" does not
let plugin = discovery.find("ls").unwrap();
assert!(plugin.is_none());
}
#[test]
fn plugin_dir_takes_priority_over_path() {
let dir = tempfile::TempDir::new().unwrap();
let manifest = r#"
name = "ls"
version = "0.1.0"
description = "Custom ls plugin"
type = "bin"
bin = "ls"
"#;
write_manifest(dir.path(), "ls", manifest);
let discovery = PluginDiscovery::new(dir.path().to_path_buf());
let plugin = discovery.find("ls").unwrap();
assert!(plugin.is_some());
assert_eq!(plugin.unwrap().source, PluginSource::PluginDir);
}
#[test]
fn empty_plugins_dir_returns_empty() {
let dir = tempfile::TempDir::new().unwrap();
let discovery = PluginDiscovery::new(dir.path().to_path_buf());
let plugins = discovery.discover_all().unwrap();
assert!(plugins.is_empty());
}
#[test]
fn nonexistent_dir_returns_empty() {
let discovery = PluginDiscovery::new(PathBuf::from("/nonexistent/plugins"));
let plugins = discovery.discover_all().unwrap();
assert!(plugins.is_empty());
}
}