Skip to content

Commit

Permalink
maybe better solution
Browse files Browse the repository at this point in the history
  • Loading branch information
lsproule committed Oct 30, 2023
1 parent 6adf9fa commit e8faa9b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 171 deletions.
141 changes: 45 additions & 96 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,104 +1,53 @@
cmake_minimum_required(VERSION 3.10)

project (
cmaker
VERSION 0.0.1
LANGUAGES CXX
)

project ( VERSION 0.0.1 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)

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()


file(GLOB_RECURSE SOURCES RELATIVE ${CMAKE_SOURCE_DIR}
"include/**.h"
"include/**.hpp"
"src/**.cpp"
"src/**.c"
)

add_executable( ${SOURCES})
include(FetchContent)

FetchContent_Declare(
cxxopts
GIT_REPOSITORY "https://github.com/jarro2783/cxxopts.git"
GIT_TAG "origin/master"
)

FetchContent_Declare(
nlohmann_json
GIT_REPOSITORY "https://github.com/nlohmann/json.git"
GIT_TAG "origin/master"
)
FetchContent_Declare(
tomlplusplus
GIT_REPOSITORY "https://github.com/marzer/tomlplusplus.git"
GIT_TAG "v3.4.0"
)

FetchContent_Declare(
cpr
GIT_REPOSITORY https://github.com/libcpr/cpr.git
GIT_TAG master
) # The commit hash for 1.10.x. Replace with the latest from: https://github.com/libcpr/cpr/releases
FetchContent_MakeAvailable(cpr)
FetchContent_MakeAvailable(nlohmann_json)
FetchContent_Declare(
cxxopts
GIT_REPOSITORY "https://github.com/jarro2783/cxxopts"
GIT_TAG "master"
)

FetchContent_MakeAvailable(cxxopts)
FetchContent_MakeAvailable(tomlplusplus)




file(GLOB_RECURSE SOURCES RELATIVE ${CMAKE_SOURCE_DIR}
"src/**.cpp"
"src/**.c"
)

include_directories(${CMAKE_SOURCE_DIR}/include)
set(HEADER_DIR ${CMAKE_SOURCE_DIR}/include)

message("Sources: ${SOURCES}")


if(NOT DEFINED RELEASE)
set(RELEASE 0)
endif()

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()

add_executable(cmaker ${SOURCES}) #test mode

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)
FetchContent_Declare(
Catch2
GIT_REPOSITORY "https://github.com/catchorg/Catch2.git"
GIT_TAG "origin/devel"
)

FetchContent_MakeAvailable(Catch2)
add_definitions(-DTEST) #test mode
target_link_libraries(cmaker Catch2::Catch2) #test mode fuck you cmake
include(CTest)
include(Catch)

endif()
endif()

target_link_libraries(${PROJECT_NAME} cxxopts)
target_link_libraries(${PROJECT_NAME} cpr::cpr)
target_link_libraries(${PROJECT_NAME} nlohmann_json)
target_link_libraries(${PROJECT_NAME} tomlplusplus::tomlplusplus)
target_link_libraries( cxxopts)

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()

include_directories(include)
set(SOURCE_DIR src)
set(BUILD_DIR build)
set_target_properties(cmaker PROPERTIES RUNTIME_OUTPUT_DIRECTORY build)
install(TARGETS cmaker DESTINATION bin)
set_target_properties( PROPERTIES RUNTIME_OUTPUT_DIRECTORY build)
125 changes: 55 additions & 70 deletions src/Command/CommandAdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@
namespace Command {
using nlohmann::json;
bool add(std::shared_ptr<Context> ctx, cxxopts::ParseResult &args) {
if (args.count("subcommand") != 0) {
if (!(args.count("subcommand") > 0)) {
std::cout << "Usage add:" << ENDL
"\tdep: adds a dependency" << ENDL
"\tflag: adds a flag" << ENDL
"\tlib: adds a library" << std::endl;
return false;
}
std::string subcommand = args["subcommand"].as<std::string>();
if (subcommand == "dep") {
addDependency(ctx, args);
}
if (subcommand == "flag") {
addFlag(ctx, args);
}
}else{
std::cout << R"EOF(
Usage add:
dep: adds a dependency
flag: adds a flag
lib: adds a library
)EOF" << std::endl;
}

return true;
}

bool addFlag(std::shared_ptr<Context> ctx, cxxopts::ParseResult &args) {
if (args.count("subcommand") == 0) {
for (auto flag : args["subcommand"].as<std::vector<std::string>>()) {
Expand All @@ -39,10 +39,6 @@ Usage add:
return true;
}





typedef struct packageResult {
std::string name;
std::string url;
Expand Down Expand Up @@ -134,77 +130,66 @@ Usage add:

bool addDependency(std::shared_ptr<Context> ctx, cxxopts::ParseResult &args) {
if (args.count("args") == 0) {
std::cout << R"EOF(
Usage add dep:
[args]: the dependencies to project
cmake add dep [args]
)EOF" << std::endl;
std::cout <<
"Usage add dep:" << ENDL
"\t[args]: the dependencies to project" << ENDL
"\tcmake add dep [args] " << std::endl;
return false;
}


for(auto arg: args.arguments()){
std::cout << arg.key() << std::endl;
}
if (args.count("subcommand") > 2) {
std::cout << "Usage: add dep [options] name url branch" << std::endl;
std::vector<packageResult> searchResults = searchPackage(args["args"].as<std::vector<std::string>>()[0]);
if(searchResults.size() == 0){
std::cout << "No results found" << std::endl;
return false;
}
else{
std::vector<packageResult> searchResults = searchPackage(args["args"].as<std::vector<std::string>>()[0]);
if(searchResults.size() == 0){
std::cout << "No results found" << std::endl;
return false;
}
std::cout << "Select a package to install: ";
std::string input;
std::cin >> input;
int index = std::stoi(input);
std::cout << "Select a package to install: ";
std::string input;
std::cin >> input;
int index = std::stoi(input);

std::cout << "Installing " << searchResults[index].name << std::endl;
std::cout << "Installing " << searchResults[index].name << std::endl;

json versionJson = Utils::fetchJson("https://raw.githubusercontent.com/cmaker-dev/index/main/index/" + searchResults[index].name + "/info.json");
std::vector<std::string> versionTokens = versionJson["versions"];
std::string version = "";
for(size_t i = 0; i < versionTokens.size(); i++){
std::cout << "[" << i << "]" << versionTokens[i] << std::endl;
if (versionTokens[i] == "master" || versionTokens[i] == "main" || versionTokens[i] == "stable"){
version = versionTokens[i];
json versionJson = Utils::fetchJson("https://raw.githubusercontent.com/cmaker-dev/index/main/index/" + searchResults[index].name + "/info.json");

std::vector<std::string> versionTokens = versionJson["versions"];
std::string version = "";
for(size_t i = 0; i < versionTokens.size(); i++){
std::cout << "[" << i << "]" << versionTokens[i] << std::endl;
if (versionTokens[i] == "master" || versionTokens[i] == "main" || versionTokens[i] == "stable"){
version = versionTokens[i];

}
}
}


std::cout << "Select a version to install [" + version + "] : ";
std::string versionInput;
std::cin >> versionInput;

int versionIndex = std::stoi(input);
if (!(versionInput == "")){
version = versionTokens[versionIndex];
}

if(checkForOverlappingDependencies(ctx, searchResults[index].name)){
std::cout << "Package already installed" << std::endl;
return false;
}
std::cout << "Select a version to install [" + version + "] : ";
std::string versionInput;
std::cin >> versionInput;

std::cout << "Adding dependency to config.toml" << std::endl;
ctx->dependencies.push_back({
int versionIndex = std::stoi(input);
if (!(versionInput == "")){
version = versionTokens[versionIndex];
}

if(checkForOverlappingDependencies(ctx, searchResults[index].name)){
std::cout << "Package already installed" << std::endl;
return false;
}

.name = searchResults[index].name,
.url = searchResults[index].url,
.version = version,
//TODO: Change target link to be the actual link
.target_link = searchResults[index].name
std::cout << "Adding dependency to config.toml" << std::endl;
ctx->dependencies.push_back({

});
std::cout << "Writing config.toml" << std::endl;
Generators::ConfigToml::writeConfig(ctx);
Generators::CMakeList::create(ctx);
.name = searchResults[index].name,
.url = searchResults[index].url,
.version = version,
//TODO: Change target link to be the actual link
.target_link = searchResults[index].name

return true;
}
});
std::cout << "Writing config.toml" << std::endl;
Generators::ConfigToml::writeConfig(ctx);
Generators::CMakeList::create(ctx);

return true;
}
} // namespace Command
1 change: 0 additions & 1 deletion src/Command/CommandGeneral.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ namespace Command {
std::cout << "Loading config.toml from " << ctx->project_path / file_name
<< std::endl;
#endif

auto data = toml::parse_file((ctx->project_path / file_name).string());
ctx->project_name = data["project"]["project_name"].value_or("");
for (auto &author : *data["project"]["authors"].as_array()) {
Expand Down
3 changes: 2 additions & 1 deletion src/Command/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ namespace Command {
("y,skip-init", "skip init", cxxopts::value<bool>()->default_value("false"))
("v,verbose", "Verbose output", cxxopts::value<bool>()->default_value("false"))
("args", "Arguments to pass to subcommand", cxxopts::value<std::vector<std::string>>());
options.help();
try {
return options.parse(argc, argv);
return options.parse(argc, argv);
} catch (std::exception &e) {
std::cout << "failed to parse options" << ENDL;
exit(1);
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ int main(int argc, char **argv) {
Command::ftp(ctx);
}
else if (command == "add"){
ParseResult options = Command::addOptions(argc, argv);
for (auto &i : options.arguments()){
cxxopts::ParseResult args = Command::addOptions(argc, argv);
for (auto &i : args.arguments()){
std::cout << i.key() << " " << i.value() << ENDL;
}
Command::loadPackageToml(ctx);
Command::add(ctx, options);
Command::add(ctx, args);

}
else if (command == "remove"){
Expand Down

0 comments on commit e8faa9b

Please sign in to comment.