diplomska_naloga/build.hpp
2024-06-07 22:05:18 +02:00

158 lines
3.0 KiB
C++

#include <cstdio>
#include <cassert>
#include <cstring>
#include <ctime>
#include <thread>
#include <vector>
#include <string>
#include <list>
#include <filesystem>
enum class Color
{
black,
red,
green,
yellow,
blue,
purple,
cyan,
white,
reset,
};
void pick_color(Color color)
{
switch (color)
{
case Color::black:
printf("\033[0;30m");
break;
case Color::red:
printf("\033[0;31m");
break;
case Color::green:
printf("\033[0;32m");
break;
case Color::yellow:
printf("\033[0;33m");
break;
case Color::blue:
printf("\033[0;34m");
break;
case Color::purple:
printf("\033[0;35m");
break;
case Color::cyan:
printf("\033[0;36m");
break;
case Color::white:
printf("\033[0;37m");
break;
case Color::reset:
printf("\033[0m");
break;
default:
break;
}
}
void print_command(std::vector<std::string> &arguments)
{
pick_color(Color::cyan);
for (auto &&i : arguments)
{
printf("%s ", i.c_str());
}
pick_color(Color::white);
printf("\n");
}
bool nob_cmd_run_sync(std::vector<std::string> &arguments)
{
print_command(arguments);
std::string command = "";
for (auto &&i : arguments)
{
command += " " + i;
}
return std::system(command.c_str()) == 0;
}
bool check_if_rebuild(std::filesystem::path &org_path, std::filesystem::path &new_path)
{
if (!std::filesystem::exists(org_path))
return false;
if (!std::filesystem::exists(new_path))
return true;
auto file_time_one = std::filesystem::last_write_time(org_path);
auto file_time_two = std::filesystem::last_write_time(new_path);
return file_time_one > file_time_two;
}
bool rebuild_my_self(std::filesystem::path src_path, int argc, const char **exec_path)
{
std::vector<std::string> input;
for (int i = 0; i < argc; ++i)
input.push_back(exec_path[i]);
std::filesystem::path exec = input[0];
if (!check_if_rebuild(src_path, exec))
return false;
std::vector<std::string> comand = {"g++", src_path, "-o", exec_path[0], "-g"};
if (!nob_cmd_run_sync(comand))
return false;
if (!nob_cmd_run_sync(input))
return false;
printf("rebuild\n");
return true;
}
struct nob_directory
{
std::vector<std::filesystem::path> files;
std::vector<std::filesystem::path> dirs;
};
nob_directory get_all_files_in_dir(std::filesystem::path directory_path)
{
namespace fs = std::filesystem;
nob_directory dir;
if (!fs::is_directory(directory_path))
return dir;
std::list<fs::path> dirs;
dirs.push_back(directory_path);
dir.dirs.push_back(directory_path);
while (!dirs.empty())
{
for (const auto &entry : fs::directory_iterator(dirs.front()))
{
// Check if the entry is a regular file
if (fs::is_regular_file(entry))
{
dir.files.push_back(entry.path());
}
// Check if the entry is a directory
else if (fs::is_directory(entry))
{
dirs.push_back(entry.path());
dir.dirs.push_back(entry.path());
}
}
dirs.pop_front();
}
return dir;
}