QmlPreview: Have a dedicated run worker factory class

More similar to what the rest is doing or heading to.

Change-Id: I835ef19810cbce146d3ae22b3881c0e89d1b1fb9
Reviewed-by: Christian Stenger <christian.stenger@qt.io>
This commit is contained in:
hjk
2023-01-05 13:15:04 +01:00
parent 67195f4270
commit 53f46e0643
3 changed files with 50 additions and 44 deletions

View File

@@ -138,12 +138,7 @@ public:
QString m_localeIsoCode; QString m_localeIsoCode;
QmlDebugTranslationClientCreator m_createDebugTranslationClientMethod; QmlDebugTranslationClientCreator m_createDebugTranslationClientMethod;
RunWorkerFactory localRunWorkerFactory{ LocalQmlPreviewSupportFactory localRunWorkerFactory;
RunWorkerFactory::make<LocalQmlPreviewSupport>(),
{Constants::QML_PREVIEW_RUN_MODE},
{}, // All runconfig.
{Constants::DESKTOP_DEVICE_TYPE}
};
RunWorkerFactory runWorkerFactory{ RunWorkerFactory runWorkerFactory{
[this](RunControl *runControl) { [this](RunControl *runControl) {

View File

@@ -7,6 +7,7 @@
#include <qmlprojectmanager/qmlmainfileaspect.h> #include <qmlprojectmanager/qmlmainfileaspect.h>
#include <projectexplorer/projectexplorer.h> #include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectexplorerconstants.h>
#include <projectexplorer/session.h> #include <projectexplorer/session.h>
#include <projectexplorer/target.h> #include <projectexplorer/target.h>
@@ -14,11 +15,12 @@
#include <qtsupport/baseqtversion.h> #include <qtsupport/baseqtversion.h>
#include <qtsupport/qtkitinformation.h> #include <qtsupport/qtkitinformation.h>
#include <utils/filepath.h>
#include <utils/port.h> #include <utils/port.h>
#include <utils/qtcprocess.h> #include <utils/qtcprocess.h>
#include <utils/url.h> #include <utils/url.h>
#include <utils/fileutils.h>
using namespace ProjectExplorer;
using namespace Utils; using namespace Utils;
namespace QmlPreview { namespace QmlPreview {
@@ -60,10 +62,10 @@ QmlPreviewRunner::QmlPreviewRunner(const QmlPreviewRunnerSetting &settings)
if (!runControl()->isRunning()) if (!runControl()->isRunning())
return; return;
this->connect(runControl(), &ProjectExplorer::RunControl::stopped, [this]() { this->connect(runControl(), &RunControl::stopped, [this] {
auto rc = new ProjectExplorer::RunControl(ProjectExplorer::Constants::QML_PREVIEW_RUN_MODE); auto rc = new RunControl(ProjectExplorer::Constants::QML_PREVIEW_RUN_MODE);
rc->copyDataFromRunControl(runControl()); rc->copyDataFromRunControl(runControl());
ProjectExplorer::ProjectExplorerPlugin::startRunControl(rc); ProjectExplorerPlugin::startRunControl(rc);
}); });
runControl()->initiateStop(); runControl()->initiateStop();
@@ -93,46 +95,57 @@ QUrl QmlPreviewRunner::serverUrl() const
return recordedData(QmlServerUrl).toUrl(); return recordedData(QmlServerUrl).toUrl();
} }
LocalQmlPreviewSupport::LocalQmlPreviewSupport(ProjectExplorer::RunControl *runControl) class LocalQmlPreviewSupport final : public SimpleTargetRunner
: SimpleTargetRunner(runControl)
{ {
setId("LocalQmlPreviewSupport"); public:
const QUrl serverUrl = Utils::urlFromLocalSocket(); LocalQmlPreviewSupport(RunControl *runControl)
: SimpleTargetRunner(runControl)
{
setId("LocalQmlPreviewSupport");
const QUrl serverUrl = Utils::urlFromLocalSocket();
QmlPreviewRunner *preview = qobject_cast<QmlPreviewRunner *>( QmlPreviewRunner *preview = qobject_cast<QmlPreviewRunner *>(
runControl->createWorker(ProjectExplorer::Constants::QML_PREVIEW_RUNNER)); runControl->createWorker(ProjectExplorer::Constants::QML_PREVIEW_RUNNER));
preview->setServerUrl(serverUrl); preview->setServerUrl(serverUrl);
addStopDependency(preview); addStopDependency(preview);
addStartDependency(preview); addStartDependency(preview);
setStartModifier([this, runControl, serverUrl] { setStartModifier([this, runControl, serverUrl] {
CommandLine cmd = commandLine(); CommandLine cmd = commandLine();
QStringList qmlProjectRunConfigurationArguments = cmd.splitArguments(); if (const auto aspect = runControl->aspect<QmlProjectManager::QmlMainFileAspect>()) {
const auto currentTarget = runControl->target();
const auto qmlBuildSystem = qobject_cast<QmlProjectManager::QmlBuildSystem *>(currentTarget->buildSystem());
const auto currentTarget = runControl->target(); const QString mainScript = aspect->mainScript;
const auto *qmlBuildSystem = qobject_cast<QmlProjectManager::QmlBuildSystem *>(currentTarget->buildSystem()); const QString currentFile = aspect->currentFile;
if (const auto aspect = runControl->aspect<QmlProjectManager::QmlMainFileAspect>()) { const QString mainScriptFromProject = qmlBuildSystem->targetFile(
const QString mainScript = aspect->mainScript; FilePath::fromString(mainScript)).toString();
const QString currentFile = aspect->currentFile;
const QString mainScriptFromProject = qmlBuildSystem->targetFile( QStringList qmlProjectRunConfigurationArguments = cmd.splitArguments();
Utils::FilePath::fromString(mainScript)).toString();
if (!currentFile.isEmpty() && qmlProjectRunConfigurationArguments.last().contains(mainScriptFromProject)) { if (!currentFile.isEmpty() && qmlProjectRunConfigurationArguments.last().contains(mainScriptFromProject)) {
qmlProjectRunConfigurationArguments.removeLast(); qmlProjectRunConfigurationArguments.removeLast();
cmd = Utils::CommandLine(cmd.executable(), qmlProjectRunConfigurationArguments); cmd = CommandLine(cmd.executable(), qmlProjectRunConfigurationArguments);
cmd.addArg(currentFile); cmd.addArg(currentFile);
}
} }
}
cmd.addArg(QmlDebug::qmlDebugLocalArguments(QmlDebug::QmlPreviewServices, serverUrl.path())); cmd.addArg(QmlDebug::qmlDebugLocalArguments(QmlDebug::QmlPreviewServices, serverUrl.path()));
setCommandLine(cmd); setCommandLine(cmd);
forceRunOnHost(); forceRunOnHost();
}); });
}
};
LocalQmlPreviewSupportFactory::LocalQmlPreviewSupportFactory()
{
setProduct<LocalQmlPreviewSupport>();
addSupportedRunMode(ProjectExplorer::Constants::QML_PREVIEW_RUN_MODE);
addSupportedDeviceType(ProjectExplorer::Constants::DESKTOP_DEVICE_TYPE);
} }
} // namespace QmlPreview } // QmlPreview

View File

@@ -44,12 +44,10 @@ private:
Internal::QmlPreviewConnectionManager m_connectionManager; Internal::QmlPreviewConnectionManager m_connectionManager;
}; };
class LocalQmlPreviewSupport : public ProjectExplorer::SimpleTargetRunner class LocalQmlPreviewSupportFactory final : public ProjectExplorer::RunWorkerFactory
{ {
Q_OBJECT
public: public:
LocalQmlPreviewSupport(ProjectExplorer::RunControl *runControl); LocalQmlPreviewSupportFactory();
}; };
} // namespace QmlPreview } // QmlPreview