Skip to content

Commit

Permalink
REFACTORED MOST OF THE PROJECT
Browse files Browse the repository at this point in the history
  • Loading branch information
DeaSTL committed Oct 25, 2023
1 parent 6fb8b72 commit a546aaf
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 120 deletions.
3 changes: 2 additions & 1 deletion src/Command/CommandAdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <iostream>
#include <string>
#include <cxxopts.hpp>
#include "../Generators/Generators.hpp"

namespace Command {

Expand Down Expand Up @@ -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;
}
}
116 changes: 0 additions & 116 deletions src/Command/CommandGeneral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,120 +98,4 @@ namespace Command {
file.close();
return true;
}
bool createCMakelists(std::shared_ptr<Context> 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<make_dep> 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
7 changes: 4 additions & 3 deletions src/Command/CommandInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <fstream>
#include <iostream>
#include <string>
#include "../Generators/Generators.hpp"

namespace Command {

Expand Down Expand Up @@ -120,15 +121,15 @@ bool createCppProject(std::shared_ptr<Context> ctx) {

createToml(ctx);
loadPackageToml(ctx);
Command::createCMakelists(ctx);
Generators::CMakeList::create(ctx);
createHelloWorldCpp(ctx);
return true;
}

bool createCProject(std::shared_ptr<Context> ctx) {
createToml(ctx);
loadPackageToml(ctx);
Command::createCMakelists(ctx);
Generators::CMakeList::create(ctx);
createHelloWorldC(ctx);
return false;
}
Expand Down Expand Up @@ -207,7 +208,7 @@ bool init(std::shared_ptr<Context> ctx, cxxopts::ParseResult &args) {
}

loadPackageToml(ctx);
createCMakelists(ctx);
Generators::CMakeList::create(ctx);
createHelloWorldCpp(ctx);
} else {
while (true) {
Expand Down
141 changes: 141 additions & 0 deletions src/Generators/CMakeGenerator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include "Generators.hpp"
#include <memory>
#include <vector>
#include <format>
#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<Command::Context> ctx, std::shared_ptr<CMakeContext> 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<Command::Context> ctx) {
std::shared_ptr<CMakeContext> cmake_context = std::make_shared<CMakeContext>();
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;
}


}
43 changes: 43 additions & 0 deletions src/Generators/Generators.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <memory>
#include <vector>
#include <format>
#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<Dep> 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<Command::Context> ctx, std::shared_ptr<CMakeContext> cmake_context);




bool create(std::shared_ptr<Command::Context> ctx);
}

0 comments on commit a546aaf

Please sign in to comment.