diff --git a/src/Command/CommandAdd.cpp b/src/Command/CommandAdd.cpp index 488a7b6..839c14b 100644 --- a/src/Command/CommandAdd.cpp +++ b/src/Command/CommandAdd.cpp @@ -2,6 +2,7 @@ #include #include #include +#include "../Generators/Generators.hpp" namespace Command { @@ -34,7 +35,7 @@ namespace Command { } std::cout << ctx->dependencies.size() << std::endl; Command::writePackageToml(ctx); - Command::createCMakelists(ctx); + Generators::CMakeList::create(ctx); return true; } } diff --git a/src/Command/CommandGeneral.cpp b/src/Command/CommandGeneral.cpp index bacf6a8..c03f956 100644 --- a/src/Command/CommandGeneral.cpp +++ b/src/Command/CommandGeneral.cpp @@ -98,120 +98,4 @@ namespace Command { file.close(); return true; } - bool createCMakelists(std::shared_ptr ctx) { - std::string cmake_minimum_required = - std::format("cmake_minimum_required(VERSION {})", ctx->cmake_version); - std::string project_name = - std::format("project ({} VERSION {} LANGUAGES CXX)", - ctx->project_name, ctx->project_version); - std::string build_type = R"V0G0N( - if (CMAKE_BUILD_TYPE STREQUAL "Release") - message("Release mode") - set(RELEASE 1) - elseif(CMAKE_BUILD_TYPE STREQUAL "Debug") - message("Debug mode") - set(RELEASE 0) - elseif(CMAKE_BUILD_TYPE STREQUAL "Test") - message("Test mode") - set(RELEASE 0) - set(TEST_MODE 1) - else() - message("Default mode") - set(RELEASE 0) - endif() - )V0G0N"; - std::string cxx_version = - std::format("set(CMAKE_CXX_STANDARD {})", ctx->lang_version); - std::string compiler = - std::format("set(CMAKE_CXX_COMPILER {})\n", ctx->compiler); - std::string source_dir = std::format("set(SOURCE_DIR {})", ctx->src_dir); - std::string build_dir = std::format("set(BUILD_DIR {})", ctx->build_dir); - std::string FetchContent = "include(FetchContent)"; - - std::string files = R"V0G0N( - file(GLOB_RECURSE SOURCES RELATIVE ${CMAKE_SOURCE_DIR} - "include/**.h" - "include/**.hpp" - "src/**.cpp" - "src/**.c" - ) - )V0G0N"; - typedef struct make_dep { - std::string fetch_declare; - std::string fetch_make_available; - std::string target_link_libraries; - } make_dep; - - std::vector dependencies; - for (Command::dependency &dep : ctx->dependencies) { - std::string FetchContent_Declare = - std::format( - R"V0G0N( - FetchContent_Declare( - {} - GIT_REPOSITORY "{}" - GIT_TAG "{}" - ) - )V0G0N", - dep.name, dep.url, dep.version); - - std::string FetchContent_MakeAvailable = - std::format("FetchContent_MakeAvailable({})", dep.name); - std::string target_link_libraries = std::format( - "target_link_libraries({} {})", ctx->project_name, dep.name); - make_dep dependency = - make_dep{FetchContent_Declare, FetchContent_MakeAvailable, - target_link_libraries}; - dependencies.push_back(dependency); - } - std::string include_fetch = "include(FetchContent)"; - - std::string include_dir = - std::format("include_directories({})", ctx->include_dir); - std::string add_executable = - std::format("add_executable({} ", ctx->project_name) + "${SOURCES})"; - std::string testing = "ok"; - std::string mode = R"V0G0N( - if(RELEASE EQUAL 1) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wextra -Wpedantic -Wall") - add_definitions(-DRELEASE) - else() - add_definitions(-DDEBUG) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -Wextra -Wpedantic -Wall") - if(TEST_MODE EQUAL 1) - endif() - endif() - )V0G0N"; - std::string set_build_dir = std::format( - "set_target_properties({} PROPERTIES RUNTIME_OUTPUT_DIRECTORY {})", - ctx->project_name, ctx->build_dir); - - std::ofstream file; - std::string file_name = "CMakeLists.txt"; - -#ifdef DEBUG - file_name = "build/CMakeLists.txt"; -#endif - remove((ctx->project_path / file_name).c_str()); - - file.open(ctx->project_path / file_name); - file << cmake_minimum_required << '\n'; - file << project_name << '\n'; - file << cxx_version << '\n'; - file << build_type << '\n'; - file << files << '\n'; - file << add_executable << '\n'; - file << FetchContent << '\n'; - for (make_dep &dep : dependencies) { - file << dep.fetch_declare << '\n'; - file << dep.fetch_make_available << '\n'; - file << dep.target_link_libraries << '\n'; - } - file << mode << '\n'; - file << include_dir << '\n'; - file << source_dir << '\n'; - file << build_dir << '\n'; - file << set_build_dir << '\n'; - return false; - } } // namespace Command diff --git a/src/Command/CommandInit.cpp b/src/Command/CommandInit.cpp index 8aa283d..b1be1fa 100644 --- a/src/Command/CommandInit.cpp +++ b/src/Command/CommandInit.cpp @@ -7,6 +7,7 @@ #include #include #include +#include "../Generators/Generators.hpp" namespace Command { @@ -120,7 +121,7 @@ bool createCppProject(std::shared_ptr ctx) { createToml(ctx); loadPackageToml(ctx); - Command::createCMakelists(ctx); + Generators::CMakeList::create(ctx); createHelloWorldCpp(ctx); return true; } @@ -128,7 +129,7 @@ bool createCppProject(std::shared_ptr ctx) { bool createCProject(std::shared_ptr ctx) { createToml(ctx); loadPackageToml(ctx); - Command::createCMakelists(ctx); + Generators::CMakeList::create(ctx); createHelloWorldC(ctx); return false; } @@ -207,7 +208,7 @@ bool init(std::shared_ptr ctx, cxxopts::ParseResult &args) { } loadPackageToml(ctx); - createCMakelists(ctx); + Generators::CMakeList::create(ctx); createHelloWorldCpp(ctx); } else { while (true) { diff --git a/src/Generators/CMakeGenerator.cpp b/src/Generators/CMakeGenerator.cpp new file mode 100644 index 0000000..2b7e2c4 --- /dev/null +++ b/src/Generators/CMakeGenerator.cpp @@ -0,0 +1,141 @@ +#include "Generators.hpp" +#include +#include +#include +#include "../Command/Command.hpp" + + +namespace Generators::CMakeList{ + /* + * Generate the dependencies for the project + * @param ctx: the context of the command + * @return a vector of dependencies that will be later combined to build the cmake file + */ + void generateDeps(std::shared_ptr ctx, std::shared_ptr cmake_context){ + for (Command::dependency &dep : ctx->dependencies) { + std::string FetchContent_Declare = + std::format( + R"V0G0N( + FetchContent_Declare( + {} + GIT_REPOSITORY "{}" + GIT_TAG "{}" + ) + )V0G0N", + dep.name, dep.url, dep.version); + + std::string FetchContent_MakeAvailable = + std::format("FetchContent_MakeAvailable({})", dep.name); + std::string target_link_libraries = std::format( + "target_link_libraries({} {})", ctx->project_name, dep.name); + Dep dependency = + Dep{FetchContent_Declare, FetchContent_MakeAvailable, + target_link_libraries}; + cmake_context->dependencies.push_back(dependency); + } + } + bool create(std::shared_ptr ctx) { + std::shared_ptr cmake_context = std::make_shared(); + cmake_context->cmake_minimum_required = + std::format("cmake_minimum_required(VERSION {})", ctx->cmake_version); + cmake_context->project_name = + std::format("project ({} VERSION {} LANGUAGES CXX)", + ctx->project_name, ctx->project_version); + cmake_context->build_type = R"V0G0N( + if (CMAKE_BUILD_TYPE STREQUAL "Release") + message("Release mode") + set(RELEASE 1) + elseif(CMAKE_BUILD_TYPE STREQUAL "Debug") + message("Debug mode") + set(RELEASE 0) + elseif(CMAKE_BUILD_TYPE STREQUAL "Test") + message("Test mode") + set(RELEASE 0) + set(TEST_MODE 1) + else() + message("Default mode") + set(RELEASE 0) + endif() + )V0G0N"; + cmake_context->cxx_version = + std::format("set(CMAKE_CXX_STANDARD {})", ctx->lang_version); + cmake_context->compiler = + std::format("set(CMAKE_CXX_COMPILER {})\n", ctx->compiler); + cmake_context->source_dir = std::format("set(SOURCE_DIR {})", ctx->src_dir); + cmake_context->build_dir = std::format("set(BUILD_DIR {})", ctx->build_dir); + cmake_context->fetch_content = "include(FetchContent)"; + + cmake_context->files = R"V0G0N( + file(GLOB_RECURSE SOURCES RELATIVE ${CMAKE_SOURCE_DIR} + "include/**.h" + "include/**.hpp" + "src/**.cpp" + "src/**.c" + ) + )V0G0N"; + + cmake_context->include_fetch = "include(FetchContent)"; + + generateDeps(ctx, cmake_context); + + cmake_context->include_dir = + std::format("include_directories({})", ctx->include_dir); + cmake_context->add_executable = + std::format("add_executable({} ", ctx->project_name) + "${SOURCES})"; + cmake_context->testing = "ok"; + cmake_context->mode = R"V0G0N( + if(RELEASE EQUAL 1) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wextra -Wpedantic -Wall") + add_definitions(-DRELEASE) + else() + add_definitions(-DDEBUG) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -O0 -Wextra -Wpedantic -Wall") + if(TEST_MODE EQUAL 1) + endif() + endif() + )V0G0N"; + cmake_context->set_build_dir = std::format( + "set_target_properties({} PROPERTIES RUNTIME_OUTPUT_DIRECTORY {})", + ctx->project_name, ctx->build_dir); + + std::ofstream file; + std::string file_name = "CMakeLists.txt"; + +#ifdef DEBUG + file_name = "build/CMakeLists.txt"; +#endif + try{ + remove((ctx->project_path / file_name).c_str()); + }catch(...){ + std::cout << "Error while removing file: " << file_name << std::endl; + return false; + } + + try{ + file.open(ctx->project_path / file_name); + }catch(...){ + std::cout << "Error while opening file: " << file_name << std::endl; + return false; + } + file << cmake_context->cmake_minimum_required << '\n'; + file << cmake_context->project_name << '\n'; + file << cmake_context->cxx_version << '\n'; + file << cmake_context->build_type << '\n'; + file << cmake_context->files << '\n'; + file << cmake_context->add_executable << '\n'; + file << cmake_context->fetch_content << '\n'; + for (Dep &dep : cmake_context->dependencies) { + file << dep.fetch_declare << '\n'; + file << dep.fetch_make_available << '\n'; + file << dep.target_link_libraries << '\n'; + } + file << cmake_context->mode << '\n'; + file << cmake_context->include_dir << '\n'; + file << cmake_context->source_dir << '\n'; + file << cmake_context->build_dir << '\n'; + file << cmake_context->set_build_dir << '\n'; + return true; + } + + +} diff --git a/src/Generators/Generators.hpp b/src/Generators/Generators.hpp new file mode 100644 index 0000000..091a157 --- /dev/null +++ b/src/Generators/Generators.hpp @@ -0,0 +1,43 @@ +#include +#include +#include +#include "../Command/Command.hpp" + + +namespace Generators::CMakeList{ + typedef struct Dep { + std::string fetch_declare; + std::string fetch_make_available; + std::string target_link_libraries; + } Dep; + typedef struct CMakeContext { + std::string cmake_minimum_required; + std::string project_name; + std::string project_version; + std::string build_type; + std::string cxx_version; + std::string compiler; + std::string source_dir; + std::string build_dir; + std::string fetch_content; + std::string include_fetch; + std::string files; + std::string include_dir; + std::string add_executable; + std::string testing; + std::string mode; + std::string set_build_dir; + std::vector dependencies; + } CMakeContext; + /* + * Generate the dependencies for the project + * @param ctx: the context of the command + * @return a vector of dependencies that will be later combined to build the cmake file + */ + void generateDeps(std::shared_ptr ctx, std::shared_ptr cmake_context); + + + + + bool create(std::shared_ptr ctx); +}