Skip to main content

hx_plugins/
loader.rs

1//! Plugin discovery and loading.
2
3use crate::config::PluginConfig;
4use crate::error::{PluginError, Result};
5use std::path::{Path, PathBuf};
6use tracing::{debug, info};
7
8/// Discovered plugin information.
9#[derive(Debug, Clone)]
10pub struct DiscoveredPlugin {
11    /// Path to the plugin file.
12    pub path: PathBuf,
13
14    /// Plugin name (filename without extension).
15    pub name: String,
16
17    /// Whether this is a project-local plugin.
18    pub is_local: bool,
19}
20
21impl DiscoveredPlugin {
22    /// Create a new discovered plugin.
23    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
38/// Discover plugins in the configured paths.
39pub 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        // Is this a local (project) plugin path?
53        let is_local = idx == 0; // First path is always project-local
54
55        // Find all .scm files in this directory
56        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
74/// Find a specific plugin by name.
75pub 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    // Add .scm extension if not present
83    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
100/// Check if the plugins directory exists for a project.
101pub fn plugins_dir_exists(project_root: &Path) -> bool {
102    project_root.join(".hx").join("plugins").exists()
103}
104
105/// Create the plugins directory for a project.
106pub 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
124/// Get information about plugin directories.
125pub struct PluginPaths {
126    /// Project-local plugins directory.
127    pub local: PathBuf,
128    /// Global plugins directory.
129    pub global: Option<PathBuf>,
130    /// Custom paths from configuration.
131    pub custom: Vec<PathBuf>,
132}
133
134impl PluginPaths {
135    /// Get plugin paths for a project.
136    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    /// Check which paths exist.
159    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}