crate_plugin_kit/store/
install.rs1use std::path::Path;
9
10use crate::cache::{Index, InstallSource};
11use crate::error::{KitError, KitResult};
12use crate::install::{self, build_host, prebuilt, Installed};
13use crate::manifest::PluginManifest;
14
15use super::CratePluginKit;
16
17impl<T> CratePluginKit<T> {
18 pub fn install(&self, name: &str, version: Option<&str>) -> KitResult<Installed> {
29 let crate_name = self.cfg.normalize_crate_name(name);
30
31 let _lock = self.lock()?;
32
33 let dir = self.paths.plugin_dir(&crate_name);
34 if dir.is_dir() {
35 return Err(KitError::AlreadyInstalled {
36 name: crate_name,
37 path: dir,
38 });
39 }
40
41 let version = self.resolve_version(&crate_name, version)?;
42 let installed = self.install_locked(&crate_name, &version)?;
43 self.record(&installed)?;
44 Ok(installed)
45 }
46
47 pub fn install_from_path(&self, plugin_dir: impl AsRef<Path>) -> KitResult<Installed> {
66 let dir = plugin_dir.as_ref().canonicalize()?;
67
68 let _lock = self.lock()?;
69
70 if !dir.join("Cargo.toml").is_file() {
73 return Err(KitError::NoManifest { dir });
74 }
75 let manifest = PluginManifest::read(&dir.join(&self.cfg.manifest_name))?;
76
77 let crate_name = self.cfg.normalize_crate_name(&manifest.plugin.name);
78 let dir_installed = self.paths.plugin_dir(&crate_name);
79 if dir_installed.is_dir() {
80 return Err(KitError::AlreadyInstalled {
81 name: crate_name,
82 path: dir_installed,
83 });
84 }
85
86 let installed = build_host::install_from_path(
87 self.config(),
88 self.paths(),
89 &crate_name,
90 &manifest.plugin.version,
91 &dir,
92 )?;
93 self.record(&installed)?;
94 Ok(installed)
95 }
96
97 pub fn update(&self, name: &str, version: Option<&str>) -> KitResult<Installed> {
103 let crate_name = self.cfg.normalize_crate_name(name);
104
105 let _lock = self.lock()?;
106
107 let recorded = Index::load(&self.paths.index_file)
110 .get(&crate_name)
111 .map(|entry| entry.source.clone());
112 if let Some(InstallSource::Local { path }) = recorded {
113 let manifest = PluginManifest::read(&path.join(&self.cfg.manifest_name))?;
114 let installed = build_host::install_from_path(
115 self.config(),
116 self.paths(),
117 &crate_name,
118 &manifest.plugin.version,
119 &path,
120 )?;
121 self.record(&installed)?;
122 return Ok(installed);
123 }
124
125 let version = self.resolve_version(&crate_name, version)?;
126 let installed = self.install_locked(&crate_name, &version)?;
127 self.record(&installed)?;
128 Ok(installed)
129 }
130
131 pub fn uninstall(&self, name: &str) -> KitResult<()> {
141 let crate_name = self.cfg.normalize_crate_name(name);
142
143 let _lock = self.lock()?;
144
145 let dir = self.paths.plugin_dir(&crate_name);
146 if !dir.is_dir() {
147 return Err(KitError::NotInstalled { name: crate_name });
148 }
149
150 if self.loaded.borrow().contains(&crate_name) {
151 return Err(KitError::PluginInUse {
152 name: crate_name.clone(),
153 });
154 }
155
156 std::fs::remove_dir_all(&dir)?;
157
158 let mut idx = Index::load(&self.paths.index_file);
159 idx.remove(&crate_name);
160 idx.save(&self.paths.index_file)?;
161
162 Ok(())
163 }
164
165 fn install_locked(&self, crate_name: &str, version: &str) -> KitResult<Installed> {
167 install::remove_dir_if_exists(&self.paths.plugin_dir(crate_name))?;
169
170 if self.cfg.prefer_prebuilt {
171 match prebuilt::try_install(
172 self.config(),
173 self.paths(),
174 self.registry(),
175 crate_name,
176 version,
177 ) {
178 Ok(Some(installed)) => return Ok(installed),
179 Ok(None) => {}
182 Err(_) => {}
186 }
187 }
188
189 build_host::install(self.config(), self.paths(), crate_name, version)
190 }
191}
192
193#[cfg(test)]
194mod tests {
195 use super::super::tests::{kit, place_plugin};
196 use super::*;
197
198 #[test]
199 fn uninstall_reports_a_plugin_that_is_not_installed() {
200 let tmp = tempfile::tempdir().unwrap();
201 let k = kit(&tmp.path().join("store"));
202
203 assert!(matches!(
204 k.uninstall("nope"),
205 Err(KitError::NotInstalled { .. })
206 ));
207 }
208
209 #[test]
210 fn uninstall_removes_the_directory() {
211 let tmp = tempfile::tempdir().unwrap();
212 let root = tmp.path().join("store");
213 let k = kit(&root);
214 let dir = place_plugin(&root, "myapp-plugin-foo");
215
216 assert!(dir.is_dir());
217 k.uninstall("foo").expect("should uninstall");
218 assert!(!dir.exists(), "the directory should have been deleted");
219 assert!(k.list().unwrap().is_empty());
220 }
221
222 #[test]
224 fn install_from_path_needs_a_cargo_manifest() {
225 let tmp = tempfile::tempdir().unwrap();
226 let root = tmp.path().join("store");
227 let k = kit(&root);
228
229 let checkout = tmp.path().join("checkout");
230 std::fs::create_dir_all(&checkout).unwrap();
231
232 match k.install_from_path(&checkout) {
233 Err(KitError::NoManifest { dir }) => {
234 assert!(dir.ends_with("checkout"), "{dir:?}")
235 }
236 other => panic!("expected NoManifest, got {other:?}"),
237 }
238 }
239
240 #[test]
243 fn install_from_path_needs_the_manifest() {
244 let tmp = tempfile::tempdir().unwrap();
245 let root = tmp.path().join("store");
246 let k = kit(&root);
247
248 let checkout = tmp.path().join("checkout");
249 std::fs::create_dir_all(checkout.join("src")).unwrap();
250 std::fs::write(
251 checkout.join("Cargo.toml"),
252 "[package]\nname = \"myapp-plugin-foo\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
253 )
254 .unwrap();
255
256 assert!(
257 k.install_from_path(&checkout).is_err(),
258 "a crate with no manifest is not a plugin checkout"
259 );
260 }
261
262 #[test]
264 fn install_from_path_refuses_when_already_installed() {
265 let tmp = tempfile::tempdir().unwrap();
266 let root = tmp.path().join("store");
267 let k = kit(&root);
268 place_plugin(&root, "myapp-plugin-foo");
269
270 let checkout = tmp.path().join("checkout");
271 std::fs::create_dir_all(checkout.join("src")).unwrap();
272 std::fs::write(
273 checkout.join("Cargo.toml"),
274 "[package]\nname = \"myapp-plugin-foo\"\nversion = \"0.1.0\"\nedition = \"2021\"\n",
275 )
276 .unwrap();
277 std::fs::write(
278 checkout.join("myapp-plugin.toml"),
279 super::super::tests::MANIFEST,
280 )
281 .unwrap();
282
283 match k.install_from_path(&checkout) {
284 Err(KitError::AlreadyInstalled { name, .. }) => {
285 assert_eq!(name, "myapp-plugin-foo")
286 }
287 other => panic!("expected AlreadyInstalled, got {other:?}"),
288 }
289 }
290
291 #[test]
297 fn install_refuses_when_already_installed() {
298 let tmp = tempfile::tempdir().unwrap();
299 let root = tmp.path().join("store");
300 let k = kit(&root);
301 place_plugin(&root, "myapp-plugin-foo");
302
303 match k.install("foo", Some("0.1.0")) {
304 Err(KitError::AlreadyInstalled { name, .. }) => {
305 assert_eq!(name, "myapp-plugin-foo")
306 }
307 other => panic!("expected AlreadyInstalled, got {other:?}"),
308 }
309 }
310}