Skip to content

Commit

Permalink
Support of setting the current working directory and environment vari…
Browse files Browse the repository at this point in the history
…ables for Java8+ VSCode projects.
  • Loading branch information
entlicher committed Sep 21, 2021
1 parent e1ca394 commit 323553f
Show file tree
Hide file tree
Showing 24 changed files with 605 additions and 108 deletions.
11 changes: 10 additions & 1 deletion cpplite/cpplite.debugger/nbproject/project.xml
Expand Up @@ -60,13 +60,22 @@
<specification-version>1.59</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.extexecution.base</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>2</release-version>
<specification-version>1.20</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.nativeimage.api</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<release-version>0</release-version>
<specification-version>0.4</specification-version>
<specification-version>0.5</specification-version>
</run-dependency>
</dependency>
<dependency>
Expand Down
Expand Up @@ -50,6 +50,7 @@
import org.netbeans.api.debugger.DebuggerInfo;
import org.netbeans.api.debugger.DebuggerManager;
import org.netbeans.api.debugger.DebuggerManagerAdapter;
import org.netbeans.api.extexecution.base.ExplicitProcessParameters;
import org.netbeans.modules.cnd.debugger.gdb2.mi.MICommand;
import org.netbeans.modules.cnd.debugger.gdb2.mi.MICommandInjector;
import org.netbeans.modules.cnd.debugger.gdb2.mi.MIConst;
Expand All @@ -75,6 +76,7 @@
import org.openide.text.Annotatable;
import org.openide.text.Line;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.Pair;
import org.openide.util.RequestProcessor;

Expand Down Expand Up @@ -818,7 +820,9 @@ public Object[] getServices () {
executable.add(Long.toString(configuration.getAttachProcessId()));
}
executable.addAll(configuration.getExecutable());
Process debuggee = new ProcessBuilder(executable).directory(configuration.getDirectory()).start();
ProcessBuilder processBuilder = new ProcessBuilder(executable);
setParameters(processBuilder, configuration);
Process debuggee = processBuilder.start();
new RequestProcessor(configuration.getDisplayName() + " (pty deallocator)").post(() -> {
try {
while (debuggee.isAlive()) {
Expand Down Expand Up @@ -878,6 +882,18 @@ public void destroy() {
};
}

private static void setParameters(ProcessBuilder processBuilder, CPPLiteDebuggerConfig configuration) {
ExplicitProcessParameters.buildExplicitParameters(Lookup.getDefault());
ExplicitProcessParameters processParameters = configuration.getProcessParameters();
if (processParameters.getWorkingDirectory() != null) {
processBuilder.directory(processParameters.getWorkingDirectory());
}
if (!processParameters.getEnvironmentVariables().isEmpty()) {
Map<String, String> environment = processBuilder.environment();
environment.putAll(processParameters.getEnvironmentVariables());
}
}

private class BreakpointsHandler extends DebuggerManagerAdapter implements PropertyChangeListener {

private final Map<String, CPPLiteBreakpoint> breakpointsById = new ConcurrentHashMap<>();
Expand Down
Expand Up @@ -19,10 +19,11 @@

package org.netbeans.modules.cpplite.debugger;

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

import org.netbeans.api.annotations.common.NullAllowed;
import org.netbeans.api.extexecution.base.ExplicitProcessParameters;

/**
*
Expand All @@ -31,16 +32,30 @@
public final class CPPLiteDebuggerConfig {

private final List<String> executable;
private final File directory;
private final ExplicitProcessParameters processParameters;
@NullAllowed
private final Long processId;
private final String debugger;

public CPPLiteDebuggerConfig(List<String> executable, File directory, @NullAllowed Long processId, String debugger) {
this.executable = executable;
this.directory = directory;
public CPPLiteDebuggerConfig(List<String> executable, ExplicitProcessParameters processParameters, @NullAllowed Long processId, String debugger) {
this.processParameters = processParameters;
this.processId = processId;
this.debugger = debugger;
if (processParameters.isArgReplacement()) {
this.executable = new ArrayList<>();
this.executable.add(executable.get(0));
if (processParameters.getArguments() != null) {
this.executable.addAll(processParameters.getArguments());
}
} else {
if (processParameters.getArguments() != null) {
this.executable = new ArrayList<>();
this.executable.addAll(executable);
this.executable.addAll(processParameters.getArguments());
} else {
this.executable = executable;
}
}
}

public String getDisplayName() {
Expand All @@ -55,10 +70,10 @@ public List<String> getExecutable() {
}

/**
* Get the directory in which the executable command is to be launched.
* Get the parameters which the executable command is to be launched with.
*/
public File getDirectory() {
return directory;
public ExplicitProcessParameters getProcessParameters() {
return processParameters;
}

/**
Expand Down
Expand Up @@ -21,6 +21,7 @@
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.netbeans.api.extexecution.base.ExplicitProcessParameters;

import org.netbeans.modules.cpplite.debugger.CPPLiteDebugger;
import org.netbeans.modules.cpplite.debugger.CPPLiteDebuggerConfig;
Expand All @@ -38,8 +39,9 @@ public static Process startInDebugger(List<String> command) throws IOException {

public static Process startInDebugger(List<String> command, File directory) throws IOException {
CPPLiteDebugger[] debugger = new CPPLiteDebugger[] { null };
ExplicitProcessParameters processParameters = ExplicitProcessParameters.builder().workingDirectory(directory).build();
Process engineProcess = CPPLiteDebugger.startDebugging(
new CPPLiteDebuggerConfig(command, directory, null, "gdb"),
new CPPLiteDebuggerConfig(command, processParameters, null, "gdb"),
engine -> {
debugger[0] = engine.lookupFirst(null, CPPLiteDebugger.class);
});
Expand Down
Expand Up @@ -31,6 +31,7 @@
import org.netbeans.api.debugger.DebuggerEngine;
import org.netbeans.api.extexecution.ExecutionDescriptor;
import org.netbeans.api.extexecution.ExecutionService;
import org.netbeans.api.extexecution.base.ExplicitProcessParameters;
import org.netbeans.modules.cpplite.debugger.CPPFrame;
import org.netbeans.modules.cpplite.debugger.CPPLiteDebugger;
import org.netbeans.modules.cpplite.debugger.CPPLiteDebuggerConfig;
Expand All @@ -42,12 +43,14 @@
import org.netbeans.modules.nativeimage.api.debug.NIFrame;
import org.netbeans.modules.nativeimage.api.debug.NILineBreakpointDescriptor;
import org.netbeans.modules.nativeimage.api.debug.NIVariable;
import org.netbeans.modules.nativeimage.api.debug.StartDebugParameters;
import org.netbeans.modules.nativeimage.spi.debug.NIDebuggerProvider;
import org.netbeans.modules.nativeimage.spi.debug.filters.FrameDisplayer;

import org.openide.LifecycleManager;
import org.openide.util.RequestProcessor;
import org.netbeans.modules.nativeimage.spi.debug.filters.VariableDisplayer;
import org.openide.util.Lookup;

/**
*
Expand Down Expand Up @@ -85,10 +88,20 @@ public void setVariablesDisplayer(VariableDisplayer variablesDisplayer) {
}

@Override
public CompletableFuture<Void> start(List<String> command, File workingDirectory, String miDebugger, String displayName, ExecutionDescriptor executionDescriptor, Consumer<DebuggerEngine> startedEngine) {
public CompletableFuture<Void> start(StartDebugParameters debugParameters, Consumer<DebuggerEngine> startedEngine) {
if (debugger != null) {
throw new IllegalStateException("Debugger has started already.");
}
List<String> command = debugParameters.getCommand();
String miDebugger = debugParameters.getDebugger();
String displayName = debugParameters.getDisplayName();
ExecutionDescriptor executionDescriptor = debugParameters.getExecutionDescriptor();
Lookup contextLookup = debugParameters.getContextLookup();
ExplicitProcessParameters explicitParameters = contextLookup != null ? ExplicitProcessParameters.buildExplicitParameters(contextLookup) : null;
if (explicitParameters == null) {
explicitParameters = ExplicitProcessParameters.builder().workingDirectory(debugParameters.getWorkingDirectory()).build();
}
final ExplicitProcessParameters processParameters = explicitParameters;
if (executionDescriptor == null) {
executionDescriptor = new ExecutionDescriptor()
.showProgress(true)
Expand All @@ -104,7 +117,7 @@ public CompletableFuture<Void> start(List<String> command, File workingDirectory
try {
LifecycleManager.getDefault().saveAll();
engineProcess = CPPLiteDebugger.startDebugging(
new CPPLiteDebuggerConfig(command, workingDirectory, null, miDebugger),
new CPPLiteDebuggerConfig(command, processParameters, null, miDebugger),
engine -> {
debugger[0] = engine.lookupFirst(null, CPPLiteDebugger.class);
this.debugger = debugger[0];
Expand Down Expand Up @@ -147,10 +160,11 @@ public CompletableFuture<Void> attach(String executablePath, long processId, Str
if (debugger != null) {
throw new IllegalStateException("Debugger has started already.");
}
ExplicitProcessParameters processParameters = ExplicitProcessParameters.builder().workingDirectory(new File(System.getProperty("user.dir", ""))).build(); // NOI18N
CompletableFuture<Void> completed = new CompletableFuture<>();
try {
CPPLiteDebugger.startDebugging(
new CPPLiteDebuggerConfig(Collections.singletonList(executablePath), new File(System.getProperty("user.dir")), processId, miDebugger),
new CPPLiteDebuggerConfig(Collections.singletonList(executablePath), processParameters, processId, miDebugger),
engine -> {
CPPLiteDebugger debugger = engine.lookupFirst(null, CPPLiteDebugger.class);
this.debugger = debugger;
Expand Down
Expand Up @@ -25,6 +25,7 @@
import junit.framework.Test;

import org.netbeans.api.debugger.DebuggerEngine;
import org.netbeans.api.extexecution.base.ExplicitProcessParameters;
import org.netbeans.junit.NbModuleSuite;
import org.netbeans.junit.NbTestCase;

Expand Down Expand Up @@ -52,8 +53,9 @@ protected final static void compileCPP(String name, File wd) throws IOException,
}

protected final void startDebugging(String name, File wd) throws IOException {
ExplicitProcessParameters processParameters = ExplicitProcessParameters.builder().workingDirectory(wd).build();
this.process = CPPLiteDebugger.startDebugging(
new CPPLiteDebuggerConfig(Arrays.asList(new File(wd, name).getAbsolutePath()), wd, null, "gdb"),
new CPPLiteDebuggerConfig(Arrays.asList(new File(wd, name).getAbsolutePath()), processParameters, null, "gdb"),
engine -> this.engine = engine);
debugger = engine.lookupFirst(null, CPPLiteDebugger.class);
debugger.addStateListener(new CPPLiteDebugger.StateListener() {
Expand Down
2 changes: 1 addition & 1 deletion ide/extexecution.base/manifest.mf
Expand Up @@ -2,6 +2,6 @@ Manifest-Version: 1.0
AutoUpdate-Show-In-Client: false
OpenIDE-Module: org.netbeans.modules.extexecution.base/2
OpenIDE-Module-Localizing-Bundle: org/netbeans/modules/extexecution/base/resources/Bundle.properties
OpenIDE-Module-Specification-Version: 1.19
OpenIDE-Module-Specification-Version: 1.20
OpenIDE-Module-Recommends: org.netbeans.spi.extexecution.base.ProcessesImplementation

0 comments on commit 323553f

Please sign in to comment.