diff --git a/src/main.cpp b/src/main.cpp index 5b0324a..f5a7b8a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -55,12 +55,124 @@ std::string scriptBuilder(std::string script, std::string args, json script_sett if (script_settings.contains("background") && script_settings.at("background").get()) builded_script = builded_script + " &"; - std::cout - << "script: " << builded_script << std::endl; + // std::cout << "script: " << builded_script << std::endl; return builded_script; } +automScript parse(const std::string filecontent) +{ + std::stringstream file(filecontent); + + std::string line, content, script; + int braceCount = 0; + bool isSettings = true; + while (std::getline(file, line)) + { + for (char c : line) + { + if (c == '{') + braceCount++; + if (c == '}') + braceCount--; + if (braceCount == 0 && isSettings) + { + content += c; + isSettings = false; + continue; + } + if (isSettings) + { + content += c; + } + else + { + script += c; + } + } + content += '\n'; + if (!isSettings) + { + script += '\n'; + } + } + + automScript result; + try + { + result.settings = json::parse(content); + } + catch (json::parse_error &e) + { + result.settings = json::parse("{}"); + } + result.script = script; + + // std::cout << "settings: " << result.settings << std::endl; + // std::cout << "script: " << result.script << std::endl; + + return result; +} + +automScript parseAndWriteBack(std::string script) +{ + std::ifstream file(script); + std::string filecontent; + std::string line; + while (std::getline(file, line)) + { + filecontent += line + "\n"; + } + file.close(); + + automScript parsed = parse(filecontent); + + std::ofstream file2(script); + file2 << parsed.script; + file2.close(); + + return parsed; +} + +std::string saveScriptInTemp(std::string script) +{ + std::string temp_file = settings.value["temp_dir"].get() + "/autom_script"; + std::ofstream file(temp_file); + + std::string script_content = ""; + std::ifstream file2(script); + std::string line; + + while (std::getline(file2, line)) + { + script_content += line + "\n"; + } + + file << script_content; + file.close(); + + std::filesystem::permissions(temp_file, std::filesystem::perms::owner_all | std::filesystem::perms::group_all | std::filesystem::perms::others_all); + + return temp_file; +} + +void cleanTempFile() +{ + std::string temp_file = settings.value["temp_dir"].get() + "/autom_script"; + std::filesystem::remove(temp_file); +} + +json mergeJson(json a, json b) +{ + json result = a; + for (auto &el : b.items()) + { + // std::cout << "key: " << el.key() << " value: " << el.value() << std::endl; + result[el.key()] = el.value(); + } + return result; +} + // run a script with is in the autom directory void runScript(int argc, char *argv[]) { @@ -109,14 +221,26 @@ void runScript(int argc, char *argv[]) { std::string pre_script = "cd " + dir + " && "; std::string args = ""; + std::string original_script = script; + + // std::cout << "original script: " << original_script << std::endl; + for (int i = 2; i < argc; i++) { args += argv[i]; args += " "; } // std::string script = pre_script + dir + "/" + argv[1] + " " + args; + + script = saveScriptInTemp(script); + + json json_settings = parseAndWriteBack(script).settings; + + script_settings = mergeJson(script_settings, json_settings); + script = scriptBuilder(script, args, script_settings); - std::cout << "executing: " << (dir + "/" + argv[1] + " " + args) << std::endl; + + // std::cout << "executing: " << (script + " " + args) << std::endl; // if (script_settings["sudo"]) // script = "sudo " + script; @@ -128,6 +252,8 @@ void runScript(int argc, char *argv[]) // system(script_settings["pre_script"].get().c_str()); system(script.c_str()); + + cleanTempFile(); return; } // } diff --git a/src/main.h b/src/main.h index f30cfdb..20a91a5 100644 --- a/src/main.h +++ b/src/main.h @@ -8,6 +8,9 @@ #include #include #include +#include "../libs/json/single_include/nlohmann/json.hpp" + +using json = nlohmann::json; #include "inputparser.h" #include "command.h" @@ -18,6 +21,12 @@ Settings settings; std::string home_dir = ""; Command command; +struct automScript +{ + json settings; + std::string script; +}; + // input function for parsing arguments and creating commands and running them void input(int argc, char *argv[]); // build a script diff --git a/src/setup.h b/src/setup.h index 95597e3..95cc19f 100644 --- a/src/setup.h +++ b/src/setup.h @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include "../libs/json/single_include/nlohmann/json.hpp" using json = nlohmann::json; @@ -24,10 +26,14 @@ public: std::string home = getenv("HOME"); std::string editor = "vim"; #endif + + std::vector folders_to_create; + Setup(/* args */) { home = home + "/.autom"; + folders_to_create = {home, home + "/temp"}; runSetup(); } @@ -38,9 +44,19 @@ public: void createFolder() { #ifdef _WIN32 - _mkdir((home).c_str()); + for (std::string folder : folders_to_create) + { + if (!std::filesystem::exists(folder)) + _mkdir(folder.c_str()); + } #else - mkdir((home).c_str(), 0777); + for (std::string folder : folders_to_create) + { + if (!std::filesystem::exists(folder)) + { + mkdir(folder.c_str(), 0777); + } + } #endif } @@ -53,6 +69,7 @@ public: json j = { {"editor", editor}, {"search_dirs", {home}}, + {"temp_dir", home + "/temp"}, {"autom_home_dir", home}}; file << j.dump(4); @@ -62,9 +79,7 @@ public: void runSetup() { - - if (!std::filesystem::exists(home)) - createFolder(); + createFolder(); if (!std::filesystem::exists(home + "/.automconfig.json")) createSettings();