From d1be129abeb2d152be30590d6a1c11d7393796d1 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Tue, 5 Jul 2016 13:20:10 +0200 Subject: [PATCH] Utils::ShellCommand: Add runSynchronous method Add runSynchronous method and move code from runCommand there. Change-Id: Ic7c452d583fd88468fc6466a683159ecd8328b48 Reviewed-by: Tobias Hunger --- src/libs/utils/shellcommand.cpp | 113 +++++++++++++++++--------------- src/libs/utils/shellcommand.h | 4 ++ 2 files changed, 65 insertions(+), 52 deletions(-) diff --git a/src/libs/utils/shellcommand.cpp b/src/libs/utils/shellcommand.cpp index a5681b7575f..f18dad69d4e 100644 --- a/src/libs/utils/shellcommand.cpp +++ b/src/libs/utils/shellcommand.cpp @@ -326,59 +326,10 @@ Utils::SynchronousProcessResponse ShellCommand::runCommand(const Utils::FileName if (!(d->m_flags & SuppressCommandLogging)) proxy->appendCommand(dir, binary, arguments); - if (d->m_flags & FullySynchronously) { + if (d->m_flags & FullySynchronously) response = runFullySynchronous(binary, arguments, proxy.data(), timeoutS, dir, interpreter); - } else { - Utils::SynchronousProcess process; - process.setExitCodeInterpreter(interpreter); - connect(this, &ShellCommand::terminate, &process, &Utils::SynchronousProcess::terminate); - process.setWorkingDirectory(dir); - - process.setProcessEnvironment(processEnvironment()); - process.setTimeoutS(timeoutS); - if (d->m_codec) - process.setCodec(d->m_codec); - - process.setFlags(processFlags()); - - // connect stderr to the output window if desired - if (d->m_flags & MergeOutputChannels) { - process.setProcessChannelMode(QProcess::MergedChannels); - } else if (d->m_progressiveOutput - || !(d->m_flags & SuppressStdErr)) { - process.setStdErrBufferedSignalsEnabled(true); - connect(&process, &Utils::SynchronousProcess::stdErrBuffered, - this, [this, proxy](const QString &text) - { - if (!(d->m_flags & SuppressStdErr)) - proxy->appendError(text); - if (d->m_progressiveOutput) - emit stdErrText(text); - }); - } - - // connect stdout to the output window if desired - if (d->m_progressParser || d->m_progressiveOutput || (d->m_flags & ShowStdOut)) { - process.setStdOutBufferedSignalsEnabled(true); - connect(&process, &Utils::SynchronousProcess::stdOutBuffered, - this, [this, proxy](const QString &text) - { - if (d->m_progressParser) - d->m_progressParser->parseProgress(text); - if (d->m_flags & ShowStdOut) - proxy->append(text); - if (d->m_progressiveOutput) { - emit stdOutText(text); - d->m_hadOutput = true; - } - }); - } - - process.setTimeOutMessageBoxEnabled(true); - - // Run! - response = process.runBlocking(binary.toString(), arguments); - } + else + response = runSynchronous(binary, arguments, proxy.data(), timeoutS, dir, interpreter); if (!d->m_aborted) { // Success/Fail message in appropriate window? @@ -454,6 +405,64 @@ Utils::SynchronousProcessResponse ShellCommand::runFullySynchronous(const Utils: return response; } +SynchronousProcessResponse ShellCommand::runSynchronous(const FileName &binary, + const QStringList &arguments, + OutputProxy *proxy, + int timeoutS, + const QString &workingDirectory, + const ExitCodeInterpreter &interpreter) +{ + Utils::SynchronousProcess process; + process.setExitCodeInterpreter(interpreter); + connect(this, &ShellCommand::terminate, &process, &Utils::SynchronousProcess::terminate); + process.setWorkingDirectory(workingDirectory); + + process.setProcessEnvironment(processEnvironment()); + process.setTimeoutS(timeoutS); + if (d->m_codec) + process.setCodec(d->m_codec); + + process.setFlags(processFlags()); + + // connect stderr to the output window if desired + if (d->m_flags & MergeOutputChannels) { + process.setProcessChannelMode(QProcess::MergedChannels); + } else if (d->m_progressiveOutput + || !(d->m_flags & SuppressStdErr)) { + process.setStdErrBufferedSignalsEnabled(true); + connect(&process, &Utils::SynchronousProcess::stdErrBuffered, + this, [this, proxy](const QString &text) + { + if (!(d->m_flags & SuppressStdErr)) + proxy->appendError(text); + if (d->m_progressiveOutput) + emit stdErrText(text); + }); + } + + // connect stdout to the output window if desired + if (d->m_progressParser || d->m_progressiveOutput || (d->m_flags & ShowStdOut)) { + process.setStdOutBufferedSignalsEnabled(true); + connect(&process, &Utils::SynchronousProcess::stdOutBuffered, + this, [this, proxy](const QString &text) + { + if (d->m_progressParser) + d->m_progressParser->parseProgress(text); + if (d->m_flags & ShowStdOut) + proxy->append(text); + if (d->m_progressiveOutput) { + emit stdOutText(text); + d->m_hadOutput = true; + } + }); + } + + process.setTimeOutMessageBoxEnabled(true); + + // Run! + return process.runBlocking(binary.toString(), arguments); +} + const QVariant &ShellCommand::cookie() const { return d->m_cookie; diff --git a/src/libs/utils/shellcommand.h b/src/libs/utils/shellcommand.h index 71667edf8c3..480865154b4 100644 --- a/src/libs/utils/shellcommand.h +++ b/src/libs/utils/shellcommand.h @@ -166,6 +166,10 @@ private: OutputProxy *proxy, int timeoutS, const QString &workingDirectory, const ExitCodeInterpreter &interpreter = defaultExitCodeInterpreter); + SynchronousProcessResponse runSynchronous(const FileName &binary, const QStringList &arguments, + OutputProxy *proxy, + int timeoutS, const QString &workingDirectory, + const ExitCodeInterpreter &interpreter = defaultExitCodeInterpreter); class Internal::ShellCommandPrivate *const d; };