Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JavaFX: parse command line arguments #2222

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions phoenicis-javafx/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<testfx.version>4.0.15-alpha</testfx.version>
</properties>
<dependencies>
<dependency>
<groupId>com.github.jankroken</groupId>
<artifactId>commandline</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.phoenicis</groupId>
<artifactId>phoenicis-configuration</artifactId>
Expand Down
158 changes: 158 additions & 0 deletions phoenicis-javafx/src/main/java/org/phoenicis/javafx/CLIController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
/*
* Copyright (C) 2015-2017 PÂRIS Quentin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

package org.phoenicis.javafx;

import com.github.jankroken.commandline.annotations.AllAvailableArguments;
import com.github.jankroken.commandline.annotations.LongSwitch;
import com.github.jankroken.commandline.annotations.Option;
import com.github.jankroken.commandline.annotations.ShortSwitch;
import javafx.application.Platform;
import org.graalvm.polyglot.Value;
import org.phoenicis.javafx.controller.MainController;
import org.phoenicis.javafx.dialogs.ErrorDialog;
import org.phoenicis.library.ShortcutRunner;
import org.phoenicis.multithreading.ControlledThreadPoolExecutorServiceCloser;
import org.phoenicis.repository.RepositoryManager;
import org.phoenicis.repository.dto.ScriptDTO;
import org.phoenicis.scripts.Installer;
import org.phoenicis.scripts.interpreter.ScriptInterpreter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import static org.phoenicis.configuration.localisation.Localisation.tr;

public class CLIController implements AutoCloseable {
private static final Logger LOGGER = LoggerFactory.getLogger(CLIController.class);
private final ConfigurableApplicationContext applicationContext;
private final RepositoryManager repositoryManager;
private final ScriptInterpreter scriptInterpreter;

public CLIController() {
applicationContext = new AnnotationConfigApplicationContext(AppConfiguration.class);
final MainController mainController = applicationContext.getBean(MainController.class);
mainController.show();
mainController.setOnClose(() -> {
try {
applicationContext.getBean(ControlledThreadPoolExecutorServiceCloser.class).setCloseImmediately(true);
applicationContext.close();
} catch (Exception e) {
LOGGER.warn("Exception while closing the application.", e);
}
});

repositoryManager = applicationContext.getBean("repositoryManager", RepositoryManager.class);
scriptInterpreter = applicationContext.getBean("scriptInterpreter", ScriptInterpreter.class);
}

@Option
@LongSwitch("run")
@ShortSwitch("r")
@AllAvailableArguments
public void runApp(List<String> arguments) {
final String shortcutName = arguments.get(0);
arguments.remove(0);

final ShortcutRunner shortcutRunner = applicationContext.getBean(ShortcutRunner.class);

if (!shortcutRunner.shortcutExists(shortcutName)) {
LOGGER.error("Requested shortcut does not exist: " + shortcutName);
return;
}

shortcutRunner.run(shortcutName, arguments, e -> {
throw new IllegalStateException(e);
});
}

@Option
@LongSwitch("script")
@ShortSwitch("s")
@AllAvailableArguments
public void runScript(List<String> arguments) {
final String scriptPath = arguments.get(0);
final File scriptFile = new File(scriptPath);
final ScriptInterpreter scriptInterpreter = applicationContext.getBean("scriptInterpreter",
ScriptInterpreter.class);
scriptInterpreter.runScript(scriptFile, e -> {
throw new IllegalStateException(e);
});
}

@Option
@LongSwitch("install")
@ShortSwitch("i")
@AllAvailableArguments
public void installApp(List<String> arguments) {
final String typeName = arguments.get(0);
final String typeId = typeName;
final String categoryName = arguments.get(1);
final String categoryId = typeId + "." + categoryName;
final String appName = arguments.get(2);
final String appId = categoryId + "." + appName;
final String scriptName = arguments.get(3);
final String scriptId = appId + "." + scriptName;

final ScriptDTO scriptDTO = repositoryManager
.getScript(Arrays.asList(typeId, categoryId, appId, scriptId));

if (scriptDTO == null) {
LOGGER.error("Requested app does not exist: " + arguments);
return;
}

final StringBuilder executeBuilder = new StringBuilder();
executeBuilder.append(String.format("TYPE_ID=\"%s\";\n", scriptDTO.getTypeId()));
executeBuilder.append(String.format("CATEGORY_ID=\"%s\";\n", scriptDTO.getCategoryId()));
executeBuilder.append(String.format("APPLICATION_ID=\"%s\";\n", scriptDTO.getApplicationId()));
executeBuilder.append(String.format("SCRIPT_ID=\"%s\";\n", scriptDTO.getId()));

executeBuilder.append(scriptDTO.getScript());
executeBuilder.append("\n");

scriptInterpreter.createInteractiveSession()
.eval(executeBuilder.toString(), result -> {
Value installer = (Value) result;

installer.as(Installer.class).go();
}, e -> Platform.runLater(() -> {
// no exception if installation is cancelled
if (!(e.getCause() instanceof InterruptedException)) {
final ErrorDialog errorDialog = ErrorDialog.builder()
.withMessage(tr("The script ended unexpectedly"))
.withException(e)
.build();

errorDialog.showAndWait();
}
}));

}

@Override
public void close() throws InterruptedException {
applicationContext.getBean(ControlledThreadPoolExecutorServiceCloser.class).close();
applicationContext.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,24 @@

package org.phoenicis.javafx;

import com.github.jankroken.commandline.CommandLineParser;
import com.github.jankroken.commandline.OptionStyle;
import javafx.application.Application;
import javafx.scene.image.Image;
import javafx.scene.text.Font;
import javafx.stage.Stage;
import org.phoenicis.javafx.controller.MainController;
import org.phoenicis.multithreading.ControlledThreadPoolExecutorServiceCloser;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

public class JavaFXApplication extends Application {
private final static Logger LOGGER = LoggerFactory.getLogger(JavaFXApplication.class);

public static void main(String[] args) {
try {
Application.launch(JavaFXApplication.class);
Application.launch(args);
} catch (Exception e) {
e.printStackTrace();
}
Expand All @@ -45,19 +46,18 @@ public void start(Stage primaryStage) {
primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("views/common/phoenicis.png")));
primaryStage.setTitle("Phoenicis");
loadFonts();
ConfigurableApplicationContext applicationContext = new AnnotationConfigApplicationContext(
AppConfiguration.class);

final MainController mainController = applicationContext.getBean(MainController.class);
mainController.show();
mainController.setOnClose(() -> {
try {
applicationContext.getBean(ControlledThreadPoolExecutorServiceCloser.class).setCloseImmediately(true);
applicationContext.close();
} catch (Exception e) {
LOGGER.warn("Exception while closing the application.", e);
}
});
try {
final List<String> parameters = getParameters().getRaw();
String[] parametersArray = new String[parameters.size()];
parametersArray = parameters.toArray(parametersArray);
final CLIController arguments = CommandLineParser.parse(CLIController.class,
parametersArray, OptionStyle.SIMPLE);

arguments.close();
} catch (IllegalAccessException | InstantiationException | InvocationTargetException | InterruptedException e) {
throw new RuntimeException(e);
}
}

private void loadFonts() {
Expand Down