Utils: Extract a CommandLine structure from a QtcProcess

We regularly pass around strings or filenames or pairs of strings
or filenames and stringlist etc the in the end will be used
as a kind of "command line", with quite a bit of ad-hoc user
code and QtcProcess::addArg etc to set them up and manipulate them.

Let's have a class for that concept.

Change-Id: I288ab939d853b32c717135a65242c584c2beab50
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
hjk
2019-05-28 18:59:45 +02:00
parent f50859e856
commit 4098be05b2
19 changed files with 56 additions and 25 deletions

View File

@@ -694,7 +694,8 @@ void QtcProcess::start()
const OsType osType = HostOsInfo::hostOs();
if (m_haveEnv) {
if (m_environment.size() == 0)
qWarning("QtcProcess::start: Empty environment set when running '%s'.", qPrintable(m_command));
qWarning("QtcProcess::start: Empty environment set when running '%s'.",
qPrintable(m_commandLine.executable().toString()));
env = m_environment;
QProcess::setEnvironment(env.toStringList());
@@ -705,7 +706,9 @@ void QtcProcess::start()
const QString &workDir = workingDirectory();
QString command;
QtcProcess::Arguments arguments;
bool success = prepareCommand(m_command, m_arguments, &command, &arguments, osType, &env, &workDir);
bool success = prepareCommand(m_commandLine.executable().toString(),
m_commandLine.arguments(),
&command, &arguments, osType, &env, &workDir);
if (osType == OsTypeWindows) {
QString args;
if (m_useCtrlCStub) {

View File

@@ -32,16 +32,32 @@
namespace Utils {
class AbstractMacroExpander;
class QTCREATOR_UTILS_EXPORT CommandLine
{
public:
CommandLine() {}
CommandLine(const FilePath &executable, const QString &arguments)
: m_executable(executable), m_arguments(arguments)
{}
FilePath executable() const { return m_executable; }
QString arguments() const { return m_arguments; }
private:
FilePath m_executable;
QString m_arguments;
};
class QTCREATOR_UTILS_EXPORT QtcProcess : public QProcess
{
Q_OBJECT
public:
QtcProcess(QObject *parent = nullptr);
void setEnvironment(const Environment &env)
{ m_environment = env; m_haveEnv = true; }
void setCommand(const QString &command, const QString &arguments)
{ m_command = command; m_arguments = arguments; }
void setEnvironment(const Environment &env) { m_environment = env; m_haveEnv = true; }
void setCommand(const CommandLine &cmdLine) { m_commandLine = cmdLine; }
void setUseCtrlCStub(bool enabled);
void start();
void terminate();
@@ -141,8 +157,7 @@ public:
};
private:
QString m_command;
QString m_arguments;
CommandLine m_commandLine;
Environment m_environment;
bool m_haveEnv = false;
bool m_useCtrlCStub = false;

View File

@@ -366,7 +366,7 @@ AndroidDeployQtStep::DeployErrorCode AndroidDeployQtStep::runDeploy()
}
m_process = new Utils::QtcProcess;
m_process->setCommand(m_command, args);
m_process->setCommand(Utils::CommandLine(Utils::FilePath::fromString(m_command), args));
m_process->setWorkingDirectory(m_workingDirectory);
m_process->setEnvironment(m_environment);

View File

@@ -930,7 +930,7 @@ void AndroidSdkManagerPrivate::getPendingLicense(SdkCmdFutureInterface &fi)
QtcProcess licenseCommand;
licenseCommand.setProcessEnvironment(AndroidConfigurations::toolsEnvironment(m_config));
bool reviewingLicenses = false;
licenseCommand.setCommand(m_config.sdkManagerToolPath().toString(), {"--licenses"});
licenseCommand.setCommand(CommandLine(m_config.sdkManagerToolPath(), "--licenses"));
if (Utils::HostOsInfo::isWindowsHost())
licenseCommand.setUseCtrlCStub(true);
licenseCommand.start();

View File

@@ -63,7 +63,7 @@ GdbServerProviderProcess::GdbServerProviderProcess(
void GdbServerProviderProcess::start(const ProjectExplorer::Runnable &runnable)
{
QTC_ASSERT(m_process->state() == QProcess::NotRunning, return);
m_process->setCommand(runnable.executable, runnable.commandLineArguments);
m_process->setCommand(runnable.commandLine());
m_process->start();
}

View File

@@ -129,7 +129,7 @@ ServerMode::ServerMode(const Environment &env,
qCInfo(cmakeServerMode)
<< "Preparing cmake:" << cmakeExecutable.toString() << argumentString
<< "in" << m_buildDirectory.toString();
m_cmakeProcess->setCommand(cmakeExecutable.toString(), argumentString);
m_cmakeProcess->setCommand(CommandLine(cmakeExecutable, argumentString));
// Delay start:
QTimer::singleShot(0, this, [argumentString, this] {

View File

@@ -519,7 +519,7 @@ void TeaLeafReader::startCMake(const QStringList &configurationArguments)
tr("Configuring \"%1\"").arg(m_parameters.projectName),
"CMake.Configure");
m_cmakeProcess->setCommand(cmake->cmakeExecutable().toString(), args);
m_cmakeProcess->setCommand(CommandLine(cmake->cmakeExecutable(), args));
emit configurationStarted();
m_cmakeProcess->start();
}

View File

@@ -663,7 +663,7 @@ void ExternalToolRunner::run()
this, &ExternalToolRunner::readStandardError);
if (!m_resolvedWorkingDirectory.isEmpty())
m_process->setWorkingDirectory(m_resolvedWorkingDirectory);
m_process->setCommand(m_resolvedExecutable.toString(), m_resolvedArguments);
m_process->setCommand(CommandLine(m_resolvedExecutable, m_resolvedArguments));
m_process->setEnvironment(m_resolvedEnvironment);
MessageManager::write(tr("Starting external tool \"%1\" %2")
.arg(m_resolvedExecutable.toUserOutput(), m_resolvedArguments),

View File

@@ -169,7 +169,7 @@ void ExecuteFilter::runHeadCommand()
}
MessageManager::write(tr("Starting command \"%1\".").arg(headCommand()));
m_process->setWorkingDirectory(d.workingDirectory);
m_process->setCommand(fullPath.toString(), d.arguments);
m_process->setCommand(Utils::CommandLine(fullPath, d.arguments));
m_process->start();
m_process->closeWriteChannel();
if (!m_process->waitForStarted(1000)) {

View File

@@ -32,6 +32,8 @@
#include <coreplugin/messagemanager.h>
using namespace Utils;
namespace Cppcheck {
namespace Internal {
@@ -153,7 +155,7 @@ void CppcheckRunner::checkQueued()
else
m_queue.begin().value() = files;
m_process->setCommand(m_binary, arguments);
m_process->setCommand(CommandLine(FilePath::fromString(m_binary), arguments));
m_process->start();
}

View File

@@ -113,7 +113,7 @@ public:
void start() override
{
m_proc.setCommand(m_runnable.executable, m_runnable.commandLineArguments);
m_proc.setCommand(m_runnable.commandLine());
m_proc.start();
}

View File

@@ -3556,7 +3556,8 @@ void GdbEngine::setupEngine()
gdbArgs << "-n";
showMessage("STARTING " + rp.debugger.executable + " " + gdbArgs.join(' '));
m_gdbProc.setCommand(rp.debugger.executable, QtcProcess::joinArgs(gdbArgs));
m_gdbProc.setCommand(CommandLine(FilePath::fromString(rp.debugger.executable),
QtcProcess::joinArgs(gdbArgs)));
if (QFileInfo(rp.debugger.workingDirectory).isDir())
m_gdbProc.setWorkingDirectory(rp.debugger.workingDirectory);
m_gdbProc.setEnvironment(rp.debugger.environment);

View File

@@ -207,7 +207,7 @@ void LldbEngine::setupEngine()
if (QFileInfo(runParameters().debugger.workingDirectory).isDir())
m_lldbProc.setWorkingDirectory(runParameters().debugger.workingDirectory);
m_lldbProc.setCommand(lldbCmd, QString());
m_lldbProc.setCommand(CommandLine(FilePath::fromString(lldbCmd), QString()));
m_lldbProc.start();
if (!m_lldbProc.waitForStarted()) {

View File

@@ -48,6 +48,8 @@
#include <algorithm>
#include <memory>
using namespace Utils;
namespace ProjectExplorer {
/*!
@@ -211,8 +213,8 @@ void AbstractProcessStep::doRun()
}
}
QString effectiveCommand = d->m_param.effectiveCommand().toString();
if (!QFileInfo::exists(effectiveCommand)) {
const CommandLine effectiveCommand{d->m_param.effectiveCommand(), d->m_param.effectiveArguments()};
if (!effectiveCommand.executable().exists()) {
processStartupFailed();
finish(false);
return;
@@ -222,7 +224,7 @@ void AbstractProcessStep::doRun()
d->m_process->setUseCtrlCStub(Utils::HostOsInfo::isWindowsHost());
d->m_process->setWorkingDirectory(wd.absolutePath());
d->m_process->setEnvironment(d->m_param.environment());
d->m_process->setCommand(effectiveCommand, d->m_param.effectiveArguments());
d->m_process->setCommand(effectiveCommand);
connect(d->m_process.get(), &QProcess::readyReadStandardOutput,
this, &AbstractProcessStep::processReadyReadStdOutput);

View File

@@ -378,7 +378,7 @@ void ApplicationLauncherPrivate::start(const Runnable &runnable, const IDevice::
#endif
if (!m_useTerminal) {
m_guiProcess.setCommand(runnable.executable, runnable.commandLineArguments);
m_guiProcess.setCommand(runnable.commandLine());
m_guiProcess.closeWriteChannel();
m_guiProcess.start();
} else {

View File

@@ -1549,4 +1549,9 @@ void RunWorker::stop()
reportStopped();
}
CommandLine Runnable::commandLine() const
{
return CommandLine(FilePath::fromString(executable), commandLineArguments);
}
} // namespace ProjectExplorer

View File

@@ -35,6 +35,7 @@
#include <utils/port.h>
#include <utils/processhandle.h>
#include <utils/qtcassert.h>
#include <utils/qtcprocess.h>
#include <utils/icon.h>
#include <QHash>
@@ -72,6 +73,8 @@ class PROJECTEXPLORER_EXPORT Runnable
public:
Runnable() = default;
Utils::CommandLine commandLine() const;
QString executable;
QString commandLineArguments;
QString workingDirectory;

View File

@@ -160,7 +160,7 @@ void WinRtDeviceFactory::autoDetect()
}
const QString args = QStringLiteral("--list-devices");
m_process->setCommand(runnerFilePath, args);
m_process->setCommand(CommandLine(FilePath::fromString(runnerFilePath), args));
qCDebug(winrtDeviceLog) << __FUNCTION__ << "Starting process" << runnerFilePath
<< "with arguments" << args;
MessageManager::write(runnerFilePath + QLatin1Char(' ') + args);

View File

@@ -224,7 +224,7 @@ void WinRtRunnerHelper::startWinRtRunner(const RunConf &conf)
}
process->setUseCtrlCStub(true);
process->setCommand(m_runnerFilePath, runnerArgs);
process->setCommand(CommandLine(FilePath::fromString(m_runnerFilePath), runnerArgs));
process->setEnvironment(m_environment);
process->setWorkingDirectory(QFileInfo(m_executableFilePath).absolutePath());
process->start();