ProjectExplorer: Use a QtcProcess member in DeviceProcess

All derived class have one one way or the other. Mid-term plan
would be to actually base DeviceProcess on QtcProcess and to
potentially dissolve it completely later.

Change-Id: Ie3bc48dcdc30bc2da5557ad26babf6959a3efa04
Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
hjk
2022-01-27 11:30:25 +01:00
parent 36a2bffc54
commit 330dfa7e84
6 changed files with 85 additions and 82 deletions

View File

@@ -107,7 +107,7 @@ public:
void start(const Runnable &runnable) override; void start(const Runnable &runnable) override;
void interrupt() override; void interrupt() override;
void terminate() override { m_process.terminate(); } void terminate() override { process()->terminate(); }
void kill() override; void kill() override;
QProcess::ProcessState state() const override; QProcess::ProcessState state() const override;
@@ -118,22 +118,18 @@ public:
QByteArray readAllStandardOutput() override; QByteArray readAllStandardOutput() override;
QByteArray readAllStandardError() override; QByteArray readAllStandardError() override;
qint64 write(const QByteArray &data) override { return m_process.write(data); } qint64 write(const QByteArray &data) override { return process()->write(data); }
private:
QtcProcess m_process;
}; };
DockerDeviceProcess::DockerDeviceProcess(const QSharedPointer<const IDevice> &device, DockerDeviceProcess::DockerDeviceProcess(const QSharedPointer<const IDevice> &device,
QObject *parent) QObject *parent)
: DeviceProcess(device, parent) : DeviceProcess(device, ProcessMode::Writer, parent)
, m_process(ProcessMode::Writer)
{ {
} }
void DockerDeviceProcess::start(const Runnable &runnable) void DockerDeviceProcess::start(const Runnable &runnable)
{ {
QTC_ASSERT(m_process.state() == QProcess::NotRunning, return); QTC_ASSERT(process()->state() == QProcess::NotRunning, return);
DockerDevice::ConstPtr dockerDevice = qSharedPointerCast<const DockerDevice>(device()); DockerDevice::ConstPtr dockerDevice = qSharedPointerCast<const DockerDevice>(device());
QTC_ASSERT(dockerDevice, return); QTC_ASSERT(dockerDevice, return);
@@ -146,65 +142,65 @@ void DockerDeviceProcess::start(const Runnable &runnable)
MessageManager::writeDisrupting(QString::fromLocal8Bit(readAllStandardError())); MessageManager::writeDisrupting(QString::fromLocal8Bit(readAllStandardError()));
}); });
disconnect(&m_process); disconnect(process());
CommandLine command = runnable.command; CommandLine command = runnable.command;
command.setExecutable( command.setExecutable(
command.executable().withNewPath(dockerDevice->mapToDevicePath(command.executable()))); command.executable().withNewPath(dockerDevice->mapToDevicePath(command.executable())));
m_process.setCommand(command); process()->setCommand(command);
m_process.setEnvironment(runnable.environment); process()->setEnvironment(runnable.environment);
m_process.setWorkingDirectory(runnable.workingDirectory); process()->setWorkingDirectory(runnable.workingDirectory);
connect(&m_process, &QtcProcess::errorOccurred, this, &DeviceProcess::errorOccurred); connect(process(), &QtcProcess::errorOccurred, this, &DeviceProcess::errorOccurred);
connect(&m_process, &QtcProcess::finished, this, &DeviceProcess::finished); connect(process(), &QtcProcess::finished, this, &DeviceProcess::finished);
connect(&m_process, &QtcProcess::readyReadStandardOutput, connect(process(), &QtcProcess::readyReadStandardOutput,
this, &DeviceProcess::readyReadStandardOutput); this, &DeviceProcess::readyReadStandardOutput);
connect(&m_process, &QtcProcess::readyReadStandardError, connect(process(), &QtcProcess::readyReadStandardError,
this, &DeviceProcess::readyReadStandardError); this, &DeviceProcess::readyReadStandardError);
connect(&m_process, &QtcProcess::started, this, &DeviceProcess::started); connect(process(), &QtcProcess::started, this, &DeviceProcess::started);
LOG("Running process:" << command.toUserOutput() LOG("Running process:" << command.toUserOutput()
<< "in" << runnable.workingDirectory.toUserOutput()); << "in" << runnable.workingDirectory.toUserOutput());
dockerDevice->runProcess(m_process); dockerDevice->runProcess(*process());
} }
void DockerDeviceProcess::interrupt() void DockerDeviceProcess::interrupt()
{ {
device()->signalOperation()->interruptProcess(m_process.processId()); device()->signalOperation()->interruptProcess(process()->processId());
} }
void DockerDeviceProcess::kill() void DockerDeviceProcess::kill()
{ {
m_process.kill(); process()->kill();
} }
QProcess::ProcessState DockerDeviceProcess::state() const QProcess::ProcessState DockerDeviceProcess::state() const
{ {
return m_process.state(); return process()->state();
} }
QProcess::ExitStatus DockerDeviceProcess::exitStatus() const QProcess::ExitStatus DockerDeviceProcess::exitStatus() const
{ {
return m_process.exitStatus(); return process()->exitStatus();
} }
int DockerDeviceProcess::exitCode() const int DockerDeviceProcess::exitCode() const
{ {
return m_process.exitCode(); return process()->exitCode();
} }
QString DockerDeviceProcess::errorString() const QString DockerDeviceProcess::errorString() const
{ {
return m_process.errorString(); return process()->errorString();
} }
QByteArray DockerDeviceProcess::readAllStandardOutput() QByteArray DockerDeviceProcess::readAllStandardOutput()
{ {
return m_process.readAllStandardOutput(); return process()->readAllStandardOutput();
} }
QByteArray DockerDeviceProcess::readAllStandardError() QByteArray DockerDeviceProcess::readAllStandardError()
{ {
return m_process.readAllStandardError(); return process()->readAllStandardError();
} }

View File

@@ -39,76 +39,75 @@ namespace Internal {
DesktopDeviceProcess::DesktopDeviceProcess(const QSharedPointer<const IDevice> &device, DesktopDeviceProcess::DesktopDeviceProcess(const QSharedPointer<const IDevice> &device,
QObject *parent) QObject *parent)
: DeviceProcess(device, parent) : DeviceProcess(device, ProcessMode::Writer, parent)
, m_process(ProcessMode::Writer)
{ {
connect(&m_process, &QtcProcess::errorOccurred, this, &DeviceProcess::errorOccurred); connect(process(), &QtcProcess::errorOccurred, this, &DeviceProcess::errorOccurred);
connect(&m_process, &QtcProcess::finished, this, &DeviceProcess::finished); connect(process(), &QtcProcess::finished, this, &DeviceProcess::finished);
connect(&m_process, &QtcProcess::readyReadStandardOutput, connect(process(), &QtcProcess::readyReadStandardOutput,
this, &DeviceProcess::readyReadStandardOutput); this, &DeviceProcess::readyReadStandardOutput);
connect(&m_process, &QtcProcess::readyReadStandardError, connect(process(), &QtcProcess::readyReadStandardError,
this, &DeviceProcess::readyReadStandardError); this, &DeviceProcess::readyReadStandardError);
connect(&m_process, &QtcProcess::started, this, &DeviceProcess::started); connect(process(), &QtcProcess::started, this, &DeviceProcess::started);
} }
void DesktopDeviceProcess::start(const Runnable &runnable) void DesktopDeviceProcess::start(const Runnable &runnable)
{ {
QTC_ASSERT(m_process.state() == QProcess::NotRunning, return); QTC_ASSERT(process()->state() == QProcess::NotRunning, return);
if (runnable.environment.size()) if (runnable.environment.size())
m_process.setEnvironment(runnable.environment); process()->setEnvironment(runnable.environment);
m_process.setWorkingDirectory(runnable.workingDirectory); process()->setWorkingDirectory(runnable.workingDirectory);
m_process.setCommand(runnable.command); process()->setCommand(runnable.command);
m_process.start(); process()->start();
} }
void DesktopDeviceProcess::interrupt() void DesktopDeviceProcess::interrupt()
{ {
device()->signalOperation()->interruptProcess(m_process.processId()); device()->signalOperation()->interruptProcess(process()->processId());
} }
void DesktopDeviceProcess::terminate() void DesktopDeviceProcess::terminate()
{ {
m_process.terminate(); process()->terminate();
} }
void DesktopDeviceProcess::kill() void DesktopDeviceProcess::kill()
{ {
m_process.kill(); process()->kill();
} }
QProcess::ProcessState DesktopDeviceProcess::state() const QProcess::ProcessState DesktopDeviceProcess::state() const
{ {
return m_process.state(); return process()->state();
} }
QProcess::ExitStatus DesktopDeviceProcess::exitStatus() const QProcess::ExitStatus DesktopDeviceProcess::exitStatus() const
{ {
return m_process.exitStatus(); return process()->exitStatus();
} }
int DesktopDeviceProcess::exitCode() const int DesktopDeviceProcess::exitCode() const
{ {
return m_process.exitCode(); return process()->exitCode();
} }
QString DesktopDeviceProcess::errorString() const QString DesktopDeviceProcess::errorString() const
{ {
return m_process.errorString(); return process()->errorString();
} }
QByteArray DesktopDeviceProcess::readAllStandardOutput() QByteArray DesktopDeviceProcess::readAllStandardOutput()
{ {
return m_process.readAllStandardOutput(); return process()->readAllStandardOutput();
} }
QByteArray DesktopDeviceProcess::readAllStandardError() QByteArray DesktopDeviceProcess::readAllStandardError()
{ {
return m_process.readAllStandardError(); return process()->readAllStandardError();
} }
qint64 DesktopDeviceProcess::write(const QByteArray &data) qint64 DesktopDeviceProcess::write(const QByteArray &data)
{ {
return m_process.write(data); return process()->write(data);
} }
} // namespace Internal } // namespace Internal

View File

@@ -53,9 +53,6 @@ public:
QByteArray readAllStandardError() override; QByteArray readAllStandardError() override;
qint64 write(const QByteArray &data) override; qint64 write(const QByteArray &data) override;
private:
Utils::QtcProcess m_process;
}; };
} // namespace Internal } // namespace Internal

View File

@@ -30,10 +30,14 @@
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
using namespace Utils;
namespace ProjectExplorer { namespace ProjectExplorer {
DeviceProcess::DeviceProcess(const IDevice::ConstPtr &device, QObject *parent) DeviceProcess::DeviceProcess(const IDevice::ConstPtr &device,
: QObject(parent), m_device(device) const QtcProcess::Setup &setup,
QObject *parent)
: QObject(parent), m_process(setup), m_device(device)
{ {
} }

View File

@@ -27,8 +27,9 @@
#include "../projectexplorer_export.h" #include "../projectexplorer_export.h"
#include <utils/qtcprocess.h>
#include <QObject> #include <QObject>
#include <QProcess>
#include <QSharedPointer> #include <QSharedPointer>
#include <QStringList> #include <QStringList>
@@ -68,10 +69,16 @@ signals:
void readyReadStandardError(); void readyReadStandardError();
protected: protected:
explicit DeviceProcess(const QSharedPointer<const IDevice> &device, QObject *parent = nullptr); explicit DeviceProcess(const QSharedPointer<const IDevice> &device,
const Utils::QtcProcess::Setup &setup,
QObject *parent = nullptr);
QSharedPointer<const IDevice> device() const; QSharedPointer<const IDevice> device() const;
Utils::QtcProcess *process() { return &m_process; }
const Utils::QtcProcess *process() const { return &m_process; }
private: private:
Utils::QtcProcess m_process;
const QSharedPointer<const IDevice> m_device; const QSharedPointer<const IDevice> m_device;
bool m_runInTerminal = false; bool m_runInTerminal = false;
}; };

View File

@@ -48,12 +48,11 @@ enum class Signal { Interrupt, Terminate, Kill };
class SshDeviceProcess::SshDeviceProcessPrivate class SshDeviceProcess::SshDeviceProcessPrivate
{ {
public: public:
SshDeviceProcessPrivate(SshDeviceProcess *q) : q(q), consoleProcess(QtcProcess::TerminalOn) {} SshDeviceProcessPrivate(SshDeviceProcess *q) : q(q) {}
SshDeviceProcess * const q; SshDeviceProcess * const q;
QSsh::SshConnection *connection = nullptr; QSsh::SshConnection *connection = nullptr;
QSsh::SshRemoteProcessPtr process; QSsh::SshRemoteProcessPtr remoteProcess;
QtcProcess consoleProcess;
Runnable runnable; Runnable runnable;
QString errorMessage; QString errorMessage;
QProcess::ExitStatus exitStatus = QProcess::NormalExit; QProcess::ExitStatus exitStatus = QProcess::NormalExit;
@@ -74,7 +73,8 @@ public:
}; };
SshDeviceProcess::SshDeviceProcess(const IDevice::ConstPtr &device, QObject *parent) SshDeviceProcess::SshDeviceProcess(const IDevice::ConstPtr &device, QObject *parent)
: DeviceProcess(device, parent), d(std::make_unique<SshDeviceProcessPrivate>(this)) : DeviceProcess(device, QtcProcess::TerminalOn, parent),
d(std::make_unique<SshDeviceProcessPrivate>(this))
{ {
connect(&d->killTimer, &QTimer::timeout, this, &SshDeviceProcess::handleKillOperationTimeout); connect(&d->killTimer, &QTimer::timeout, this, &SshDeviceProcess::handleKillOperationTimeout);
} }
@@ -185,32 +185,32 @@ void SshDeviceProcess::handleConnected()
QTC_ASSERT(d->state == SshDeviceProcessPrivate::Connecting, return); QTC_ASSERT(d->state == SshDeviceProcessPrivate::Connecting, return);
d->setState(SshDeviceProcessPrivate::Connected); d->setState(SshDeviceProcessPrivate::Connected);
d->process = runInTerminal() && d->runnable.command.isEmpty() d->remoteProcess = runInTerminal() && d->runnable.command.isEmpty()
? d->connection->createRemoteShell() ? d->connection->createRemoteShell()
: d->connection->createRemoteProcess(fullCommandLine(d->runnable)); : d->connection->createRemoteProcess(fullCommandLine(d->runnable));
const QString display = d->displayName(); const QString display = d->displayName();
if (!display.isEmpty()) if (!display.isEmpty())
d->process->requestX11Forwarding(display); d->remoteProcess->requestX11Forwarding(display);
if (runInTerminal()) { if (runInTerminal()) {
connect(&d->consoleProcess, &QtcProcess::errorOccurred, connect(process(), &QtcProcess::errorOccurred,
this, &DeviceProcess::errorOccurred); this, &DeviceProcess::errorOccurred);
connect(&d->consoleProcess, &QtcProcess::started, connect(process(), &QtcProcess::started,
this, &SshDeviceProcess::handleProcessStarted); this, &SshDeviceProcess::handleProcessStarted);
connect(&d->consoleProcess, &QtcProcess::finished, connect(process(), &QtcProcess::finished,
this, [this] { handleProcessFinished(d->consoleProcess.errorString()); }); this, [this] { handleProcessFinished(process()->errorString()); });
d->consoleProcess.setAbortOnMetaChars(false); process()->setAbortOnMetaChars(false);
d->consoleProcess.setCommand(d->process->fullLocalCommandLine(true)); process()->setCommand(d->remoteProcess->fullLocalCommandLine(true));
d->consoleProcess.start(); process()->start();
} else { } else {
connect(d->process.get(), &QSsh::SshRemoteProcess::started, connect(d->remoteProcess.get(), &QSsh::SshRemoteProcess::started,
this, &SshDeviceProcess::handleProcessStarted); this, &SshDeviceProcess::handleProcessStarted);
connect(d->process.get(), &QSsh::SshRemoteProcess::done, connect(d->remoteProcess.get(), &QSsh::SshRemoteProcess::done,
this, &SshDeviceProcess::handleProcessFinished); this, &SshDeviceProcess::handleProcessFinished);
connect(d->process.get(), &QSsh::SshRemoteProcess::readyReadStandardOutput, connect(d->remoteProcess.get(), &QSsh::SshRemoteProcess::readyReadStandardOutput,
this, &SshDeviceProcess::handleStdout); this, &SshDeviceProcess::handleStdout);
connect(d->process.get(), &QSsh::SshRemoteProcess::readyReadStandardError, connect(d->remoteProcess.get(), &QSsh::SshRemoteProcess::readyReadStandardError,
this, &SshDeviceProcess::handleStderr); this, &SshDeviceProcess::handleStderr);
d->process->start(); d->remoteProcess->start();
} }
} }
@@ -251,7 +251,7 @@ void SshDeviceProcess::handleProcessStarted()
void SshDeviceProcess::handleProcessFinished(const QString &error) void SshDeviceProcess::handleProcessFinished(const QString &error)
{ {
d->errorMessage = error; d->errorMessage = error;
d->exitCode = runInTerminal() ? d->consoleProcess.exitCode() : d->process->exitCode(); d->exitCode = runInTerminal() ? process()->exitCode() : d->remoteProcess->exitCode();
if (d->killOperation && error.isEmpty()) if (d->killOperation && error.isEmpty())
d->errorMessage = tr("The process was ended forcefully."); d->errorMessage = tr("The process was ended forcefully.");
d->setState(SshDeviceProcessPrivate::Inactive); d->setState(SshDeviceProcessPrivate::Inactive);
@@ -260,7 +260,7 @@ void SshDeviceProcess::handleProcessFinished(const QString &error)
void SshDeviceProcess::handleStdout() void SshDeviceProcess::handleStdout()
{ {
QByteArray output = d->process->readAllStandardOutput(); QByteArray output = d->remoteProcess->readAllStandardOutput();
if (output.isEmpty()) if (output.isEmpty())
return; return;
d->stdOut += output; d->stdOut += output;
@@ -269,7 +269,7 @@ void SshDeviceProcess::handleStdout()
void SshDeviceProcess::handleStderr() void SshDeviceProcess::handleStderr()
{ {
QByteArray output = d->process->readAllStandardError(); QByteArray output = d->remoteProcess->readAllStandardError();
if (output.isEmpty()) if (output.isEmpty())
return; return;
d->stdErr += output; d->stdErr += output;
@@ -356,12 +356,12 @@ void SshDeviceProcess::SshDeviceProcessPrivate::setState(SshDeviceProcess::SshDe
killOperation->disconnect(q); killOperation->disconnect(q);
killOperation.clear(); killOperation.clear();
if (q->runInTerminal()) if (q->runInTerminal())
QMetaObject::invokeMethod(&consoleProcess, &QtcProcess::stopProcess, Qt::QueuedConnection); QMetaObject::invokeMethod(q->process(), &QtcProcess::stopProcess, Qt::QueuedConnection);
} }
killTimer.stop(); killTimer.stop();
consoleProcess.disconnect(); q->process()->disconnect();
if (process) if (remoteProcess)
process->disconnect(q); remoteProcess->disconnect(q);
if (connection) { if (connection) {
connection->disconnect(q); connection->disconnect(q);
QSsh::SshConnectionManager::releaseConnection(connection); QSsh::SshConnectionManager::releaseConnection(connection);
@@ -372,7 +372,7 @@ void SshDeviceProcess::SshDeviceProcessPrivate::setState(SshDeviceProcess::SshDe
qint64 SshDeviceProcess::write(const QByteArray &data) qint64 SshDeviceProcess::write(const QByteArray &data)
{ {
QTC_ASSERT(!runInTerminal(), return -1); QTC_ASSERT(!runInTerminal(), return -1);
return d->process->write(data); return d->remoteProcess->write(data);
} }
} // namespace ProjectExplorer } // namespace ProjectExplorer