diff --git a/.github/workflows/c-cpp.yml b/.github/workflows/c-cpp.yml deleted file mode 100644 index 852909d..0000000 --- a/.github/workflows/c-cpp.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: C/C++ CI - -on: - push: - branches: [ "main", "dev" ] - pull_request: - branches: [ "main", "dev" ] - -jobs: - build: - strategy: - matrix: - os: - - ubuntu-latest - - macos-latest - - windows-latest - - runs-on: ${{matrix.os}} - - steps: - - uses: actions/checkout@v3 - # - name: configure - # run: ./configure - - name: make - run: make - # - name: make check - # run: make check - # - name: make distcheck - # run: make distcheck diff --git a/.github/workflows/cmake.yml b/.github/workflows/cmake.yml new file mode 100644 index 0000000..38165c6 --- /dev/null +++ b/.github/workflows/cmake.yml @@ -0,0 +1,36 @@ +name: CMake + +on: + push: + branches: [ "main", "dev" ] + pull_request: + branches: [ "main", "dev" ] + +env: + # Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.) + BUILD_TYPE: Release + +jobs: + build: + # The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac. + # You can convert this to a matrix build if you need cross-platform coverage. + # See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Configure CMake + # Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make. + # See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type + run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} + + - name: Build + # Build your program with the given configuration + run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} + + - name: Test + working-directory: ${{github.workspace}}/build + # Execute tests defined by the CMake configuration. + # See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail + run: ctest -C ${{env.BUILD_TYPE}} diff --git a/src/command.cpp b/src/command.cpp index 3bce3d0..0d7f9fc 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -1,75 +1,75 @@ -#include "command.h" - -Command::Command() {} - -Command::~Command() {} - -// add a command to the command map -void Command::addCommand(std::string name, void (*func)(int argc, char *argv[])) -{ - CommandInfo mycommand{ - name, - "", - func}; - commands[name] = mycommand; -} - -void Command::addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])) -{ - CommandInfo mycommand{ - name, - description, - func}; - commands[name] = mycommand; -} - -// add a default command to the command map -void Command::addDefaultCommand(void (*func)(int argc, char *argv[])) -{ - defaultCommand = func; -} - -// run a command -void Command::runCommand(std::string name, int argc, char *argv[]) -{ - // std::cout << "Running command: " << name << std::endl; - if (this->isInCommands(name)) - { - - for (int i = 0; i < argc; i++) - { - argv[i] = argv[i + 1]; - } - - commands[name].func(argc, argv); - } - else - { - defaultCommand(argc, argv); - } -} - -// check if a command is in the command map -bool Command::isInCommands(std::string name) -{ - for (auto const &command : commands) - { - if (command.first == name) - { - // std::cout << "Command found: " << command.first << std::endl; - return true; - } - } - // std::cout << "Command not found: " << name << std::endl; - return false; -} - -std::string Command::listCommands() -{ - std::string list = ""; - for (auto const &command : commands) - { - list += "\t" + command.second.name + " " + command.second.description + "\n"; - } - return list; +#include "command.h" + +Command::Command() {} + +Command::~Command() {} + +// add a command to the command map +void Command::addCommand(std::string name, void (*func)(int argc, char *argv[])) +{ + CommandInfo mycommand{ + name, + "", + func}; + commands[name] = mycommand; +} + +void Command::addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])) +{ + CommandInfo mycommand{ + name, + description, + func}; + commands[name] = mycommand; +} + +// add a default command to the command map +void Command::addDefaultCommand(void (*func)(int argc, char *argv[])) +{ + defaultCommand = func; +} + +// run a command +void Command::runCommand(std::string name, int argc, char *argv[]) +{ + // std::cout << "Running command: " << name << std::endl; + if (this->isInCommands(name)) + { + + for (int i = 0; i < argc; i++) + { + argv[i] = argv[i + 1]; + } + + commands[name].func(argc, argv); + } + else + { + defaultCommand(argc, argv); + } +} + +// check if a command is in the command map +bool Command::isInCommands(std::string name) +{ + for (auto const &command : commands) + { + if (command.first == name) + { + // std::cout << "Command found: " << command.first << std::endl; + return true; + } + } + // std::cout << "Command not found: " << name << std::endl; + return false; +} + +std::string Command::listCommands() +{ + std::string list = ""; + for (auto const &command : commands) + { + list += "\t" + command.second.name + " " + command.second.description + "\n"; + } + return list; } \ No newline at end of file diff --git a/src/command.h b/src/command.h index a6bff41..692af25 100644 --- a/src/command.h +++ b/src/command.h @@ -1,41 +1,41 @@ -#ifndef COMMAND_H -#define COMMAND_H - -#include -#include -#include - - -struct CommandInfo -{ - std::string name; - std::string description; - void (*func)(int argc, char *argv[]); -}; - -class Command -{ -public: - Command(void); - ~Command(void); - - // add a command to the command map - void addCommand(std::string name, void (*func)(int argc, char *argv[])); - void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])); - // add a default command to the command map - void addDefaultCommand(void (*func)(int argc, char *argv[])); - // run a command - void runCommand(std::string name, int argc, char *argv[]); - // check if a command is in the command map - bool isInCommands(std::string name); - - std::string listCommands(); - -private: - // map of commands - std::map commands; - // default command - void (*defaultCommand)(int argc, char *argv[]); -}; - +#ifndef COMMAND_H +#define COMMAND_H + +#include +#include +#include + + +struct CommandInfo +{ + std::string name; + std::string description; + void (*func)(int argc, char *argv[]); +}; + +class Command +{ +public: + Command(void); + ~Command(void); + + // add a command to the command map + void addCommand(std::string name, void (*func)(int argc, char *argv[])); + void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])); + // add a default command to the command map + void addDefaultCommand(void (*func)(int argc, char *argv[])); + // run a command + void runCommand(std::string name, int argc, char *argv[]); + // check if a command is in the command map + bool isInCommands(std::string name); + + std::string listCommands(); + +private: + // map of commands + std::map commands; + // default command + void (*defaultCommand)(int argc, char *argv[]); +}; + #endif // COMMAND_H \ No newline at end of file diff --git a/src/inputparser.cpp b/src/inputparser.cpp index 63c18ac..cc6070a 100644 --- a/src/inputparser.cpp +++ b/src/inputparser.cpp @@ -1,42 +1,42 @@ -#include "./inputparser.h" - -InputParser::InputParser(int argc, char *argv[]) -{ - for (int i = 1; i < argc; i++) - { - std::string arg(argv[i]); - if (arg.find("-") == 0) - { - std::string key = arg.substr(1); - if (i + 1 < argc) - { - std::string value(argv[i + 1]); - if (value.find("-") != 0) - { - args[key] = value; - i++; - } - else - { - args[key] = ""; - } - } - else - { - args[key] = ""; - } - } - } -} - -// get the value of a key -std::string InputParser::getValue(std::string key) -{ - return args[key]; -} - -// check if a key exists -bool InputParser::hasKey(std::string key) -{ - return args.count(key) > 0; +#include "./inputparser.h" + +InputParser::InputParser(int argc, char *argv[]) +{ + for (int i = 1; i < argc; i++) + { + std::string arg(argv[i]); + if (arg.find("-") == 0) + { + std::string key = arg.substr(1); + if (i + 1 < argc) + { + std::string value(argv[i + 1]); + if (value.find("-") != 0) + { + args[key] = value; + i++; + } + else + { + args[key] = ""; + } + } + else + { + args[key] = ""; + } + } + } +} + +// get the value of a key +std::string InputParser::getValue(std::string key) +{ + return args[key]; +} + +// check if a key exists +bool InputParser::hasKey(std::string key) +{ + return args.count(key) > 0; } \ No newline at end of file diff --git a/src/inputparser.h b/src/inputparser.h index 58061ae..5fddc53 100644 --- a/src/inputparser.h +++ b/src/inputparser.h @@ -1,22 +1,22 @@ -#ifndef INPUTPARSER_H -#define INPUTPARSER_H - -#include -#include -#include -#include -#include - -class InputParser -{ - std::map args; - -public: - InputParser(int argc, char *argv[]); - // get the value of a key - std::string getValue(std::string key); - // check if a key exists - bool hasKey(std::string key); -}; - +#ifndef INPUTPARSER_H +#define INPUTPARSER_H + +#include +#include +#include +#include +#include + +class InputParser +{ + std::map args; + +public: + InputParser(int argc, char *argv[]); + // get the value of a key + std::string getValue(std::string key); + // check if a key exists + bool hasKey(std::string key); +}; + #endif // INPUTPARSER_H \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 548a5a2..3c08e6e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,119 +1,119 @@ -#include "main.h" - -int main(int argc, char *argv[]) -{ - -#ifdef _WIN32 - _mkdir(dir.c_str()); -#else - mkdir(dir.c_str(), 0777); -#endif - - input(argc, argv); - return 0; -} - -// input function for parsing arguments and creating commands and running them -void input(int argc, char *argv[]) -{ - InputParser input(argc, argv); - - // std::cout << " [script] - Runs a script if autom has not command with that name" << std::endl; - command.addCommand("run", "[script] - Runs a script", runScript); - command.addCommand("help", "- Shows this help message", help); - command.addCommand("ls", "- Lists all scripts ", listScripts); - command.addCommand("add", "[script] - Adds a script", addScript); - command.addCommand("new", "[script] - Adds a script", addScript); - command.addCommand("edit", "[script] - Edits a script", editScript); - command.addCommand("remove", "[script] - Remove a script", removeScript); - command.addDefaultCommand(runScript); - command.runCommand(argv[1], argc, argv); -} - -// run a script with is in the autom directory -void runScript(int argc, char *argv[]) -{ - - // std::cout << "Running script: " << argv[1] << std::endl; - std::string pre_script = "cd " + dir + " && "; - std::string script = pre_script + dir + "/" + argv[1]; - // std::cout << "Running script: " << script << std::endl; - system(script.c_str()); -} - -// list all scripts in the autom directory -void listScripts(int argc, char *argv[]) -{ - std::cout << "Scripts:" << std::endl; - for (const auto &entry : std::filesystem::directory_iterator(dir)) - { - std::string name = entry.path().filename().string(); - std::cout << " " << name << std::endl; - } -} - -// add a script in the autom directory -void addScript(int argc, char *argv[]) -{ - - if (std::filesystem::exists(dir + "/" + argv[1])) - { - std::cout << "Script " << argv[1] << " already exists" << std::endl; - return; - } - - std::cout << "Adding script: " << argv[1] << std::endl; - std::string script = dir + "/" + argv[1]; - std::ofstream file(script); - -#ifdef _WIN32 - file << "@echo off" << std::endl; - _chmod(script.c_str(), _S_IREAD | _S_IWRITE); -#else - file << "#!/bin/bash" << std::endl; - system(("chmod +x " + script).c_str()); -#endif - - file.close(); - - editScript(argv[1]); -} - -// edit a script in the autom directory -void editScript(int argc, char *argv[]) -{ - editScript(argv[1]); -} - -void editScript(std::string name) -{ - std::string script = dir + "/" + name; -#ifdef _WIN32 - system(("notepad " + script).c_str()); -#else - system(("nano " + script).c_str()); -#endif -} - -void removeScript(int argc, char *argv[]) -{ - std::string script = dir + "/" + argv[1]; - if (std::filesystem::exists(script)) - { - std::cout << "Removing script: " << argv[1] << std::endl; - std::filesystem::remove(script); - } - else - { - std::cout << "Script " << argv[1] << " does not exist" << std::endl; - } -} - -// help function for showing help message -void help(int argc, char *argv[]) -{ - std::cout << "Usage: autom [command] [options]" << std::endl; - std::cout << "Commands:" << std::endl; - std::cout << "\t[script] - Runs a script if autom has not command with that name" << std::endl; - std::cout << command.listCommands() << std::endl; +#include "main.h" + +int main(int argc, char *argv[]) +{ + +#ifdef _WIN32 + _mkdir(dir.c_str()); +#else + mkdir(dir.c_str(), 0777); +#endif + + input(argc, argv); + return 0; +} + +// input function for parsing arguments and creating commands and running them +void input(int argc, char *argv[]) +{ + InputParser input(argc, argv); + + // std::cout << " [script] - Runs a script if autom has not command with that name" << std::endl; + command.addCommand("run", "[script] - Runs a script", runScript); + command.addCommand("help", "- Shows this help message", help); + command.addCommand("ls", "- Lists all scripts ", listScripts); + command.addCommand("add", "[script] - Adds a script", addScript); + command.addCommand("new", "[script] - Adds a script", addScript); + command.addCommand("edit", "[script] - Edits a script", editScript); + command.addCommand("remove", "[script] - Remove a script", removeScript); + command.addDefaultCommand(runScript); + command.runCommand(argv[1], argc, argv); +} + +// run a script with is in the autom directory +void runScript(int argc, char *argv[]) +{ + + // std::cout << "Running script: " << argv[1] << std::endl; + std::string pre_script = "cd " + dir + " && "; + std::string script = pre_script + dir + "/" + argv[1]; + // std::cout << "Running script: " << script << std::endl; + system(script.c_str()); +} + +// list all scripts in the autom directory +void listScripts(int argc, char *argv[]) +{ + std::cout << "Scripts:" << std::endl; + for (const auto &entry : std::filesystem::directory_iterator(dir)) + { + std::string name = entry.path().filename().string(); + std::cout << " " << name << std::endl; + } +} + +// add a script in the autom directory +void addScript(int argc, char *argv[]) +{ + + if (std::filesystem::exists(dir + "/" + argv[1])) + { + std::cout << "Script " << argv[1] << " already exists" << std::endl; + return; + } + + std::cout << "Adding script: " << argv[1] << std::endl; + std::string script = dir + "/" + argv[1]; + std::ofstream file(script); + +#ifdef _WIN32 + file << "@echo off" << std::endl; + _chmod(script.c_str(), _S_IREAD | _S_IWRITE); +#else + file << "#!/bin/bash" << std::endl; + system(("chmod +x " + script).c_str()); +#endif + + file.close(); + + editScript(argv[1]); +} + +// edit a script in the autom directory +void editScript(int argc, char *argv[]) +{ + editScript(argv[1]); +} + +void editScript(std::string name) +{ + std::string script = dir + "/" + name; +#ifdef _WIN32 + system(("notepad " + script).c_str()); +#else + system(("nano " + script).c_str()); +#endif +} + +void removeScript(int argc, char *argv[]) +{ + std::string script = dir + "/" + argv[1]; + if (std::filesystem::exists(script)) + { + std::cout << "Removing script: " << argv[1] << std::endl; + std::filesystem::remove(script); + } + else + { + std::cout << "Script " << argv[1] << " does not exist" << std::endl; + } +} + +// help function for showing help message +void help(int argc, char *argv[]) +{ + std::cout << "Usage: autom [command] [options]" << std::endl; + std::cout << "Commands:" << std::endl; + std::cout << "\t[script] - Runs a script if autom has not command with that name" << std::endl; + std::cout << command.listCommands() << std::endl; } \ No newline at end of file diff --git a/src/main.h b/src/main.h index 9c6f034..3c5d9c0 100644 --- a/src/main.h +++ b/src/main.h @@ -1,42 +1,42 @@ - -#include -#include -#include -#include -#include - -#include "inputparser.h" -#include "command.h" - -// directory for autom scripts -#ifdef _WIN32 -#include -std::string home = getenv("USERPROFILE"); -#else -std::string home = getenv("HOME"); -#endif - -// directory for autom scripts -std::string dir = home + "/autom"; -Command command = Command(); - -// input function for parsing arguments and creating commands and running them -void input(int argc, char *argv[]); -// run a script with is in the autom directory -void runScript(int argc, char *argv[]); -// list all scripts in the autom directory -void listScripts(int argc, char *argv[]); -// add a script in the autom directory -void addScript(int argc, char *argv[]); -// edit a script in the autom directory -void editScript(int argc, char *argv[]); -void editScript(std::string name); -// remove a script in the autom directory -void removeScript(int argc, char *argv[]); -// help function for showing help message -void help(int argc, char *argv[]); -// void create(int argc,char *argv[]); -// void remove(int argc,char *argv[]); -// void list(int argc,char *argv[]); -// void edit(int argc,char *argv[]); -// void run(int argc,char *argv[]); + +#include +#include +#include +#include +#include + +#include "inputparser.h" +#include "command.h" + +// directory for autom scripts +#ifdef _WIN32 +#include +std::string home = getenv("USERPROFILE"); +#else +std::string home = getenv("HOME"); +#endif + +// directory for autom scripts +std::string dir = home + "/autom"; +Command command = Command(); + +// input function for parsing arguments and creating commands and running them +void input(int argc, char *argv[]); +// run a script with is in the autom directory +void runScript(int argc, char *argv[]); +// list all scripts in the autom directory +void listScripts(int argc, char *argv[]); +// add a script in the autom directory +void addScript(int argc, char *argv[]); +// edit a script in the autom directory +void editScript(int argc, char *argv[]); +void editScript(std::string name); +// remove a script in the autom directory +void removeScript(int argc, char *argv[]); +// help function for showing help message +void help(int argc, char *argv[]); +// void create(int argc,char *argv[]); +// void remove(int argc,char *argv[]); +// void list(int argc,char *argv[]); +// void edit(int argc,char *argv[]); +// void run(int argc,char *argv[]);