Refactor into a oop

This commit is contained in:
2026-06-28 08:18:15 -04:00
parent 1c7909b8f6
commit 1813b3f5cf
9 changed files with 184 additions and 173 deletions

View File

@@ -1,7 +1,7 @@
#include "actions.h" #include "app.h"
int runCmd(std::string cmd, bool dryrun) { int Application::runCmd(std::string cmd) {
if (dryrun) { if (m_DryRun) {
spdlog::get("cmd")->warn("[DRYRUN] Running '{}'", cmd); spdlog::get("cmd")->warn("[DRYRUN] Running '{}'", cmd);
return 0; return 0;
} }
@@ -27,15 +27,14 @@ int runCmd(std::string cmd, bool dryrun) {
return exitCode; return exitCode;
} }
void buildDir(const std::filesystem::path& path, bool dryrun) { void Application::buildDir(const std::filesystem::path& path) {
if (dryrun) { if (m_DryRun) {
spdlog::get("build")->warn("[DRYRUN] Building '{}'", path.string()); spdlog::get("build")->warn("[DRYRUN] Building '{}'", path.string());
return; return;
} }
spdlog::get("build")->info("Building '{}'", path.string()); spdlog::get("build")->info("Building '{}'", path.string());
if (int exitCode = if (int exitCode = runCmd(std::format("cd {} && ninja", path.string()));
runCmd(std::format("cd {} && ninja", path.string()), dryrun);
exitCode != 0) { exitCode != 0) {
spdlog::get("build")->critical("Command exited with exit code: {}", spdlog::get("build")->critical("Command exited with exit code: {}",
exitCode); exitCode);
@@ -43,9 +42,9 @@ void buildDir(const std::filesystem::path& path, bool dryrun) {
} }
} }
void configureDir(const std::filesystem::path& path, std::string buildStr, void Application::configureDir(const std::filesystem::path& path,
bool dryrun) { std::string buildStr) {
if (dryrun) { if (m_DryRun) {
spdlog::get("configure") spdlog::get("configure")
->warn("[DRYRUN] Configuring '{}'", path.string()); ->warn("[DRYRUN] Configuring '{}'", path.string());
return; return;
@@ -54,8 +53,7 @@ void configureDir(const std::filesystem::path& path, std::string buildStr,
spdlog::get("configure")->info("Configuring '{}'", path.string()); spdlog::get("configure")->info("Configuring '{}'", path.string());
if (int exitCode = runCmd( if (int exitCode = runCmd(
std::format("cd {} && cmake ../../ -G Ninja -DCMAKE_BUILD_TYPE={}", std::format("cd {} && cmake ../../ -G Ninja -DCMAKE_BUILD_TYPE={}",
path.string(), buildStr), path.string(), buildStr));
dryrun);
exitCode != 0) { exitCode != 0) {
spdlog::get("configure") spdlog::get("configure")
->critical("Command exited with exit code: {}", exitCode); ->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) { void Application::cleanDir(const std::filesystem::path& path) {
if (dryrun) { if (m_DryRun) {
spdlog::get("clean")->warn("[DRYRUN] Cleaning '{}'", path.string()); spdlog::get("clean")->warn("[DRYRUN] Cleaning '{}'", path.string());
return; return;
} }
spdlog::get("clean")->info("Cleaning '{}'", path.string()); spdlog::get("clean")->info("Cleaning '{}'", path.string());
if (int exitCode = if (int exitCode =
runCmd(std::format("cd {} && ninja clean", path.string()), dryrun); runCmd(std::format("cd {} && ninja clean", path.string()));
exitCode != 0) { exitCode != 0) {
spdlog::get("clean")->critical("Command exited with exit code: {}", spdlog::get("clean")->critical("Command exited with exit code: {}",
exitCode); exitCode);

View File

@@ -1,15 +0,0 @@
#ifndef BUILD_SCRIPT_ACTIONS
#define BUILD_SCRIPT_ACTIONS
#include <iostream>
#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

50
src/app.cpp Normal file
View File

@@ -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.");
}

39
src/app.h Normal file
View File

@@ -0,0 +1,39 @@
#ifndef BUILD_SCRIPT_APP
#define BUILD_SCRIPT_APP
#include <filesystem>
#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

View File

@@ -1,109 +1,109 @@
#include "commands.h" #include "app.h"
void buildProject(const BuildType buildType, bool dryrun) { void Application::buildProject() {
checkBuildDirs(buildType, dryrun); checkBuildDirs();
switch (buildType) { switch (m_BuildType) {
case none: case none:
configureDir("build/debug", "Debug", dryrun); configureDir("build/debug", "Debug");
buildDir("build/debug", dryrun); buildDir("build/debug");
configureDir("build/release", "Release", dryrun); configureDir("build/release", "Release");
buildDir("build/release", dryrun); buildDir("build/release");
configureDir("build/relwithdeb", "RelWithDeb", dryrun); configureDir("build/relwithdeb", "RelWithDeb");
buildDir("build/relwithdeb", dryrun); buildDir("build/relwithdeb");
configureDir("build/minsizerel", "MinSizeRel", dryrun); configureDir("build/minsizerel", "MinSizeRel");
buildDir("build/minsizerel", dryrun); buildDir("build/minsizerel");
break; break;
case debug: case debug:
configureDir("build/debug", "Debug", dryrun); configureDir("build/debug", "Debug");
buildDir("build/debug", dryrun); buildDir("build/debug");
break; break;
case release: case release:
configureDir("build/release", "Release", dryrun); configureDir("build/release", "Release");
buildDir("build/release", dryrun); buildDir("build/release");
break; break;
case relwithdeb: case relwithdeb:
configureDir("build/relwithdeb", "RelWithDeb", dryrun); configureDir("build/relwithdeb", "RelWithDeb");
buildDir("build/relwithdeb", dryrun); buildDir("build/relwithdeb");
break; break;
case minsizerel: case minsizerel:
configureDir("build/minsizerel", "MinSizeRel", dryrun); configureDir("build/minsizerel", "MinSizeRel");
buildDir("build/minsizerel", dryrun); buildDir("build/minsizerel");
break; break;
} }
} }
void configureProject(const BuildType buildType, bool dryrun) { void Application::configureProject() {
checkBuildDirs(buildType, dryrun); checkBuildDirs();
switch (buildType) { switch (m_BuildType) {
case none: case none:
configureDir("build/debug", "Debug", dryrun); configureDir("build/debug", "Debug");
configureDir("build/release", "Release", dryrun); configureDir("build/release", "Release");
configureDir("build/relwithdeb", "RelWithDeb", dryrun); configureDir("build/relwithdeb", "RelWithDeb");
configureDir("build/minsizerel", "MinSizeRel", dryrun); configureDir("build/minsizerel", "MinSizeRel");
break; break;
case debug: case debug:
configureDir("build/debug", "Debug", dryrun); configureDir("build/debug", "Debug");
break; break;
case release: case release:
configureDir("build/release", "Release", dryrun); configureDir("build/release", "Release");
break; break;
case relwithdeb: case relwithdeb:
configureDir("build/relwithbeb", "RelWithDeb", dryrun); configureDir("build/relwithbeb", "RelWithDeb");
break; break;
case minsizerel: case minsizerel:
configureDir("build/minsizerel", "MinSizeRel", dryrun); configureDir("build/minsizerel", "MinSizeRel");
break; break;
} }
} }
void cleanProject(const BuildType buildType, bool dryrun) { void Application::cleanProject() {
checkBuildDirs(buildType, dryrun); checkBuildDirs();
switch (buildType) { switch (m_BuildType) {
case none: case none:
cleanDir("build/debug", dryrun); cleanDir("build/debug");
cleanDir("build/release", dryrun); cleanDir("build/release");
cleanDir("build/relwithdeb", dryrun); cleanDir("build/relwithdeb");
cleanDir("build/minsizerel", dryrun); cleanDir("build/minsizerel");
break; break;
case debug: case debug:
cleanDir("build/debug", dryrun); cleanDir("build/debug");
break; break;
case release: case release:
cleanDir("build/release", dryrun); cleanDir("build/release");
break; break;
case relwithdeb: case relwithdeb:
cleanDir("build/relwithdeb", dryrun); cleanDir("build/relwithdeb");
break; break;
case minsizerel: case minsizerel:
cleanDir("build/minsizerel", dryrun); cleanDir("build/minsizerel");
break; break;
} }
} }
void deleteProject(const BuildType buildType, bool dryrun) { void Application::deleteProject() {
switch (buildType) { switch (m_BuildType) {
case none: case none:
deleteDir("build", dryrun); deleteDir("build");
break; break;
case debug: case debug:
deleteDir("build/debug", dryrun); deleteDir("build/debug");
break; break;
case release: case release:
deleteDir("build/release", dryrun); deleteDir("build/release");
break; break;
case relwithdeb: case relwithdeb:
deleteDir("build/relwithdeb", dryrun); deleteDir("build/relwithdeb");
break; break;
case minsizerel: case minsizerel:
deleteDir("build/minsizerel", dryrun); deleteDir("build/minsizerel");
break; break;
} }
} }
void rebuildProject(const BuildType buildType, bool dryrun) { void Application::rebuildProject() {
deleteProject(buildType, dryrun); deleteProject();
checkBuildDirs(buildType, dryrun); checkBuildDirs();
configureProject(buildType, dryrun); configureProject();
buildProject(buildType, dryrun); buildProject();
} }

View File

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

View File

@@ -1,11 +1,10 @@
#include "filesystem.h"
#include <iostream> #include <iostream>
#include "app.h"
#include "logging.h" #include "logging.h"
void deleteDir(const std::filesystem::path& path, bool dryrun) { void Application::deleteDir(const std::filesystem::path& path) {
if (dryrun) { if (m_DryRun) {
spdlog::get("filesystem") spdlog::get("filesystem")
->warn("[DRYRUN] Deleting '{}'", path.string()); ->warn("[DRYRUN] Deleting '{}'", path.string());
return; return;
@@ -27,8 +26,8 @@ void deleteDir(const std::filesystem::path& path, bool dryrun) {
std::filesystem::remove_all(path); std::filesystem::remove_all(path);
} }
void createDir(const std::filesystem::path& path, bool dryrun) { void Application::createDir(const std::filesystem::path& path) {
if (dryrun) { if (m_DryRun) {
spdlog::get("filesystem") spdlog::get("filesystem")
->warn("[DRYRUN] Creating '{}'", path.string()); ->warn("[DRYRUN] Creating '{}'", path.string());
return; 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."); spdlog::get("filesystem")->debug("Checking 'build' directory.");
if (!std::filesystem::is_directory("build")) { if (!std::filesystem::is_directory("build")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->warn("'build' is not a directory. Deleting."); ->warn("'build' is not a directory. Deleting.");
deleteDir("build", dryrun); deleteDir("build");
} }
if (!std::filesystem::exists("build")) { if (!std::filesystem::exists("build")) {
spdlog::get("filesystem")->info("Creating 'build' directory"); spdlog::get("filesystem")->info("Creating 'build' directory");
createDir("build", dryrun); createDir("build");
} }
switch (buildType) { switch (m_BuildType) {
case none: case none:
spdlog::get("filesystem") spdlog::get("filesystem")
->debug("Checking 'build/debug' directory."); ->debug("Checking 'build/debug' directory.");
if (!std::filesystem::is_directory("build/debug")) { if (!std::filesystem::is_directory("build/debug")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->warn("'build/debug' is not a directory. Deleting."); ->warn("'build/debug' is not a directory. Deleting.");
deleteDir("build/debug", dryrun); deleteDir("build/debug");
} }
if (!std::filesystem::exists("build/debug")) { if (!std::filesystem::exists("build/debug")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->info("Creating 'build/debug' directory."); ->info("Creating 'build/debug' directory.");
createDir("build/debug", dryrun); createDir("build/debug");
} }
spdlog::get("filesystem") spdlog::get("filesystem")
@@ -73,12 +72,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) {
if (!std::filesystem::is_directory("build/release")) { if (!std::filesystem::is_directory("build/release")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->warn("'build/release' is not a directory. Deleting."); ->warn("'build/release' is not a directory. Deleting.");
deleteDir("build/release", dryrun); deleteDir("build/release");
} }
if (!std::filesystem::exists("build/release")) { if (!std::filesystem::exists("build/release")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->info("Creating 'build/release' directory."); ->info("Creating 'build/release' directory.");
createDir("build/release", dryrun); createDir("build/release");
} }
spdlog::get("filesystem") spdlog::get("filesystem")
@@ -86,12 +85,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) {
if (!std::filesystem::is_directory("build/relwithdeb")) { if (!std::filesystem::is_directory("build/relwithdeb")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->warn("'build/relwithdeb' is not a directory. Deleting."); ->warn("'build/relwithdeb' is not a directory. Deleting.");
deleteDir("build/relwithdeb", dryrun); deleteDir("build/relwithdeb");
} }
if (!std::filesystem::exists("build/relwithdeb")) { if (!std::filesystem::exists("build/relwithdeb")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->info("Creating 'build/relwithdeb' directory."); ->info("Creating 'build/relwithdeb' directory.");
createDir("build/relwithdeb", dryrun); createDir("build/relwithdeb");
} }
spdlog::get("filesystem") spdlog::get("filesystem")
@@ -99,12 +98,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) {
if (!std::filesystem::is_directory("build/minsizerel")) { if (!std::filesystem::is_directory("build/minsizerel")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->warn("'build/minsizerel' is not a directory. Deleting."); ->warn("'build/minsizerel' is not a directory. Deleting.");
deleteDir("build/minsizerel", dryrun); deleteDir("build/minsizerel");
} }
if (!std::filesystem::exists("build/minsizerel")) { if (!std::filesystem::exists("build/minsizerel")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->info("Creating 'build/minsizerel' directory."); ->info("Creating 'build/minsizerel' directory.");
createDir("build/minsizerel", dryrun); createDir("build/minsizerel");
}; };
break; break;
@@ -114,12 +113,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) {
if (!std::filesystem::is_directory("build/debug")) { if (!std::filesystem::is_directory("build/debug")) {
spdlog::get("filsystem") spdlog::get("filsystem")
->warn("'build/debug' is not a directory. Deleting."); ->warn("'build/debug' is not a directory. Deleting.");
deleteDir("build/debug", dryrun); deleteDir("build/debug");
} }
if (!std::filesystem::exists("build/debug")) { if (!std::filesystem::exists("build/debug")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->info("Creating 'build/debug' directory."); ->info("Creating 'build/debug' directory.");
createDir("build/debug", dryrun); createDir("build/debug");
} }
break; break;
case release: case release:
@@ -128,12 +127,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) {
if (!std::filesystem::is_directory("build/release")) { if (!std::filesystem::is_directory("build/release")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->warn("'build/release' is not a directory. Deleting."); ->warn("'build/release' is not a directory. Deleting.");
deleteDir("build/release", dryrun); deleteDir("build/release");
} }
if (!std::filesystem::exists("build/release")) { if (!std::filesystem::exists("build/release")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->info("Creating 'build/release' directory."); ->info("Creating 'build/release' directory.");
createDir("build/release", dryrun); createDir("build/release");
} }
break; break;
case relwithdeb: case relwithdeb:
@@ -142,12 +141,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) {
if (!std::filesystem::is_directory("build/relwithdeb")) { if (!std::filesystem::is_directory("build/relwithdeb")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->warn("'build/relwithdeb' is not a directory. Deleting."); ->warn("'build/relwithdeb' is not a directory. Deleting.");
deleteDir("build/relwithdeb", dryrun); deleteDir("build/relwithdeb");
} }
if (!std::filesystem::exists("build/relwithdeb")) { if (!std::filesystem::exists("build/relwithdeb")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->info("Creating 'build/relwithdeb' directory."); ->info("Creating 'build/relwithdeb' directory.");
createDir("build/relwithdeb", dryrun); createDir("build/relwithdeb");
}; };
break; break;
case minsizerel: case minsizerel:
@@ -156,12 +155,12 @@ void checkBuildDirs(const BuildType buildType, bool dryrun) {
if (!std::filesystem::is_directory("build/minsizerel")) { if (!std::filesystem::is_directory("build/minsizerel")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->warn("'build/minsizerel' is not a directory. Deleting."); ->warn("'build/minsizerel' is not a directory. Deleting.");
deleteDir("build/minsizerel", dryrun); deleteDir("build/minsizerel");
} }
if (!std::filesystem::exists("build/minsizerel")) { if (!std::filesystem::exists("build/minsizerel")) {
spdlog::get("filesystem") spdlog::get("filesystem")
->info("Creating 'build/minsizerel' directory."); ->info("Creating 'build/minsizerel' directory.");
createDir("build/minsizerel", dryrun); createDir("build/minsizerel");
}; };
break; break;
} }

View File

@@ -1,12 +0,0 @@
#ifndef BUILD_SCRIPT_FILESYSTEM
#define BUILD_SCRIPT_FILESYSTEM
#include <filesystem>
#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

View File

@@ -1,7 +1,5 @@
#include "app.h"
#include "args.h" #include "args.h"
#include "commands.h"
#include "logging.h"
#include "types.h"
#ifdef _WIN32 #ifdef _WIN32
#define popen _popen #define popen _popen
@@ -10,41 +8,8 @@
int main(const int argc, char** argv) { int main(const int argc, char** argv) {
Options options = parse(argc, argv); Options options = parse(argc, argv);
Application app(options);
initLogging(); app.run();
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!");
return 0; return 0;
} }