From 6bfe59735555d708b045e11f73bc1a54a35bba61 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 5 Aug 2021 15:03:52 +0200
Subject: [PATCH] SCRIPT: Execute scripts if passed to vivy as argument

---
 src/Lib/Script/LuaContext.cc  |  6 +++---
 src/Lib/Script/ScriptStore.cc | 17 +++++++++++++++++
 src/Lib/Script/ScriptStore.hh |  6 ++++++
 src/Main.cc                   |  4 +++-
 src/VivyCli.cc                | 19 +++++++++++++++++++
 src/VivyCli.hh                | 19 +++++++++++++++++++
 6 files changed, 67 insertions(+), 4 deletions(-)
 create mode 100644 src/VivyCli.cc
 create mode 100644 src/VivyCli.hh

diff --git a/src/Lib/Script/LuaContext.cc b/src/Lib/Script/LuaContext.cc
index 4a9d54f9..155c4852 100644
--- a/src/Lib/Script/LuaContext.cc
+++ b/src/Lib/Script/LuaContext.cc
@@ -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
diff --git a/src/Lib/Script/ScriptStore.cc b/src/Lib/Script/ScriptStore.cc
index adf9bc7d..53ac9c7f 100644
--- a/src/Lib/Script/ScriptStore.cc
+++ b/src/Lib/Script/ScriptStore.cc
@@ -1,5 +1,6 @@
 #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();
+}
diff --git a/src/Lib/Script/ScriptStore.hh b/src/Lib/Script/ScriptStore.hh
index c2176264..88f84515 100644
--- a/src/Lib/Script/ScriptStore.hh
+++ b/src/Lib/Script/ScriptStore.hh
@@ -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 };
diff --git a/src/Main.cc b/src/Main.cc
index d3a75093..7fb3b3ff 100644
--- a/src/Main.cc
+++ b/src/Main.cc
@@ -1,8 +1,10 @@
 #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());
 }
diff --git a/src/VivyCli.cc b/src/VivyCli.cc
new file mode 100644
index 00000000..9cb4b576
--- /dev/null
+++ b/src/VivyCli.cc
@@ -0,0 +1,19 @@
+#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;
+}
diff --git a/src/VivyCli.hh b/src/VivyCli.hh
new file mode 100644
index 00000000..9ceef273
--- /dev/null
+++ b/src/VivyCli.hh
@@ -0,0 +1,19 @@
+#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;
+};
+}
-- 
GitLab