add cmake workflow

This commit is contained in:
lucaspalomodevelop 2023-05-16 20:11:19 +02:00
parent a355286f5c
commit c8eed814d9
8 changed files with 372 additions and 365 deletions

View File

@ -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

36
.github/workflows/cmake.yml vendored Normal file
View File

@ -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}}

View File

@ -1,75 +1,75 @@
#include "command.h" #include "command.h"
Command::Command() {} Command::Command() {}
Command::~Command() {} Command::~Command() {}
// add a command to the command map // add a command to the command map
void Command::addCommand(std::string name, void (*func)(int argc, char *argv[])) void Command::addCommand(std::string name, void (*func)(int argc, char *argv[]))
{ {
CommandInfo mycommand{ CommandInfo mycommand{
name, name,
"", "",
func}; func};
commands[name] = mycommand; commands[name] = mycommand;
} }
void Command::addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[])) void Command::addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[]))
{ {
CommandInfo mycommand{ CommandInfo mycommand{
name, name,
description, description,
func}; func};
commands[name] = mycommand; commands[name] = mycommand;
} }
// add a default command to the command map // add a default command to the command map
void Command::addDefaultCommand(void (*func)(int argc, char *argv[])) void Command::addDefaultCommand(void (*func)(int argc, char *argv[]))
{ {
defaultCommand = func; defaultCommand = func;
} }
// run a command // run a command
void Command::runCommand(std::string name, int argc, char *argv[]) void Command::runCommand(std::string name, int argc, char *argv[])
{ {
// std::cout << "Running command: " << name << std::endl; // std::cout << "Running command: " << name << std::endl;
if (this->isInCommands(name)) if (this->isInCommands(name))
{ {
for (int i = 0; i < argc; i++) for (int i = 0; i < argc; i++)
{ {
argv[i] = argv[i + 1]; argv[i] = argv[i + 1];
} }
commands[name].func(argc, argv); commands[name].func(argc, argv);
} }
else else
{ {
defaultCommand(argc, argv); defaultCommand(argc, argv);
} }
} }
// check if a command is in the command map // check if a command is in the command map
bool Command::isInCommands(std::string name) bool Command::isInCommands(std::string name)
{ {
for (auto const &command : commands) for (auto const &command : commands)
{ {
if (command.first == name) if (command.first == name)
{ {
// std::cout << "Command found: " << command.first << std::endl; // std::cout << "Command found: " << command.first << std::endl;
return true; return true;
} }
} }
// std::cout << "Command not found: " << name << std::endl; // std::cout << "Command not found: " << name << std::endl;
return false; return false;
} }
std::string Command::listCommands() std::string Command::listCommands()
{ {
std::string list = ""; std::string list = "";
for (auto const &command : commands) for (auto const &command : commands)
{ {
list += "\t" + command.second.name + " " + command.second.description + "\n"; list += "\t" + command.second.name + " " + command.second.description + "\n";
} }
return list; return list;
} }

View File

@ -1,41 +1,41 @@
#ifndef COMMAND_H #ifndef COMMAND_H
#define COMMAND_H #define COMMAND_H
#include <map> #include <map>
#include <string> #include <string>
#include <iostream> #include <iostream>
struct CommandInfo struct CommandInfo
{ {
std::string name; std::string name;
std::string description; std::string description;
void (*func)(int argc, char *argv[]); void (*func)(int argc, char *argv[]);
}; };
class Command class Command
{ {
public: public:
Command(void); Command(void);
~Command(void); ~Command(void);
// add a command to the command map // add a command to the command map
void addCommand(std::string name, void (*func)(int argc, char *argv[])); 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[])); void addCommand(std::string name, std::string description, void (*func)(int argc, char *argv[]));
// add a default command to the command map // add a default command to the command map
void addDefaultCommand(void (*func)(int argc, char *argv[])); void addDefaultCommand(void (*func)(int argc, char *argv[]));
// run a command // run a command
void runCommand(std::string name, int argc, char *argv[]); void runCommand(std::string name, int argc, char *argv[]);
// check if a command is in the command map // check if a command is in the command map
bool isInCommands(std::string name); bool isInCommands(std::string name);
std::string listCommands(); std::string listCommands();
private: private:
// map of commands // map of commands
std::map<std::string, CommandInfo> commands; std::map<std::string, CommandInfo> commands;
// default command // default command
void (*defaultCommand)(int argc, char *argv[]); void (*defaultCommand)(int argc, char *argv[]);
}; };
#endif // COMMAND_H #endif // COMMAND_H

View File

@ -1,42 +1,42 @@
#include "./inputparser.h" #include "./inputparser.h"
InputParser::InputParser(int argc, char *argv[]) InputParser::InputParser(int argc, char *argv[])
{ {
for (int i = 1; i < argc; i++) for (int i = 1; i < argc; i++)
{ {
std::string arg(argv[i]); std::string arg(argv[i]);
if (arg.find("-") == 0) if (arg.find("-") == 0)
{ {
std::string key = arg.substr(1); std::string key = arg.substr(1);
if (i + 1 < argc) if (i + 1 < argc)
{ {
std::string value(argv[i + 1]); std::string value(argv[i + 1]);
if (value.find("-") != 0) if (value.find("-") != 0)
{ {
args[key] = value; args[key] = value;
i++; i++;
} }
else else
{ {
args[key] = ""; args[key] = "";
} }
} }
else else
{ {
args[key] = ""; args[key] = "";
} }
} }
} }
} }
// get the value of a key // get the value of a key
std::string InputParser::getValue(std::string key) std::string InputParser::getValue(std::string key)
{ {
return args[key]; return args[key];
} }
// check if a key exists // check if a key exists
bool InputParser::hasKey(std::string key) bool InputParser::hasKey(std::string key)
{ {
return args.count(key) > 0; return args.count(key) > 0;
} }

View File

@ -1,22 +1,22 @@
#ifndef INPUTPARSER_H #ifndef INPUTPARSER_H
#define INPUTPARSER_H #define INPUTPARSER_H
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
#include <map> #include <map>
class InputParser class InputParser
{ {
std::map<std::string, std::string> args; std::map<std::string, std::string> args;
public: public:
InputParser(int argc, char *argv[]); InputParser(int argc, char *argv[]);
// get the value of a key // get the value of a key
std::string getValue(std::string key); std::string getValue(std::string key);
// check if a key exists // check if a key exists
bool hasKey(std::string key); bool hasKey(std::string key);
}; };
#endif // INPUTPARSER_H #endif // INPUTPARSER_H

View File

@ -1,119 +1,119 @@
#include "main.h" #include "main.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
#ifdef _WIN32 #ifdef _WIN32
_mkdir(dir.c_str()); _mkdir(dir.c_str());
#else #else
mkdir(dir.c_str(), 0777); mkdir(dir.c_str(), 0777);
#endif #endif
input(argc, argv); input(argc, argv);
return 0; return 0;
} }
// 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[])
{ {
InputParser input(argc, argv); InputParser input(argc, argv);
// std::cout << " [script] - Runs a script if autom has not command with that name" << std::endl; // 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("run", "[script] - Runs a script", runScript);
command.addCommand("help", "- Shows this help message", help); command.addCommand("help", "- Shows this help message", help);
command.addCommand("ls", "- Lists all scripts ", listScripts); command.addCommand("ls", "- Lists all scripts ", listScripts);
command.addCommand("add", "[script] - Adds a script", addScript); command.addCommand("add", "[script] - Adds a script", addScript);
command.addCommand("new", "[script] - Adds a script", addScript); command.addCommand("new", "[script] - Adds a script", addScript);
command.addCommand("edit", "[script] - Edits a script", editScript); command.addCommand("edit", "[script] - Edits a script", editScript);
command.addCommand("remove", "[script] - Remove a script", removeScript); command.addCommand("remove", "[script] - Remove a script", removeScript);
command.addDefaultCommand(runScript); command.addDefaultCommand(runScript);
command.runCommand(argv[1], argc, argv); command.runCommand(argv[1], argc, argv);
} }
// 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[])
{ {
// std::cout << "Running script: " << argv[1] << std::endl; // std::cout << "Running script: " << argv[1] << std::endl;
std::string pre_script = "cd " + dir + " && "; std::string pre_script = "cd " + dir + " && ";
std::string script = pre_script + dir + "/" + argv[1]; std::string script = pre_script + dir + "/" + argv[1];
// std::cout << "Running script: " << script << std::endl; // std::cout << "Running script: " << script << std::endl;
system(script.c_str()); system(script.c_str());
} }
// list all scripts in the autom directory // list all scripts in the autom directory
void listScripts(int argc, char *argv[]) void listScripts(int argc, char *argv[])
{ {
std::cout << "Scripts:" << std::endl; std::cout << "Scripts:" << std::endl;
for (const auto &entry : std::filesystem::directory_iterator(dir)) for (const auto &entry : std::filesystem::directory_iterator(dir))
{ {
std::string name = entry.path().filename().string(); std::string name = entry.path().filename().string();
std::cout << " " << name << std::endl; std::cout << " " << name << std::endl;
} }
} }
// add a script in the autom directory // add a script in the autom directory
void addScript(int argc, char *argv[]) void addScript(int argc, char *argv[])
{ {
if (std::filesystem::exists(dir + "/" + argv[1])) if (std::filesystem::exists(dir + "/" + argv[1]))
{ {
std::cout << "Script " << argv[1] << " already exists" << std::endl; std::cout << "Script " << argv[1] << " already exists" << std::endl;
return; return;
} }
std::cout << "Adding script: " << argv[1] << std::endl; std::cout << "Adding script: " << argv[1] << std::endl;
std::string script = dir + "/" + argv[1]; std::string script = dir + "/" + argv[1];
std::ofstream file(script); std::ofstream file(script);
#ifdef _WIN32 #ifdef _WIN32
file << "@echo off" << std::endl; file << "@echo off" << std::endl;
_chmod(script.c_str(), _S_IREAD | _S_IWRITE); _chmod(script.c_str(), _S_IREAD | _S_IWRITE);
#else #else
file << "#!/bin/bash" << std::endl; file << "#!/bin/bash" << std::endl;
system(("chmod +x " + script).c_str()); system(("chmod +x " + script).c_str());
#endif #endif
file.close(); file.close();
editScript(argv[1]); editScript(argv[1]);
} }
// edit a script in the autom directory // edit a script in the autom directory
void editScript(int argc, char *argv[]) void editScript(int argc, char *argv[])
{ {
editScript(argv[1]); editScript(argv[1]);
} }
void editScript(std::string name) void editScript(std::string name)
{ {
std::string script = dir + "/" + name; std::string script = dir + "/" + name;
#ifdef _WIN32 #ifdef _WIN32
system(("notepad " + script).c_str()); system(("notepad " + script).c_str());
#else #else
system(("nano " + script).c_str()); system(("nano " + script).c_str());
#endif #endif
} }
void removeScript(int argc, char *argv[]) void removeScript(int argc, char *argv[])
{ {
std::string script = dir + "/" + argv[1]; std::string script = dir + "/" + argv[1];
if (std::filesystem::exists(script)) if (std::filesystem::exists(script))
{ {
std::cout << "Removing script: " << argv[1] << std::endl; std::cout << "Removing script: " << argv[1] << std::endl;
std::filesystem::remove(script); std::filesystem::remove(script);
} }
else else
{ {
std::cout << "Script " << argv[1] << " does not exist" << std::endl; std::cout << "Script " << argv[1] << " does not exist" << std::endl;
} }
} }
// help function for showing help message // help function for showing help message
void help(int argc, char *argv[]) void help(int argc, char *argv[])
{ {
std::cout << "Usage: autom [command] [options]" << std::endl; std::cout << "Usage: autom [command] [options]" << std::endl;
std::cout << "Commands:" << 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 << "\t[script] - Runs a script if autom has not command with that name" << std::endl;
std::cout << command.listCommands() << std::endl; std::cout << command.listCommands() << std::endl;
} }

View File

@ -1,42 +1,42 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <sys/stat.h> #include <sys/stat.h>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include "inputparser.h" #include "inputparser.h"
#include "command.h" #include "command.h"
// directory for autom scripts // directory for autom scripts
#ifdef _WIN32 #ifdef _WIN32
#include <direct.h> #include <direct.h>
std::string home = getenv("USERPROFILE"); std::string home = getenv("USERPROFILE");
#else #else
std::string home = getenv("HOME"); std::string home = getenv("HOME");
#endif #endif
// directory for autom scripts // directory for autom scripts
std::string dir = home + "/autom"; std::string dir = home + "/autom";
Command command = Command(); Command command = Command();
// 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[]);
// 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[]);
// list all scripts in the autom directory // list all scripts in the autom directory
void listScripts(int argc, char *argv[]); void listScripts(int argc, char *argv[]);
// add a script in the autom directory // add a script in the autom directory
void addScript(int argc, char *argv[]); void addScript(int argc, char *argv[]);
// edit a script in the autom directory // edit a script in the autom directory
void editScript(int argc, char *argv[]); void editScript(int argc, char *argv[]);
void editScript(std::string name); void editScript(std::string name);
// remove a script in the autom directory // remove a script in the autom directory
void removeScript(int argc, char *argv[]); void removeScript(int argc, char *argv[]);
// help function for showing help message // help function for showing help message
void help(int argc, char *argv[]); void help(int argc, char *argv[]);
// void create(int argc,char *argv[]); // void create(int argc,char *argv[]);
// void remove(int argc,char *argv[]); // void remove(int argc,char *argv[]);
// void list(int argc,char *argv[]); // void list(int argc,char *argv[]);
// void edit(int argc,char *argv[]); // void edit(int argc,char *argv[]);
// void run(int argc,char *argv[]); // void run(int argc,char *argv[]);