some more early thoughts about syntax
This commit is contained in:
parent
0c40906eca
commit
60b2220b97
@ -1,10 +1,12 @@
|
||||
set(SOURCES
|
||||
main.cpp
|
||||
project.cpp
|
||||
dependency.cpp
|
||||
)
|
||||
|
||||
set(HEADERS
|
||||
project.h
|
||||
dependency.cpp
|
||||
)
|
||||
|
||||
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})
|
||||
|
25
src/dependency.cpp
Normal file
25
src/dependency.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
#include "dependency.h"
|
||||
|
||||
Dependency::Dependency(
|
||||
const std::string& path,
|
||||
Type type,
|
||||
const std::optional<std::string>& name,
|
||||
const std::optional<std::string>& version
|
||||
):
|
||||
path(path),
|
||||
type(type),
|
||||
name(name),
|
||||
version(version)
|
||||
{}
|
||||
|
||||
Dependency::Type Dependency::getType() const {
|
||||
return type;
|
||||
}
|
||||
|
||||
std::optional<std::string> Dependency::getName() const {
|
||||
return name;
|
||||
}
|
||||
|
||||
std::optional<std::string> Dependency::getVersion() const {
|
||||
return version;
|
||||
}
|
31
src/dependency.h
Normal file
31
src/dependency.h
Normal file
@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <optional>
|
||||
|
||||
class Dependency {
|
||||
public:
|
||||
enum class Type {
|
||||
automatic,
|
||||
simple,
|
||||
mason
|
||||
};
|
||||
|
||||
Dependency(
|
||||
const std::string& path,
|
||||
Type type = Type::automatic,
|
||||
const std::optional<std::string>& name = std::nullopt,
|
||||
const std::optional<std::string>& version = std::nullopt
|
||||
);
|
||||
|
||||
Type getType() const;
|
||||
std::optional<std::string> getName() const;
|
||||
std::optional<std::string> getVersion() const;
|
||||
|
||||
const std::string path;
|
||||
|
||||
private:
|
||||
Type type;
|
||||
std::optional<std::string> name;
|
||||
std::optional<std::string> version;
|
||||
};
|
@ -21,6 +21,7 @@ int main(int argc, char *argv[]) {
|
||||
return -1;
|
||||
} else {
|
||||
std::cout << "successfully parsed project " << root.getName() << std::endl;
|
||||
std::cout << "dependencies count is " << root.dependenciesCount() << std::endl;
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,15 +1,26 @@
|
||||
#include "project.h"
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
#include <string_view>
|
||||
#include <exception>
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
constexpr std::string_view entry("mason.json");
|
||||
|
||||
std::map<std::string, Dependency::Type> types({
|
||||
{"simple", Dependency::Type::simple},
|
||||
{"none", Dependency::Type::simple},
|
||||
{"mason", Dependency::Type::mason},
|
||||
{"auto", Dependency::Type::automatic},
|
||||
{"automatic", Dependency::Type::automatic}
|
||||
});
|
||||
|
||||
Project::Project(const std::filesystem::path& location):
|
||||
location(location),
|
||||
status(Status::unknown),
|
||||
name()
|
||||
name(),
|
||||
logStorage(),
|
||||
dependencies()
|
||||
{}
|
||||
|
||||
bool Project::read() {
|
||||
@ -32,9 +43,17 @@ bool Project::read() {
|
||||
return false;
|
||||
}
|
||||
|
||||
json data;
|
||||
try {
|
||||
nlohmann::json data = nlohmann::json::parse(file);
|
||||
name = data.at("name");
|
||||
data = json::parse(file);
|
||||
} catch (const json::exception& e) {
|
||||
log(e.what());
|
||||
status = Status::error;
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
parse(data);
|
||||
} catch (const std::exception& e) {
|
||||
log(e.what());
|
||||
status = Status::error;
|
||||
@ -44,6 +63,74 @@ bool Project::read() {
|
||||
return true;
|
||||
}
|
||||
|
||||
void Project::parse(const json& data) {
|
||||
const json& nm = data.at("name");
|
||||
if (!nm.is_string())
|
||||
throw 1;
|
||||
|
||||
name = nm;
|
||||
|
||||
const json& dpd = data.at("dependencies");
|
||||
if (!dpd.is_array())
|
||||
throw 1;
|
||||
|
||||
for (const json& dep : dpd) {
|
||||
switch (dep.type()) {
|
||||
case json::value_t::string:
|
||||
createDepencencyFromString(dep);
|
||||
break;
|
||||
case json::value_t::object:
|
||||
createDepencencyFromObject(dep);
|
||||
break;
|
||||
default:
|
||||
throw 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Project::createDepencencyFromString(const std::string& entry) {
|
||||
dependencies.emplace(entry, entry);
|
||||
}
|
||||
|
||||
void Project::createDepencencyFromObject(const nlohmann::json& entry) {
|
||||
std::string url;
|
||||
json::const_iterator itr = entry.find("url");
|
||||
if (itr == entry.end()) {
|
||||
itr = entry.find("path");
|
||||
if (itr == entry.end())
|
||||
throw 1;
|
||||
}
|
||||
if (!itr->is_string())
|
||||
throw 1;
|
||||
|
||||
url = *itr;
|
||||
|
||||
std::optional<std::string> name = std::nullopt;
|
||||
itr = entry.find("name");
|
||||
if (itr != entry.end() && itr->is_string())
|
||||
name = *itr;
|
||||
|
||||
std::optional<std::string> version = std::nullopt;
|
||||
itr = entry.find("version");
|
||||
if (itr != entry.end() && itr->is_string())
|
||||
version = *itr;
|
||||
|
||||
Dependency::Type type = Dependency::Type::automatic;
|
||||
itr = entry.find("type");
|
||||
if (itr != entry.end() && itr->is_string()) {
|
||||
std::map<std::string, Dependency::Type>::const_iterator titr = types.find(*itr);
|
||||
if (titr != types.end())
|
||||
type = titr->second;
|
||||
}
|
||||
|
||||
dependencies.emplace(url, Dependency{url, type, name, version});
|
||||
}
|
||||
|
||||
uint32_t Project::dependenciesCount() const {
|
||||
return dependencies.size();
|
||||
}
|
||||
|
||||
|
||||
void Project::log(const std::string& message) const {
|
||||
logStorage.emplace_back(message);
|
||||
}
|
||||
|
@ -4,6 +4,12 @@
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include <list>
|
||||
#include <map>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include "dependency.h"
|
||||
|
||||
class Project {
|
||||
public:
|
||||
@ -20,14 +26,19 @@ public:
|
||||
Status getStatus() const;
|
||||
Log getLog() const;
|
||||
std::string getName() const;
|
||||
uint32_t dependenciesCount() const;
|
||||
|
||||
private:
|
||||
void log(const std::string& message) const;
|
||||
void parse(const nlohmann::json& json);
|
||||
void createDepencencyFromString(const std::string& entry);
|
||||
void createDepencencyFromObject(const nlohmann::json& entry);
|
||||
|
||||
private:
|
||||
std::filesystem::path location;
|
||||
Status status;
|
||||
std::string name;
|
||||
mutable Log logStorage;
|
||||
std::map<std::string, Dependency> dependencies;
|
||||
|
||||
};
|
||||
|
@ -1,4 +1,12 @@
|
||||
{
|
||||
"name" : "test",
|
||||
"version" : "0.0.1"
|
||||
"version" : "0.0.1",
|
||||
"dependencies" : [
|
||||
"somePath",
|
||||
"https://git.macaw.me/blue/mason.git",
|
||||
{
|
||||
"name" : "aProject",
|
||||
"path" : "https://nowhere.to"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user