|
1 | 1 | package org.hydev.mcpm.client.uninstall;
|
2 | 2 |
|
3 |
| -import org.hydev.mcpm.client.injector.PluginLoader; |
| 3 | +import org.hydev.mcpm.client.database.PluginTracker; |
| 4 | +import org.hydev.mcpm.client.injector.LocalJarBoundary; |
4 | 5 | import org.hydev.mcpm.client.injector.PluginNotFoundException;
|
| 6 | +import org.hydev.mcpm.client.injector.UnloadBoundary; |
5 | 7 |
|
6 | 8 | import java.io.File;
|
7 |
| -import java.util.Arrays; |
| 9 | + |
| 10 | +import static org.hydev.mcpm.client.uninstall.UninstallResult.State.FAILED_TO_DELETE; |
| 11 | +import static org.hydev.mcpm.client.uninstall.UninstallResult.State.SUCCESS; |
8 | 12 |
|
9 | 13 |
|
10 | 14 | /**
|
|
13 | 17 | * @author Anushka (https://github.com/aanushkasharma)
|
14 | 18 | */
|
15 | 19 | public class Uninstaller implements UninstallBoundary {
|
| 20 | + private final PluginTracker tracker; |
| 21 | + private final UnloadBoundary unloader; |
| 22 | + private final LocalJarBoundary jarFinder; |
| 23 | + |
| 24 | + public Uninstaller(PluginTracker tracker, UnloadBoundary unloader, LocalJarBoundary jarFinder) { |
| 25 | + this.tracker = tracker; |
| 26 | + this.unloader = unloader; |
| 27 | + this.jarFinder = jarFinder; |
| 28 | + } |
| 29 | + |
16 | 30 | @Override
|
17 |
| - public void uninstall(String name) throws PluginNotFoundException { |
18 |
| - PluginLoader pil = new PluginLoader(); |
19 |
| - pil.unloadPlugin(name); |
20 |
| - |
21 |
| - // Find plugin file |
22 |
| - // could be useful for removing dependencies (?) |
23 |
| - var pluginFolder = new File("plugins"); |
24 |
| - var files = pluginFolder.listFiles(); |
25 |
| - if (files == null) { |
26 |
| - files = new File[]{}; |
| 31 | + public UninstallResult uninstall(UninstallInput input) throws PluginNotFoundException { |
| 32 | + // 1. Unload plugin |
| 33 | + // This will throw PluginNotFoundException if the plugin isn't loaded. If it is loaded, |
| 34 | + // this will return the jar of the currently loaded plugin, which is the most precise. |
| 35 | + // Since people may still uninstall a plugin when it's not loaded, we ignore the not |
| 36 | + // found error here. |
| 37 | + File jar = null; |
| 38 | + if (unloader != null) { |
| 39 | + try { |
| 40 | + jar = unloader.unloadPlugin(input.name()); |
| 41 | + } |
| 42 | + catch (PluginNotFoundException ignored) { } |
27 | 43 | }
|
28 | 44 |
|
29 |
| - var plugins = Arrays.stream(files) |
30 |
| - .filter(plugin -> plugin.getName().endsWith(name + ".jar")).toList(); |
| 45 | + // 2. If it isn't loaded, find the plugin jar file in local file system |
| 46 | + if (jar == null) { |
| 47 | + // This will throw PluginNotFoundException when a plugin of the name in the file system |
| 48 | + // could not be found. |
| 49 | + jar = jarFinder.findJar(input.name()); |
| 50 | + } |
31 | 51 |
|
32 |
| - if (plugins.size() == 0) { |
33 |
| - throw new PluginNotFoundException("Plugin " + name + " not found."); |
| 52 | + // 3. Delete plugin jar |
| 53 | + if (!jar.delete()) { |
| 54 | + return new UninstallResult(FAILED_TO_DELETE); |
34 | 55 | }
|
35 | 56 |
|
36 |
| - File file = new File(plugins.get(0).toString()); |
| 57 | + // 4. Remove manually installed flag from the tracker |
| 58 | + tracker.removeEntry(input.name()); |
37 | 59 |
|
38 |
| - // Delete plugin |
39 |
| - if (file.delete()) { |
40 |
| - System.out.println("File deleted successfully"); |
41 |
| - } else { |
42 |
| - System.out.println("Failed to delete the file"); |
| 60 | + // 5. Remove orphan dependencies |
| 61 | + if (input.recursive()) { |
| 62 | + var orphans = tracker.listOrphanPlugins(false); |
| 63 | + for (var orphan : orphans) { |
| 64 | + try { |
| 65 | + uninstall(new UninstallInput(orphan, true)); |
| 66 | + } |
| 67 | + catch (PluginNotFoundException ignored) { } |
| 68 | + } |
43 | 69 | }
|
| 70 | + |
| 71 | + return new UninstallResult(SUCCESS); |
44 | 72 | }
|
45 | 73 | }
|
0 commit comments