Utils::ShellCommand: Add runSynchronous method

Add runSynchronous method and move code from runCommand there.

Change-Id: Ic7c452d583fd88468fc6466a683159ecd8328b48
Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
Tobias Hunger
2016-07-05 13:20:10 +02:00
parent c2075f6a73
commit d1be129abe
2 changed files with 65 additions and 52 deletions

View File

@@ -326,59 +326,10 @@ Utils::SynchronousProcessResponse ShellCommand::runCommand(const Utils::FileName
if (!(d->m_flags & SuppressCommandLogging)) if (!(d->m_flags & SuppressCommandLogging))
proxy->appendCommand(dir, binary, arguments); proxy->appendCommand(dir, binary, arguments);
if (d->m_flags & FullySynchronously) { if (d->m_flags & FullySynchronously)
response = runFullySynchronous(binary, arguments, proxy.data(), timeoutS, dir, interpreter); response = runFullySynchronous(binary, arguments, proxy.data(), timeoutS, dir, interpreter);
} else { else
Utils::SynchronousProcess process; response = runSynchronous(binary, arguments, proxy.data(), timeoutS, dir, interpreter);
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);
}
if (!d->m_aborted) { if (!d->m_aborted) {
// Success/Fail message in appropriate window? // Success/Fail message in appropriate window?
@@ -454,6 +405,64 @@ Utils::SynchronousProcessResponse ShellCommand::runFullySynchronous(const Utils:
return response; 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 const QVariant &ShellCommand::cookie() const
{ {
return d->m_cookie; return d->m_cookie;

View File

@@ -166,6 +166,10 @@ private:
OutputProxy *proxy, OutputProxy *proxy,
int timeoutS, const QString &workingDirectory, int timeoutS, const QString &workingDirectory,
const ExitCodeInterpreter &interpreter = defaultExitCodeInterpreter); 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; class Internal::ShellCommandPrivate *const d;
}; };