Skip to content

Commit

Permalink
Added command execution
Browse files Browse the repository at this point in the history
  • Loading branch information
gbevin committed Jul 16, 2024
1 parent 1bbf73f commit 14d6b06
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 130 deletions.
21 changes: 1 addition & 20 deletions src/main/java/rife/bld/idea/config/BldBuildCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,5 @@
*/
package rife.bld.idea.config;

import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.util.NlsSafe;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public interface BldBuildCommand {
@Nullable
@NlsSafe
String getName();

@Nullable
@NlsSafe
String getDisplayName();

@Nullable
@Nls(capitalization = Nls.Capitalization.Sentence) String getNotEmptyDescription();

void run(DataContext dataContext, List<?> additionalProperties, BldBuildListener buildListener);
public record BldBuildCommand(String name, String displayName, String description) {
}
1 change: 0 additions & 1 deletion src/main/java/rife/bld/idea/config/BldConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public void setupComplete() {
}

public void setBuildCommandList(ArrayList<BldBuildCommand> commands) {

buildCommands_.clear();
buildCommands_.addAll(commands);
ApplicationManager.getApplication().invokeLater(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public final class BldExplorerTreeStructure extends AbstractTreeStructure {
private final Object dependenciesFolder_ = new Object();

private static final Comparator<BldBuildCommand> commandComparator = (command1, command2) -> {
final String name1 = command1.getDisplayName();
final String name1 = command1.displayName();
if (name1 == null) return -1;
final String name2 = command2.getDisplayName();
final String name2 = command2.displayName();
if (name2 == null) return 1;
return name1.compareToIgnoreCase(name2);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.intellij.openapi.roots.ui.CellAppearanceEx;
import com.intellij.openapi.roots.ui.util.CompositeAppearance;
import com.intellij.openapi.util.Comparing;
import com.intellij.ui.JBColor;
import com.intellij.ui.SimpleColoredComponent;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -48,7 +47,7 @@ public boolean update() {

final var color = UIUtil.getLabelForeground();
var nameAttributes = new TextAttributes(color, null, null, EffectType.BOXED, Font.PLAIN);
highlightedText_.getEnding().addText(command_.getDisplayName(), nameAttributes);
highlightedText_.getEnding().addText(command_.displayName(), nameAttributes);

myName = highlightedText_.getText();

Expand All @@ -63,7 +62,7 @@ public CellAppearanceEx getHighlightedText() {
public void customize(@NotNull SimpleColoredComponent component) {
getHighlightedText().customize(component);
component.setIcon(getIcon());
var toolTipText = getCommand().getNotEmptyDescription();
var toolTipText = getCommand().description();
component.setToolTipText(toolTipText);
}
}
32 changes: 6 additions & 26 deletions src/main/java/rife/bld/idea/execution/BldExecution.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,21 @@
import com.intellij.execution.process.ProcessEvent;
import com.intellij.execution.process.ProcessOutputType;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectUtil;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import rife.bld.idea.config.BldBuildCommand;
import rife.bld.idea.config.BldBuildListener;
import rife.bld.idea.config.BldConfiguration;
import rife.bld.idea.console.BldConsoleManager;
import rife.bld.idea.utils.BldConstants;
import rife.bld.wrapper.Wrapper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -95,34 +92,17 @@ public void listTasks() {
var json = new JSONObject(output);
var json_commands = json.getJSONObject("commands");
for (var json_command_key : json_commands.keySet()) {
commands.add(new BldBuildCommand() {
@Override
public @Nullable String getName() {
return json_command_key;
}

@Override
public @Nullable String getDisplayName() {
return json_command_key;
}

@Nls(capitalization = Nls.Capitalization.Sentence)
@Override
public @Nullable String getNotEmptyDescription() {
return json_commands.getString(json_command_key);
}

@Override
public void run(DataContext dataContext, List<?> additionalProperties, BldBuildListener buildListener) {

}
});
commands.add(new BldBuildCommand(json_command_key, json_command_key, json_commands.getString(json_command_key)));
}

BldConfiguration.getInstance(project_).setBuildCommandList(commands);
}

public List<String> executeCommands(boolean silent, String... commands) {
return executeCommands(silent, Arrays.asList(commands));
}

public List<String> executeCommands(boolean silent, List<String> commands) {
if (projectDir_ == null || bldMainClass_ == null) {
return Collections.emptyList();
}
Expand Down
136 changes: 134 additions & 2 deletions src/main/java/rife/bld/idea/project/BldProjectWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,35 @@
package rife.bld.idea.project;

import com.intellij.execution.RunManagerListener;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.icons.AllIcons;
import com.intellij.ide.DataManager;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.Task;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.SimpleToolWindowPanel;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.NlsSafe;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.ColoredTreeCellRenderer;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.SimpleTextAttributes;
import com.intellij.ui.TreeUIHelper;
import com.intellij.ui.tree.AsyncTreeModel;
import com.intellij.ui.tree.StructureTreeModel;
import com.intellij.ui.treeStructure.Tree;
import com.intellij.util.EditSourceOnDoubleClickHandler;
import com.intellij.util.ui.JBUI;
import com.intellij.util.ui.tree.TreeUtil;
import org.jetbrains.annotations.NotNull;
import rife.bld.idea.config.BldBuildCommand;
import rife.bld.idea.config.BldConfigurationListener;
import rife.bld.idea.project.actions.RefreshAction;
import rife.bld.idea.project.actions.RunAction;
import rife.bld.idea.config.explorer.nodeDescriptors.BldCommandNodeDescriptor;
import rife.bld.idea.console.BldConsoleManager;
import rife.bld.idea.execution.BldExecution;
import rife.bld.idea.config.BldConfiguration;
import rife.bld.idea.config.explorer.BldExplorerTreeStructure;
import rife.bld.idea.config.explorer.nodeDescriptors.BldNodeDescriptor;
Expand All @@ -31,6 +42,11 @@

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public final class BldProjectWindow extends SimpleToolWindowPanel implements DataProvider, Disposable {
private Project project_;
Expand Down Expand Up @@ -78,6 +94,13 @@ public void configurationChanged() {
TreeUtil.installActions(tree_);
TreeUIHelper.getInstance().installTreeSpeedSearch(tree_);

new EditSourceOnDoubleClickHandler.TreeMouseListener(tree_, null) {
@Override
protected void processDoubleClick(@NotNull MouseEvent e, @NotNull DataContext dataContext, @NotNull TreePath treePath) {
runSelection(DataManager.getInstance().getDataContext(tree_), true);
}
}.installOn(tree_);

project.getMessageBus().connect(this).subscribe(RunManagerListener.TOPIC, new RunManagerListener() {
@Override
public void beforeRunTasksChanged () {
Expand Down Expand Up @@ -137,4 +160,113 @@ public void customizeCellRenderer(@NotNull JTree tree,
}
}

private void runSelection(final DataContext dataContext, final boolean moveFocusToEditor) {
if (!canRunSelection()) {
return;
}

final var commands = getCommandNamesFromPaths(tree_.getSelectionPaths());

var commands_info = String.join(" ", commands);
new Task.Backgroundable(project_, BldBundle.message("bld.project.progress.commands", commands_info), true) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
BldConsoleManager.showTaskMessage(BldBundle.message("bld.project.console.commands", commands_info), ConsoleViewContentType.USER_INPUT, project_);
BldExecution.getInstance(project_).executeCommands(false, commands);
}
}.queue();

if (moveFocusToEditor) {
ToolWindowManager.getInstance(project_).activateEditorComponent();
}
}

private boolean canRunSelection() {
if (tree_ == null) {
return false;
}
final TreePath[] paths = tree_.getSelectionPaths();
if (paths == null) {
return false;
}
for (final TreePath path : paths) {
final DefaultMutableTreeNode node = (DefaultMutableTreeNode)path.getLastPathComponent();
final Object userObject = node.getUserObject();
return userObject instanceof BldCommandNodeDescriptor;
}
return true;
}

private static List<@NlsSafe String> getCommandNamesFromPaths(TreePath[] paths) {
if (paths == null || paths.length == 0) {
return Collections.emptyList();
}
final List<String> targets = new ArrayList<>();
for (final TreePath path : paths) {
final Object userObject = ((DefaultMutableTreeNode)path.getLastPathComponent()).getUserObject();
if (!(userObject instanceof BldCommandNodeDescriptor)) {
continue;
}
final BldBuildCommand target = ((BldCommandNodeDescriptor)userObject).getCommand();
targets.add(target.name());
}
return targets;
}

private final class RefreshAction extends AnAction implements DumbAware {
public RefreshAction() {
super(BldBundle.messagePointer("refresh.bld.command.action.name"),
BldBundle.messagePointer("refresh.bld.command.action.description"), AllIcons.Actions.Refresh);
}

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
new Task.Backgroundable(project_, BldBundle.message("bld.project.progress.refresh"), true) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
BldConsoleManager.showTaskMessage(BldBundle.message("bld.project.console.refresh"), ConsoleViewContentType.USER_INPUT, project_);

BldExecution.getInstance(project_).listTasks();
}
}.queue();
}

@Override
public void update(@NotNull AnActionEvent event) {
final var presentation = event.getPresentation();
presentation.setText(BldBundle.messagePointer("refresh.bld.command.action.name"));
presentation.setEnabled(true);
}

@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.EDT;
}
}

private final class RunAction extends AnAction implements DumbAware {
public RunAction() {
super(BldBundle.messagePointer("run.bld.command.action.name"),
BldBundle.messagePointer("run.bld.command.action.description"), AllIcons.Actions.Execute);
}

@Override
public void actionPerformed(@NotNull AnActionEvent e) {
runSelection(e.getDataContext(), true);
}

@Override
public void update(@NotNull AnActionEvent event) {
final var presentation = event.getPresentation();
presentation.setText(BldBundle.messagePointer("run.bld.command.action.name"));
presentation.setEnabled(true);
presentation.setEnabled(canRunSelection());
}

@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.EDT;
}
}

}
36 changes: 0 additions & 36 deletions src/main/java/rife/bld/idea/project/actions/RefreshAction.java

This file was deleted.

38 changes: 0 additions & 38 deletions src/main/java/rife/bld/idea/project/actions/RunAction.java

This file was deleted.

Loading

0 comments on commit 14d6b06

Please sign in to comment.