Search in sources :

Example 81 with ProcessEvent

use of com.intellij.execution.process.ProcessEvent in project ballerina by ballerina-lang.

the class BallerinaApplicationRunningState method startProcess.

@NotNull
@Override
protected ProcessHandler startProcess() throws ExecutionException {
    ProcessHandler processHandler = super.startProcess();
    processHandler.addProcessListener(new ProcessAdapter() {

        @Override
        public void startNotified(ProcessEvent event) {
            if (myHistoryProcessHandler != null) {
                myHistoryProcessHandler.apply(processHandler);
            }
        }
    });
    return processHandler;
}
Also used : ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) ProcessHandler(com.intellij.execution.process.ProcessHandler) NotNull(org.jetbrains.annotations.NotNull)

Example 82 with ProcessEvent

use of com.intellij.execution.process.ProcessEvent in project azure-tools-for-java by Microsoft.

the class SpringCloudDeploymentConfigurationState method execute.

@Override
@Nullable
public ExecutionResult execute(Executor executor, @NotNull ProgramRunner<?> runner) throws ExecutionException {
    final RunProcessHandler processHandler = new RunProcessHandler();
    processHandler.addDefaultListener();
    processHandler.startNotify();
    processHandler.setProcessTerminatedHandler(RunProcessHandler.DO_NOTHING);
    final ConsoleMessager messager = new ConsoleMessager(processHandler);
    final ConsoleView consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(this.project).getConsole();
    consoleView.attachToProcess(processHandler);
    final Disposable subscribe = Mono.fromCallable(() -> this.execute(messager)).doOnTerminate(processHandler::notifyComplete).subscribeOn(Schedulers.boundedElastic()).subscribe((res) -> messager.success("Deploy succeed!"), messager::error);
    processHandler.addProcessListener(new ProcessAdapter() {

        @Override
        public void processTerminated(@NotNull ProcessEvent event) {
            subscribe.dispose();
        }
    });
    return new DefaultExecutionResult(consoleView, processHandler);
}
Also used : Disposable(reactor.core.Disposable) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) DefaultExecutionResult(com.intellij.execution.DefaultExecutionResult) ConsoleView(com.intellij.execution.ui.ConsoleView) ProcessEvent(com.intellij.execution.process.ProcessEvent) RunProcessHandler(com.microsoft.intellij.RunProcessHandler) Nullable(org.jetbrains.annotations.Nullable)

Example 83 with ProcessEvent

use of com.intellij.execution.process.ProcessEvent in project azure-tools-for-java by Microsoft.

the class RunProcessHandler method addDefaultListener.

public void addDefaultListener() {
    ProcessListener defaultListener = new ProcessListener() {

        @Override
        public void startNotified(ProcessEvent processEvent) {
        }

        @Override
        public void processTerminated(ProcessEvent processEvent) {
        }

        @Override
        public void processWillTerminate(ProcessEvent processEvent, boolean b) {
            notifyProcessTerminated(0);
        }

        @Override
        public void onTextAvailable(ProcessEvent processEvent, Key key) {
        }
    };
    addProcessListener(defaultListener);
}
Also used : ProcessEvent(com.intellij.execution.process.ProcessEvent) ProcessListener(com.intellij.execution.process.ProcessListener) Key(com.intellij.openapi.util.Key)

Example 84 with ProcessEvent

use of com.intellij.execution.process.ProcessEvent in project azure-tools-for-java by Microsoft.

the class DockerHostRunState method executeSteps.

@Override
public String executeSteps(@NotNull RunProcessHandler processHandler, @NotNull Operation operation) throws Exception {
    final String[] runningContainerId = { null };
    processHandler.addProcessListener(new ProcessListener() {

        @Override
        public void startNotified(ProcessEvent processEvent) {
        }

        @Override
        public void processTerminated(ProcessEvent processEvent) {
        }

        @Override
        public void processWillTerminate(ProcessEvent processEvent, boolean b) {
            try {
                DockerClient docker = DockerUtil.getDockerClient(dataModel.getDockerHost(), dataModel.isTlsEnabled(), dataModel.getDockerCertPath());
                DockerUtil.stopContainer(docker, runningContainerId[0]);
            } catch (Exception e) {
            // ignore
            }
        }

        @Override
        public void onTextAvailable(ProcessEvent processEvent, Key key) {
        }
    });
    processHandler.setText("Starting job ...  ");
    String basePath = project.getBasePath();
    if (basePath == null) {
        processHandler.println("Project base path is null.", ProcessOutputTypes.STDERR);
        throw new FileNotFoundException("Project base path is null.");
    }
    // locate artifact to specified location
    String targetFilePath = dataModel.getTargetPath();
    processHandler.setText(String.format("Locating artifact ... [%s]", targetFilePath));
    // validate dockerfile
    Path targetDockerfile = Paths.get(dataModel.getDockerFilePath());
    processHandler.setText(String.format("Validating dockerfile ... [%s]", targetDockerfile));
    if (!targetDockerfile.toFile().exists()) {
        throw new FileNotFoundException("Dockerfile not found.");
    }
    // replace placeholder if exists
    String content = new String(Files.readAllBytes(targetDockerfile));
    content = content.replaceAll(Constant.DOCKERFILE_ARTIFACT_PLACEHOLDER, Paths.get(basePath).toUri().relativize(Paths.get(targetFilePath).toUri()).getPath());
    Files.write(targetDockerfile, content.getBytes());
    // build image
    String imageNameWithTag = String.format("%s:%s", dataModel.getImageName(), dataModel.getTagName());
    processHandler.setText(String.format("Building image ...  [%s]", imageNameWithTag));
    DockerClient docker = DockerUtil.getDockerClient(dataModel.getDockerHost(), dataModel.isTlsEnabled(), dataModel.getDockerCertPath());
    DockerUtil.ping(docker);
    DockerUtil.buildImage(docker, imageNameWithTag, targetDockerfile.getParent(), targetDockerfile.getFileName().toString(), new DockerProgressHandler(processHandler));
    // docker run
    String containerServerPort = getPortFromDockerfile(content);
    if (StringUtils.isBlank(containerServerPort)) {
        if (StringUtils.endsWith(targetFilePath, MavenConstants.TYPE_WAR)) {
            containerServerPort = "80";
        } else {
            containerServerPort = "8080";
        }
    }
    String containerId = DockerUtil.createContainer(docker, String.format("%s:%s", dataModel.getImageName(), dataModel.getTagName()), containerServerPort);
    runningContainerId[0] = containerId;
    Container container = DockerUtil.runContainer(docker, containerId);
    // props
    String hostname = new URI(dataModel.getDockerHost()).getHost();
    String publicPort = null;
    ImmutableList<Container.PortMapping> ports = container.ports();
    if (ports != null) {
        for (Container.PortMapping portMapping : ports) {
            if (StringUtils.equals(containerServerPort, String.valueOf(portMapping.privatePort()))) {
                publicPort = String.valueOf(portMapping.publicPort());
            }
        }
    }
    processHandler.setText(String.format(Constant.MESSAGE_CONTAINER_STARTED, (hostname != null ? hostname : "localhost") + (publicPort != null ? ":" + publicPort : "")));
    return null;
}
Also used : Path(java.nio.file.Path) DockerProgressHandler(com.microsoft.azure.toolkit.intellij.webapp.docker.utils.DockerProgressHandler) DockerClient(com.spotify.docker.client.DockerClient) ProcessEvent(com.intellij.execution.process.ProcessEvent) ProcessListener(com.intellij.execution.process.ProcessListener) FileNotFoundException(java.io.FileNotFoundException) URI(java.net.URI) FileNotFoundException(java.io.FileNotFoundException) Container(com.spotify.docker.client.messages.Container) Key(com.intellij.openapi.util.Key)

Example 85 with ProcessEvent

use of com.intellij.execution.process.ProcessEvent in project moe-ide-integration by multi-os-engine.

the class MOERefreshXcodeProject method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    final DataContext dataContext = e.getDataContext();
    final Module module = (Module) dataContext.getData(LangDataKeys.MODULE.getName());
    if (module == null) {
        Messages.showErrorDialog("Failed to locate module", "Injecting/Refreshing Xcode Settings Error");
        return;
    }
    boolean isMaven = ModuleUtils.isMOEMavenModule(module);
    if (!isMaven) {
        ProgressManager.getInstance().runProcessWithProgressSynchronously(new Runnable() {

            @Override
            public void run() {
                ProgressIndicator progress = ProgressManager.getInstance().getProgressIndicator();
                if (progress == null) {
                    progress = new EmptyProgressIndicator();
                }
                progress.pushState();
                try {
                    progress.setText(ACTION_PROGRESS_LABEL);
                    runInternal();
                } catch (final Throwable t) {
                    t.printStackTrace(System.err);
                    UIUtil.invokeLaterIfNeeded(new Runnable() {

                        @Override
                        public void run() {
                            String message = t.getMessage();
                            if (message == null || message.length() == 0) {
                                message = "Unknown error";
                            }
                            Messages.showErrorDialog(message, "Injecting/Refreshing Xcode Settings Error");
                            final MOEToolWindow toolWindow = MOEToolWindow.getInstance(module.getProject());
                            toolWindow.show();
                        }
                    });
                } finally {
                    progress.popState();
                }
            }

            private void runInternal() throws IOException, ExecutionException {
                final GeneralCommandLine commandLine = MOEGradleRunner.construct(module, "moeUpdateXcodeSettings");
                final OSProcessHandler handler = new OSProcessHandler(commandLine);
                handler.setShouldDestroyProcessRecursively(true);
                // Configure output
                final MOEToolWindow toolWindow = MOEToolWindow.getInstance(module.getProject());
                toolWindow.clear();
                handler.addProcessListener(new ProcessAdapter() {

                    @Override
                    public void onTextAvailable(ProcessEvent event, Key outputType) {
                        if (ProcessOutputTypes.STDERR.equals(outputType)) {
                            toolWindow.error(event.getText());
                        } else if (ProcessOutputTypes.STDOUT.equals(outputType)) {
                            toolWindow.log(event.getText());
                        }
                    }
                });
                handler.startNotify();
                // Start and wait
                handler.waitFor();
                final int exitValue = handler.getProcess().exitValue();
                if (exitValue != 0) {
                    throw new IOException(ACTION_TITLE + " finished with non-zero exit value (" + exitValue + ")");
                }
            }
        }, ACTION_TITLE, true, module.getProject());
    } else {
        CompilerTask compilerTask = new CompilerTask(module.getProject(), "", false, true, true, true);
        compilerTask.start(new Runnable() {

            @Override
            public void run() {
                MOEMavenTask task = new MOEMavenTask(module, ACTION_TITLE, false);
                task.setGoal("moe:updateXcodeSettings");
                if (!task.runTask()) {
                    ModuleUtils.runInDispatchedThread(new Runnable() {

                        @Override
                        public void run() {
                            Messages.showErrorDialog("Unable to refresh Xcode project", "Injecting/Refreshing Xcode Settings Error");
                        }
                    });
                }
            }
        }, null);
        ModuleUtils.runInDispatchedThread(new Runnable() {

            @Override
            public void run() {
                MOEMavenTask task = new MOEMavenTask(module, ACTION_TITLE, false);
                task.setGoal("moe:updateXcodeSettings");
                if (!task.runTask()) {
                    Messages.showErrorDialog("Unable to refresh Xcode project", "Injecting/Refreshing Xcode Settings Error");
                }
            }
        });
    }
}
Also used : EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProcessAdapter(com.intellij.execution.process.ProcessAdapter) ProcessEvent(com.intellij.execution.process.ProcessEvent) CompilerTask(com.intellij.compiler.progress.CompilerTask) IOException(java.io.IOException) MOEMavenTask(org.moe.idea.maven.MOEMavenTask) DataContext(com.intellij.openapi.actionSystem.DataContext) MOEToolWindow(org.moe.idea.ui.MOEToolWindow) EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) OSProcessHandler(com.intellij.execution.process.OSProcessHandler) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) Module(com.intellij.openapi.module.Module) ExecutionException(com.intellij.execution.ExecutionException) Key(com.intellij.openapi.util.Key)

Aggregations

ProcessEvent (com.intellij.execution.process.ProcessEvent)90 ProcessAdapter (com.intellij.execution.process.ProcessAdapter)82 Key (com.intellij.openapi.util.Key)34 OSProcessHandler (com.intellij.execution.process.OSProcessHandler)25 NotNull (org.jetbrains.annotations.NotNull)24 ExecutionException (com.intellij.execution.ExecutionException)22 ProcessHandler (com.intellij.execution.process.ProcessHandler)18 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)16 ExecutionEnvironment (com.intellij.execution.runners.ExecutionEnvironment)12 IOException (java.io.IOException)12 Nullable (org.jetbrains.annotations.Nullable)12 Project (com.intellij.openapi.project.Project)9 File (java.io.File)9 ProcessListener (com.intellij.execution.process.ProcessListener)8 RunContentDescriptor (com.intellij.execution.ui.RunContentDescriptor)7 ExecutionEnvironmentBuilder (com.intellij.execution.runners.ExecutionEnvironmentBuilder)5 ProgramRunner (com.intellij.execution.runners.ProgramRunner)5 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 DefaultRunExecutor (com.intellij.execution.executors.DefaultRunExecutor)4 Disposable (com.intellij.openapi.Disposable)4