From 1da12c620597edd27ff5089343e094205600bc2a Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Thu, 19 Dec 2013 21:38:00 +0400 Subject: [PATCH] Cleanup (PTY runner decoupled from GeneralCommandLine) --- .../configurations/GeneralCommandLine.java | 46 +++----------- .../configurations/PtyCommandLine.java | 61 +++++++++++++++++++ 2 files changed, 69 insertions(+), 38 deletions(-) create mode 100644 platform/platform-api/src/com/intellij/execution/configurations/PtyCommandLine.java diff --git a/platform/platform-api/src/com/intellij/execution/configurations/GeneralCommandLine.java b/platform/platform-api/src/com/intellij/execution/configurations/GeneralCommandLine.java index 0b6dc4ca270f..a42a1bfab18a 100644 --- a/platform/platform-api/src/com/intellij/execution/configurations/GeneralCommandLine.java +++ b/platform/platform-api/src/com/intellij/execution/configurations/GeneralCommandLine.java @@ -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 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 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 commands, Map env) throws IOException { - return PtyProcess.exec(ArrayUtil.toStringArray(commands), env, workDirectory != null ? workDirectory.getPath() : null, true); - } - } - - private Process startProcess(List commands) throws IOException { + protected Process startProcess(@NotNull List 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 environment) { + protected void setupEnvironment(@NotNull Map 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()); } diff --git a/platform/platform-api/src/com/intellij/execution/configurations/PtyCommandLine.java b/platform/platform-api/src/com/intellij/execution/configurations/PtyCommandLine.java new file mode 100644 index 000000000000..5e3d74caa1f6 --- /dev/null +++ b/platform/platform-api/src/com/intellij/execution/configurations/PtyCommandLine.java @@ -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 commands) throws IOException { + if (SystemInfo.isUnix) { + try { + Map 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); + } +}