1use crate::config::PluginConfig;
4use crate::error::{PluginError, Result};
5use std::path::{Path, PathBuf};
6use tracing::{debug, info};
7
8#[derive(Debug, Clone)]
10pub struct DiscoveredPlugin {
11 pub path: PathBuf,
13
14 pub name: String,
16
17 pub is_local: bool,
19}
20
21impl DiscoveredPlugin {
22 pub fn new(path: PathBuf, is_local: bool) -> Self {
24 let name = path
25 .file_stem()
26 .and_then(|s| s.to_str())
27 .unwrap_or("unknown")
28 .to_string();
29
30 DiscoveredPlugin {
31 path,
32 name,
33 is_local,
34 }
35 }
36}
37
38pub fn discover_plugins(
40 config: &PluginConfig,
41 project_root: &Path,
42) -> Result<Vec<DiscoveredPlugin>> {
43 let mut plugins = Vec::new();
44 let paths = config.all_paths(project_root);
45
46 for (idx, base_path) in paths.iter().enumerate() {
47 if !base_path.exists() {
48 debug!("Plugin path does not exist: {}", base_path.display());
49 continue;
50 }
51
52 let is_local = idx == 0; let pattern = base_path.join("*.scm");
57 match glob::glob(&pattern.to_string_lossy()) {
58 Ok(entries) => {
59 for entry in entries.flatten() {
60 debug!("Discovered plugin: {}", entry.display());
61 plugins.push(DiscoveredPlugin::new(entry, is_local));
62 }
63 }
64 Err(e) => {
65 debug!("Failed to glob plugins in {}: {}", base_path.display(), e);
66 }
67 }
68 }
69
70 info!("Discovered {} plugins", plugins.len());
71 Ok(plugins)
72}
73
74pub fn find_plugin(
76 name: &str,
77 config: &PluginConfig,
78 project_root: &Path,
79) -> Result<DiscoveredPlugin> {
80 let paths = config.all_paths(project_root);
81
82 let filename = if name.ends_with(".scm") {
84 name.to_string()
85 } else {
86 format!("{}.scm", name)
87 };
88
89 for (idx, base_path) in paths.iter().enumerate() {
90 let plugin_path = base_path.join(&filename);
91 if plugin_path.exists() {
92 let is_local = idx == 0;
93 return Ok(DiscoveredPlugin::new(plugin_path, is_local));
94 }
95 }
96
97 Err(PluginError::not_found(PathBuf::from(name)))
98}
99
100pub fn plugins_dir_exists(project_root: &Path) -> bool {
102 project_root.join(".hx").join("plugins").exists()
103}
104
105pub fn create_plugins_dir(project_root: &Path) -> Result<PathBuf> {
107 let plugins_dir = project_root.join(".hx").join("plugins");
108
109 if !plugins_dir.exists() {
110 std::fs::create_dir_all(&plugins_dir).map_err(|e| {
111 PluginError::io(
112 format!(
113 "failed to create plugins directory: {}",
114 plugins_dir.display()
115 ),
116 e,
117 )
118 })?;
119 }
120
121 Ok(plugins_dir)
122}
123
124pub struct PluginPaths {
126 pub local: PathBuf,
128 pub global: Option<PathBuf>,
130 pub custom: Vec<PathBuf>,
132}
133
134impl PluginPaths {
135 pub fn for_project(config: &PluginConfig, project_root: &Path) -> Self {
137 let local = project_root.join(".hx").join("plugins");
138
139 let global =
140 directories::BaseDirs::new().map(|dirs| dirs.config_dir().join("hx").join("plugins"));
141
142 let custom = config
143 .paths
144 .iter()
145 .map(|p| {
146 let expanded = shellexpand(p);
147 PathBuf::from(expanded)
148 })
149 .collect();
150
151 PluginPaths {
152 local,
153 global,
154 custom,
155 }
156 }
157
158 pub fn existing(&self) -> Vec<&PathBuf> {
160 let mut paths = Vec::new();
161
162 if self.local.exists() {
163 paths.push(&self.local);
164 }
165
166 for path in &self.custom {
167 if path.exists() {
168 paths.push(path);
169 }
170 }
171
172 if let Some(ref global) = self.global
173 && global.exists()
174 {
175 paths.push(global);
176 }
177
178 paths
179 }
180}
181
182fn shellexpand(path: &str) -> String {
183 if path.starts_with("~/")
184 && let Some(dirs) = directories::BaseDirs::new()
185 {
186 return format!("{}{}", dirs.home_dir().display(), &path[1..]);
187 }
188 path.to_string()
189}
190
191#[cfg(test)]
192mod tests {
193 use super::*;
194 use tempfile::tempdir;
195
196 #[test]
197 fn test_discover_plugins_empty() {
198 let temp = tempdir().unwrap();
199 let config = PluginConfig::default();
200 let plugins = discover_plugins(&config, temp.path()).unwrap();
201 assert!(plugins.is_empty());
202 }
203
204 #[test]
205 fn test_create_plugins_dir() {
206 let temp = tempdir().unwrap();
207 let plugins_dir = create_plugins_dir(temp.path()).unwrap();
208 assert!(plugins_dir.exists());
209 assert!(plugins_dir.ends_with(".hx/plugins"));
210 }
211}