Skip to content

Commit

Permalink
refactor: Perform a major code refactoring
Browse files Browse the repository at this point in the history
* refactor: Move private definitions to unnamed namespaces

Signed-off-by: Swarnava Mukherjee <[email protected]>

* refactor: Update scaffold to demonstrate usage of private definitions in unnamed namespaces

Signed-off-by: Swarnava Mukherjee <[email protected]>

* lint: Lint fix

Signed-off-by: Swarnava Mukherjee <[email protected]>

* lint: Fix linter reported issues in scaffold content

Signed-off-by: Swarnava Mukherjee <[email protected]>

* refactor: Remove private designated comparator altogether

Signed-off-by: Swarnava Mukherjee <[email protected]>

* refactor: Use domain model name for type

Signed-off-by: Swarnava Mukherjee <[email protected]>

* feat: Make scaffold content more informational

Signed-off-by: Swarnava Mukherjee <[email protected]>

* lint: Remove extraneous spaces from indirection operator

Signed-off-by: Swarnava Mukherjee <[email protected]>

* lint: Correct alignment

Signed-off-by: Swarnava Mukherjee <[email protected]>

* fix: Update sample test-cases

Signed-off-by: Swarnava Mukherjee <[email protected]>

---------

Signed-off-by: Swarnava Mukherjee <[email protected]>
  • Loading branch information
swar-mukh authored Jul 11, 2024
1 parent 222dcfc commit 24ebfbd
Show file tree
Hide file tree
Showing 14 changed files with 300 additions and 193 deletions.
212 changes: 157 additions & 55 deletions headers/assets/scaffold_texts.hpp

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions headers/commands.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
#include <string>

namespace commands {
void create_project(const std::string project_name);
void create_project(const std::string& project_name);

void create_file(const std::string file_name);
void create_file(const std::string& file_name);

void compile_project();
void clear_build();
Expand All @@ -21,4 +21,4 @@ namespace commands {
void show_usage();
}

#endif
#endif
6 changes: 3 additions & 3 deletions headers/workspace/env_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace workspace::env_manager {

using ALLOWED_ENV_DATA_TYPES = std::variant<bool, int, float, string>;

ALLOWED_ENV_DATA_TYPES get_env(const string key);
void prepare_env(std::map<string, string> env);
ALLOWED_ENV_DATA_TYPES get_env(const string& key);
void prepare_env(std::map<string, string>& env);
}

#endif
#endif
15 changes: 8 additions & 7 deletions headers/workspace/modification_identifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
#include <filesystem>
#include <set>
#include <string>
#include <utility>

#include "workspace/project_config.hpp"

namespace workspace::modification_identifier {
namespace fs = std::filesystem;

using FileHash = decltype(fs::hash_value(std::declval<fs::path>()));

struct SourceFile {
std::size_t hash;
FileHash hash;
std::string file_name;

std::size_t last_modified_timestamp;
Expand All @@ -20,19 +23,17 @@ namespace workspace::modification_identifier {

mutable bool affected;
mutable bool was_successful;
};

struct __SourceFileComparator {
bool operator()(const SourceFile& left, const SourceFile& right) const {
return left.hash < right.hash;
bool operator<(const SourceFile& another_file) const {
return this->hash < another_file.hash;
}
};

using SourceFiles = std::set<SourceFile, __SourceFileComparator>;
using SourceFiles = std::set<SourceFile>;

std::size_t get_current_fileclock_timestamp();
SourceFiles list_all_files_annotated(const workspace::project_config::Project& project);
void persist_annotations(SourceFiles& bucket);
void persist_annotations(const SourceFiles& bucket);
}

#endif
16 changes: 7 additions & 9 deletions headers/workspace/project_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ namespace workspace::project_config {
struct Author {
string name;
string email_id;
};

struct __AuthorComparator {
bool operator()(const Author &left, const Author &right) const {
return left.name > right.name;

bool operator<(const Author &another_author) const {
return this->name < another_author.name;
}
};

Expand All @@ -40,16 +38,16 @@ namespace workspace::project_config {
string description;
string version;

std::set<Author, __AuthorComparator> authors;
std::set<Author> authors;
std::set<Platform> platforms;
Config config;
};

string platform_to_string(const Platform platform);
Platform string_to_platform(const string platform);
string platform_to_string(const Platform& platform);
Platform string_to_platform(const string& platform);

Project convert_cfg_to_model();
string convert_model_to_cfg(const Project project, const bool add_disclaimer_text = true);
string convert_model_to_cfg(const Project& project, const bool add_disclaimer_text = true);
}

#endif
10 changes: 6 additions & 4 deletions headers/workspace/scaffold.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@
#include "workspace/modification_identifier.hpp"

namespace workspace::scaffold {
using std::string;

const std::regex IMPORT_R{ "@FILE_NAME" };
const std::regex GUARD_R{ "@GUARD" };
const std::regex NAMESPACE_R{ "@NAMESPACE" };
const std::regex RELATIVE_SRC_R{ "@RELATIVE_SRC_FILE_NAME" };

void create_file(const std::string project_name, const std::string file_name, const bool verbose = true);
bool create_directory(const std::string project_name, const std::string sub_directory = "", const bool multi_directory = false, const bool verbose = true);
void create_file(const string& project_name, const string& file_name, const bool verbose = true);
bool create_directory(const string& project_name, const string& sub_directory = "", const bool multi_directory = false, const bool verbose = true);
void create_build_tree_as_necessary();
void create_internals_tree_as_necessary();

void purge_old_binaries(const std::string path, workspace::modification_identifier::SourceFiles& annotated_files);
void purge_old_binaries(const string& path, const workspace::modification_identifier::SourceFiles& annotated_files);

void exit_if_command_not_invoked_from_within_workspace();
}

#endif
#endif
16 changes: 8 additions & 8 deletions headers/workspace/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ namespace workspace::util {

string change_case(string text, const TextCase casing);
std::tuple<bool, string> does_name_contain_special_characters(const string& text, const bool is_it_for_project);
std::tuple<bool, string> is_valid_project_name(const string project_name);
std::tuple<bool, string> is_valid_file_name(const string file_name);
std::tuple<string, string, string> get_qualified_names(const string full_file_path);
string convert_stemmed_name_to_guard_name(const string stemmed_name);
string convert_stemmed_name_to_namespace_name(const string stemmed_name);
std::tuple<string, string> get_key_value_pair_from_line(const string line, const string delimiter);
string get_platform_formatted_filename(const string file_name);
std::tuple<bool, string> is_valid_project_name(const string& project_name);
std::tuple<bool, string> is_valid_file_name(const string& file_name);
std::tuple<string, string, string> get_qualified_names(const string& full_file_path);
string convert_stemmed_name_to_guard_name(const string& stemmed_name);
string convert_stemmed_name_to_namespace_name(const string& stemmed_name);
std::tuple<string, string> get_key_value_pair_from_line(const string& line, const string& delimiter);
string get_platform_formatted_filename(const string& file_name);
string get_platform_formatted_filename(std::filesystem::path path);
string get_ISO_date();
}

#endif
#endif
8 changes: 4 additions & 4 deletions src/commands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace commands {
using std::string;
using namespace workspace::project_config;

void create_project(const string project_name) {
void create_project(const string& project_name) {
if (fs::exists(project_name)) {
cout << "Directory '" << project_name << "' already exists!" << endl;
return;
Expand Down Expand Up @@ -72,7 +72,7 @@ namespace commands {
}
}

void create_file(const string file_name) {
void create_file(const string& file_name) {
const auto [is_valid, reason_if_any] = workspace::util::is_valid_file_name(file_name);

if (!is_valid) {
Expand Down Expand Up @@ -193,15 +193,15 @@ namespace commands {
const string SEPARATOR{ fs::path::preferred_separator };

for (auto dir_entry = fs::recursive_directory_iterator("build"); dir_entry != fs::recursive_directory_iterator(); ++dir_entry) {
const string normalised_path{ workspace::util::get_platform_formatted_filename(dir_entry -> path().string()) };
const string normalised_path{ workspace::util::get_platform_formatted_filename(dir_entry->path().string()) };

if (!normalised_path.starts_with(BUILD_PATH)) {
dir_entry.disable_recursion_pending();
}

if (fs::is_directory(*dir_entry)) {
const int files_count = std::count_if(
fs::directory_iterator(dir_entry -> path()),
fs::directory_iterator(dir_entry->path()),
{},
[](auto& file){ return file.is_regular_file(); }
);
Expand Down
22 changes: 8 additions & 14 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
#include <cstdlib>
#include <exception>
#include <iostream>
#include <string>
#include <vector>

#include "commands.hpp"
#include "workspace/scaffold.hpp"

using std::cerr;
using std::cout;
using std::endl;
using std::exception;
using std::string;
using std::vector;

void parse_commands_and_execute(vector <string> arguments) {
void parse_commands_and_execute(std::vector<std::string>& arguments) {
try {
if (arguments.size() == 3) {
if (arguments[1].compare("create-project") == 0) {
Expand Down Expand Up @@ -47,27 +41,27 @@ void parse_commands_and_execute(vector <string> arguments) {
} else {
commands::show_usage();
}
} catch (const exception & e) {
cerr << "Exception: " << e.what() << endl << endl;
} catch (const std::exception& e) {
std::cerr << "Exception: " << e.what() << std::endl << std::endl;
std::exit(EXIT_FAILURE);
} catch (...) {
cerr << "Something went wrong!" << endl << endl;
std::cerr << "Something went wrong!" << std::endl << std::endl;
std::exit(EXIT_FAILURE);
}
}

int main(const int argc, char *argv[]) {
vector<string> args(argv, argv + argc);
std::vector<std::string> args(argv, argv + argc);

cout << endl;
std::cout << std::endl;

if (args.size() == 1) {
commands::show_usage();
} else {
parse_commands_and_execute(args);
}

cout << endl;
std::cout << std::endl;

return EXIT_SUCCESS;
}
48 changes: 26 additions & 22 deletions src/workspace/env_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

#include "workspace/util.hpp"

namespace workspace::env_manager {
namespace {
using namespace workspace::env_manager;

namespace fs = std::filesystem;

using std::cerr;
Expand All @@ -26,15 +28,15 @@ namespace workspace::env_manager {
std::map<string, string> env_template;
std::map<string, ALLOWED_ENV_DATA_TYPES> env_values;

std::map<string, std::function<ALLOWED_ENV_DATA_TYPES(const string, const string)>> PARSERS{
{ "bool", [](const string key, const string value) {
std::map<string, std::function<ALLOWED_ENV_DATA_TYPES(const string&, const string&)>> PARSERS{
{ "bool", [](const string& key, const string& value) {
if (value.compare("true") == 0 || value.compare("false") == 0) {
return value.compare("true") == 0 ? true : false;
} else {
throw std::invalid_argument("Could not parse value for '" + key + "' to 'bool' type. Expected either 'true' or 'false'.");
}
}},
{ "int", [](const string key, const string value) {
{ "int", [](const string& key, const string& value) {
try {
return std::stoi(value);
} catch (const std::invalid_argument &e) {
Expand All @@ -43,7 +45,7 @@ namespace workspace::env_manager {
throw std::invalid_argument("Value for '" + key + "' falls out of range of 'int' type.");
}
}},
{ "float", [](const string key, const string value) {
{ "float", [](const string& key, const string& value) {
try {
return std::stof(value);
} catch (const std::invalid_argument &e) {
Expand All @@ -52,10 +54,10 @@ namespace workspace::env_manager {
throw std::invalid_argument("Value for '" + key + "' falls out of range of 'float' type.");
}
}},
{ "string", []([[maybe_unused]] const string _, const string value) { return value; } },
{ "string", []([[maybe_unused]] const string& _, const string& value) { return value; } },
};

void __set(const string key, const string value) {
void set_kv(const string& key, const string& value) {
if (key.compare("a_bool_entry") == 0) {
env_values["a_bool_entry"] = PARSERS["bool"](key, value);
} else if (key.compare("an_int_entry") == 0) {
Expand All @@ -67,15 +69,7 @@ namespace workspace::env_manager {
}
}

ALLOWED_ENV_DATA_TYPES get_env(const string key) {
if (env_values.contains(key)) {
return env_values[key];
} else {
throw std::invalid_argument("Trying to access invalid key '" + key + "'");
}
}

void __read_template_file() {
void read_template_file() {
const string template_file_name{ "environments/.env.template" };

if (fs::exists(template_file_name)) {
Expand All @@ -98,7 +92,7 @@ namespace workspace::env_manager {
}
}

void __read_env_file(const string env) {
void read_env_file(const string& env) {
const string env_file_name{ "environments/" + env + ".env" };

ifstream env_file(env_file_name);
Expand All @@ -112,19 +106,29 @@ namespace workspace::env_manager {
if (!env_template.contains(key)) {
throw std::domain_error("Key '" + key + "' absent in 'environments/.env.template'");
} else {
__set(key, value);
set_kv(key, value);
}
}
}
}

void prepare_env(std::map<string, string> env) {
namespace workspace::env_manager {
ALLOWED_ENV_DATA_TYPES get_env(const string& key) {
if (env_values.contains(key)) {
return env_values[key];
} else {
throw std::invalid_argument("Trying to access invalid key '" + key + "'");
}
}

void prepare_env(std::map<string, string>& env) {
try {
__read_template_file();
read_template_file();

if (env["env"].length() != 0) {
__read_env_file(env["env"]);
read_env_file(env["env"]);
} else {
__read_env_file("local");
read_env_file("local");
}
} catch (const std::exception &e) {
cerr << endl << "Exception: " << e.what() << endl << endl;
Expand Down
Loading

0 comments on commit 24ebfbd

Please sign in to comment.