Skip to content

Commit dbd0ad3

Browse files
committed
Start import
1 parent f38843b commit dbd0ad3

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public ExportPluginsResult export(ExportPluginsInput input) {
2323
if (plugins == null) {
2424
return new ExportPluginsResult(ExportPluginsResult.State.FAILED_TO_FETCH_PLUGINS, null);
2525
}
26-
var models = plugins.stream().map(p -> new ExportModel(p.name(), p.version()));
26+
var models = plugins.stream().map(p -> new ExportModel(p.name(), p.version())).toList();
2727

2828
try {
2929
var answer = JACKSON.writeValueAsString(models);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.hydev.mcpm.client.export;
2+
3+
public class ImportException extends Exception {
4+
public ImportException(Throwable cause) {
5+
super(cause);
6+
}
7+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.hydev.mcpm.client.export;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import org.hydev.mcpm.client.database.inputs.SearchPackagesType;
6+
import org.hydev.mcpm.client.installer.InstallBoundary;
7+
import org.hydev.mcpm.client.installer.input.InstallInput;
8+
import org.hydev.mcpm.utils.Pair;
9+
10+
import java.util.List;
11+
12+
import static org.hydev.mcpm.Constants.JACKSON;
13+
14+
public record ImportInteractor(InstallBoundary install) implements ImportPluginsBoundary {
15+
@Override
16+
public ImportResult importPlugins(String input) throws ImportException {
17+
try {
18+
var plugins = JACKSON.readValue(input, new TypeReference<List<ExportModel>>() { });
19+
// TODO: Install specific versions
20+
var results = plugins.stream()
21+
.map(p -> new Pair<>(p.name(),
22+
install.installPlugin(new InstallInput(p.name(), SearchPackagesType.BY_NAME, true, true))))
23+
.collect(Pair.toMap());
24+
25+
return new ImportResult(ImportResult.State.SUCCESS, results);
26+
} catch (JsonProcessingException e) {
27+
throw new ImportException(e);
28+
}
29+
}
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.hydev.mcpm.client.export;
2+
3+
/**
4+
* Defines how a user can export their plugins.
5+
*
6+
* @author Peter (https://github.com/MstrPikachu)
7+
* @since 2022-11-19
8+
*/
9+
public interface ImportPluginsBoundary {
10+
/**
11+
* Install and load the plugins
12+
*
13+
* @param input The plugins to import
14+
* @return Whether the export was successful or not
15+
*/
16+
ImportResult importPlugins(String input) throws ImportException;
17+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.hydev.mcpm.client.export;
2+
3+
import org.hydev.mcpm.client.installer.InstallResult;
4+
5+
import java.util.EnumSet;
6+
import java.util.Map;
7+
8+
/**
9+
* Result for importing plugins
10+
*
11+
* @param installResults Result of each install
12+
*/
13+
public record ImportResult(Map<String, InstallResult> installResults) {
14+
public enum State {
15+
SUCCESS,
16+
PARTIAL_SUCCESS,
17+
FAILURE
18+
}
19+
20+
public State getState()
21+
{
22+
boolean success = true;
23+
boolean fail = false;
24+
25+
EnumSet<InstallResult> = new RegularEnumSet<>();
26+
for (InstallResult x : installResults.values()) {
27+
success &= x.type();
28+
fail |= x.type();
29+
}
30+
}
31+
}

src/main/java/org/hydev/mcpm/client/search/SearcherByKeyword.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public Map<String, List<PluginModel>> constructSearchMaps(List<PluginModel> plug
3838
if (v.isPresent() && v.get().meta() != null &&
3939
v.get().meta().description() != null && !v.get().meta().description().equals("")) {
4040
String[] keywords = v.get().meta().description()
41-
.replaceAll("[^a-zA-Z ]", "").toLowerCase().split("\\s+");
41+
.replaceAll("[^a-zA-Z\s]", "").toLowerCase().split("\s+");
4242
for (String keyword : new HashSet<>(List.of(keywords))) {
4343
if (!models.containsKey(keyword))
4444
models.put(keyword, new ArrayList<>());

0 commit comments

Comments
 (0)