Skip to content

Commit 2abb02d

Browse files
Use LocalJarBoundary in uninstall use case
1 parent b80fd52 commit 2abb02d

File tree

4 files changed

+67
-31
lines changed

4 files changed

+67
-31
lines changed

src/main/java/org/hydev/mcpm/client/uninstall/UninstallBoundary.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public interface UninstallBoundary {
99
/**
1010
* Uninstalls plugin based on its given name
1111
*
12-
* @param name given name of plugin
12+
* @param input Uninstall input
1313
*/
14-
void uninstall(String name) throws PluginNotFoundException;
14+
UninstallResult uninstall(UninstallInput input) throws PluginNotFoundException;
1515
}

src/main/java/org/hydev/mcpm/client/uninstall/UninstallInput.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
package org.hydev.mcpm.client.uninstall;
22

3-
import org.hydev.mcpm.client.database.inputs.SearchPackagesType;
4-
import org.hydev.mcpm.client.injector.PluginLoader;
5-
63
/**
74
* Uninstall Plugin input
85
*
96
* @param name Plugin name
10-
* @param ulb PluginLoader object
11-
* @param spType SearchPackagesType
127
* @param recursive remove dependencies or not
138
*/
149
public record UninstallInput(
1510
String name,
16-
PluginLoader ulb,
17-
SearchPackagesType spType,
1811
boolean recursive
1912
) { }
2013

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.hydev.mcpm.client.uninstall;
2+
3+
/**
4+
* Result for uninstalling
5+
*
6+
* @author Anushka (https://github.com/aanushkasharma)
7+
* @since 2022-11-27
8+
*/
9+
public record UninstallResult(State state)
10+
{
11+
public enum State {
12+
FAILED_TO_DELETE,
13+
SUCCESS
14+
}
15+
}
Lines changed: 50 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package org.hydev.mcpm.client.uninstall;
22

3-
import org.hydev.mcpm.client.injector.PluginLoader;
3+
import org.hydev.mcpm.client.database.PluginTracker;
4+
import org.hydev.mcpm.client.injector.LocalJarBoundary;
45
import org.hydev.mcpm.client.injector.PluginNotFoundException;
6+
import org.hydev.mcpm.client.injector.UnloadBoundary;
57

68
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;
812

913

1014
/**
@@ -13,33 +17,57 @@
1317
* @author Anushka (https://github.com/aanushkasharma)
1418
*/
1519
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+
1630
@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) { }
2743
}
2844

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+
}
3151

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);
3455
}
3556

36-
File file = new File(plugins.get(0).toString());
57+
// 4. Remove manually installed flag from the tracker
58+
tracker.removeEntry(input.name());
3759

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+
}
4369
}
70+
71+
return new UninstallResult(SUCCESS);
4472
}
4573
}

0 commit comments

Comments
 (0)