From 1813b3f5cf65376c5857ed1e2d051a7983268d2d Mon Sep 17 00:00:00 2001 From: firewire Date: Sun, 28 Jun 2026 08:18:15 -0400 Subject: [PATCH] Refactor into a oop --- src/actions.cpp | 28 ++++++------ src/actions.h | 15 ------- src/app.cpp | 50 +++++++++++++++++++++ src/app.h | 39 ++++++++++++++++ src/commands.cpp | 108 ++++++++++++++++++++++----------------------- src/commands.h | 13 ------ src/filesystem.cpp | 51 +++++++++++---------- src/filesystem.h | 12 ----- src/main.cpp | 41 ++--------------- 9 files changed, 184 insertions(+), 173 deletions(-) delete mode 100644 src/actions.h create mode 100644 src/app.cpp create mode 100644 src/app.h delete mode 100644 src/commands.h delete mode 100644 src/filesystem.h diff --git a/src/actions.cpp b/src/actions.cpp index d983187..71d3a50 100644 --- a/src/actions.cpp +++ b/src/actions.cpp @@ -1,7 +1,7 @@ -#include "actions.h" +#include "app.h" -int runCmd(std::string cmd, bool dryrun) { - if (dryrun) { +int Application::runCmd(std::string cmd) { + if (m_DryRun) { spdlog::get("cmd")->warn("[DRYRUN] Running '{}'", cmd); return 0; } @@ -27,15 +27,14 @@ int runCmd(std::string cmd, bool dryrun) { return exitCode; } -void buildDir(const std::filesystem::path& path, bool dryrun) { - if (dryrun) { +void Application::buildDir(const std::filesystem::path& path) { + if (m_DryRun) { spdlog::get("build")->warn("[DRYRUN] Building '{}'", path.string()); return; } spdlog::get("build")->info("Building '{}'", path.string()); - if (int exitCode = - runCmd(std::format("cd {} && ninja", path.string()), dryrun); + if (int exitCode = runCmd(std::format("cd {} && ninja", path.string())); exitCode != 0) { spdlog::get("build")->critical("Command exited with exit code: {}", exitCode); @@ -43,9 +42,9 @@ void buildDir(const std::filesystem::path& path, bool dryrun) { } } -void configureDir(const std::filesystem::path& path, std::string buildStr, - bool dryrun) { - if (dryrun) { +void Application::configureDir(const std::filesystem::path& path, + std::string buildStr) { + if (m_DryRun) { spdlog::get("configure") ->warn("[DRYRUN] Configuring '{}'", path.string()); return; @@ -54,8 +53,7 @@ void configureDir(const std::filesystem::path& path, std::string buildStr, spdlog::get("configure")->info("Configuring '{}'", path.string()); if (int exitCode = runCmd( std::format("cd {} && cmake ../../ -G Ninja -DCMAKE_BUILD_TYPE={}", - path.string(), buildStr), - dryrun); + path.string(), buildStr)); exitCode != 0) { spdlog::get("configure") ->critical("Command exited with exit code: {}", exitCode); @@ -63,15 +61,15 @@ void configureDir(const std::filesystem::path& path, std::string buildStr, } } -void cleanDir(const std::filesystem::path& path, bool dryrun) { - if (dryrun) { +void Application::cleanDir(const std::filesystem::path& path) { + if (m_DryRun) { spdlog::get("clean")->warn("[DRYRUN] Cleaning '{}'", path.string()); return; } spdlog::get("clean")->info("Cleaning '{}'", path.string()); if (int exitCode = - runCmd(std::format("cd {} && ninja clean", path.string()), dryrun); + runCmd(std::format("cd {} && ninja clean", path.string())); exitCode != 0) { spdlog::get("clean")->critical("Command exited with exit code: {}", exitCode); diff --git a/src/actions.h b/src/actions.h deleted file mode 100644 index a63c2d8..0000000 --- a/src/actions.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef BUILD_SCRIPT_ACTIONS -#define BUILD_SCRIPT_ACTIONS - -#include - -#include "filesystem.h" -#include "logging.h" - -int runCmd(std::string cmd, bool dryrun); -void buildDir(const std::filesystem::path& path, bool dryrun); -void configureDir(const std::filesystem::path& path, std::string buildStr, - bool dryrun); -void cleanDir(const std::filesystem::path& path, bool dryrun); - -#endif // !BUILD_SCRIPT_ACTIONS diff --git a/src/app.cpp b/src/app.cpp new file mode 100644 index 0000000..6495116 --- /dev/null +++ b/src/app.cpp @@ -0,0 +1,50 @@ +#include "app.h" + +#include "args.h" + +Application::Application(Options options) + : m_DryRun(options.dryrun.value()), + m_Verbose(options.verbose.value()), + m_BuildType(options.buildType.value()), + m_Command(options.command) { + createAllLoggers(); +} + +void Application::run() { + spdlog::get("app")->debug("Running command"); + switch (m_Command) { + case build: + buildProject(); + break; + case configure: + configureProject(); + break; + case clean: + cleanProject(); + break; + case del: + deleteProject(); + break; + case rebuild: + rebuildProject(); + break; + } + + spdlog::get("app")->info("All work done!"); +} + +void Application::createAllLoggers() { + initLogging(); + createLogger("app", nullptr, m_Verbose); + spdlog::get("app")->debug("Creating logger: 'build'."); + createLogger("build", nullptr, m_Verbose); + spdlog::get("app")->debug("Creating logger: 'configure'."); + createLogger("configure", nullptr, m_Verbose); + spdlog::get("app")->debug("Creating logger: 'clean'."); + createLogger("clean", nullptr, m_Verbose); + spdlog::get("app")->debug("Creating logger: 'filesystem'."); + createLogger("filesystem", nullptr, m_Verbose); + spdlog::get("app")->debug("Creating logger: 'cmd'."); + createLogger("cmd", nullptr, m_Verbose); + spdlog::get("app")->debug("Loggers created."); +} diff --git a/src/app.h b/src/app.h new file mode 100644 index 0000000..3259039 --- /dev/null +++ b/src/app.h @@ -0,0 +1,39 @@ +#ifndef BUILD_SCRIPT_APP +#define BUILD_SCRIPT_APP + +#include + +#include "args.h" +#include "logging.h" +#include "types.h" + +class Application { + public: + Application(Options options); + void run(); + + private: + bool m_DryRun; + bool m_Verbose; + BuildType m_BuildType; + Command m_Command; + + void createAllLoggers(); + + void buildProject(); + void configureProject(); + void cleanProject(); + void deleteProject(); + void rebuildProject(); + + void deleteDir(const std::filesystem::path& path); + void createDir(const std::filesystem::path& path); + void checkBuildDirs(); + + int runCmd(std::string cmd); + void buildDir(const std::filesystem::path& path); + void configureDir(const std::filesystem::path& path, std::string buildStr); + void cleanDir(const std::filesystem::path& path); +}; + +#endif // !BUILD_SCRIPT_APP diff --git a/src/commands.cpp b/src/commands.cpp index 9145920..0c782e4 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -1,109 +1,109 @@ -#include "commands.h" +#include "app.h" -void buildProject(const BuildType buildType, bool dryrun) { - checkBuildDirs(buildType, dryrun); - switch (buildType) { +void Application::buildProject() { + checkBuildDirs(); + switch (m_BuildType) { case none: - configureDir("build/debug", "Debug", dryrun); - buildDir("build/debug", dryrun); - configureDir("build/release", "Release", dryrun); - buildDir("build/release", dryrun); - configureDir("build/relwithdeb", "RelWithDeb", dryrun); - buildDir("build/relwithdeb", dryrun); - configureDir("build/minsizerel", "MinSizeRel", dryrun); - buildDir("build/minsizerel", dryrun); + configureDir("build/debug", "Debug"); + buildDir("build/debug"); + configureDir("build/release", "Release"); + buildDir("build/release"); + configureDir("build/relwithdeb", "RelWithDeb"); + buildDir("build/relwithdeb"); + configureDir("build/minsizerel", "MinSizeRel"); + buildDir("build/minsizerel"); break; case debug: - configureDir("build/debug", "Debug", dryrun); - buildDir("build/debug", dryrun); + configureDir("build/debug", "Debug"); + buildDir("build/debug"); break; case release: - configureDir("build/release", "Release", dryrun); - buildDir("build/release", dryrun); + configureDir("build/release", "Release"); + buildDir("build/release"); break; case relwithdeb: - configureDir("build/relwithdeb", "RelWithDeb", dryrun); - buildDir("build/relwithdeb", dryrun); + configureDir("build/relwithdeb", "RelWithDeb"); + buildDir("build/relwithdeb"); break; case minsizerel: - configureDir("build/minsizerel", "MinSizeRel", dryrun); - buildDir("build/minsizerel", dryrun); + configureDir("build/minsizerel", "MinSizeRel"); + buildDir("build/minsizerel"); break; } } -void configureProject(const BuildType buildType, bool dryrun) { - checkBuildDirs(buildType, dryrun); - switch (buildType) { +void Application::configureProject() { + checkBuildDirs(); + switch (m_BuildType) { case none: - configureDir("build/debug", "Debug", dryrun); - configureDir("build/release", "Release", dryrun); - configureDir("build/relwithdeb", "RelWithDeb", dryrun); - configureDir("build/minsizerel", "MinSizeRel", dryrun); + configureDir("build/debug", "Debug"); + configureDir("build/release", "Release"); + configureDir("build/relwithdeb", "RelWithDeb"); + configureDir("build/minsizerel", "MinSizeRel"); break; case debug: - configureDir("build/debug", "Debug", dryrun); + configureDir("build/debug", "Debug"); break; case release: - configureDir("build/release", "Release", dryrun); + configureDir("build/release", "Release"); break; case relwithdeb: - configureDir("build/relwithbeb", "RelWithDeb", dryrun); + configureDir("build/relwithbeb", "RelWithDeb"); break; case minsizerel: - configureDir("build/minsizerel", "MinSizeRel", dryrun); + configureDir("build/minsizerel", "MinSizeRel"); break; } } -void cleanProject(const BuildType buildType, bool dryrun) { - checkBuildDirs(buildType, dryrun); +void Application::cleanProject() { + checkBuildDirs(); - switch (buildType) { + switch (m_BuildType) { case none: - cleanDir("build/debug", dryrun); - cleanDir("build/release", dryrun); - cleanDir("build/relwithdeb", dryrun); - cleanDir("build/minsizerel", dryrun); + cleanDir("build/debug"); + cleanDir("build/release"); + cleanDir("build/relwithdeb"); + cleanDir("build/minsizerel"); break; case debug: - cleanDir("build/debug", dryrun); + cleanDir("build/debug"); break; case release: - cleanDir("build/release", dryrun); + cleanDir("build/release"); break; case relwithdeb: - cleanDir("build/relwithdeb", dryrun); + cleanDir("build/relwithdeb"); break; case minsizerel: - cleanDir("build/minsizerel", dryrun); + cleanDir("build/minsizerel"); break; } } -void deleteProject(const BuildType buildType, bool dryrun) { - switch (buildType) { +void Application::deleteProject() { + switch (m_BuildType) { case none: - deleteDir("build", dryrun); + deleteDir("build"); break; case debug: - deleteDir("build/debug", dryrun); + deleteDir("build/debug"); break; case release: - deleteDir("build/release", dryrun); + deleteDir("build/release"); break; case relwithdeb: - deleteDir("build/relwithdeb", dryrun); + deleteDir("build/relwithdeb"); break; case minsizerel: - deleteDir("build/minsizerel", dryrun); + deleteDir("build/minsizerel"); break; } } -void rebuildProject(const BuildType buildType, bool dryrun) { - deleteProject(buildType, dryrun); - checkBuildDirs(buildType, dryrun); - configureProject(buildType, dryrun); - buildProject(buildType, dryrun); +void Application::rebuildProject() { + deleteProject(); + checkBuildDirs(); + configureProject(); + buildProject(); } diff --git a/src/commands.h b/src/commands.h deleted file mode 100644 index c589768..0000000 --- a/src/commands.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef BUILD_SCRIPT_COMMANDS -#define BUILD_SCRIPT_COMMANDS - -#include "actions.h" -#include "types.h" - -void buildProject(const BuildType buildType, bool dryrun); -void configureProject(const BuildType buildType, bool dryrun); -void cleanProject(const BuildType buildType, bool dryrun); -void deleteProject(const BuildType buildType, bool dryrun); -void rebuildProject(const BuildType buildType, bool dryrun); - -#endif // !BUILD_SCRIPT_COMMANDS diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 7740013..91523bb 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -1,11 +1,10 @@ -#include "filesystem.h" - #include +#include "app.h" #include "logging.h" -void deleteDir(const std::filesystem::path& path, bool dryrun) { - if (dryrun) { +void Application::deleteDir(const std::filesystem::path& path) { + if (m_DryRun) { spdlog::get("filesystem") ->warn("[DRYRUN] Deleting '{}'", path.string()); return; @@ -27,8 +26,8 @@ void deleteDir(const std::filesystem::path& path, bool dryrun) { std::filesystem::remove_all(path); } -void createDir(const std::filesystem::path& path, bool dryrun) { - if (dryrun) { +void Application::createDir(const std::filesystem::path& path) { + if (m_DryRun) { spdlog::get("filesystem") ->warn("[DRYRUN] Creating '{}'", path.string()); return; @@ -41,31 +40,31 @@ void createDir(const std::filesystem::path& path, bool dryrun) { } } -void checkBuildDirs(const BuildType buildType, bool dryrun) { +void Application::checkBuildDirs() { spdlog::get("filesystem")->debug("Checking 'build' directory."); if (!std::filesystem::is_directory("build")) { spdlog::get("filesystem") ->warn("'build' is not a directory. Deleting."); - deleteDir("build", dryrun); + deleteDir("build"); } if (!std::filesystem::exists("build")) { spdlog::get("filesystem")->info("Creating 'build' directory"); - createDir("build", dryrun); + createDir("build"); } - switch (buildType) { + switch (m_BuildType) { case none: spdlog::get("filesystem") ->debug("Checking 'build/debug' directory."); if (!std::filesystem::is_directory("build/debug")) { spdlog::get("filesystem") ->warn("'build/debug' is not a directory. Deleting."); - deleteDir("build/debug", dryrun); + deleteDir("build/debug"); } if (!std::filesystem::exists("build/debug")) { spdlog::get("filesystem") ->info("Creating 'build/debug' directory."); - createDir("build/debug", dryrun); + createDir("build/debug"); } spdlog::get("filesystem") @@ -73,12 +72,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) { if (!std::filesystem::is_directory("build/release")) { spdlog::get("filesystem") ->warn("'build/release' is not a directory. Deleting."); - deleteDir("build/release", dryrun); + deleteDir("build/release"); } if (!std::filesystem::exists("build/release")) { spdlog::get("filesystem") ->info("Creating 'build/release' directory."); - createDir("build/release", dryrun); + createDir("build/release"); } spdlog::get("filesystem") @@ -86,12 +85,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) { if (!std::filesystem::is_directory("build/relwithdeb")) { spdlog::get("filesystem") ->warn("'build/relwithdeb' is not a directory. Deleting."); - deleteDir("build/relwithdeb", dryrun); + deleteDir("build/relwithdeb"); } if (!std::filesystem::exists("build/relwithdeb")) { spdlog::get("filesystem") ->info("Creating 'build/relwithdeb' directory."); - createDir("build/relwithdeb", dryrun); + createDir("build/relwithdeb"); } spdlog::get("filesystem") @@ -99,12 +98,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) { if (!std::filesystem::is_directory("build/minsizerel")) { spdlog::get("filesystem") ->warn("'build/minsizerel' is not a directory. Deleting."); - deleteDir("build/minsizerel", dryrun); + deleteDir("build/minsizerel"); } if (!std::filesystem::exists("build/minsizerel")) { spdlog::get("filesystem") ->info("Creating 'build/minsizerel' directory."); - createDir("build/minsizerel", dryrun); + createDir("build/minsizerel"); }; break; @@ -114,12 +113,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) { if (!std::filesystem::is_directory("build/debug")) { spdlog::get("filsystem") ->warn("'build/debug' is not a directory. Deleting."); - deleteDir("build/debug", dryrun); + deleteDir("build/debug"); } if (!std::filesystem::exists("build/debug")) { spdlog::get("filesystem") ->info("Creating 'build/debug' directory."); - createDir("build/debug", dryrun); + createDir("build/debug"); } break; case release: @@ -128,12 +127,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) { if (!std::filesystem::is_directory("build/release")) { spdlog::get("filesystem") ->warn("'build/release' is not a directory. Deleting."); - deleteDir("build/release", dryrun); + deleteDir("build/release"); } if (!std::filesystem::exists("build/release")) { spdlog::get("filesystem") ->info("Creating 'build/release' directory."); - createDir("build/release", dryrun); + createDir("build/release"); } break; case relwithdeb: @@ -142,12 +141,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) { if (!std::filesystem::is_directory("build/relwithdeb")) { spdlog::get("filesystem") ->warn("'build/relwithdeb' is not a directory. Deleting."); - deleteDir("build/relwithdeb", dryrun); + deleteDir("build/relwithdeb"); } if (!std::filesystem::exists("build/relwithdeb")) { spdlog::get("filesystem") ->info("Creating 'build/relwithdeb' directory."); - createDir("build/relwithdeb", dryrun); + createDir("build/relwithdeb"); }; break; case minsizerel: @@ -156,12 +155,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) { if (!std::filesystem::is_directory("build/minsizerel")) { spdlog::get("filesystem") ->warn("'build/minsizerel' is not a directory. Deleting."); - deleteDir("build/minsizerel", dryrun); + deleteDir("build/minsizerel"); } if (!std::filesystem::exists("build/minsizerel")) { spdlog::get("filesystem") ->info("Creating 'build/minsizerel' directory."); - createDir("build/minsizerel", dryrun); + createDir("build/minsizerel"); }; break; } diff --git a/src/filesystem.h b/src/filesystem.h deleted file mode 100644 index d383c2f..0000000 --- a/src/filesystem.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef BUILD_SCRIPT_FILESYSTEM -#define BUILD_SCRIPT_FILESYSTEM - -#include - -#include "types.h" - -void deleteDir(const std::filesystem::path& path, bool dryrun); -void createDir(const std::filesystem::path& path, bool dryrun); -void checkBuildDirs(const BuildType buildType, bool dryrun); - -#endif // !BUILD_SCRIPT_FILESYSTEM diff --git a/src/main.cpp b/src/main.cpp index 2b5d760..2cd21fd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,5 @@ +#include "app.h" #include "args.h" -#include "commands.h" -#include "logging.h" -#include "types.h" #ifdef _WIN32 #define popen _popen @@ -10,41 +8,8 @@ int main(const int argc, char** argv) { Options options = parse(argc, argv); + Application app(options); - initLogging(); - createLogger("app", nullptr, options.verbose.value()); - spdlog::get("app")->debug("Creating logger: 'build'."); - createLogger("build", nullptr, options.verbose.value()); - spdlog::get("app")->debug("Creating logger: 'configure'."); - createLogger("configure", nullptr, options.verbose.value()); - spdlog::get("app")->debug("Creating logger: 'clean'."); - createLogger("clean", nullptr, options.verbose.value()); - spdlog::get("app")->debug("Creating logger: 'filesystem'."); - createLogger("filesystem", nullptr, options.verbose.value()); - spdlog::get("app")->debug("Creating logger: 'cmd'."); - createLogger("cmd", nullptr, options.verbose.value()); - spdlog::get("app")->debug("Loggers created."); - - spdlog::get("app")->debug("Running command"); - switch (options.command) { - case build: - buildProject(options.buildType.value(), options.dryrun.value()); - break; - case configure: - configureProject(options.buildType.value(), options.dryrun.value()); - break; - case clean: - cleanProject(options.buildType.value(), options.dryrun.value()); - break; - case del: - deleteProject(options.buildType.value(), options.dryrun.value()); - break; - case rebuild: - rebuildProject(options.buildType.value(), options.dryrun.value()); - break; - } - - spdlog::get("app")->info("All work done!"); - + app.run(); return 0; }