Added include diagram JSON model generator
This commit is contained in:
@@ -205,16 +205,36 @@ void generate_diagram(const std::string &od, const std::string &name,
|
||||
dynamic_cast<diagram_config &>(*diagram), translation_units,
|
||||
verbose);
|
||||
|
||||
auto path = std::filesystem::path{od} / fmt::format("{}.puml", name);
|
||||
std::ofstream ofs;
|
||||
ofs.open(path, std::ofstream::out | std::ofstream::trunc);
|
||||
for (const auto generator_type : generators) {
|
||||
if (generator_type == generator_type_t::plantuml) {
|
||||
auto path =
|
||||
std::filesystem::path{od} / fmt::format("{}.puml", name);
|
||||
std::ofstream ofs;
|
||||
ofs.open(path, std::ofstream::out | std::ofstream::trunc);
|
||||
|
||||
ofs << clanguml::include_diagram::generators::plantuml::generator(
|
||||
dynamic_cast<diagram_config &>(*diagram), *model);
|
||||
ofs << clanguml::include_diagram::generators::plantuml::
|
||||
generator(
|
||||
dynamic_cast<diagram_config &>(*diagram), *model);
|
||||
|
||||
ofs.close();
|
||||
ofs.close();
|
||||
|
||||
LOG_INFO("Written {} diagram to {}", name, path.string());
|
||||
LOG_INFO("Written {} diagram to {}", name, path.string());
|
||||
}
|
||||
else if (generator_type == generator_type_t::json) {
|
||||
auto path =
|
||||
std::filesystem::path{od} / fmt::format("{}.json", name);
|
||||
std::ofstream ofs;
|
||||
|
||||
ofs.open(path, std::ofstream::out | std::ofstream::trunc);
|
||||
ofs << clanguml::include_diagram::generators::json::generator(
|
||||
dynamic_cast<clanguml::config::include_diagram &>(*diagram),
|
||||
*model);
|
||||
|
||||
ofs.close();
|
||||
|
||||
LOG_INFO("Written {} diagram to {}", name, path.string());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include "common/generators/generators.h"
|
||||
#include "common/model/diagram_filter.h"
|
||||
#include "config/config.h"
|
||||
#include "include_diagram/generators/json/include_diagram_generator.h"
|
||||
#include "include_diagram/generators/plantuml/include_diagram_generator.h"
|
||||
#include "package_diagram/generators/json/package_diagram_generator.h"
|
||||
#include "package_diagram/generators/plantuml/package_diagram_generator.h"
|
||||
|
||||
@@ -17,3 +17,21 @@
|
||||
*/
|
||||
|
||||
#include "source_file.h"
|
||||
|
||||
namespace clanguml::common::model {
|
||||
|
||||
std::string to_string(source_file_t sf)
|
||||
{
|
||||
switch (sf) {
|
||||
case source_file_t::kDirectory:
|
||||
return "directory";
|
||||
case source_file_t::kHeader:
|
||||
return "header";
|
||||
case source_file_t::kImplementation:
|
||||
return "implementation";
|
||||
default:
|
||||
assert(false);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace clanguml::common::model
|
||||
|
||||
@@ -36,6 +36,8 @@ namespace clanguml::common::model {
|
||||
|
||||
enum class source_file_t { kDirectory, kHeader, kImplementation };
|
||||
|
||||
std::string to_string(source_file_t sf);
|
||||
|
||||
struct fs_path_sep {
|
||||
#ifdef _WIN32
|
||||
static constexpr std::string_view value = "\\";
|
||||
|
||||
107
src/include_diagram/generators/json/include_diagram_generator.cc
Normal file
107
src/include_diagram/generators/json/include_diagram_generator.cc
Normal file
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* src/include_diagram/generators/json/include_diagram_generator.cc
|
||||
*
|
||||
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "include_diagram_generator.h"
|
||||
|
||||
#include "util/error.h"
|
||||
|
||||
namespace clanguml::include_diagram::generators::json {
|
||||
|
||||
generator::generator(diagram_config &config, diagram_model &model)
|
||||
: common_generator<diagram_config, diagram_model>{config, model}
|
||||
{
|
||||
}
|
||||
|
||||
void generator::generate_relationships(
|
||||
const source_file &f, nlohmann::json &parent) const
|
||||
{
|
||||
LOG_DBG("Generating relationships for file {}", f.full_name(true));
|
||||
|
||||
namespace json_common = clanguml::common::generators::json;
|
||||
|
||||
if (f.type() == common::model::source_file_t::kDirectory) {
|
||||
util::for_each(f, [this, &parent](const auto &file) {
|
||||
generate_relationships(
|
||||
dynamic_cast<const source_file &>(*file), parent);
|
||||
});
|
||||
}
|
||||
else {
|
||||
util::for_each_if(
|
||||
f.relationships(),
|
||||
[this](const auto &r) { return m_model.should_include(r.type()); },
|
||||
[&f, this](const auto &r) {
|
||||
nlohmann::json rel = r;
|
||||
rel["source"] = std::to_string(f.id());
|
||||
json_["relationships"].push_back(std::move(rel));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void generator::generate(const source_file &f, nlohmann::json &parent) const
|
||||
{
|
||||
nlohmann::json j;
|
||||
j["id"] = std::to_string(f.id());
|
||||
j["name"] = f.name();
|
||||
j["display_name"] = f.full_name(false);
|
||||
|
||||
if (f.type() == common::model::source_file_t::kDirectory) {
|
||||
LOG_DBG("Generating directory {}", f.name());
|
||||
|
||||
j["type"] = "folder";
|
||||
|
||||
util::for_each(f, [this, &j](const auto &file) {
|
||||
generate(dynamic_cast<const source_file &>(*file), j);
|
||||
});
|
||||
|
||||
parent["elements"].push_back(std::move(j));
|
||||
}
|
||||
else {
|
||||
if (m_model.should_include(f)) {
|
||||
LOG_DBG("Generating file {}", f.name());
|
||||
|
||||
j["type"] = "file";
|
||||
j["file_kind"] = to_string(f.type());
|
||||
|
||||
parent["elements"].push_back(std::move(j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void generator::generate(std::ostream &ostr) const
|
||||
{
|
||||
json_["name"] = m_model.name();
|
||||
json_["diagram_type"] = "include";
|
||||
|
||||
json_["elements"] = std::vector<nlohmann::json>{};
|
||||
json_["relationships"] = std::vector<nlohmann::json>{};
|
||||
|
||||
// Generate files and folders
|
||||
util::for_each_if(
|
||||
m_model, [](const auto & /*f*/) { return true; },
|
||||
[this, &ostr](const auto &f) {
|
||||
generate(dynamic_cast<source_file &>(*f), json_);
|
||||
});
|
||||
|
||||
// Process file include relationships
|
||||
util::for_each(m_model, [this](const auto &f) {
|
||||
generate_relationships(dynamic_cast<source_file &>(*f), json_);
|
||||
});
|
||||
|
||||
ostr << json_;
|
||||
}
|
||||
} // namespace clanguml::include_diagram::generators::json
|
||||
@@ -0,0 +1,69 @@
|
||||
/**
|
||||
* src/include_diagram/generators/json/include_diagram_generator.h
|
||||
*
|
||||
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "common/generators/json/generator.h"
|
||||
#include "common/model/package.h"
|
||||
#include "common/model/relationship.h"
|
||||
#include "common/model/source_file.h"
|
||||
#include "config/config.h"
|
||||
#include "include_diagram/model/diagram.h"
|
||||
#include "include_diagram/visitor/translation_unit_visitor.h"
|
||||
#include "util/util.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
namespace clanguml {
|
||||
namespace include_diagram {
|
||||
namespace generators {
|
||||
namespace json {
|
||||
|
||||
using diagram_config = clanguml::config::include_diagram;
|
||||
using diagram_model = clanguml::include_diagram::model::diagram;
|
||||
|
||||
template <typename C, typename D>
|
||||
using common_generator = clanguml::common::generators::json::generator<C, D>;
|
||||
|
||||
using clanguml::common::model::access_t;
|
||||
using clanguml::common::model::package;
|
||||
using clanguml::common::model::relationship_t;
|
||||
using clanguml::common::model::source_file;
|
||||
using namespace clanguml::util;
|
||||
|
||||
class generator : public common_generator<diagram_config, diagram_model> {
|
||||
public:
|
||||
generator(diagram_config &config, diagram_model &model);
|
||||
|
||||
void generate_relationships(
|
||||
const source_file &p, nlohmann::json &parent) const;
|
||||
|
||||
void generate(const source_file &e, nlohmann::json &parent) const;
|
||||
|
||||
void generate(std::ostream &ostr) const override;
|
||||
|
||||
private:
|
||||
mutable nlohmann::json json_;
|
||||
};
|
||||
|
||||
} // namespace json
|
||||
} // namespace generators
|
||||
} // namespace include_diagram
|
||||
} // namespace clanguml
|
||||
@@ -37,7 +37,7 @@ void generator::generate_relationships(
|
||||
for (const auto &r : p.relationships()) {
|
||||
nlohmann::json rel = r;
|
||||
rel["source"] = std::to_string(p.id());
|
||||
parent["relationships"].push_back(std::move(rel));
|
||||
json_["relationships"].push_back(std::move(rel));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user