Skip to content

Commit f492f62

Browse files
authored
[PR] Merge pull request #100 from CSC207-2022F-UofT/Uninstall-removingFiles
removing files in uninstall
2 parents 3cd8663 + 34a27a7 commit f492f62

File tree

14 files changed

+173
-34
lines changed

14 files changed

+173
-34
lines changed

src/main/java/org/hydev/mcpm/client/arguments/ParserFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.hydev.mcpm.client.arguments.parsers.*;
44
import org.hydev.mcpm.client.display.presenters.InstallPresenter;
55
import org.hydev.mcpm.client.display.presenters.SearchPresenter;
6+
import org.hydev.mcpm.client.display.presenters.UninstallPresenter;
67

78
import java.util.List;
89
import java.util.stream.Stream;
@@ -22,6 +23,7 @@ private ParserFactory() { }
2223
public static List<CommandParser> baseParsers(ControllerFactoryBoundary factory) {
2324
var searchPresenter = new SearchPresenter(factory.pageBoundary());
2425
var installPresenter = new InstallPresenter();
26+
var uninstallPresenter = new UninstallPresenter();
2527

2628
/*
2729
* Add general parsers to this list!
@@ -38,7 +40,7 @@ public static List<CommandParser> baseParsers(ControllerFactoryBoundary factory)
3840
new InstallParser(factory.installController(), installPresenter),
3941
new RefreshParser(factory.refreshController()),
4042
new PageParser(factory.pageBoundary()),
41-
new UninstallParser(factory.uninstallController()),
43+
new UninstallParser(factory.uninstallController(), uninstallPresenter),
4244
new UpdateParser(factory.updateController())
4345
);
4446
}

src/main/java/org/hydev/mcpm/client/arguments/parsers/UninstallParser.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
import net.sourceforge.argparse4j.inf.Namespace;
55
import net.sourceforge.argparse4j.inf.Subparser;
66
import org.hydev.mcpm.client.commands.controllers.UninstallController;
7-
import org.hydev.mcpm.client.uninstall.UninstallResult;
7+
import org.hydev.mcpm.client.commands.presenters.UninstallResultPresenter;
88

99
import java.util.function.Consumer;
1010

1111
/**
1212
* Command parser for the uninstallation use case
1313
*/
14-
public record UninstallParser(UninstallController controller) implements CommandParser {
14+
public record UninstallParser(UninstallController controller, UninstallResultPresenter presenter)
15+
implements CommandParser
16+
{
1517
@Override
1618
public String name() {
1719
return "uninstall";
@@ -41,13 +43,6 @@ public void run(Namespace details, Consumer<String> log)
4143

4244
// Uninstall
4345
var result = controller.uninstall(name, details.getBoolean("recursive"));
44-
45-
// Print result
46-
if (result.state() == UninstallResult.State.FAILED_TO_DELETE) {
47-
log.accept("&cFailed to delete plugin file");
48-
}
49-
if (result.state() == UninstallResult.State.SUCCESS) {
50-
log.accept("&aPlugin " + name + " uninstalled successfully!");
51-
}
46+
presenter.displayResult(name, result, log);
5247
}
5348
}

src/main/java/org/hydev/mcpm/client/commands/presenters/ListResultPresenter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import org.hydev.mcpm.client.list.ListResult;
44

5+
/**
6+
* Presenter for the list command
7+
*/
58
public interface ListResultPresenter {
69
/**
710
* Display the associated output to the console
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.hydev.mcpm.client.commands.presenters;
2+
3+
import org.hydev.mcpm.client.uninstall.UninstallResult;
4+
5+
import java.util.function.Consumer;
6+
7+
/**
8+
* Interface for presenting uninstall result
9+
*/
10+
public interface UninstallResultPresenter {
11+
/**
12+
* Display uninstall result
13+
*
14+
* @param result Result
15+
* @param log Logger
16+
*/
17+
void displayResult(String name, UninstallResult result, Consumer<String> log);
18+
}

src/main/java/org/hydev/mcpm/client/display/presenters/ListPresenter.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88

99
import static org.hydev.mcpm.client.display.presenters.Table.tabulate;
1010

11+
/**
12+
* Presenter for the list result
13+
*/
1114
public class ListPresenter implements ListResultPresenter {
1215
private final Consumer<String> log;
1316

@@ -34,8 +37,7 @@ public void displayResult(ListResult listResult) {
3437
list.stream().map(p -> List.of("&a" + p.name(), "&e" + p.getFirstAuthor(), p.version())).toList(),
3538
List.of(":Name", "Author", "Version:"));
3639

37-
this.log.accept(listResult.type().reason() + " (Parameter " + listResult.listType() + ")\n" + table);
38-
40+
this.log.accept(listResult.type().reason() + " (Parameter " + listResult.listType() + ")\n" + table);
3941
}
4042
}
4143
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.hydev.mcpm.client.display.presenters;
2+
3+
import org.hydev.mcpm.client.commands.presenters.UninstallResultPresenter;
4+
import org.hydev.mcpm.client.uninstall.UninstallResult;
5+
6+
import java.util.function.Consumer;
7+
8+
/**
9+
* Implementation of UninstallResultPresenter
10+
*/
11+
public class UninstallPresenter implements UninstallResultPresenter {
12+
/**
13+
* Display one result
14+
*
15+
* @param name Name of the plugin
16+
* @param state State
17+
* @param log Logger object
18+
*/
19+
private void display(String name, UninstallResult.State state, Consumer<String> log) {
20+
var prefix = "[" + name + "] ";
21+
switch (state) {
22+
case NOT_FOUND -> log.accept(prefix + "&cPlugin not found");
23+
case FAILED_TO_DELETE -> log.accept(prefix + "&cFailed to delete plugin file");
24+
case SUCCESS -> log.accept(prefix + "&aPlugin uninstalled successfully!");
25+
default -> log.accept(state.name());
26+
}
27+
}
28+
29+
@Override
30+
public void displayResult(String name, UninstallResult result, Consumer<String> log) {
31+
display(name, result.state(), log);
32+
result.dependencies().forEach((depName, depState) -> display(depName, depState, log));
33+
}
34+
}

src/main/java/org/hydev/mcpm/client/list/ListAllInteractor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
/**
1010
* Implementation for the ListAll functionality
1111
*/
12-
public record ListAllInteractor(PluginTracker pluginTracker, CheckForUpdatesBoundary checkForUpdatesBoundary) implements ListAllBoundary {
12+
public record ListAllInteractor(PluginTracker pluginTracker, CheckForUpdatesBoundary checkForUpdatesBoundary)
13+
implements ListAllBoundary {
1314
/**
1415
* listAllInteractor interacts with the LocalPluginTracker to get the list of
1516
* plugins, according to a specified

src/main/java/org/hydev/mcpm/client/loader/UnloadBoundary.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ public interface UnloadBoundary
1111
* Dynamically unload a local plugin through JVM reflections and classloader hacks
1212
*
1313
* @param name Loaded plugin name
14-
* @return Jar file
1514
* @throws PluginNotFoundException If a loaded plugin of the name isn't found
1615
*/
1716
File unloadPlugin(String name) throws PluginNotFoundException;
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+
* Interface for removing a file
5+
*/
6+
7+
public interface FileRemove {
8+
/**
9+
* Remove file for given plugin (step 2 and 3 of Uninstaller use case)
10+
*
11+
* @param pluginName Name of the plugin to remove
12+
* @return int 0 - true, 1 - NOT_FOUND, 2 - FAILED_TO_DELETE
13+
*/
14+
FileRemoveResult removeFile(String pluginName);
15+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.hydev.mcpm.client.uninstall;
2+
3+
/**
4+
* Result for removing a file
5+
*/
6+
public enum FileRemoveResult
7+
{
8+
SUCCESS,
9+
NOT_FOUND,
10+
FAILED_TO_DELETE
11+
}

0 commit comments

Comments
 (0)