cli: Ajoute une Factory

This commit is contained in:
Colin FRIZOT
2023-01-17 11:06:51 +01:00
parent 455a64d013
commit c887d1be7d
8 changed files with 47 additions and 12 deletions

View File

@@ -30,4 +30,8 @@ namespace make {
<< " return 0;\n" << " return 0;\n"
<< "}\n"; << "}\n";
} }
std::string_view CCompilatorStrategy::getMainName(){
return "main.c";
}
} }

View File

@@ -8,5 +8,6 @@ namespace make {
std::ostream &writeStart(std::ostream &out) override; std::ostream &writeStart(std::ostream &out) override;
std::ostream &writeEnd(std::ostream &out) override; std::ostream &writeEnd(std::ostream &out) override;
std::ostream &writeMain(std::ostream &out) override; std::ostream &writeMain(std::ostream &out) override;
std::string_view getMainName() override;
}; };
} }

View File

@@ -8,6 +8,7 @@ namespace make {
virtual std::ostream &writeStart(std::ostream &out) = 0; virtual std::ostream &writeStart(std::ostream &out) = 0;
virtual std::ostream &writeEnd(std::ostream &out) = 0; virtual std::ostream &writeEnd(std::ostream &out) = 0;
virtual std::ostream &writeMain(std::ostream &out) = 0; virtual std::ostream &writeMain(std::ostream &out) = 0;
virtual std::string_view getMainName() = 0;
virtual ~CompilatorStrategy() = default; virtual ~CompilatorStrategy() = default;
}; };
} }

View File

@@ -30,4 +30,8 @@ namespace make {
<< " return 0;\n" << " return 0;\n"
<< "}\n"; << "}\n";
} }
std::string_view CppCompilatorStrategy::getMainName(){
return "main.cpp";
}
} }

View File

@@ -8,5 +8,7 @@ namespace make {
std::ostream &writeStart(std::ostream &out) override; std::ostream &writeStart(std::ostream &out) override;
std::ostream &writeEnd(std::ostream &out) override; std::ostream &writeEnd(std::ostream &out) override;
std::ostream &writeMain(std::ostream &out) override; std::ostream &writeMain(std::ostream &out) override;
std::string_view getMainName() override;
}; };
} }

View File

@@ -0,0 +1,18 @@
#include "SourceFactory.h"
#include "../make/CppCompilatorStrategy.h"
#include "../make/CCompilatorStrategy.h"
namespace make {
std::unique_ptr<make::CompilatorStrategy> SourceFactory::fabrique(ProjectKind kind){
switch (kind) {
case make::ProjectKind::C:
return std::make_unique<make::CCompilatorStrategy>();
break;
case make::ProjectKind::Cpp:
return std::make_unique<make::CppCompilatorStrategy>();
break;
default:
return nullptr;
}
}
}

View File

@@ -0,0 +1,13 @@
#pragma once
#include "../make/CompilatorStrategy.h"
#include "ProjectKind.h"
#include <memory>
namespace make {
class SourceFactory {
public:
static std::unique_ptr<make::CompilatorStrategy> fabrique(ProjectKind kind);
};
}

View File

@@ -2,6 +2,7 @@
#include "../io/oki.h" #include "../io/oki.h"
#include "../make/CppCompilatorStrategy.h" #include "../make/CppCompilatorStrategy.h"
#include "../make/CCompilatorStrategy.h" #include "../make/CCompilatorStrategy.h"
#include "../make/SourceFactory.h"
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
@@ -32,22 +33,13 @@ namespace op {
manifest << "\n[dependencies]\n"; manifest << "\n[dependencies]\n";
std::unique_ptr<make::CompilatorStrategy> strategy; std::unique_ptr<make::CompilatorStrategy> strategy;
switch (options.kind) { strategy = make::SourceFactory::fabrique(options.kind);
case make::ProjectKind::C:
strategy = std::make_unique<make::CCompilatorStrategy>();
break;
case make::ProjectKind::Cpp:
strategy = std::make_unique<make::CppCompilatorStrategy>();
break;
default:
throw std::range_error("Unknown project kind");
}
fs::path src{workingDirectory / "src"}; fs::path src{workingDirectory / "src"};
fs::create_directories(src); fs::create_directories(src);
if(!options.lib) { if(!options.lib) {
if(!fs::exists(src / "main.c")){ if(!fs::exists(src / strategy->getMainName())){
std::ofstream main{src / "main.c"}; std::ofstream main{src / strategy->getMainName()};
if (!main) { if (!main) {
std::cerr << "Cannot write main source file: " << strerror(errno); std::cerr << "Cannot write main source file: " << strerror(errno);
return 1; return 1;