improved CLI

This commit is contained in:
Jorijn van der Graaf 2024-12-29 00:51:02 +01:00
commit fbc13a9897
5 changed files with 112 additions and 66 deletions

View file

@ -79,6 +79,28 @@ ConfigData CollapseConfig(nlohmann::json& configs,nlohmann::json& config) {
void Project::Build(std::string configuration) {
for(const Configuration& config : configurations) {
if(config.name == configuration){
Build(config, config.outputDir);
return;
}
}
throw std::runtime_error("Configuration: " + configuration + " not found.");
}
void Project::Build(std::string configuration, std::string outputDir) {
for(const Configuration& config : configurations) {
if(config.name == configuration){
Build(config, outputDir);
return;
}
}
throw std::runtime_error("Configuration: " + configuration + " not found.");
}
void Project::Build(Configuration configuration) {
Build(configuration, configuration.outputDir);
}
void Project::Build(Configuration config, std::string outputDir) {
if (!std::filesystem::exists(config.buildDir)) {
std::filesystem::create_directory(config.buildDir);
} else {
@ -86,10 +108,10 @@ void Project::Build(std::string configuration) {
std::filesystem::remove_all(entry.path());
}
}
if (!std::filesystem::exists(config.outputDir)) {
std::filesystem::create_directory(config.outputDir);
if (!std::filesystem::exists(outputDir)) {
std::filesystem::create_directory(outputDir);
} else {
for (const auto& entry : std::filesystem::directory_iterator(config.outputDir)){
for (const auto& entry : std::filesystem::directory_iterator(outputDir)){
std::filesystem::remove_all(entry.path());
}
}
@ -113,11 +135,8 @@ void Project::Build(std::string configuration) {
for(std::thread& thread : threads){
thread.join();
}
system(std::format("clang++ {}-O{} -o {}/{}", files, config.optimizationLevel, config.outputDir, name).c_str());
return;
}
}
throw std::runtime_error("Configuration: " + configuration + " not found.");
system(std::format("clang++ {}-O{} -o {}/{}", files, config.optimizationLevel, outputDir, name).c_str());
}
Project Project::LoadFromJSON(std::string file) {

View file

@ -11,6 +11,9 @@ export namespace Crafter::Build {
std::vector<Configuration> configurations;
Project(std::string name, std::vector<Configuration> configurations);
void Build(std::string configuration);
void Build(std::string configuration, std::string outputDir);
void Build(Configuration configuration);
void Build(Configuration configuration, std::string outputDir);
void SaveToJSON(std::string path);
static Project LoadFromJSON(std::string file);
};

View file

@ -55,22 +55,15 @@ int main() {
std::println("Hello World!");
}
```
Save and close, then run ``crafter-build debug``. Now you can run the ``hello-world`` executable that has appeared in the ``build`` folder
Save and close, then run ``crafter-build debug``. Now you can run the ``hello-world`` executable that has appeared in the ``bin`` folder
## CLI arguments
```bash
crafter-build <configuration>
```
builds the project with the specified configuration, using the defualt project filename ``project.json``
```bash
crafter-build <configuration> <filename>
```
builds the project with the specified configuration, using the specified project filename
``--help`` Displays this help message.
```bash
crafter-build --help
```
displays this help message
``-c`` The name of the configuration to build.
``-p`` The name of the project file.
``-o`` Overrides the output folder.

View file

@ -1,3 +1,4 @@
#include <cstdint>
#include <iostream>
#include <fstream>
#include <print>
@ -6,21 +7,31 @@ import Crafter.Build;
using namespace Crafter::Build;
int main(int argc, char* argv[]) {
if(argc == 1) {
std::println("No configuration provided, use --help for help");
} else if(argc == 2) {
if(std::string(argv[1]) == "--help"){
std::println("<configuration> to build this configuration with the defualt project filename \"project.json\"\n<configuration> <project> to build this configuration with the specified project filename\nhelp displays this help message");
} else {
Project project = Project::LoadFromJSON("./project.json");
project.Build(argv[1]);
std::println("No arguments provided, use --help for help");
}
std::string filename = "project.json";
std::string configuration;
std::string outputDir;
for (std::uint_fast32_t i = 1; i < argc; i++) {
std::string arg = std::string(argv[i]);
if(arg == "--help"){
std::println("--help\tDisplays this help message.\n-c The name of the configuration to build.\n-p\nThe name of the project file.\n-o Overrides the output folder.\n");
return 0;
} else if(argc == 3) {
Project project = Project::LoadFromJSON(argv[2]);
project.Build(argv[1]);
return 0;
} else if(arg == "-c"){
configuration = argv[++i];
} else if(arg == "-o"){
outputDir = argv[++i];
} else if(arg == "-p"){
filename = argv[++i];
} else{
std::println("Too many arguments provided");
std::println("Unkown argument: {}", argv[i]);
return 1;
}
}
Project project = Project::LoadFromJSON(filename);
if(outputDir.empty()){
project.Build(configuration);
} else{
project.Build(configuration, outputDir);
}
}

View file

@ -10,13 +10,33 @@
"output_dir": "./bin"
},
{
"name": "debug",
"name": "executable",
"extends": ["base"],
"type":"executable"
},
{
"name": "lib",
"extends": ["base"],
"type":"lib"
},
{
"name": "debug",
"extends": ["executable"],
"optimization_level": "0"
},
{
"name": "release",
"extends": ["base"],
"extends": ["executable"],
"optimization_level": "3"
},
{
"name": "debug-lib",
"extends": ["lib"],
"optimization_level": "0"
},
{
"name": "release-lib",
"extends": ["lib"],
"optimization_level": "3"
}
]