mirror of
https://github.com/lucaspalomodevelop/autom.git
synced 2026-03-12 23:27:21 +00:00
Merge pull request #23 from lucaspalomodevelop/feat/config_from_script
Feat/config from script
This commit is contained in:
commit
4594052503
132
src/main.cpp
132
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<bool>())
|
if (script_settings.contains("background") && script_settings.at("background").get<bool>())
|
||||||
builded_script = builded_script + " &";
|
builded_script = builded_script + " &";
|
||||||
|
|
||||||
std::cout
|
// std::cout << "script: " << builded_script << std::endl;
|
||||||
<< "script: " << builded_script << std::endl;
|
|
||||||
|
|
||||||
return builded_script;
|
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<std::string>() + "/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<std::string>() + "/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
|
// run a script with is in the autom directory
|
||||||
void runScript(int argc, char *argv[])
|
void runScript(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@ -109,14 +221,26 @@ void runScript(int argc, char *argv[])
|
|||||||
{
|
{
|
||||||
std::string pre_script = "cd " + dir + " && ";
|
std::string pre_script = "cd " + dir + " && ";
|
||||||
std::string args = "";
|
std::string args = "";
|
||||||
|
std::string original_script = script;
|
||||||
|
|
||||||
|
// std::cout << "original script: " << original_script << std::endl;
|
||||||
|
|
||||||
for (int i = 2; i < argc; i++)
|
for (int i = 2; i < argc; i++)
|
||||||
{
|
{
|
||||||
args += argv[i];
|
args += argv[i];
|
||||||
args += " ";
|
args += " ";
|
||||||
}
|
}
|
||||||
// std::string script = pre_script + dir + "/" + argv[1] + " " + 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);
|
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"])
|
// if (script_settings["sudo"])
|
||||||
// script = "sudo " + script;
|
// script = "sudo " + script;
|
||||||
@ -128,6 +252,8 @@ void runScript(int argc, char *argv[])
|
|||||||
// system(script_settings["pre_script"].get<std::string>().c_str());
|
// system(script_settings["pre_script"].get<std::string>().c_str());
|
||||||
|
|
||||||
system(script.c_str());
|
system(script.c_str());
|
||||||
|
|
||||||
|
cleanTempFile();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// }
|
// }
|
||||||
|
|||||||
@ -8,6 +8,9 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include "../libs/json/single_include/nlohmann/json.hpp"
|
||||||
|
|
||||||
|
using json = nlohmann::json;
|
||||||
|
|
||||||
#include "inputparser.h"
|
#include "inputparser.h"
|
||||||
#include "command.h"
|
#include "command.h"
|
||||||
@ -18,6 +21,12 @@ Settings settings;
|
|||||||
std::string home_dir = "";
|
std::string home_dir = "";
|
||||||
Command command;
|
Command command;
|
||||||
|
|
||||||
|
struct automScript
|
||||||
|
{
|
||||||
|
json settings;
|
||||||
|
std::string script;
|
||||||
|
};
|
||||||
|
|
||||||
// input function for parsing arguments and creating commands and running them
|
// input function for parsing arguments and creating commands and running them
|
||||||
void input(int argc, char *argv[]);
|
void input(int argc, char *argv[]);
|
||||||
// build a script
|
// build a script
|
||||||
|
|||||||
23
src/setup.h
23
src/setup.h
@ -8,6 +8,8 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <algorithm>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "../libs/json/single_include/nlohmann/json.hpp"
|
#include "../libs/json/single_include/nlohmann/json.hpp"
|
||||||
using json = nlohmann::json;
|
using json = nlohmann::json;
|
||||||
@ -24,10 +26,14 @@ public:
|
|||||||
std::string home = getenv("HOME");
|
std::string home = getenv("HOME");
|
||||||
std::string editor = "vim";
|
std::string editor = "vim";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
std::vector<std::string> folders_to_create;
|
||||||
|
|
||||||
Setup(/* args */)
|
Setup(/* args */)
|
||||||
{
|
{
|
||||||
home = home + "/.autom";
|
home = home + "/.autom";
|
||||||
|
|
||||||
|
folders_to_create = {home, home + "/temp"};
|
||||||
runSetup();
|
runSetup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,9 +44,19 @@ public:
|
|||||||
void createFolder()
|
void createFolder()
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
_mkdir((home).c_str());
|
for (std::string folder : folders_to_create)
|
||||||
|
{
|
||||||
|
if (!std::filesystem::exists(folder))
|
||||||
|
_mkdir(folder.c_str());
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
mkdir((home).c_str(), 0777);
|
for (std::string folder : folders_to_create)
|
||||||
|
{
|
||||||
|
if (!std::filesystem::exists(folder))
|
||||||
|
{
|
||||||
|
mkdir(folder.c_str(), 0777);
|
||||||
|
}
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -53,6 +69,7 @@ public:
|
|||||||
json j = {
|
json j = {
|
||||||
{"editor", editor},
|
{"editor", editor},
|
||||||
{"search_dirs", {home}},
|
{"search_dirs", {home}},
|
||||||
|
{"temp_dir", home + "/temp"},
|
||||||
{"autom_home_dir", home}};
|
{"autom_home_dir", home}};
|
||||||
|
|
||||||
file << j.dump(4);
|
file << j.dump(4);
|
||||||
@ -62,8 +79,6 @@ public:
|
|||||||
|
|
||||||
void runSetup()
|
void runSetup()
|
||||||
{
|
{
|
||||||
|
|
||||||
if (!std::filesystem::exists(home))
|
|
||||||
createFolder();
|
createFolder();
|
||||||
|
|
||||||
if (!std::filesystem::exists(home + "/.automconfig.json"))
|
if (!std::filesystem::exists(home + "/.automconfig.json"))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user