mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
refactoring hg command execution process
* process Listener changed tp CapturingProcessAdapter; * string writers changed to String to reuse standard process adapter and to simplify HgCommandResult
This commit is contained in:
@@ -12,52 +12,43 @@
|
||||
// limitations under the License.
|
||||
package org.zmlx.hg4idea.execution;
|
||||
|
||||
import com.intellij.openapi.util.text.LineTokenizer;
|
||||
import com.intellij.execution.process.ProcessOutput;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public final class HgCommandResult {
|
||||
|
||||
public static final HgCommandResult CANCELLED = new HgCommandResult(new StringWriter(), new StringWriter(), 1);
|
||||
//should be deleted and use ProcessOutput without wrapper
|
||||
|
||||
private final StringWriter out;
|
||||
private final StringWriter err;
|
||||
private final int exitValue;
|
||||
public static final HgCommandResult CANCELLED = new HgCommandResult(new ProcessOutput(1));
|
||||
@NotNull ProcessOutput myOutput;
|
||||
|
||||
private List<String> outLines;
|
||||
private List<String> errLines;
|
||||
|
||||
public HgCommandResult(StringWriter out, StringWriter err, int exitValue) {
|
||||
this.out = out;
|
||||
this.err = err;
|
||||
this.exitValue = exitValue;
|
||||
public HgCommandResult(@NotNull ProcessOutput processOutput) {
|
||||
myOutput = processOutput;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<String> getOutputLines() {
|
||||
if (outLines == null) {
|
||||
outLines = Arrays.asList(LineTokenizer.tokenize(out.getBuffer(), false));
|
||||
}
|
||||
return outLines;
|
||||
return myOutput.getStdoutLines();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<String> getErrorLines() {
|
||||
if (errLines == null) {
|
||||
errLines = Arrays.asList(LineTokenizer.tokenize(err.getBuffer(), false));
|
||||
}
|
||||
return errLines;
|
||||
return myOutput.getStderrLines();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getRawOutput() {
|
||||
return out.toString();
|
||||
return myOutput.getStdout();
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public String getRawError() {
|
||||
return err.toString();
|
||||
return myOutput.getStderr();
|
||||
}
|
||||
|
||||
public int getExitValue() {
|
||||
return exitValue;
|
||||
return myOutput.getExitCode();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,10 +14,7 @@ package org.zmlx.hg4idea.execution;
|
||||
|
||||
import com.intellij.execution.ExecutionException;
|
||||
import com.intellij.execution.configurations.GeneralCommandLine;
|
||||
import com.intellij.execution.process.OSProcessHandler;
|
||||
import com.intellij.execution.process.ProcessEvent;
|
||||
import com.intellij.execution.process.ProcessListener;
|
||||
import com.intellij.execution.process.ProcessOutputTypes;
|
||||
import com.intellij.execution.process.*;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.progress.ProgressIndicator;
|
||||
import com.intellij.openapi.progress.ProgressManager;
|
||||
@@ -27,7 +24,6 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.StringWriter;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
@@ -35,7 +31,6 @@ import java.util.List;
|
||||
public final class ShellCommand {
|
||||
|
||||
private final GeneralCommandLine myCommandLine;
|
||||
private int myExitCode;
|
||||
|
||||
public ShellCommand(@Nullable List<String> commandLine, @Nullable String dir, @Nullable Charset charset) {
|
||||
if (commandLine == null || commandLine.isEmpty()) {
|
||||
@@ -56,24 +51,11 @@ public final class ShellCommand {
|
||||
|
||||
@NotNull
|
||||
public HgCommandResult execute(final boolean showTextOnIndicator) throws ShellCommandException, InterruptedException {
|
||||
final StringWriter out = new StringWriter();
|
||||
final StringWriter err = new StringWriter();
|
||||
final ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
|
||||
try {
|
||||
final Process process = myCommandLine.createProcess();
|
||||
final OSProcessHandler processHandler = new OSProcessHandler(process, myCommandLine.toString(), myCommandLine.getCharset());
|
||||
|
||||
processHandler.addProcessListener(new ProcessListener() {
|
||||
public void startNotified(final ProcessEvent event) {
|
||||
}
|
||||
|
||||
public void processTerminated(final ProcessEvent event) {
|
||||
myExitCode = event.getExitCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void processWillTerminate(ProcessEvent event, boolean willBeDestroyed) {
|
||||
}
|
||||
OSProcessHandler processHandler = new OSProcessHandler(process, myCommandLine.toString(), myCommandLine.getCharset());
|
||||
CapturingProcessAdapter outputAdapter = new CapturingProcessAdapter() {
|
||||
|
||||
@Override
|
||||
public void onTextAvailable(ProcessEvent event, Key outputType) {
|
||||
@@ -84,26 +66,25 @@ public final class ShellCommand {
|
||||
if (indicator != null && showTextOnIndicator) {
|
||||
indicator.setText2(line);
|
||||
}
|
||||
out.write(line);
|
||||
addToOutput(line, ProcessOutputTypes.STDOUT);
|
||||
}
|
||||
}
|
||||
else if (ProcessOutputTypes.STDERR == outputType) {
|
||||
while (lines.hasNext()) {
|
||||
err.write(lines.next());
|
||||
}
|
||||
else {
|
||||
super.onTextAvailable(event, outputType);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
};
|
||||
processHandler.addProcessListener(outputAdapter);
|
||||
processHandler.startNotify();
|
||||
while (!processHandler.waitFor(300)) {
|
||||
if (indicator != null && indicator.isCanceled()) {
|
||||
processHandler.destroyProcess();
|
||||
myExitCode = 255;
|
||||
outputAdapter.getOutput().setExitCode(255);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return new HgCommandResult(out, err, myExitCode);
|
||||
ProcessOutput output = outputAdapter.getOutput();
|
||||
return new HgCommandResult(output);
|
||||
}
|
||||
catch (ExecutionException e) {
|
||||
throw new ShellCommandException(e);
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.zmlx.hg4idea.push;
|
||||
|
||||
import com.intellij.execution.process.ProcessOutput;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.junit.Test;
|
||||
@@ -22,7 +23,6 @@ import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.zmlx.hg4idea.execution.HgCommandResult;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -67,9 +67,9 @@ public class HgPushParseTest {
|
||||
|
||||
@Test
|
||||
public void testValid() {
|
||||
StringWriter outWriter = new StringWriter();
|
||||
outWriter.write(myOutput);
|
||||
ProcessOutput processOutput = new ProcessOutput(0);
|
||||
processOutput.appendStdout(myOutput);
|
||||
assertEquals(" Wrong commits number for " + myOutput, myExpected,
|
||||
HgPusher.getNumberOfPushedCommits(new HgCommandResult(outWriter, new StringWriter(), 0)));
|
||||
HgPusher.getNumberOfPushedCommits(new HgCommandResult(processOutput)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user