Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 6bfe5973 rédigé par Kubat's avatar Kubat
Parcourir les fichiers

SCRIPT: Execute scripts if passed to vivy as argument

parent 93861b96
Branches
Aucune étiquette associée trouvée
1 requête de fusion!16Add a way to execute a Lua file with Vivy in a click-click way
......@@ -20,15 +20,15 @@ LuaContext::LuaContext(ScriptStore::LoggerType &out, ScriptStore::LoggerType &er
if (!libVivy.open(QIODevice::ReadOnly | QIODevice::Text))
qFatal("Failed to find packaged libVivy (:lia/lib.lua)");
// Register this context
contextList.emplace(L, this);
// Load all C++ objects
SampleObject::Register(L);
FreeFunctions::Register(L);
// Load the lib
loadString(libVivy.readAll().toStdString().c_str());
// Register this context
contextList.emplace(L, this);
}
LuaContext::~LuaContext() noexcept
......
#include "ScriptStore.hh"
#include "../Uuid.hh"
#include <iostream>
using namespace Vivy;
using namespace std::string_literals;
......@@ -38,3 +39,19 @@ ScriptStore::resetLoadedScripts() noexcept
{
luaContext.reset(new Script::LuaContext(streamOut, streamErr));
}
void
ScriptStore::executeScript(Uuid id) noexcept
{
auto script = documents.at(id);
luaContext->loadDocument(script);
std::string bufferOut;
bufferOut.resize(outBufferSize + 1);
std::cout << "STDOUT:\n";
while (streamOut.readsome(bufferOut.data(), static_cast<std::streamsize>(bufferOut.size())))
std::cout << bufferOut.data();
std::cout << "STDERR:\n";
while (streamErr.readsome(bufferOut.data(), static_cast<std::streamsize>(bufferOut.size())))
std::cout << bufferOut.data();
}
......@@ -11,18 +11,24 @@ class LuaContext;
namespace Vivy
{
// TODO: For now the execution of a script is handled by the ScriptStore, FW
// will be to create a LuaExecutor that will handle the execution of scripts.
class ScriptStore final : public CRTPStore<ScriptStore, ScriptDocument> {
VIVY_STORAGE_CLASS(ScriptStore, ScriptDocument)
static constexpr inline int outBufferSize = 1024;
public:
using Item = std::tuple<Uuid, QString>;
using LoggerType = std::stringstream;
explicit ScriptStore() noexcept;
// TODO: Things that should be moved to the LuaExecutor class.
void resetLoadedScripts() noexcept;
void loadScriptFolder(const QString &folderPath);
std::vector<Item> getLoadedScripts() const noexcept;
void executeScript(Uuid) noexcept;
private:
std::unique_ptr<Script::LuaContext> luaContext{ nullptr };
......
#include "VivyApplication.hh"
#include "VivyCli.hh"
#include "Lib/Script/LuaContext.hh"
int
main(int argc, char **argv) noexcept
{
return Vivy::VivyApplication(argc, argv).exec();
return (argc >= 2) ? (Vivy::VivyCli(argc, argv).exec())
: (Vivy::VivyApplication(argc, argv).exec());
}
#include "VivyCli.hh"
using namespace Vivy;
VivyCli::VivyCli(int &argc, char **argv) noexcept
{
if (argc >= 2) {
selectedDoc = scriptStore.loadDocument(QString(argv[1]));
}
}
int
VivyCli::exec() noexcept
{
if (selectedDoc == nullptr)
return 1;
scriptStore.executeScript(selectedDoc->getUuid());
return 0;
}
#pragma once
#ifndef __cplusplus
#error "This is a C++ header"
#endif
#include "Lib/Script/ScriptStore.hh"
namespace Vivy
{
class VivyCli final {
ScriptStore scriptStore{};
std::shared_ptr<ScriptDocument> selectedDoc{ nullptr };
public:
VivyCli(int &argc, char **argv) noexcept;
int exec() noexcept;
};
}
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter