#include #include #include #include #include #include #include #include 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 &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 &arguments) { print_command(arguments); std::string command = ""; for (auto &&i : arguments) { command += " " + i; } return std::system(command.c_str()) == 0; } bool check_if_rebuild(const std::filesystem::path &org_path, const 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 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 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 files; std::vector 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 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; } std::vector get_includes(const std::filesystem::path &path) { std::vector ret; std::ifstream ifs(path); std::string line; while (std::getline(ifs, line)) { if (!line[0] == '#' && !line.empty()) break; if (line[line.size() - 1] == '\"') { ret.push_back(line.substr(10, line.length() - 10 - 1)); } } return ret; }