Skip to content

Commit

Permalink
Re-implement GUI plugin CLI args
Browse files Browse the repository at this point in the history
  • Loading branch information
NebelNidas committed Mar 10, 2024
1 parent bfe612f commit 6545ecc
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 28 deletions.
2 changes: 2 additions & 0 deletions matcher-cli/src/main/java/matcher/cli/Main.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package matcher.cli;

import matcher.cli.provider.builtin.AdditionalPluginsCliParameterProvider;
import matcher.cli.provider.builtin.AutomatchCliCommandProvider;
import matcher.config.Config;

Expand All @@ -13,6 +14,7 @@ public static void main(String[] args) {
MatcherCli matcherCli = new MatcherCli(false);

// Register all default providers.
matcherCli.registerParameterProvider(new AdditionalPluginsCliParameterProvider());
matcherCli.registerCommandProvider(new AutomatchCliCommandProvider());

// Parse, handle errors, delegate to the correct provider.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package matcher.cli.provider.builtin;

import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

import com.beust.jcommander.Parameter;

import matcher.PluginLoader;
import matcher.cli.provider.CliParameterProvider;

/**
* Provides the default {@code --additional-plugins} parameter.
* If the parameter is present, the passed plugins are automatically loaded.
*/
public class AdditionalPluginsCliParameterProvider implements CliParameterProvider {
@Parameter(names = {BuiltinCliParameters.ADDITIONAL_PLUGINS})
List<Path> additionalPlugins = Collections.emptyList();

@Override
public Object getDataHolder() {
return this;
}

@Override
public void processArgs() {
PluginLoader.run(additionalPlugins);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* All CLI parameters the CLI module handles by default.
*/
public class BuiltinCliParameters {
public static final String ADDITIONAL_PLUGINS = "--additional-plugins";
public static final String INPUTS_A = "--inputs-a";
public static final String INPUTS_B = "--inputs-b";
public static final String CLASSPATH_A = "--classpath-a";
Expand Down
17 changes: 1 addition & 16 deletions matcher-core/src/main/java/matcher/PluginLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand All @@ -16,21 +15,7 @@
import java.util.stream.Stream;

public class PluginLoader {
public static void run(String[] args) {
List<Path> pluginPaths = new ArrayList<>();
pluginPaths.add(Paths.get("plugins"));

for (int i = 0; i < args.length; i++) {
switch (args[i]) {
case "--additional-plugins":
while (i+1 < args.length && !args[i+1].startsWith("--")) {
pluginPaths.add(Path.of(args[++i]));
}

break;
}
}

public static void run(List<Path> pluginPaths) {
List<URL> urls = new ArrayList<>();

for (Path path : pluginPaths) {
Expand Down
18 changes: 17 additions & 1 deletion matcher-gui/src/main/java/matcher/gui/Main.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
package matcher.gui;

import java.util.Collections;
import java.nio.file.Paths;

import javafx.application.Application;

import matcher.PluginLoader;
import matcher.cli.MatcherCli;
import matcher.cli.provider.builtin.AdditionalPluginsCliParameterProvider;
import matcher.config.Config;
import matcher.gui.cli.PreLaunchGuiCliParameterProvider;

public class Main {
public static void main(String[] args) {
Config.init();
PluginLoader.run(args);
PluginLoader.run(Collections.singletonList(Paths.get("plugins")));

handlePreLaunchStartupArgs(args);
Application.launch(MatcherGui.class, args);
}

private static void handlePreLaunchStartupArgs(String[] args) {
MatcherCli cli = new MatcherCli(true);

cli.registerParameterProvider(new AdditionalPluginsCliParameterProvider());
cli.registerParameterProvider(new PreLaunchGuiCliParameterProvider());
cli.processArgs(args);
}
}
14 changes: 3 additions & 11 deletions matcher-gui/src/main/java/matcher/gui/MatcherGui.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import matcher.config.ProjectConfig;
import matcher.config.Theme;
import matcher.gui.cli.PostLaunchGuiCliParameterProvider;
import matcher.gui.cli.PreLaunchGuiCliParameterProvider;
import matcher.gui.srcprocess.BuiltinDecompiler;
import matcher.gui.ui.BottomPane;
import matcher.gui.ui.GuiConstants;
Expand Down Expand Up @@ -108,27 +107,20 @@ public void start(Stage stage) {
l.accept(this);
}

handleStartupArgs(true);
updateCss();

stage.setScene(scene);
stage.setTitle("Matcher");
stage.show();

border.requestFocus();
handleStartupArgs(false);
handlePostLaunchStartupArgs();
}

void handleStartupArgs(boolean preLaunch) {
void handlePostLaunchStartupArgs() {
String[] args = getParameters().getRaw().toArray(String[]::new);
MatcherCli cli = new MatcherCli(true);

if (preLaunch) {
cli.registerParameterProvider(new PreLaunchGuiCliParameterProvider());
} else {
cli.registerParameterProvider(new PostLaunchGuiCliParameterProvider(this));
}

cli.registerParameterProvider(new PostLaunchGuiCliParameterProvider(this));
cli.processArgs(args);
}

Expand Down

0 comments on commit 6545ecc

Please sign in to comment.