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) {
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);

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) {
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();
}

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 "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;
}

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 "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;
}