Skip to content

Commit f38843b

Browse files
committed
Change serialization to json
1 parent 1659938 commit f38843b

File tree

9 files changed

+45
-40
lines changed

9 files changed

+45
-40
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.hydev.mcpm.client.commands.controllers.UninstallController;
3030
import org.hydev.mcpm.client.commands.controllers.UnloadController;
3131
import org.hydev.mcpm.client.commands.controllers.UpdateController;
32+
import org.hydev.mcpm.client.export.PasteBinStorage;
3233
import org.hydev.mcpm.client.updater.CheckForUpdatesInteractor;
3334
import org.hydev.mcpm.client.list.ListAllInteractor;
3435
import org.hydev.mcpm.client.local.LocalPluginTracker;
@@ -44,8 +45,10 @@
4445
import org.hydev.mcpm.client.installer.SpigotPluginDownloader;
4546
import org.hydev.mcpm.client.uninstall.Uninstaller;
4647
import org.hydev.mcpm.client.updater.UpdateInteractor;
48+
import org.hydev.mcpm.utils.ColorLogger;
4749

4850
import java.util.List;
51+
import java.util.function.Consumer;
4952
import java.util.stream.Stream;
5053

5154
/**
@@ -82,8 +85,10 @@ public static List<CommandParser> baseParsers(boolean isMinecraft) {
8285
var updateChecker = new CheckForUpdatesInteractor(matcher);
8386
var updater = new UpdateInteractor(updateChecker, installer, tracker);
8487

88+
var storage = new PasteBinStorage();
89+
8590
// Controllers
86-
var exportPluginsController = new ExportPluginsController(new ExportInteractor(tracker));
91+
var exportPluginsController = new ExportPluginsController(new ExportInteractor(tracker, storage));
8792
var listController = new ListController(new ListAllInteractor(tracker));
8893
var searchController = new SearchPackagesController(searcher, pager);
8994
var mirrorController = new MirrorController(mirror);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ public void configure(Subparser parser)
4444
@Override
4545
public void run(Namespace details, Consumer<String> log)
4646
{
47-
controller.export(new ExportPluginsInput(details.get("cache"), details.get("outfile")), log);
47+
controller.export(new ExportPluginsInput(details.get("cache")), log);
4848
}
4949
}
Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
package org.hydev.mcpm.client.export;
22

3-
import org.hydev.mcpm.client.local.LocalPluginTracker;
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import org.hydev.mcpm.client.database.tracker.PluginTracker;
45

5-
import java.io.PrintStream;
6+
7+
import static org.hydev.mcpm.Constants.JACKSON;
68

79
/**
810
* An implementation of ExportPluginsBoundary that fetches from a database.
911
*/
10-
public class ExportInteractor implements ExportPluginsBoundary {
11-
12-
private final LocalPluginTracker tracker;
13-
14-
public ExportInteractor(LocalPluginTracker tracker) {
15-
this.tracker = tracker;
16-
}
17-
12+
public record ExportInteractor(PluginTracker tracker, StringStorage storage) implements ExportPluginsBoundary {
1813

1914
/**
2015
* Outputs the plugins on each line as its name and version separated by a space.
@@ -26,11 +21,16 @@ public ExportInteractor(LocalPluginTracker tracker) {
2621
public ExportPluginsResult export(ExportPluginsInput input) {
2722
var plugins = tracker.listInstalled();
2823
if (plugins == null) {
29-
return new ExportPluginsResult(ExportPluginsResult.State.FAILED_TO_FETCH_PLUGINS);
24+
return new ExportPluginsResult(ExportPluginsResult.State.FAILED_TO_FETCH_PLUGINS, null);
25+
}
26+
var models = plugins.stream().map(p -> new ExportModel(p.name(), p.version()));
27+
28+
try {
29+
var answer = JACKSON.writeValueAsString(models);
30+
return new ExportPluginsResult(ExportPluginsResult.State.SUCCESS, answer);
31+
} catch (JsonProcessingException e) {
32+
// Should never happen
33+
throw new RuntimeException(e);
3034
}
31-
32-
PrintStream ps = new PrintStream(input.out());
33-
plugins.forEach(p -> ps.printf("%s %s\n", p.name(), p.version()));
34-
return new ExportPluginsResult(ExportPluginsResult.State.SUCCESS);
3535
}
3636
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.hydev.mcpm.client.export;
2+
3+
/**
4+
* Record storing information to export a plugin
5+
*
6+
* @param name Name of the plugin
7+
* @param version Version of the plugin
8+
*/
9+
public record ExportModel(
10+
String name,
11+
String version
12+
) {
13+
}
Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package org.hydev.mcpm.client.export;
22

3-
import java.io.OutputStream;
4-
53
/**
64
* Input for the ExportPluginsBoundary boundary.
75
*
8-
* @param cache if true, will use local cache of plugins to export. Otherwise, will fetch plugins from the database.
9-
* @param out OutputStream to write to
6+
* @param pastebin Link to the pastebin to write to
107
*/
118
public record ExportPluginsInput(
12-
boolean cache,
13-
OutputStream out
9+
String pastebin
1410
) {
15-
}
11+
}

src/main/java/org/hydev/mcpm/client/export/ExportPluginsResult.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package org.hydev.mcpm.client.export;
22

3+
import org.jetbrains.annotations.Nullable;
4+
35
/**
46
* Results returned from ExportPluginBoundary.
57
*
68
* @param state The outcome of the export. Must be SUCCESS for other values to be valid.
79
*/
810
public record ExportPluginsResult(
9-
State state
11+
State state,
12+
@Nullable String export
1013
) {
1114
/**
1215
* Outcome of the plugin export

src/main/java/org/hydev/mcpm/client/database/export/FixedFileStorage.java renamed to src/main/java/org/hydev/mcpm/client/export/FixedFileStorage.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.hydev.mcpm.client.database.export;
1+
package org.hydev.mcpm.client.export;
22

33
import java.io.IOException;
44
import java.nio.charset.StandardCharsets;
@@ -7,11 +7,6 @@
77

88
/**
99
* Fixed local file string storage. This stores the string to a specific file.
10-
*
11-
* @param path Path of the file
12-
* @author Peter (https://github.com/MstrPikachu)
13-
* @author Azalea (https://github.com/hykilpikonna)
14-
* @since 2022-11-23
1510
*/
1611
public record FixedFileStorage(Path path) implements StringStorage
1712
{

src/main/java/org/hydev/mcpm/client/database/export/PasteBinStorage.java renamed to src/main/java/org/hydev/mcpm/client/export/PasteBinStorage.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.hydev.mcpm.client.database.export;
1+
package org.hydev.mcpm.client.export;
22

33
import org.apache.hc.client5.http.fluent.Request;
44
import org.apache.hc.core5.http.ContentType;
@@ -9,9 +9,6 @@
99
* Store string content to a pastebin that supports raw text requests (e.g. https://github.com/w4/bin)
1010
*
1111
* @param host Pastebin host URL
12-
* @author Peter (https://github.com/MstrPikachu)
13-
* @author Azalea (https://github.com/hykilpikonna)
14-
* @since 2022-11-23
1512
*/
1613
public record PasteBinStorage(String host) implements StringStorage
1714
{

src/main/java/org/hydev/mcpm/client/database/export/StringStorage.java renamed to src/main/java/org/hydev/mcpm/client/export/StringStorage.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
1-
package org.hydev.mcpm.client.database.export;
1+
package org.hydev.mcpm.client.export;
22

33
import java.io.IOException;
44

55
/**
66
* Basic string content storage
7-
*
8-
* @author Peter (https://github.com/MstrPikachu)
9-
* @author Azalea (https://github.com/hykilpikonna)
10-
* @since 2022-11-23
117
*/
128
public interface StringStorage
139
{

0 commit comments

Comments
 (0)