forked from qt-creator/qt-creator
Lua: Make FilePath:dirEntries async
Change-Id: I0d1a918a34ca1fefe1dffdf4c5d766e6bb7aaf77 Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
@@ -4,6 +4,8 @@
|
|||||||
#include "../luaengine.h"
|
#include "../luaengine.h"
|
||||||
#include "../luaqttypes.h"
|
#include "../luaqttypes.h"
|
||||||
|
|
||||||
|
#include <utils/async.h>
|
||||||
|
#include <utils/futuresynchronizer.h>
|
||||||
#include <utils/hostosinfo.h>
|
#include <utils/hostosinfo.h>
|
||||||
|
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
@@ -14,70 +16,28 @@ namespace Lua::Internal {
|
|||||||
|
|
||||||
void addUtilsModule()
|
void addUtilsModule()
|
||||||
{
|
{
|
||||||
LuaEngine::registerProvider("__utils", [](sol::state_view lua) -> sol::object {
|
LuaEngine::registerProvider(
|
||||||
sol::table utils = lua.create_table();
|
"Utils",
|
||||||
|
[futureSync = Utils::FutureSynchronizer()](sol::state_view lua) mutable -> sol::object {
|
||||||
|
auto async = lua.script("return require('async')", "_utils_").get<sol::table>();
|
||||||
|
|
||||||
utils.set_function("waitms_cb", [](int ms, const sol::function &cb) {
|
sol::table utils = lua.create_table();
|
||||||
QTimer *timer = new QTimer();
|
|
||||||
timer->setSingleShot(true);
|
utils.set_function("waitms_cb", [](int ms, const sol::function &cb) {
|
||||||
timer->setInterval(ms);
|
QTimer *timer = new QTimer();
|
||||||
QObject::connect(timer, &QTimer::timeout, timer, [cb, timer]() {
|
timer->setSingleShot(true);
|
||||||
cb();
|
timer->setInterval(ms);
|
||||||
timer->deleteLater();
|
QObject::connect(timer, &QTimer::timeout, &LuaEngine::instance(), [cb, timer]() {
|
||||||
|
cb();
|
||||||
|
timer->deleteLater();
|
||||||
|
});
|
||||||
|
timer->start();
|
||||||
});
|
});
|
||||||
timer->start();
|
|
||||||
});
|
|
||||||
|
|
||||||
return utils;
|
auto dirEntries_cb = [&futureSync](
|
||||||
});
|
const FilePath &p,
|
||||||
|
const sol::table &options,
|
||||||
LuaEngine::registerProvider("Utils", [](sol::state_view lua) -> sol::object {
|
const sol::function &cb) {
|
||||||
sol::table utils = lua.script(
|
|
||||||
R"(
|
|
||||||
local u = require("__utils")
|
|
||||||
local a = require("async")
|
|
||||||
|
|
||||||
return {
|
|
||||||
waitms_cb = u.waitms_cb,
|
|
||||||
waitms = a.wrap(u.waitms_cb)
|
|
||||||
}
|
|
||||||
)",
|
|
||||||
"_utils_")
|
|
||||||
.get<sol::table>();
|
|
||||||
|
|
||||||
auto hostOsInfoType = utils.new_usertype<HostOsInfo>("HostOsInfo");
|
|
||||||
hostOsInfoType["isWindowsHost"] = &HostOsInfo::isWindowsHost;
|
|
||||||
hostOsInfoType["isMacHost"] = &HostOsInfo::isMacHost;
|
|
||||||
hostOsInfoType["isLinuxHost"] = &HostOsInfo::isLinuxHost;
|
|
||||||
hostOsInfoType["os"] = sol::var([]() {
|
|
||||||
if (HostOsInfo::isMacHost())
|
|
||||||
return "mac";
|
|
||||||
else if (HostOsInfo::isLinuxHost())
|
|
||||||
return "linux";
|
|
||||||
else if (HostOsInfo::isWindowsHost())
|
|
||||||
return "windows";
|
|
||||||
else
|
|
||||||
return "unknown";
|
|
||||||
}());
|
|
||||||
|
|
||||||
auto filePathType = utils.new_usertype<FilePath>(
|
|
||||||
"FilePath",
|
|
||||||
sol::call_constructor,
|
|
||||||
sol::constructors<FilePath()>(),
|
|
||||||
"fromUserInput",
|
|
||||||
&FilePath::fromUserInput,
|
|
||||||
"searchInPath",
|
|
||||||
[](const FilePath &self) { return self.searchInPath(); },
|
|
||||||
"exists",
|
|
||||||
&FilePath::exists,
|
|
||||||
"resolveSymlinks",
|
|
||||||
&FilePath::resolveSymlinks,
|
|
||||||
"isExecutableFile",
|
|
||||||
&FilePath::isExecutableFile,
|
|
||||||
"dirEntries",
|
|
||||||
[](sol::this_state s, const FilePath &p, const sol::table &options) -> sol::table {
|
|
||||||
sol::state_view lua(s);
|
|
||||||
sol::table result = lua.create_table();
|
|
||||||
const QStringList nameFilters = options.get_or<QStringList>("nameFilters", {});
|
const QStringList nameFilters = options.get_or<QStringList>("nameFilters", {});
|
||||||
QDir::Filters fileFilters
|
QDir::Filters fileFilters
|
||||||
= (QDir::Filters) options.get_or<int>("fileFilters", QDir::NoFilter);
|
= (QDir::Filters) options.get_or<int>("fileFilters", QDir::NoFilter);
|
||||||
@@ -85,33 +45,87 @@ return {
|
|||||||
= (QDirIterator::IteratorFlags)
|
= (QDirIterator::IteratorFlags)
|
||||||
options.get_or<int>("flags", QDirIterator::NoIteratorFlags);
|
options.get_or<int>("flags", QDirIterator::NoIteratorFlags);
|
||||||
|
|
||||||
FileFilter filter(nameFilters);
|
FileFilter filter(nameFilters, fileFilters, flags);
|
||||||
p.iterateDirectory(
|
|
||||||
[&result](const FilePath &item) {
|
|
||||||
result.add(item);
|
|
||||||
return IterationPolicy::Continue;
|
|
||||||
},
|
|
||||||
FileFilter(nameFilters, fileFilters, flags));
|
|
||||||
|
|
||||||
return result;
|
QFuture<FilePath> future = Utils::asyncRun([p, filter](QPromise<FilePath> &promise) {
|
||||||
},
|
p.iterateDirectory(
|
||||||
"nativePath",
|
[&promise](const FilePath &item) {
|
||||||
&FilePath::nativePath,
|
if (promise.isCanceled())
|
||||||
"toUserOutput",
|
return IterationPolicy::Stop;
|
||||||
&FilePath::toUserOutput,
|
|
||||||
"fileName",
|
|
||||||
&FilePath::fileName,
|
|
||||||
"currentWorkingPath",
|
|
||||||
&FilePath::currentWorkingPath,
|
|
||||||
"parentDir",
|
|
||||||
&FilePath::parentDir,
|
|
||||||
"resolvePath",
|
|
||||||
sol::overload(
|
|
||||||
[](const FilePath &p, const QString &path) { return p.resolvePath(path); },
|
|
||||||
[](const FilePath &p, const FilePath &path) { return p.resolvePath(path); }));
|
|
||||||
|
|
||||||
return utils;
|
promise.addResult(item);
|
||||||
});
|
return IterationPolicy::Continue;
|
||||||
|
},
|
||||||
|
filter);
|
||||||
|
});
|
||||||
|
|
||||||
|
futureSync.addFuture<FilePath>(future);
|
||||||
|
|
||||||
|
Utils::onFinished<FilePath>(
|
||||||
|
future, &LuaEngine::instance(), [cb](const QFuture<FilePath> &future) {
|
||||||
|
cb(future.results());
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
utils.set_function("__dirEntries_cb__", dirEntries_cb);
|
||||||
|
|
||||||
|
sol::function wrap = async["wrap"].get<sol::function>();
|
||||||
|
|
||||||
|
utils["waitms"] = wrap(utils["waitms_cb"]);
|
||||||
|
|
||||||
|
auto hostOsInfoType = utils.new_usertype<HostOsInfo>("HostOsInfo");
|
||||||
|
hostOsInfoType["isWindowsHost"] = &HostOsInfo::isWindowsHost;
|
||||||
|
hostOsInfoType["isMacHost"] = &HostOsInfo::isMacHost;
|
||||||
|
hostOsInfoType["isLinuxHost"] = &HostOsInfo::isLinuxHost;
|
||||||
|
hostOsInfoType["os"] = sol::var([]() {
|
||||||
|
if (HostOsInfo::isMacHost())
|
||||||
|
return "mac";
|
||||||
|
else if (HostOsInfo::isLinuxHost())
|
||||||
|
return "linux";
|
||||||
|
else if (HostOsInfo::isWindowsHost())
|
||||||
|
return "windows";
|
||||||
|
else
|
||||||
|
return "unknown";
|
||||||
|
}());
|
||||||
|
|
||||||
|
sol::usertype<FilePath> filePathType = utils.new_usertype<FilePath>(
|
||||||
|
"FilePath",
|
||||||
|
sol::call_constructor,
|
||||||
|
sol::constructors<FilePath()>(),
|
||||||
|
sol::meta_function::to_string,
|
||||||
|
&FilePath::toUserOutput,
|
||||||
|
"fromUserInput",
|
||||||
|
&FilePath::fromUserInput,
|
||||||
|
"searchInPath",
|
||||||
|
[](const FilePath &self) { return self.searchInPath(); },
|
||||||
|
"exists",
|
||||||
|
&FilePath::exists,
|
||||||
|
"resolveSymlinks",
|
||||||
|
&FilePath::resolveSymlinks,
|
||||||
|
"isExecutableFile",
|
||||||
|
&FilePath::isExecutableFile,
|
||||||
|
"isDir",
|
||||||
|
&FilePath::isDir,
|
||||||
|
"nativePath",
|
||||||
|
&FilePath::nativePath,
|
||||||
|
"toUserOutput",
|
||||||
|
&FilePath::toUserOutput,
|
||||||
|
"fileName",
|
||||||
|
&FilePath::fileName,
|
||||||
|
"currentWorkingPath",
|
||||||
|
&FilePath::currentWorkingPath,
|
||||||
|
"parentDir",
|
||||||
|
&FilePath::parentDir,
|
||||||
|
"resolvePath",
|
||||||
|
sol::overload(
|
||||||
|
[](const FilePath &p, const QString &path) { return p.resolvePath(path); },
|
||||||
|
[](const FilePath &p, const FilePath &path) { return p.resolvePath(path); }));
|
||||||
|
|
||||||
|
utils["FilePath"]["dirEntries_cb"] = utils["__dirEntries_cb__"];
|
||||||
|
utils["FilePath"]["dirEntries"] = wrap(utils["__dirEntries_cb__"]);
|
||||||
|
|
||||||
|
return utils;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Lua::Internal
|
} // namespace Lua::Internal
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ function utils.FilePath:searchInPath() end
|
|||||||
---@field fileFilters? integer The filters to use (combination of QDir.Filters.*), defaults to QDir.Filters.NoFilter
|
---@field fileFilters? integer The filters to use (combination of QDir.Filters.*), defaults to QDir.Filters.NoFilter
|
||||||
---@field flags? integer The iterator flags (combination of QDirIterator.Flags.*), defaults to QDirIterator.Flags.NoIteratorFlags
|
---@field flags? integer The iterator flags (combination of QDirIterator.Flags.*), defaults to QDirIterator.Flags.NoIteratorFlags
|
||||||
|
|
||||||
---Returns all entries in the directory
|
---Returns all entries in the directory. Call `a.wait` on the returned value to get the result.
|
||||||
---@param options DirEntriesOptions
|
---@param options DirEntriesOptions
|
||||||
---@return FilePath[]
|
---@return FilePath[]
|
||||||
function utils.FilePath:dirEntries(options) end
|
function utils.FilePath:dirEntries(options) end
|
||||||
|
|||||||
Reference in New Issue
Block a user