Skip to content

Commit

Permalink
Merge pull request #141 from nocalhost/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ovaldi authored Nov 1, 2021
2 parents c09b7e0 + 8866b53 commit dad9b8c
Show file tree
Hide file tree
Showing 47 changed files with 463 additions and 206 deletions.
5 changes: 5 additions & 0 deletions .run/nocalhost-intellij-plugin [buildPlugin].run.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="nocalhost-intellij-plugin [buildPlugin]" type="GradleRunConfiguration" factoryName="Gradle">
<ExternalSystemSettings>
<option name="env">
<map>
<entry key="ORG_GRADLE_PROJECT_platformVersion" value="211" />
</map>
</option>
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="externalSystemIdString" value="GRADLE" />
Expand Down
10 changes: 9 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ if (project.hasProperty("baseIDE")) {
val platformVersion = prop("platformVersion").toInt()
val ideaVersion = prop("ideaVersion")
val nocalhostVersion = prop("version")
val changelogVersion = nocalhostVersion.replace("(\\d+\\.\\d+\\.)(\\d+)".toRegex(), "$1x")

val terminalPlugin = "terminal"
var javascriptPlugin = "JavaScript"
Expand Down Expand Up @@ -106,7 +107,14 @@ tasks {
patchPluginXml {
pluginId.set("dev.nocalhost.nocalhost-intellij-plugin")
pluginDescription.set(provider { file("description.html").readText() })
changeNotes.set(provider { file("changenotes.html").readText() })
changeNotes.set(
"""
<h2>Version $nocalhostVersion</h2>
<p>
<a href="https://nocalhost.dev/docs/changelogs/$changelogVersion/">https://nocalhost.dev/docs/changelogs/$changelogVersion</a>
</p>
"""
);
}

publishPlugin {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ public class NocalhostProfileState extends CommandLineState {
private static final String DEFAULT_SHELL = "sh";

private final List<Disposable> disposables = Lists.newArrayList();
private final AtomicReference<NocalhostDevInfo> devInfoHolder = new AtomicReference<>(null);
private final AtomicReference<NocalhostRunnerContext> refContext = new AtomicReference<>(null);

public NocalhostProfileState(ExecutionEnvironment environment) {
super(environment);
}

@Override
protected @NotNull ProcessHandler startProcess() throws ExecutionException {
NocalhostDevInfo dev = devInfoHolder.get();
NocalhostRunnerContext dev = refContext.get();
if (dev == null) {
throw new ExecutionException("Call prepareDevInfo() before this method");
throw new ExecutionException("Call prepare() before this method");
}
NocalhostContext context = dev.getContext();

Expand All @@ -79,23 +79,25 @@ public NocalhostProfileState(ExecutionEnvironment environment) {
}

public String getDebugPort() {
NocalhostDevInfo dev = devInfoHolder.get();
NocalhostRunnerContext dev = refContext.get();
return dev.getDebug().getLocalPort();
}

public void prepareDevInfo() throws ExecutionException {
public void prepare() throws ExecutionException {
try {
var context = NocalhostContextManager.getInstance(getEnvironment().getProject()).getContext();
if (context == null) {
throw new ExecutionException("Service is not in dev mode.");
throw new ExecutionException("Nocalhost context is null.");
}

var desService = NhctlUtil.getDescribeService(context);
if (!NhctlDescribeServiceUtil.developStarted(desService) || !projectPathMatched(desService)) {
if ( ! NhctlDescribeServiceUtil.developStarted(desService)) {
throw new ExecutionException("Service is not in dev mode.");
}

if (!isSyncStatusIdle()) {
if ( ! isProjectPathMatched(desService)) {
throw new ExecutionException("Project path does not match.");
}
if ( ! isSyncStatusIdle()) {
throw new ExecutionException("File sync has not yet completed.");
}

Expand All @@ -114,8 +116,8 @@ public void prepareDevInfo() throws ExecutionException {
throw new ExecutionException("Service container config not found.");
}

NocalhostDevInfo.Command command = new NocalhostDevInfo.Command(resolveRunCommand(container), resolveDebugCommand(container));
NocalhostDevInfo.Debug debug = null;
NocalhostRunnerContext.Debug debug = null;
NocalhostRunnerContext.Command command = new NocalhostRunnerContext.Command(resolveRunCommand(container), resolveDebugCommand(container));
if (isDebugExecutor()) {
if (!StringUtils.isNotEmpty(command.getDebug())) {
throw new ExecutionException("Debug command not configured");
Expand All @@ -131,15 +133,15 @@ public void prepareDevInfo() throws ExecutionException {
throw new ExecutionException("Remote debug port not configured.");
}
String localPort = startDebugPortForward(context, remotePort);
debug = new NocalhostDevInfo.Debug(remotePort, localPort);
debug = new NocalhostRunnerContext.Debug(remotePort, localPort);
}
} else {
if (!StringUtils.isNotEmpty(command.getRun())) {
throw new ExecutionException("Run command not configured");
}
}

devInfoHolder.set(new NocalhostDevInfo(
refContext.set(new NocalhostRunnerContext(
debug,
container.getDev().getShell(),
command,
Expand All @@ -162,7 +164,7 @@ private void doCreateTunnel(ServiceContainer container) throws ExecutionExceptio

var cmd = new GeneralCommandLine(Lists.newArrayList(
NhctlUtil.binaryPath(), "ssh", "reverse",
"--pod", NhctlUtil.getDevPodName(context),
"--pod", NhctlUtil.getDevPodName(project, context),
"--local", port,
"--remote", port,
"--sshport", "50022",
Expand Down Expand Up @@ -204,7 +206,7 @@ private String startDebugPortForward(NocalhostContext context, String remotePort
NhctlCommand nhctlCommand = ApplicationManager.getApplication().getService(NhctlCommand.class);

try {
var podName = NhctlUtil.getDevPodName(context);
var podName = NhctlUtil.getDevPodName(getEnvironment().getProject(), context);
NhctlPortForwardStartOptions nhctlPortForwardStartOptions = new NhctlPortForwardStartOptions(context.getKubeConfigPath(), context.getNamespace());
nhctlPortForwardStartOptions.setDevPorts(List.of(":" + remotePort));
nhctlPortForwardStartOptions.setDeployment(context.getServiceName());
Expand All @@ -229,8 +231,8 @@ private String startDebugPortForward(NocalhostContext context, String remotePort
}

public void stopDebugPortForward() {
NocalhostDevInfo dev = devInfoHolder.get();
NocalhostDevInfo.Debug debug = dev.getDebug();
NocalhostRunnerContext dev = refContext.get();
NocalhostRunnerContext.Debug debug = dev.getDebug();
if (debug == null) {
return;
}
Expand Down Expand Up @@ -274,7 +276,7 @@ private NhctlRawConfig getNhctlConfig(NocalhostContext nocalhostContext)
return nhctlCommand.getConfig(nocalhostContext.getApplicationName(), opts, NhctlRawConfig.class);
}

private boolean projectPathMatched(NhctlDescribeService nhctlDescribeService) {
private boolean isProjectPathMatched(NhctlDescribeService nhctlDescribeService) {
var basePath = Paths.get(getEnvironment().getProject().getBasePath()).toString();
for (String path : nhctlDescribeService.getLocalAbsoluteSyncDirFromDevStartPlugin()) {
if (StringUtils.equals(basePath, path)) {
Expand Down Expand Up @@ -314,11 +316,11 @@ private static String resolveDebugPort(ServiceContainer serviceContainer) {
}

public void startup() throws ExecutionException {
var context = devInfoHolder.get();
if (context == null) {
throw new ExecutionException("Call prepareDevInfo() before this method");
var dev = refContext.get();
if (dev == null) {
throw new ExecutionException("Call prepare() before this method");
}
if (context.getContainer().getDev().isHotReload()) {
if (dev.getContainer().getDev().isHotReload()) {
disposables.add(new HotReload(getEnvironment()).withExec());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public boolean canRun(@NotNull String executorId, @NotNull RunProfile profile) {
public void execute(@NotNull ExecutionEnvironment environment) throws ExecutionException {
ExecutionManager.getInstance(environment.getProject()).startRunProfile(environment, state -> {
if (state instanceof NocalhostProfileState) {
((NocalhostProfileState) state).prepareDevInfo();
((NocalhostProfileState) state).prepare();
}
FileDocumentManager.getInstance().saveAllDocuments();
ExecutionResult executionResult = state.execute(environment.getExecutor(), this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@AllArgsConstructor
@Getter
@Setter
public class NocalhostDevInfo {
public class NocalhostRunnerContext {
private Debug debug;
private String shell;
private Command command;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public boolean canRun(@NotNull String executorId, @NotNull RunProfile profile) {
public void execute(@NotNull ExecutionEnvironment environment) throws ExecutionException {
ExecutionManager.getInstance(environment.getProject()).startRunProfile(environment, state -> {
NocalhostProfileState nocalhostProfileState = (NocalhostProfileState) state;
nocalhostProfileState.prepareDevInfo();
nocalhostProfileState.prepare();
FileDocumentManager.getInstance().saveAllDocuments();
return attachDlv(state, environment, nocalhostProfileState.getDebugPort());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public boolean canRun(@NotNull String executorId, @NotNull RunProfile profile) {
public void execute(@NotNull ExecutionEnvironment environment) throws ExecutionException {
ExecutionManager.getInstance(environment.getProject()).startRunProfile(environment, state -> {
NocalhostProfileState nocalhostProfileState = (NocalhostProfileState) state;
nocalhostProfileState.prepareDevInfo();
nocalhostProfileState.prepare();
FileDocumentManager.getInstance().saveAllDocuments();
RemoteConnection connection = new RemoteConnection(true, "127.0.0.1", nocalhostProfileState.getDebugPort(), false);
return attachVirtualMachine(state, environment, connection);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void execute(@NotNull ExecutionEnvironment environment) throws ExecutionE
FileDocumentManager.getInstance().saveAllDocuments();
ExecutionManager.getInstance(environment.getProject()).startRunProfile(environment, state -> {
var _state = (NocalhostProfileState) state;
_state.prepareDevInfo();
_state.prepare();

var conf = (NocalhostNodeConfiguration)environment.getRunProfile();
conf.setPort(Integer.parseInt(_state.getDebugPort()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void execute(@NotNull ExecutionEnvironment environment) throws ExecutionE
}
ExecutionManager.getInstance(project).startRunProfile(environment, state -> {
if (state instanceof NocalhostProfileState) {
((NocalhostProfileState) state).prepareDevInfo();
((NocalhostProfileState) state).prepare();
}

FileDocumentManager.getInstance().saveAllDocuments();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import com.intellij.execution.ExecutionManager;
import com.intellij.execution.ExecutionResult;
import com.intellij.execution.configurations.RunProfile;
import com.intellij.execution.configurations.RunProfileState;
import com.intellij.execution.configurations.RunnerSettings;
import com.intellij.execution.executors.DefaultDebugExecutor;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.runners.ProgramRunner;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.util.PathMappingSettings;
import com.intellij.xdebugger.XDebugProcessStarter;
import com.intellij.xdebugger.XDebuggerManager;
Expand All @@ -19,13 +19,6 @@
import java.io.IOException;
import java.net.ServerSocket;

import dev.nocalhost.plugin.intellij.commands.NhctlCommand;
import dev.nocalhost.plugin.intellij.commands.data.NhctlConfigOptions;
import dev.nocalhost.plugin.intellij.commands.data.NhctlRawConfig;
import dev.nocalhost.plugin.intellij.data.NocalhostContext;
import dev.nocalhost.plugin.intellij.exception.NocalhostExecuteCmdException;
import dev.nocalhost.plugin.intellij.service.NocalhostContextManager;

public class NocalhostPythonDebugRunner implements ProgramRunner<RunnerSettings> {
public static final String NOCALHOST_PYTHON_DEBUG_RUNNER = "NocalhostPythonDebugRunner";

Expand All @@ -47,14 +40,13 @@ private ServerSocket createServerSocket(int port) throws ExecutionException {
}
}

private @NotNull PathMappingSettings createPathMappingSettings(@NotNull ExecutionEnvironment environment) throws ExecutionException {
private @NotNull PathMappingSettings createPathMappingSettings(@NotNull ExecutionEnvironment environment, @NotNull RunProfileState state) throws ExecutionException {
var setting = new PathMappingSettings();
try {
var context = NocalhostContextManager.getInstance(environment.getProject()).getContext();
setting.add(new PathMappingSettings.PathMapping() {
{
setLocalRoot(environment.getProject().getBasePath());
setRemoteRoot(getWorkDir(context));
setRemoteRoot(getWorkDir(state));
}
});
} catch (Exception ex) {
Expand All @@ -68,7 +60,7 @@ public void execute(@NotNull ExecutionEnvironment environment) throws ExecutionE
PyRemoteDebugConfiguration conf = (PyRemoteDebugConfiguration) environment.getRunProfile();
ExecutionManager.getInstance(environment.getProject()).startRunProfile(environment, state -> {
((NocalhostPythonProfileState) state).prepare();
conf.setMappingSettings(createPathMappingSettings(environment));
conf.setMappingSettings(createPathMappingSettings(environment, state));
ServerSocket socket = createServerSocket(conf.getPort());
ExecutionResult result = state.execute(environment.getExecutor(), NocalhostPythonDebugRunner.this);
XDebugProcessStarter starter = new XDebugProcessStarterImpl(environment, conf, result, socket);
Expand All @@ -79,20 +71,12 @@ public void execute(@NotNull ExecutionEnvironment environment) throws ExecutionE
});
}

private String getWorkDir(@NotNull NocalhostContext service) throws ExecutionException, InterruptedException, NocalhostExecuteCmdException, IOException {
var opts = new NhctlConfigOptions(service.getKubeConfigPath(), service.getNamespace());
opts.setDeployment(service.getServiceName());
opts.setControllerType(service.getServiceType());
var config = ApplicationManager
.getApplication()
.getService(NhctlCommand.class)
.getConfig(service.getApplicationName(), opts, NhctlRawConfig.class);
var bucket = config.getContainers();
var container = bucket.isEmpty() ? null : bucket.get(0);
private String getWorkDir(@NotNull RunProfileState state) throws Exception {
var dev = ((NocalhostPythonProfileState) state).getRunnerContext();
try {
return container.getDev().getWorkDir();
return dev.getContainer().getDev().getWorkDir();
} catch (Exception ex) {
throw new ExecutionException("The configuration of the service container is incorrect.");
throw new Exception("Please check your dev config.");
}
}
}
Loading

0 comments on commit dad9b8c

Please sign in to comment.