Skip to content

Commit

Permalink
Improvements to startup and bld execution flow
Browse files Browse the repository at this point in the history
  • Loading branch information
gbevin committed Jul 16, 2024
1 parent 3af2a5e commit 1bbf73f
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 58 deletions.
3 changes: 2 additions & 1 deletion src/main/java/rife/bld/idea/ProjectOpenStartupActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class ProjectOpenStartupActivity implements ProjectActivity {
@Nullable
@Override
public Object execute(@NotNull Project project, @NotNull Continuation<? super Unit> continuation) {
BldExecution.listTasks(project);
BldExecution.getInstance(project).setupProject();
BldExecution.getInstance(project).listTasks();
return null;
}
}
31 changes: 13 additions & 18 deletions src/main/java/rife/bld/idea/config/BldConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@
package rife.bld.idea.config;

import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.Service;
import com.intellij.openapi.components.State;
import com.intellij.openapi.components.Storage;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.SimpleModificationTracker;
import com.intellij.util.EventDispatcher;
import org.jdom.Element;
import org.jetbrains.annotations.Nls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -28,17 +20,12 @@
public final class BldConfiguration implements Disposable {
private final Project project_;
private final List<BldBuildCommand> buildCommands_ = new CopyOnWriteArrayList<>();
private final EventDispatcher<BldConfigurationListener> myEventDispatcher = EventDispatcher.create(BldConfigurationListener.class);
private final EventDispatcher<BldConfigurationListener> eventDispatcher_ = EventDispatcher.create(BldConfigurationListener.class);

private volatile boolean initialized_ = false;

public BldConfiguration(final Project project) {
project_ = project;

ApplicationManager.getApplication().invokeLater(
() -> myEventDispatcher.getMulticaster().configurationLoaded(),
ModalityState.any()
);
}

public static BldConfiguration getInstance(final @NotNull Project project) {
Expand All @@ -58,22 +45,30 @@ public List<BldBuildCommand> getBuildCommandList() {
return buildCommands_;
}

public void setBuildCommandList(ArrayList<BldBuildCommand> commands) {
public void setupComplete() {
initialized_ = true;

ApplicationManager.getApplication().invokeLater(
() -> eventDispatcher_.getMulticaster().configurationLoaded(),
ModalityState.any()
);
}

public void setBuildCommandList(ArrayList<BldBuildCommand> commands) {

buildCommands_.clear();
buildCommands_.addAll(commands);
ApplicationManager.getApplication().invokeLater(
() -> myEventDispatcher.getMulticaster().configurationChanged(),
() -> eventDispatcher_.getMulticaster().configurationChanged(),
ModalityState.any()
);
}

public void addBldConfigurationListener(final BldConfigurationListener listener) {
myEventDispatcher.addListener(listener);
eventDispatcher_.addListener(listener);
}

public void removeBldConfigurationListener(final BldConfigurationListener listener) {
myEventDispatcher.removeListener(listener);
eventDispatcher_.removeListener(listener);
}
}
4 changes: 2 additions & 2 deletions src/main/java/rife/bld/idea/console/BldConsoleViewPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public StopAction() {
public void actionPerformed(AnActionEvent e) {
var project = e.getProject();
if (project != null) {
BldExecution.terminateBldProcess(project);
BldExecution.getInstance(project).terminateBldProcess();
BldConsoleManager.getConsole(project).print(BldBundle.message("bld.command.terminated"), ConsoleViewContentType.ERROR_OUTPUT);
}
}
Expand All @@ -111,7 +111,7 @@ public void update(AnActionEvent event) {
if (project == null) {
return;
}
presentation.setEnabled(BldExecution.hasActiveBldProcess(project));
presentation.setEnabled(BldExecution.getInstance(project).hasActiveBldProcess());
}

@Override
Expand Down
90 changes: 57 additions & 33 deletions src/main/java/rife/bld/idea/execution/BldExecution.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand All @@ -34,27 +35,60 @@

import org.json.JSONObject;

public class BldExecution {
private static final ConcurrentHashMap<Project, Process> runningBldProcesses_ = new ConcurrentHashMap<>();
@Service(Service.Level.PROJECT)
public final class BldExecution {
private final Project project_;
private final ConcurrentHashMap<Project, Process> runningBldProcesses_ = new ConcurrentHashMap<>();

private VirtualFile projectDir_ = null;
private String bldMainClass_ = null;

public BldExecution(@NotNull Project project) {
project_ = project;
}

public static BldExecution getInstance(final @NotNull Project project) {
return project.getService(BldExecution.class);
}

public static boolean hasActiveBldProcess(@NotNull Project project) {
return runningBldProcesses_.containsKey(project);
public boolean hasActiveBldProcess() {
return runningBldProcesses_.containsKey(project_);
}

public static void terminateBldProcess(Project project) {
var process = runningBldProcesses_.get(project);
public void terminateBldProcess() {
var process = runningBldProcesses_.get(project_);
if (process != null) {
process.destroy();
runningBldProcesses_.remove(project, process);
runningBldProcesses_.remove(project_, process);
}
}

public static void listTasks(@NotNull Project project) {
var output = String.join("", executeCommands(project, true, "help", Wrapper.JSON_ARGUMENT));
BldConsoleManager.showTaskMessage("Detected the bld commands\n", ConsoleViewContentType.SYSTEM_OUTPUT, project);
public void setupProject() {
projectDir_ = ProjectUtil.guessProjectDir(project_);
if (projectDir_ == null) {
BldConsoleManager.showTaskMessage("Could not find project directory\n", ConsoleViewContentType.ERROR_OUTPUT, project_);
return;
}

bldMainClass_ = guessBldMainClass(projectDir_);
if (bldMainClass_ == null) {
BldConsoleManager.showTaskMessage("Could not find bld main class for project " + projectDir_ + "\n", ConsoleViewContentType.ERROR_OUTPUT, project_);
return;
}

BldConsoleManager.showTaskMessage("Found bld main class: " + bldMainClass_ + "\n", ConsoleViewContentType.SYSTEM_OUTPUT, project_);

BldConfiguration.getInstance(project_).setupComplete();
}

public void listTasks() {
var output = String.join("", executeCommands(true, "help", Wrapper.JSON_ARGUMENT));
if (output.isEmpty()) {
BldConsoleManager.showTaskMessage("Failed to detect the bld commands.\n", ConsoleViewContentType.ERROR_OUTPUT, project_);
return;
}

BldConsoleManager.showTaskMessage("Detected the bld commands\n", ConsoleViewContentType.SYSTEM_OUTPUT, project_);

var commands = new ArrayList<BldBuildCommand>();

Expand Down Expand Up @@ -85,32 +119,22 @@ public void run(DataContext dataContext, List<?> additionalProperties, BldBuildL
});
}

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

public static List<String> executeCommands(@NotNull Project project, boolean silent, String... commands) {
var project_dir = ProjectUtil.guessProjectDir(project);
if (project_dir == null) {
BldConsoleManager.showTaskMessage("Could not find project directory\n", ConsoleViewContentType.ERROR_OUTPUT, project);
public List<String> executeCommands(boolean silent, String... commands) {
if (projectDir_ == null || bldMainClass_ == null) {
return Collections.emptyList();
}

var bldMainClass = guessBldMainClass(project, project_dir);
if (bldMainClass == null) {
BldConsoleManager.showTaskMessage("Could not find bld main class for project " + project_dir + "\n", ConsoleViewContentType.ERROR_OUTPUT, project);
return Collections.emptyList();
}

BldConsoleManager.showTaskMessage("Found bld main class: " + bldMainClass + "\n", ConsoleViewContentType.SYSTEM_OUTPUT, project);

final var command_line = new GeneralCommandLine();
command_line.setWorkDirectory(project_dir.getCanonicalPath());
command_line.setWorkDirectory(projectDir_.getCanonicalPath());
command_line.setExePath("java");
command_line.addParameter("-jar");
command_line.addParameter(project_dir.getCanonicalPath() + "/lib/bld/bld-wrapper.jar");
command_line.addParameter(project_dir.getCanonicalPath() + "/bld");
command_line.addParameter(projectDir_.getCanonicalPath() + "/lib/bld/bld-wrapper.jar");
command_line.addParameter(projectDir_.getCanonicalPath() + "/bld");
command_line.addParameter(Wrapper.BUILD_ARGUMENT);
command_line.addParameter(bldMainClass);
command_line.addParameter(bldMainClass_);
if (commands != null) {
command_line.addParameters(commands);
}
Expand All @@ -120,7 +144,7 @@ public static List<String> executeCommands(@NotNull Project project, boolean sil
process = command_line.createProcess();
}
catch (ExecutionException e) {
BldConsoleManager.showTaskMessage(e.getMessage() != null ? e.getMessage() : e.toString(), ConsoleViewContentType.ERROR_OUTPUT, project);
BldConsoleManager.showTaskMessage(e.getMessage() != null ? e.getMessage() : e.toString(), ConsoleViewContentType.ERROR_OUTPUT, project_);
return Collections.emptyList();
}

Expand All @@ -132,21 +156,21 @@ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType
super.onTextAvailable(event, outputType);
if (!outputType.equals(ProcessOutputType.SYSTEM)) {
if (!silent) {
BldConsoleManager.showTaskMessage(event.getText(), ConsoleViewContentType.NORMAL_OUTPUT, project);
BldConsoleManager.showTaskMessage(event.getText(), ConsoleViewContentType.NORMAL_OUTPUT, project_);
}
output.add(event.getText());
}
}
});

runningBldProcesses_.put(project, process);
runningBldProcesses_.put(project_, process);
process_handler.runProcess();
runningBldProcesses_.remove(project, process);
runningBldProcesses_.remove(project_, process);

return output;
}

private static String guessBldMainClass(@NotNull Project project, @NotNull VirtualFile projectDir) {
private String guessBldMainClass(@NotNull VirtualFile projectDir) {
var project_bld = projectDir.findChild("bld");
if (project_bld == null) {
project_bld = projectDir.findChild("bld.bat");
Expand All @@ -159,7 +183,7 @@ private static String guessBldMainClass(@NotNull Project project, @NotNull Virtu
return matcher.group(1);
}
} catch (IOException e) {
BldConsoleManager.showTaskMessage("Unable to read contents of " + project_bld + "\n", ConsoleViewContentType.ERROR_OUTPUT, project);
BldConsoleManager.showTaskMessage("Unable to read contents of " + project_bld + "\n", ConsoleViewContentType.ERROR_OUTPUT, project_);
}
}
return null;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/rife/bld/idea/utils/BldConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

public class BldConstants {
public static final String BLD = "bld";
public static final String PROJECT_NAME = "Bld Project";
public static final String CONSOLE_NAME = "Bld Console";
public static final String PROJECT_NAME = "bld";
public static final String CONSOLE_NAME = "bld console";
public static final String BLD_CONSOLE_TOOLBAR = "BldConsoleToolbar";
public static final String BLD_EXPLORER_TOOLBAR = "BldExplorerToolbar";
public static final Pattern BUILD_MAIN_CLASS = Pattern.compile(Wrapper.BUILD_ARGUMENT + "\\s(\\S+)");;
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<resource-bundle>messages.BldBundle</resource-bundle>

<extensions defaultExtensionNs="com.intellij">
<toolWindow id="Bld Console" icon="BldIcons.Icon" factoryClass="rife.bld.idea.console.BldConsoleWindowFactory" anchor="bottom"/>
<toolWindow id="Bld Project" icon="BldIcons.Icon" factoryClass="rife.bld.idea.project.BldProjectWindowFactory" anchor="right"/>
<toolWindow id="bld console" icon="BldIcons.Icon" factoryClass="rife.bld.idea.console.BldConsoleWindowFactory" anchor="bottom"/>
<toolWindow id="bld" icon="BldIcons.Icon" factoryClass="rife.bld.idea.project.BldProjectWindowFactory" anchor="right"/>
<postStartupActivity implementation="rife.bld.idea.ProjectOpenStartupActivity"/>
</extensions>

Expand Down

0 comments on commit 1bbf73f

Please sign in to comment.