forked from qt-creator/qt-creator
Lua: Add "id" to ScriptPluginSpec
Change-Id: Iabc766e511bc07bc3379aa13aed0aec04b3584e8 Reviewed-by: Alessandro Portale <alessandro.portale@qt.io>
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
#include <coreplugin/messagemanager.h>
|
#include <coreplugin/messagemanager.h>
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
|
#include <utils/hostosinfo.h>
|
||||||
#include <utils/lua.h>
|
#include <utils/lua.h>
|
||||||
#include <utils/stringutils.h>
|
#include <utils/stringutils.h>
|
||||||
#include <utils/theme/theme.h>
|
#include <utils/theme/theme.h>
|
||||||
@@ -56,6 +57,31 @@ public:
|
|||||||
QTemporaryDir appDataDir;
|
QTemporaryDir appDataDir;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
QObject *ScriptPluginSpec::setup(
|
||||||
|
sol::state_view lua,
|
||||||
|
const QString &id,
|
||||||
|
const QString &name,
|
||||||
|
const Utils::FilePath appDataPath,
|
||||||
|
const Utils::FilePath pluginLocation)
|
||||||
|
{
|
||||||
|
lua.new_usertype<ScriptPluginSpec>(
|
||||||
|
"PluginSpec",
|
||||||
|
sol::no_constructor,
|
||||||
|
"id",
|
||||||
|
sol::property([](ScriptPluginSpec &self) { return self.id; }),
|
||||||
|
"name",
|
||||||
|
sol::property([](ScriptPluginSpec &self) { return self.name; }),
|
||||||
|
"pluginDirectory",
|
||||||
|
sol::property([pluginLocation]() { return pluginLocation; }));
|
||||||
|
|
||||||
|
auto guardObject = std::make_unique<QObject>();
|
||||||
|
auto guardObjectPtr = guardObject.get();
|
||||||
|
|
||||||
|
lua["PluginSpec"] = ScriptPluginSpec{id, name, appDataPath, std::move(guardObject)};
|
||||||
|
|
||||||
|
return guardObjectPtr;
|
||||||
|
}
|
||||||
|
|
||||||
void prepareLuaState(
|
void prepareLuaState(
|
||||||
sol::state &lua,
|
sol::state &lua,
|
||||||
const QString &name,
|
const QString &name,
|
||||||
@@ -85,13 +111,11 @@ void prepareLuaState(
|
|||||||
Core::MessageManager::writeSilently(QString("%1 %2").arg(p, msg));
|
Core::MessageManager::writeSilently(QString("%1 %2").arg(p, msg));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
const expected_str<FilePath> tmpDir = HostOsInfo::root().tmpDir();
|
||||||
lua.new_usertype<ScriptPluginSpec>(
|
QTC_ASSERT_EXPECTED(tmpDir, return);
|
||||||
"PluginSpec", sol::no_constructor, "name", sol::property([](ScriptPluginSpec &self) {
|
QString id = name;
|
||||||
return self.name;
|
id = id.replace(QRegularExpression("[^a-zA-Z0-9_]"), "_").toLower();
|
||||||
}));
|
ScriptPluginSpec::setup(lua, id, name, appDataPath, *tmpDir);
|
||||||
|
|
||||||
lua["PluginSpec"] = ScriptPluginSpec{name, appDataPath, std::make_unique<QObject>()};
|
|
||||||
|
|
||||||
for (const auto &[name, func] : d->m_providers.asKeyValueRange()) {
|
for (const auto &[name, func] : d->m_providers.asKeyValueRange()) {
|
||||||
lua["package"]["preload"][name.toStdString()] = [func = func](const sol::this_state &s) {
|
lua["package"]["preload"][name.toStdString()] = [func = func](const sol::this_state &s) {
|
||||||
@@ -247,18 +271,8 @@ expected_str<sol::protected_function> prepareSetup(
|
|||||||
const FilePath appDataPath = Core::ICore::userResourcePath() / "plugin-data" / "lua"
|
const FilePath appDataPath = Core::ICore::userResourcePath() / "plugin-data" / "lua"
|
||||||
/ pluginSpec.location().fileName();
|
/ pluginSpec.location().fileName();
|
||||||
|
|
||||||
lua.new_usertype<ScriptPluginSpec>(
|
QObject *guard = ScriptPluginSpec::setup(
|
||||||
"PluginSpec",
|
lua, pluginSpec.id(), pluginSpec.name(), appDataPath, pluginSpec.location());
|
||||||
sol::no_constructor,
|
|
||||||
"name",
|
|
||||||
sol::property([](ScriptPluginSpec &self) { return self.name; }),
|
|
||||||
"pluginDirectory",
|
|
||||||
sol::property([p = pluginSpec.location()]() { return p; }));
|
|
||||||
|
|
||||||
auto guardObject = std::make_unique<QObject>();
|
|
||||||
auto guardObjectPtr = guardObject.get();
|
|
||||||
|
|
||||||
lua["PluginSpec"] = ScriptPluginSpec{pluginSpec.name(), appDataPath, std::move(guardObject)};
|
|
||||||
|
|
||||||
// TODO: only register what the plugin requested
|
// TODO: only register what the plugin requested
|
||||||
for (const auto &[name, func] : d->m_providers.asKeyValueRange()) {
|
for (const auto &[name, func] : d->m_providers.asKeyValueRange()) {
|
||||||
@@ -291,7 +305,7 @@ expected_str<sol::protected_function> prepareSetup(
|
|||||||
|
|
||||||
qCDebug(logLuaEngine) << "Hooks table found: " << hookTable.has_value();
|
qCDebug(logLuaEngine) << "Hooks table found: " << hookTable.has_value();
|
||||||
if (hookTable) {
|
if (hookTable) {
|
||||||
auto connectResult = connectHooks(lua, *hookTable, {}, guardObjectPtr);
|
auto connectResult = connectHooks(lua, *hookTable, {}, guard);
|
||||||
if (!connectResult)
|
if (!connectResult)
|
||||||
return make_unexpected(connectResult.error());
|
return make_unexpected(connectResult.error());
|
||||||
}
|
}
|
||||||
|
@@ -31,6 +31,14 @@ struct CoroutineState
|
|||||||
|
|
||||||
struct ScriptPluginSpec
|
struct ScriptPluginSpec
|
||||||
{
|
{
|
||||||
|
static QObject *setup(
|
||||||
|
sol::state_view lua,
|
||||||
|
const QString &id,
|
||||||
|
const QString &name,
|
||||||
|
const Utils::FilePath appDataPath,
|
||||||
|
const Utils::FilePath pluginLocation);
|
||||||
|
|
||||||
|
QString id;
|
||||||
QString name;
|
QString name;
|
||||||
Utils::FilePath appDataPath;
|
Utils::FilePath appDataPath;
|
||||||
std::unique_ptr<QObject> connectionGuard;
|
std::unique_ptr<QObject> connectionGuard;
|
||||||
|
Reference in New Issue
Block a user