use of com.intellij.execution.process.ProcessEvent in project android by JetBrains.
the class AndroidCommonUtils method executeZipAlign.
@Nullable
public static String executeZipAlign(@NotNull String zipAlignPath, @NotNull File source, @NotNull File destination) {
List<String> commandLine = Arrays.asList(zipAlignPath, "-f", "4", source.getAbsolutePath(), destination.getAbsolutePath());
final ProcessBuilder processBuilder = new ProcessBuilder(commandLine);
BaseOSProcessHandler handler;
try {
handler = new BaseOSProcessHandler(processBuilder.start(), StringUtil.join(commandLine, " "), null);
} catch (IOException e) {
return e.getMessage();
}
final StringBuilder builder = new StringBuilder();
handler.addProcessListener(new ProcessAdapter() {
@Override
public void onTextAvailable(ProcessEvent event, Key outputType) {
builder.append(event.getText());
}
});
handler.startNotify();
handler.waitFor();
int exitCode = handler.getProcess().exitValue();
return exitCode != 0 ? builder.toString() : null;
}
use of com.intellij.execution.process.ProcessEvent in project android by JetBrains.
the class AndroidUtils method executeCommand.
@NotNull
public static ExecutionStatus executeCommand(@NotNull GeneralCommandLine commandLine, @Nullable final OutputProcessor processor, @Nullable WaitingStrategies.Strategy strategy) throws ExecutionException {
LOG.info(commandLine.getCommandLineString());
OSProcessHandler handler = new OSProcessHandler(commandLine);
final ProcessAdapter listener = new ProcessAdapter() {
@Override
public void onTextAvailable(final ProcessEvent event, final Key outputType) {
if (processor != null) {
final String message = event.getText();
processor.onTextAvailable(message);
}
}
};
if (!(strategy instanceof WaitingStrategies.DoNotWait)) {
handler.addProcessListener(listener);
}
handler.startNotify();
try {
if (!(strategy instanceof WaitingStrategies.WaitForever)) {
if (strategy instanceof WaitingStrategies.WaitForTime) {
handler.waitFor(((WaitingStrategies.WaitForTime) strategy).getTimeMs());
}
} else {
handler.waitFor();
}
} catch (ProcessCanceledException e) {
return ExecutionStatus.ERROR;
}
if (!handler.isProcessTerminated()) {
return ExecutionStatus.TIMEOUT;
}
if (!(strategy instanceof WaitingStrategies.DoNotWait)) {
handler.removeProcessListener(listener);
}
int exitCode = handler.getProcess().exitValue();
return exitCode == 0 ? ExecutionStatus.SUCCESS : ExecutionStatus.ERROR;
}
use of com.intellij.execution.process.ProcessEvent in project intellij-community by JetBrains.
the class ExternalSystemUtil method runTask.
public static void runTask(@NotNull final ExternalSystemTaskExecutionSettings taskSettings, @NotNull final String executorId, @NotNull final Project project, @NotNull final ProjectSystemId externalSystemId, @Nullable final TaskCallback callback, @NotNull final ProgressExecutionMode progressExecutionMode, boolean activateToolWindowBeforeRun) {
ExecutionEnvironment environment = createExecutionEnvironment(project, externalSystemId, taskSettings, executorId);
if (environment == null)
return;
RunnerAndConfigurationSettings runnerAndConfigurationSettings = environment.getRunnerAndConfigurationSettings();
assert runnerAndConfigurationSettings != null;
runnerAndConfigurationSettings.setActivateToolWindowBeforeRun(activateToolWindowBeforeRun);
final TaskUnderProgress task = new TaskUnderProgress() {
@Override
public void execute(@NotNull ProgressIndicator indicator) {
indicator.setIndeterminate(true);
final Semaphore targetDone = new Semaphore();
final Ref<Boolean> result = new Ref<>(false);
final Disposable disposable = Disposer.newDisposable();
project.getMessageBus().connect(disposable).subscribe(ExecutionManager.EXECUTION_TOPIC, new ExecutionListener() {
public void processStartScheduled(@NotNull final String executorIdLocal, @NotNull final ExecutionEnvironment environmentLocal) {
if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
targetDone.down();
}
}
public void processNotStarted(@NotNull final String executorIdLocal, @NotNull final ExecutionEnvironment environmentLocal) {
if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
targetDone.up();
}
}
public void processStarted(@NotNull final String executorIdLocal, @NotNull final ExecutionEnvironment environmentLocal, @NotNull final ProcessHandler handler) {
if (executorId.equals(executorIdLocal) && environment.equals(environmentLocal)) {
handler.addProcessListener(new ProcessAdapter() {
public void processTerminated(ProcessEvent event) {
result.set(event.getExitCode() == 0);
targetDone.up();
}
});
}
}
});
try {
ApplicationManager.getApplication().invokeAndWait(() -> {
try {
environment.getRunner().execute(environment);
} catch (ExecutionException e) {
targetDone.up();
LOG.error(e);
}
}, ModalityState.defaultModalityState());
} catch (Exception e) {
LOG.error(e);
Disposer.dispose(disposable);
return;
}
targetDone.waitFor();
Disposer.dispose(disposable);
if (callback != null) {
if (result.get()) {
callback.onSuccess();
} else {
callback.onFailure();
}
}
}
};
final String title = AbstractExternalSystemTaskConfigurationType.generateName(project, taskSettings);
switch(progressExecutionMode) {
case NO_PROGRESS_SYNC:
task.execute(new EmptyProgressIndicator());
break;
case MODAL_SYNC:
new Task.Modal(project, title, true) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
task.execute(indicator);
}
}.queue();
break;
case NO_PROGRESS_ASYNC:
ApplicationManager.getApplication().executeOnPooledThread(() -> task.execute(new EmptyProgressIndicator()));
break;
case IN_BACKGROUND_ASYNC:
new Task.Backgroundable(project, title) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
task.execute(indicator);
}
}.queue();
break;
case START_IN_FOREGROUND_ASYNC:
new Task.Backgroundable(project, title, true, PerformInBackgroundOption.DEAF) {
@Override
public void run(@NotNull ProgressIndicator indicator) {
task.execute(indicator);
}
}.queue();
}
}
use of com.intellij.execution.process.ProcessEvent in project intellij-community by JetBrains.
the class RunContentExecutor method run.
public void run() {
FileDocumentManager.getInstance().saveAllDocuments();
// Use user-provided console if exist. Create new otherwise
ConsoleView view = (myUserProvidedConsole != null ? myUserProvidedConsole : createConsole());
view.attachToProcess(myProcess);
if (myAfterCompletion != null) {
myProcess.addProcessListener(new ProcessAdapter() {
@Override
public void processTerminated(ProcessEvent event) {
ApplicationManager.getApplication().invokeLater(myAfterCompletion);
}
});
}
myProcess.startNotify();
}
use of com.intellij.execution.process.ProcessEvent in project intellij-community by JetBrains.
the class ExecutionManagerImpl method startRunProfile.
@Override
public void startRunProfile(@NotNull final RunProfileStarter starter, @NotNull final RunProfileState state, @NotNull final ExecutionEnvironment environment) {
final Project project = environment.getProject();
RunContentDescriptor reuseContent = getContentManager().getReuseContent(environment);
if (reuseContent != null) {
reuseContent.setExecutionId(environment.getExecutionId());
environment.setContentToReuse(reuseContent);
}
final Executor executor = environment.getExecutor();
project.getMessageBus().syncPublisher(EXECUTION_TOPIC).processStartScheduled(executor.getId(), environment);
Runnable startRunnable;
startRunnable = () -> {
if (project.isDisposed()) {
return;
}
RunProfile profile = environment.getRunProfile();
project.getMessageBus().syncPublisher(EXECUTION_TOPIC).processStarting(executor.getId(), environment);
starter.executeAsync(state, environment).done(descriptor -> {
AppUIUtil.invokeOnEdt(() -> {
if (descriptor != null) {
final Trinity<RunContentDescriptor, RunnerAndConfigurationSettings, Executor> trinity = Trinity.create(descriptor, environment.getRunnerAndConfigurationSettings(), executor);
myRunningConfigurations.add(trinity);
Disposer.register(descriptor, () -> myRunningConfigurations.remove(trinity));
getContentManager().showRunContent(executor, descriptor, environment.getContentToReuse());
final ProcessHandler processHandler = descriptor.getProcessHandler();
if (processHandler != null) {
if (!processHandler.isStartNotified()) {
processHandler.startNotify();
}
project.getMessageBus().syncPublisher(EXECUTION_TOPIC).processStarted(executor.getId(), environment, processHandler);
ProcessExecutionListener listener = new ProcessExecutionListener(project, executor.getId(), environment, processHandler, descriptor);
processHandler.addProcessListener(listener);
boolean terminating = processHandler.isProcessTerminating();
boolean terminated = processHandler.isProcessTerminated();
if (terminating || terminated) {
listener.processWillTerminate(new ProcessEvent(processHandler), false);
if (terminated) {
int exitCode = processHandler.isStartNotified() ? processHandler.getExitCode() : -1;
listener.processTerminated(new ProcessEvent(processHandler, exitCode));
}
}
}
environment.setContentToReuse(descriptor);
} else {
project.getMessageBus().syncPublisher(EXECUTION_TOPIC).processNotStarted(executor.getId(), environment);
}
}, o -> project.isDisposed());
}).rejected(e -> {
if (!(e instanceof ProcessCanceledException)) {
ExecutionException error = e instanceof ExecutionException ? (ExecutionException) e : new ExecutionException(e);
ExecutionUtil.handleExecutionError(project, ExecutionManager.getInstance(project).getContentManager().getToolWindowIdByEnvironment(environment), profile, error);
}
LOG.info(e);
project.getMessageBus().syncPublisher(EXECUTION_TOPIC).processNotStarted(executor.getId(), environment);
});
};
if (ApplicationManager.getApplication().isUnitTestMode() && !myForceCompilationInTests) {
startRunnable.run();
} else {
compileAndRun(() -> TransactionGuard.submitTransaction(project, startRunnable), environment, state, () -> {
if (!project.isDisposed()) {
project.getMessageBus().syncPublisher(EXECUTION_TOPIC).processNotStarted(executor.getId(), environment);
}
});
}
}
Aggregations