Skip to content

Commit

Permalink
Liberty run and debug configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Kathryn Kodama <kathryn.s.kodama@gmail.com>
  • Loading branch information
kathrynkodama committed Nov 24, 2022
1 parent 741843c commit 540f411
Show file tree
Hide file tree
Showing 24 changed files with 984 additions and 158 deletions.
15 changes: 8 additions & 7 deletions src/main/java/io/openliberty/tools/intellij/LibertyExplorer.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ public static Tree buildTree(Project project, Color backgroundColor) {
mavenBuildFiles = LibertyProjectUtil.getMavenBuildFiles(project);
gradleBuildFiles = LibertyProjectUtil.getGradleBuildFiles(project);
} catch (IOException | SAXException | ParserConfigurationException e) {
LOGGER.error("Could not find Open Liberty Maven or Gradle projects in workspace",
e.getMessage());
LOGGER.warn("Could not find Liberty Maven or Gradle projects in workspace",
e);
return null;
}

Expand All @@ -109,13 +109,14 @@ public static Tree buildTree(Project project, Color backgroundColor) {
String projectName = null;
VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile == null) {
LOGGER.error("Could not resolve current Maven project");
LOGGER.error(String.format("Could not resolve current Maven project %s", psiFile));
break;
}
LibertyModuleNode node;
try {
projectName = LibertyMavenUtil.getProjectNameFromPom(virtualFile);
} catch (Exception e) {
LOGGER.error("Could not resolve project name from pom.xml", e.getMessage());
LOGGER.warn(String.format("Could not resolve project name from build file: %s", virtualFile), e);
}
if (projectName == null) {
projectName = project.getName();
Expand Down Expand Up @@ -150,13 +151,14 @@ public static Tree buildTree(Project project, Color backgroundColor) {
String projectName = null;
VirtualFile virtualFile = psiFile.getVirtualFile();
if (virtualFile == null) {
LOGGER.error("Could not resolve current Gradle project");
LOGGER.error(String.format("Could not resolve current Gradle project %s", buildFile));
break;
}
LibertyModuleNode node;
try {
projectName = LibertyGradleUtil.getProjectName(virtualFile);
} catch (Exception e) {
LOGGER.error("Could not resolve project name from settings.gradle", e.getMessage());
LOGGER.warn(String.format("Could not resolve project name for project %s", virtualFile), e);
}
if (projectName == null) {
projectName = project.getName();
Expand Down Expand Up @@ -312,7 +314,6 @@ private static void executeAction(Tree tree) {
action = (LibertyGeneralAction) am.getAction(Constants.VIEW_GRADLE_TEST_REPORT_ACTION_ID);
}
if (action != null) {
// TODO set Liberty module for the corresponding action
action.setLibertyModule(module);
// calls action on double click
action.actionPerformed(new AnActionEvent(null, DataManager.getInstance().getDataContext(),
Expand Down
48 changes: 46 additions & 2 deletions src/main/java/io/openliberty/tools/intellij/LibertyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,29 @@ public class LibertyModule {
private String name;
private boolean validContainerVersion;

public LibertyModule(Project project, VirtualFile buildFile, String name, String projectType, boolean validContainerVersion) {
private String customStartParams;
// FIXME not currently being used, need to enable runInContainer checkbox in LibertyRunConfiguration see https://github.com/OpenLiberty/liberty-tools-intellij/issues/160
private boolean runInContainer;

private boolean debugMode;

public LibertyModule(Project project) {
this.project = project;
this.customStartParams = "";
this.runInContainer = false;
this.debugMode = false;
}

public LibertyModule(Project project, VirtualFile buildFile, String name, String projectType, boolean validContainerVersion) {
this(project);
this.buildFile = buildFile;
this.name = name;
this.projectType = projectType;
this.validContainerVersion = validContainerVersion;
}

public LibertyModule(Project project, BuildFile buildFile) {
this.project = project;
this(project);
this.buildFile = buildFile.getBuildFile().getVirtualFile();
this.name = buildFile.getProjectName();
this.projectType = buildFile.getProjectType();
Expand Down Expand Up @@ -80,4 +93,35 @@ public Project getProject() {
public void setProject(Project project) {
this.project = project;
}

public String getCustomStartParams() {
if (customStartParams == null) {
clearCustomStartParams();
}
return customStartParams;
}

public void setCustomStartParams(String customStartParams) {
this.customStartParams = customStartParams;
}

public void clearCustomStartParams() {
customStartParams = "";
}

public boolean runInContainer() {
return runInContainer;
}

public void setRunInContainer(boolean runInContainer) {
this.runInContainer = runInContainer;
}

public boolean isDebugMode() {
return debugMode;
}

public void setDebugMode(boolean debugMode) {
this.debugMode = debugMode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import javax.swing.tree.DefaultMutableTreeNode;

public class LibertyModuleNode extends DefaultMutableTreeNode {
private LibertyModule libertyModule;
private final LibertyModule libertyModule;

public LibertyModuleNode(LibertyModule libertyModule) {
super(libertyModule.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public synchronized static LibertyModules getInstance() {

/**
* Add tracked Liberty project to workspace
*
* @param module LibertyModule
*/
public void addLibertyModule(LibertyModule module) {
Expand All @@ -48,6 +49,7 @@ public void addLibertyModule(LibertyModule module) {

/**
* Get a Liberty module associated with the corresponding build file
*
* @param buildFile build file
* @return LibertyModule
*/
Expand All @@ -69,6 +71,7 @@ public LibertyModule getLibertyProjectFromString(String buildFile) throws Malfor

/**
* Returns all build files associated with a Liberty project
*
* @return List<VirtualFile> Liberty project build files
*/
public List<VirtualFile> getLibertyBuildFiles() {
Expand All @@ -79,6 +82,7 @@ public List<VirtualFile> getLibertyBuildFiles() {

/**
* Returns all Liberty modules in the workspace
*
* @return List<LibertyModule> Liberty project modules
*/
public List<LibertyModule> getLibertyModules() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,27 @@
*******************************************************************************/
package io.openliberty.tools.intellij.actions;

import com.intellij.openapi.ui.InputValidator;
import com.intellij.openapi.ui.Messages;
import io.openliberty.tools.intellij.LibertyPluginIcons;
import io.openliberty.tools.intellij.util.Constants;
import io.openliberty.tools.intellij.util.LibertyActionUtil;
import io.openliberty.tools.intellij.util.LibertyProjectUtil;
import com.intellij.execution.ExecutionManager;
import com.intellij.execution.RunManager;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.executors.DefaultRunExecutor;
import com.intellij.execution.impl.RunManagerImpl;
import com.intellij.execution.runners.ExecutionEnvironmentBuilder;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import io.openliberty.tools.intellij.runConfiguration.LibertyRunConfiguration;
import io.openliberty.tools.intellij.runConfiguration.LibertyRunConfigurationType;
import io.openliberty.tools.intellij.util.LocalizedResourceUtil;
import org.jetbrains.plugins.terminal.ShellTerminalWidget;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
* Opens the Liberty run config view for the corresponding Liberty module. Creates a new Liberty run config if one does not exist.
*/
public class LibertyDevCustomStartAction extends LibertyGeneralAction {

public LibertyDevCustomStartAction() {
Expand All @@ -26,52 +38,60 @@ public LibertyDevCustomStartAction() {

@Override
protected void executeLibertyAction() {
String msg;
String initialVal;
if (projectType.equals(Constants.LIBERTY_MAVEN_PROJECT)) {
msg = LocalizedResourceUtil.getMessage("start.liberty.dev.custom.params.message.maven");
initialVal = "";
} else {
msg = LocalizedResourceUtil.getMessage("start.liberty.dev.custom.params.message.gradle");
initialVal = "";
}
// open run config
RunManager runManager = RunManager.getInstance(project);
List<RunnerAndConfigurationSettings> libertySettings = runManager.getConfigurationSettingsList(LibertyRunConfigurationType.getInstance());
List<RunnerAndConfigurationSettings> libertyModuleSettings = new ArrayList<>();

InputValidator validator = new InputValidator() {
@Override
public boolean checkInput(String inputString) {
if (inputString != null && !inputString.startsWith("-")) {
return false;
libertySettings.forEach(setting -> {
// find all Liberty run configs associated with this build file
LibertyRunConfiguration runConfig = (LibertyRunConfiguration) setting.getConfiguration();
try {
VirtualFile vBuildFile = VfsUtil.findFileByURL(new URL(runConfig.getBuildFile()));
if (vBuildFile.equals(libertyModule.getBuildFile())) {
libertyModuleSettings.add(setting);
}
return true;
} catch (MalformedURLException e) {
String msg = LocalizedResourceUtil.getMessage("liberty.build.file.does.not.resolve", actionCmd, project.getName());
notifyError(msg);
LOGGER.warn(String.format("Could not get Liberty run configuration. ", msg));
}

@Override
public boolean canClose(String inputString) {
return true;
});
RunnerAndConfigurationSettings selectedLibertyConfig;
if (libertyModuleSettings.isEmpty()) {
// create new run config
selectedLibertyConfig = createNewLibertyRunConfig(runManager);
} else {
// TODO possible enhancement: if 1+ run configs, prompt user to select the one they want
// 1+ run configs found for the given project
RunnerAndConfigurationSettings selectedConfig = runManager.getSelectedConfiguration();
if (libertyModuleSettings.contains(selectedConfig)) {
// if the selected config is for the Liberty module, use that run config
selectedLibertyConfig = selectedConfig;
} else {
// pick first in list run config in list
selectedLibertyConfig = libertyModuleSettings.get(0);
}
};

String customParams = Messages.showInputDialog(project, msg,
LocalizedResourceUtil.getMessage("liberty.dev.custom.params"),
LibertyPluginIcons.libertyIcon_40, initialVal, validator);

String startCmd = null;
if (customParams == null) {
return;
}
if (projectType.equals(Constants.LIBERTY_MAVEN_PROJECT)) {
startCmd = "mvn io.openliberty.tools:liberty-maven-plugin:dev " + customParams;
} else if (projectType.equals(Constants.LIBERTY_GRADLE_PROJECT)) {
startCmd = "gradle libertyDev " + customParams;
// opens run config dialog
selectedLibertyConfig.setEditBeforeRun(true);
ExecutionEnvironmentBuilder builder = ExecutionEnvironmentBuilder.createOrNull(DefaultRunExecutor.getRunExecutorInstance(), selectedLibertyConfig);
if (builder != null) {
ExecutionManager.getInstance(project).restartRunProfile(builder.build());
}
}

ShellTerminalWidget widget = LibertyProjectUtil.getTerminalWidget(project, projectName, true);
if (widget == null) {
LOGGER.debug("Unable to start Liberty dev mode with custom parameters, could not get or create terminal widget for " + projectName);
return;
}
String cdToProjectCmd = "cd \"" + buildFile.getParent().getCanonicalPath() + "\"";
LibertyActionUtil.executeCommand(widget, cdToProjectCmd);
LibertyActionUtil.executeCommand(widget, startCmd);
/**
* Creates a new run config for the libertyModule selected
*
* @param runManager
* @return RunnerAndConfigurationSettings newly created run config settings
*/
protected RunnerAndConfigurationSettings createNewLibertyRunConfig(RunManager runManager) {
RunnerAndConfigurationSettings runConfigSettings = runManager.createConfiguration(runManager.suggestUniqueName(libertyModule.getName(), LibertyRunConfigurationType.getInstance()), LibertyRunConfigurationType.class);
LibertyRunConfiguration libertyRunConfiguration = (LibertyRunConfiguration) runConfigSettings.getConfiguration();
// pre-populate build file and name
libertyRunConfiguration.setBuildFile(String.valueOf(libertyModule.getBuildFile()));
return runConfigSettings;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,7 @@
*******************************************************************************/
package io.openliberty.tools.intellij.actions;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationListener;
import com.intellij.notification.NotificationType;
import com.intellij.notification.Notifications;
import io.openliberty.tools.intellij.LibertyPluginIcons;
import io.openliberty.tools.intellij.util.Constants;
import io.openliberty.tools.intellij.util.LibertyActionUtil;
import io.openliberty.tools.intellij.util.LibertyProjectUtil;
import io.openliberty.tools.intellij.util.LocalizedResourceUtil;
import org.jetbrains.plugins.terminal.ShellTerminalWidget;

Expand All @@ -29,18 +22,8 @@ public LibertyDevRunTestsAction() {
@Override
protected void executeLibertyAction() {
String runTestsCommand = " ";
ShellTerminalWidget widget = LibertyProjectUtil.getTerminalWidget(project, projectName, false);

ShellTerminalWidget widget = getTerminalWidget(false);
if (widget == null) {
Notification notif = new Notification(Constants.LIBERTY_DEV_DASHBOARD_ID
, LibertyPluginIcons.libertyIcon
, LocalizedResourceUtil.getMessage("liberty.dev.not.started.notification.title")
, ""
, LocalizedResourceUtil.getMessage("liberty.dev.not.started.notification.content", projectName)
, NotificationType.WARNING
, NotificationListener.URL_OPENING_LISTENER);
Notifications.Bus.notify(notif, project);
LOGGER.error("Cannot run tests, corresponding project terminal does not exist.");
return;
}
LibertyActionUtil.executeCommand(widget, runTestsCommand);
Expand Down
Loading

0 comments on commit 540f411

Please sign in to comment.