crate_plugin_kit/store/
list.rs1use std::path::{Path, PathBuf};
7
8use crate::cache::Index;
9use crate::error::KitResult;
10use crate::manifest::PluginManifest;
11use crate::registry::{CrateInfo, CrateSummary};
12
13use super::{CratePluginKit, PluginInfo};
14
15impl<T> CratePluginKit<T> {
16 pub fn list(&self) -> KitResult<Vec<PluginInfo>> {
22 let idx = Index::load(&self.paths.index_file);
23 let mut out = Vec::new();
24
25 let entries = match std::fs::read_dir(&self.paths.plugins) {
26 Ok(e) => e,
27 Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(out),
28 Err(e) => return Err(e.into()),
29 };
30
31 for entry in entries {
32 let entry = entry?;
33 if !entry.file_type()?.is_dir() {
34 continue;
35 }
36 let crate_name = entry.file_name().to_string_lossy().into_owned();
37
38 let manifest_path = entry.path().join(&self.cfg.manifest_name);
41 let Ok(manifest) = PluginManifest::read(&manifest_path) else {
42 continue;
43 };
44
45 let source = idx.get(&crate_name).map(|e| e.source).unwrap_or_default();
46
47 out.push(PluginInfo {
48 name: manifest.plugin.name.clone(),
49 crate_name,
50 version: manifest.plugin.version.clone(),
51 family: manifest.plugin.family.clone(),
52 abi: manifest.plugin.abi,
53 dir: entry.path(),
54 source,
55 });
56 }
57
58 out.sort_by(|a, b| a.crate_name.cmp(&b.crate_name));
59 Ok(out)
60 }
61
62 pub fn manifest_of(&self, name: &str) -> KitResult<PluginManifest> {
64 let crate_name = self.cfg.normalize_crate_name(name);
65 let path = self
66 .paths
67 .manifest_path(&crate_name, &self.cfg.manifest_name);
68 PluginManifest::read(&path)
69 }
70
71 pub fn resolve(&self, name: &str, relative: impl AsRef<Path>) -> PathBuf {
73 let crate_name = self.cfg.normalize_crate_name(name);
74 self.paths.plugin_dir(&crate_name).join(relative)
75 }
76
77 pub fn search(&self, keyword: &str, limit: usize) -> KitResult<Vec<CrateSummary>> {
79 self.registry.search(keyword, limit)
80 }
81
82 pub fn view(&self, name: &str) -> KitResult<Option<CrateInfo>> {
84 let crate_name = self.cfg.normalize_crate_name(name);
85 self.registry.view(&crate_name)
86 }
87}
88
89#[cfg(test)]
90mod tests {
91 use super::super::tests::{kit, place_plugin};
92 use crate::cache::InstallSource;
93 use crate::error::KitError;
94
95 #[test]
96 fn a_fresh_store_lists_nothing() {
97 let tmp = tempfile::tempdir().unwrap();
98 let k = kit(&tmp.path().join("store"));
99 assert!(k.list().unwrap().is_empty());
100 }
101
102 #[test]
105 fn list_tolerates_a_missing_plugins_directory() {
106 let tmp = tempfile::tempdir().unwrap();
107 let root = tmp.path().join("store");
108 let k = kit(&root);
109
110 std::fs::remove_dir_all(&root).unwrap();
111 assert!(k.list().unwrap().is_empty());
112 }
113
114 #[test]
118 fn list_reads_manifests_from_disk_not_the_cache() {
119 let tmp = tempfile::tempdir().unwrap();
120 let root = tmp.path().join("store");
121 let k = kit(&root);
122
123 place_plugin(&root, "myapp-plugin-foo");
124
125 let all = k.list().unwrap();
126 assert_eq!(all.len(), 1, "the cache is empty but the disk has a plugin");
127 let info = &all[0];
128 assert_eq!(info.name, "foo");
129 assert_eq!(info.crate_name, "myapp-plugin-foo");
130 assert_eq!(info.version, "0.1.0");
131 assert_eq!(info.abi, Some(1));
132 assert_eq!(info.family.as_deref(), Some("node"));
133 assert_eq!(info.source, InstallSource::BuildHost);
135 }
136
137 #[test]
139 fn list_skips_a_directory_with_a_broken_manifest() {
140 let tmp = tempfile::tempdir().unwrap();
141 let root = tmp.path().join("store");
142 let k = kit(&root);
143
144 place_plugin(&root, "myapp-plugin-good");
145 let bad = root.join("plugins").join("myapp-plugin-bad");
146 std::fs::create_dir_all(&bad).unwrap();
147 std::fs::write(bad.join("myapp-plugin.toml"), "this is not toml").unwrap();
148
149 let empty = root.join("plugins").join("myapp-plugin-empty");
151 std::fs::create_dir_all(&empty).unwrap();
152
153 let all = k.list().unwrap();
154 assert_eq!(all.len(), 1, "only the good one should be listed: {all:?}");
155 assert_eq!(all[0].crate_name, "myapp-plugin-good");
156 }
157
158 #[test]
159 fn list_is_sorted_by_crate_name() {
160 let tmp = tempfile::tempdir().unwrap();
161 let root = tmp.path().join("store");
162 let k = kit(&root);
163
164 for name in ["myapp-plugin-c", "myapp-plugin-a", "myapp-plugin-b"] {
165 place_plugin(&root, name);
166 }
167
168 let names: Vec<_> = k
169 .list()
170 .unwrap()
171 .into_iter()
172 .map(|i| i.crate_name)
173 .collect();
174 assert_eq!(
175 names,
176 vec!["myapp-plugin-a", "myapp-plugin-b", "myapp-plugin-c"]
177 );
178 }
179
180 #[test]
181 fn manifest_of_reads_the_plugin() {
182 let tmp = tempfile::tempdir().unwrap();
183 let root = tmp.path().join("store");
184 let k = kit(&root);
185 place_plugin(&root, "myapp-plugin-foo");
186
187 let m = k
188 .manifest_of("foo")
189 .expect("the short name should work too");
190 assert_eq!(m.plugin.name, "foo");
191 assert!(m.extra.contains_key("detect"), "the host's section is kept");
192 }
193
194 #[test]
195 fn manifest_of_reports_a_missing_plugin() {
196 let tmp = tempfile::tempdir().unwrap();
197 let k = kit(&tmp.path().join("store"));
198 assert!(matches!(
199 k.manifest_of("nope"),
200 Err(KitError::ManifestRead { .. })
201 ));
202 }
203
204 #[test]
205 fn resolve_points_into_the_plugin_directory() {
206 let tmp = tempfile::tempdir().unwrap();
207 let root = tmp.path().join("store");
208 let k = kit(&root);
209
210 assert_eq!(
211 k.resolve("foo", "extra.txt"),
212 root.join("plugins")
213 .join("myapp-plugin-foo")
214 .join("extra.txt")
215 );
216 assert_eq!(k.resolve("foo", "x"), k.resolve("myapp-plugin-foo", "x"));
218 }
219}