Cleanup (PTY runner decoupled from GeneralCommandLine)

This commit is contained in:
Roman Shevchenko
2013-12-19 21:38:00 +04:00
parent f34a02493f
commit 1da12c6205
2 changed files with 69 additions and 38 deletions
@@ -15,7 +15,6 @@
*/
package com.intellij.execution.configurations;
import com.google.common.collect.Maps;
import com.intellij.execution.CommandLineUtil;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.Platform;
@@ -27,12 +26,10 @@ import com.intellij.openapi.util.SystemInfo;
import com.intellij.openapi.util.UserDataHolder;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.CharsetToolkit;
import com.intellij.util.ArrayUtil;
import com.intellij.util.EnvironmentUtil;
import com.intellij.util.PlatformUtils;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.text.CaseInsensitiveStringHashingStrategy;
import com.pty4j.PtyProcess;
import gnu.trove.THashMap;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
@@ -65,10 +62,8 @@ public class GeneralCommandLine implements UserDataHolder {
private Charset myCharset = CharsetToolkit.getDefaultSystemCharset();
private boolean myRedirectErrorStream = false;
private Map<Object, Object> myUserData = null;
private boolean myStartProcessWithPty = false;
public GeneralCommandLine() {
}
public GeneralCommandLine() { }
public GeneralCommandLine(@NotNull String... command) {
this(Arrays.asList(command));
@@ -147,15 +142,6 @@ public class GeneralCommandLine implements UserDataHolder {
return myPassParentEnvironment;
}
/**
* If argument is true the process created with Pseudo-Terminal (PTY).
* This works only on Unix. On Windows the option is ignored.
*/
public GeneralCommandLine withPty(boolean startProcessWithPty) {
myStartProcessWithPty = startProcessWithPty;
return this;
}
public void addParameters(final String... parameters) {
for (String parameter : parameters) {
addParameter(parameter);
@@ -185,6 +171,10 @@ public class GeneralCommandLine implements UserDataHolder {
myCharset = charset;
}
public boolean isRedirectErrorStream() {
return myRedirectErrorStream;
}
public void setRedirectErrorStream(final boolean redirectErrorStream) {
myRedirectErrorStream = redirectErrorStream;
}
@@ -255,20 +245,6 @@ public class GeneralCommandLine implements UserDataHolder {
}
try {
if (myStartProcessWithPty && SystemInfo.isUnix) {
try {
Map<String, String> env = Maps.newHashMap();
setupEnvironment(env);
if (myRedirectErrorStream) {
LOG.error("Launching process with PTY and redirected error stream is unsupported yet");
}
return PtyProcessHolder.doExec(myWorkDirectory, commands, env);
}
catch (Throwable e) {
LOG.error("Couldn't run process with PTY", e);
}
}
return startProcess(commands);
}
catch (IOException e) {
@@ -277,13 +253,7 @@ public class GeneralCommandLine implements UserDataHolder {
}
}
private static class PtyProcessHolder { // holder for lazy PtyProcess class loading
private static Process doExec(File workDirectory, List<String> commands, Map<String, String> env) throws IOException {
return PtyProcess.exec(ArrayUtil.toStringArray(commands), env, workDirectory != null ? workDirectory.getPath() : null, true);
}
}
private Process startProcess(List<String> commands) throws IOException {
protected Process startProcess(@NotNull List<String> commands) throws IOException {
ProcessBuilder builder = new ProcessBuilder(commands);
setupEnvironment(builder.environment());
builder.directory(myWorkDirectory);
@@ -304,11 +274,11 @@ public class GeneralCommandLine implements UserDataHolder {
}
}
private void setupEnvironment(final Map<String, String> environment) {
protected void setupEnvironment(@NotNull Map<String, String> environment) {
environment.clear();
if (myPassParentEnvironment) {
environment.putAll(PlatformUtils.isAppCode() ? System.getenv() // Temporarily fix for OC-8606
environment.putAll(PlatformUtils.isAppCode() ? System.getenv() // Temporarily fix for OC-8606
: EnvironmentUtil.getEnvironmentMap());
}
@@ -0,0 +1,61 @@
/*
* Copyright 2000-2013 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.execution.configurations;
import com.google.common.collect.Maps;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.util.ArrayUtil;
import com.pty4j.PtyProcess;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
/**
* A flavor of GeneralCommandLine to start processes with Pseudo-Terminal (PTY).
*
* Note: this works only on Unix, on Windows regular processes are used instead.
*/
public class PtyCommandLine extends GeneralCommandLine {
private static final Logger LOG = Logger.getInstance("#com.intellij.execution.configurations.PtyCommandLine");
public PtyCommandLine() { }
@Override
protected Process startProcess(@NotNull List<String> commands) throws IOException {
if (SystemInfo.isUnix) {
try {
Map<String, String> env = Maps.newHashMap();
setupEnvironment(env);
if (isRedirectErrorStream()) {
LOG.error("Launching process with PTY and redirected error stream is unsupported yet");
}
File workDirectory = getWorkDirectory();
return PtyProcess.exec(ArrayUtil.toStringArray(commands), env, workDirectory != null ? workDirectory.getPath() : null, true);
}
catch (Throwable e) {
LOG.error("Couldn't run process with PTY", e);
}
}
return super.startProcess(commands);
}
}