2024-09-18 14:08:36 +02:00
|
|
|
// Copyright (C) The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
|
|
|
|
|
|
|
#include "../luaengine.h"
|
|
|
|
|
|
|
|
#include <projectexplorer/buildmanager.h>
|
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
#include <projectexplorer/projectexplorer.h>
|
|
|
|
#include <projectexplorer/projectexplorerconstants.h>
|
|
|
|
#include <projectexplorer/projectmanager.h>
|
|
|
|
#include <projectexplorer/runconfiguration.h>
|
|
|
|
#include <projectexplorer/runcontrol.h>
|
|
|
|
#include <projectexplorer/target.h>
|
|
|
|
|
|
|
|
#include <utils/commandline.h>
|
|
|
|
#include <utils/processinterface.h>
|
|
|
|
|
|
|
|
using namespace ProjectExplorer;
|
|
|
|
using namespace Utils;
|
|
|
|
|
|
|
|
namespace Lua::Internal {
|
|
|
|
|
|
|
|
void setupProjectModule()
|
|
|
|
{
|
|
|
|
registerProvider("Project", [](sol::state_view lua) -> sol::object {
|
|
|
|
const ScriptPluginSpec *pluginSpec = lua.get<ScriptPluginSpec *>("PluginSpec");
|
|
|
|
QObject *guard = pluginSpec->connectionGuard.get();
|
|
|
|
|
|
|
|
sol::table result = lua.create_table();
|
|
|
|
|
2024-09-27 14:54:24 +02:00
|
|
|
result.new_usertype<Kit>(
|
|
|
|
"Kit",
|
|
|
|
sol::no_constructor,
|
|
|
|
"supportedPlatforms",
|
|
|
|
[](Kit *kit) {
|
|
|
|
const auto set = kit->supportedPlatforms();
|
|
|
|
return QList<Utils::Id>(set.constBegin(), set.constEnd());
|
|
|
|
});
|
|
|
|
|
2024-09-18 14:08:36 +02:00
|
|
|
result.new_usertype<RunConfiguration>(
|
|
|
|
"RunConfiguration",
|
|
|
|
sol::no_constructor,
|
|
|
|
"runnable",
|
2024-09-27 14:54:24 +02:00
|
|
|
sol::property(&RunConfiguration::runnable),
|
|
|
|
"kit",
|
|
|
|
sol::property(&RunConfiguration::kit));
|
2024-09-18 14:08:36 +02:00
|
|
|
|
|
|
|
result.new_usertype<Project>(
|
|
|
|
"Project",
|
|
|
|
sol::no_constructor,
|
2024-09-27 14:54:24 +02:00
|
|
|
"displayName",
|
|
|
|
sol::property(&Project::displayName),
|
2024-09-18 14:08:36 +02:00
|
|
|
"directory",
|
|
|
|
sol::property(&Project::projectDirectory),
|
|
|
|
"activeRunConfiguration",
|
|
|
|
[](Project *project) { return project->activeTarget()->activeRunConfiguration(); });
|
|
|
|
|
|
|
|
result["startupProject"] = [] { return ProjectManager::instance()->startupProject(); };
|
|
|
|
|
|
|
|
result["canRunStartupProject"] =
|
|
|
|
[](const QString &mode) -> std::pair<bool, std::variant<QString, sol::lua_nil_t>> {
|
2024-09-19 09:31:30 +02:00
|
|
|
auto result = ProjectExplorerPlugin::canRunStartupProject(Id::fromString(mode));
|
2024-09-18 14:08:36 +02:00
|
|
|
if (result)
|
|
|
|
return std::make_pair(true, sol::lua_nil);
|
|
|
|
return std::make_pair(false, result.error());
|
|
|
|
};
|
|
|
|
|
|
|
|
result["runStartupProject"] =
|
2024-09-27 14:54:24 +02:00
|
|
|
[guard](const sol::optional<ProcessRunData> &runnable,
|
|
|
|
const sol::optional<QString> &displayName) {
|
2024-09-18 14:08:36 +02:00
|
|
|
auto project = ProjectManager::instance()->startupProject();
|
|
|
|
if (!project)
|
|
|
|
throw sol::error("No startup project");
|
|
|
|
|
|
|
|
auto runConfiguration = project->activeTarget()->activeRunConfiguration();
|
|
|
|
|
|
|
|
if (!runConfiguration)
|
|
|
|
throw sol::error("No active run configuration");
|
|
|
|
|
|
|
|
auto rc = std::make_unique<RunControl>(ProjectExplorer::Constants::NORMAL_RUN_MODE);
|
|
|
|
rc->copyDataFromRunConfiguration(runConfiguration);
|
|
|
|
|
|
|
|
if (runnable) {
|
|
|
|
rc->setCommandLine(runnable->command);
|
|
|
|
rc->setWorkingDirectory(runnable->workingDirectory);
|
|
|
|
rc->setEnvironment(runnable->environment);
|
|
|
|
}
|
|
|
|
|
2024-09-27 14:54:24 +02:00
|
|
|
if (displayName)
|
|
|
|
rc->setDisplayName(displayName.value());
|
|
|
|
|
2024-09-18 14:08:36 +02:00
|
|
|
BuildForRunConfigStatus status = BuildManager::potentiallyBuildForRunConfig(
|
|
|
|
runConfiguration);
|
|
|
|
|
|
|
|
auto startRun = [rc = std::move(rc)]() mutable {
|
|
|
|
if (!rc->createMainWorker())
|
|
|
|
return;
|
|
|
|
ProjectExplorerPlugin::startRunControl(rc.release());
|
|
|
|
};
|
|
|
|
|
|
|
|
if (status == BuildForRunConfigStatus::Building) {
|
|
|
|
QObject::connect(
|
|
|
|
BuildManager::instance(),
|
|
|
|
&BuildManager::buildQueueFinished,
|
|
|
|
guard,
|
|
|
|
[startRun = std::move(startRun)](bool success) mutable {
|
|
|
|
if (success)
|
|
|
|
startRun();
|
|
|
|
},
|
|
|
|
Qt::SingleShotConnection);
|
|
|
|
} else {
|
|
|
|
startRun();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2024-09-27 14:54:24 +02:00
|
|
|
result["stopRunConfigurationsByName"] =
|
|
|
|
[](const QString &displayName, const std::optional<bool> &force) -> int {
|
|
|
|
const auto runControls = ProjectExplorerPlugin::instance()->allRunControls();
|
|
|
|
|
|
|
|
int stoppedCount = 0;
|
|
|
|
for (const auto rc : runControls) {
|
|
|
|
if (rc && rc->displayName() == displayName) {
|
|
|
|
stoppedCount++;
|
|
|
|
|
2024-11-01 11:36:54 +01:00
|
|
|
if (force.has_value() && *force) {
|
2024-09-27 14:54:24 +02:00
|
|
|
rc->forceStop();
|
|
|
|
} else {
|
|
|
|
rc->initiateStop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return stoppedCount;
|
|
|
|
};
|
|
|
|
|
2024-09-18 14:08:36 +02:00
|
|
|
result["RunMode"] = lua.create_table_with(
|
|
|
|
"Normal", Constants::NORMAL_RUN_MODE, "Debug", Constants::DEBUG_RUN_MODE);
|
|
|
|
|
2024-09-27 14:54:24 +02:00
|
|
|
result["Platforms"] = lua.create_table_with(
|
|
|
|
"Desktop", Utils::Id(Constants::DESKTOP_DEVICE_TYPE));
|
|
|
|
|
2024-09-18 14:08:36 +02:00
|
|
|
return result;
|
|
|
|
});
|
|
|
|
|
|
|
|
// startupProjectChanged
|
2024-11-04 18:38:19 +01:00
|
|
|
registerHook("projects.startupProjectChanged", [](sol::main_function func, QObject *guard) {
|
2024-09-18 14:08:36 +02:00
|
|
|
QObject::connect(
|
|
|
|
ProjectManager::instance(),
|
|
|
|
&ProjectManager::startupProjectChanged,
|
|
|
|
guard,
|
2024-09-19 09:31:30 +02:00
|
|
|
[func](Project *project) {
|
|
|
|
expected_str<void> res = void_safe_call(func, project);
|
2024-09-18 14:08:36 +02:00
|
|
|
QTC_CHECK_EXPECTED(res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// projectAdded
|
2024-11-04 18:38:19 +01:00
|
|
|
registerHook("projects.projectAdded", [](sol::main_function func, QObject *guard) {
|
2024-09-18 14:08:36 +02:00
|
|
|
QObject::connect(
|
|
|
|
ProjectManager::instance(),
|
|
|
|
&ProjectManager::projectAdded,
|
|
|
|
guard,
|
2024-09-19 09:31:30 +02:00
|
|
|
[func](Project *project) {
|
|
|
|
expected_str<void> res = void_safe_call(func, project);
|
2024-09-18 14:08:36 +02:00
|
|
|
QTC_CHECK_EXPECTED(res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// projectRemoved
|
2024-11-04 18:38:19 +01:00
|
|
|
registerHook("projects.projectRemoved", [](sol::main_function func, QObject *guard) {
|
2024-09-18 14:08:36 +02:00
|
|
|
QObject::connect(
|
|
|
|
ProjectManager::instance(),
|
|
|
|
&ProjectManager::projectRemoved,
|
|
|
|
guard,
|
2024-09-19 09:31:30 +02:00
|
|
|
[func](Project *project) {
|
|
|
|
expected_str<void> res = void_safe_call(func, project);
|
2024-09-18 14:08:36 +02:00
|
|
|
QTC_CHECK_EXPECTED(res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// aboutToRemoveProject
|
2024-11-04 18:38:19 +01:00
|
|
|
registerHook("projects.aboutToRemoveProject", [](sol::main_function func, QObject *guard) {
|
2024-09-18 14:08:36 +02:00
|
|
|
QObject::connect(
|
|
|
|
ProjectManager::instance(),
|
|
|
|
&ProjectManager::aboutToRemoveProject,
|
|
|
|
guard,
|
2024-09-19 09:31:30 +02:00
|
|
|
[func](Project *project) {
|
|
|
|
expected_str<void> res = void_safe_call(func, project);
|
2024-09-18 14:08:36 +02:00
|
|
|
QTC_CHECK_EXPECTED(res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// runActionsUpdated
|
2024-11-04 18:38:19 +01:00
|
|
|
registerHook("projects.runActionsUpdated", [](sol::main_function func, QObject *guard) {
|
2024-09-18 14:08:36 +02:00
|
|
|
QObject::connect(
|
|
|
|
ProjectExplorerPlugin::instance(),
|
|
|
|
&ProjectExplorerPlugin::runActionsUpdated,
|
|
|
|
guard,
|
|
|
|
[func]() {
|
2024-09-19 09:31:30 +02:00
|
|
|
expected_str<void> res = void_safe_call(func);
|
2024-09-18 14:08:36 +02:00
|
|
|
QTC_CHECK_EXPECTED(res);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Lua::Internal
|