refactoring

self-hosting
MoAlyousef 4 years ago
parent 65d03aa50e
commit c2885a09f2

@ -5,6 +5,7 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <map> #include <map>
#include <new>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#include <stdio.h> #include <stdio.h>
@ -28,20 +29,30 @@ inline std::string to_upper(const std::string &str) {
return temp; return temp;
} }
template <typename... Args>
std::string format(const char *fmt, Args... args) {
auto sz = snprintf(nullptr, 0, fmt, args...) + 1;
char *buf = new char[sz];
int ret = snprintf(buf, sz, fmt, args...);
if (ret != sz - 1)
throw std::runtime_error("Error formatting string!");
std::string temp(buf, buf + sz - 1);
delete[] buf;
return temp;
}
} // namespace detail } // namespace detail
int generate_project(const char *str) { int generate_project(const char *str) {
fs::create_directory("src"); fs::create_directory("src");
auto dir_name = fs::current_path().stem().string(); const auto dir_name = fs::current_path().stem().string();
char mainbuf[100]; std::string mainbuf;
// Assuming current standards of max filename length < 270 const auto tomlbuf = detail::format(cmake_toml, dir_name.c_str(), dir_name.c_str(), str);
char tomlbuf[1000];
(void)snprintf(tomlbuf, 1000, cmake_toml, dir_name.c_str(), dir_name.c_str(), str);
if (!strcmp(str, "exe")) { if (!strcmp(str, "exe")) {
(void)snprintf(mainbuf, 100, hello_world, "main"); mainbuf = detail::format(hello_world, "main");
} else if (!strcmp(str, "static") || !strcmp(str, "shared")) { } else if (!strcmp(str, "static") || !strcmp(str, "shared")) {
fs::create_directory("include"); fs::create_directory("include");
(void)snprintf(mainbuf, 100, hello_world, "test"); mainbuf = detail::format(hello_world, "test");
} else { } else {
throw std::runtime_error("Unknown project type. Types are exe, shared, static!"); throw std::runtime_error("Unknown project type. Types are exe, shared, static!");
} }
@ -121,8 +132,9 @@ int generate_cmake(const char *path) {
} }
if (toml.contains("find-package")) { if (toml.contains("find-package")) {
std::map<std::string, std::string> deps = using pkg_map = std::map<std::string, std::string>;
toml::find<std::map<std::string, std::string>>(toml, "find-package"); pkg_map deps =
toml::find<pkg_map>(toml, "find-package");
for (const auto &dep : deps) { for (const auto &dep : deps) {
ss << "find_package(" << dep.first; ss << "find_package(" << dep.first;
if (dep.second != "*") { if (dep.second != "*") {
@ -134,9 +146,8 @@ int generate_cmake(const char *path) {
} }
if (toml.contains("fetch-content")) { if (toml.contains("fetch-content")) {
std::map<std::string, std::map<std::string, std::string>> deps = using content_map = std::map<std::string, std::map<std::string, std::string>>;
toml::find<std::map<std::string, std::map<std::string, std::string>>>(toml, content_map deps = toml::find<content_map>(toml, "fetch-content");
"fetch-content");
ss << "include(FetchContent)\n\n"; ss << "include(FetchContent)\n\n";
for (const auto &dep : deps) { for (const auto &dep : deps) {
ss << "FetchContent_Declare(\n\t" << dep.first << "\n"; ss << "FetchContent_Declare(\n\t" << dep.first << "\n";

Loading…
Cancel
Save