Added default diagram generation error for empty diagrams (#246)

This commit is contained in:
Bartek Kryza
2024-03-04 19:55:55 +01:00
parent cb44c3ded4
commit baef768f6c
27 changed files with 315 additions and 10 deletions

View File

@@ -249,6 +249,11 @@ void diagram::remove_redundant_dependencies()
}
}
bool diagram::is_empty() const
{
return element_view<class_>::is_empty() &&
element_view<enum_>::is_empty() && element_view<concept_>::is_empty();
}
} // namespace clanguml::class_diagram::model
namespace clanguml::common::model {

View File

@@ -249,6 +249,13 @@ public:
*/
inja::json context() const override;
/**
* @brief Check whether the diagram is empty
*
* @return True, if diagram is empty
*/
bool is_empty() const override;
private:
template <typename ElementT>
bool add_with_namespace_path(std::unique_ptr<ElementT> &&e);

View File

@@ -107,6 +107,8 @@ cli_flow_t cli_handler::parse(int argc, const char **argv)
"Query the specific compiler driver to extract system paths and add to "
"compile commands (e.g. arm-none-eabi-g++)");
#endif
app.add_flag("--allow-empty-diagrams", allow_empty_diagrams,
"Do not raise an error when generated diagram model is empty");
app.add_option(
"--add-class-diagram", add_class_diagram, "Add class diagram config");
app.add_option("--add-sequence-diagram", add_sequence_diagram,
@@ -302,6 +304,10 @@ cli_flow_t cli_handler::handle_post_config_options()
LOG_INFO("Loaded clang-uml config from {}", config_path);
if (allow_empty_diagrams) {
config.allow_empty_diagrams.set(true);
}
//
// Override selected config options from command line
//

View File

@@ -165,6 +165,7 @@ public:
bool list_diagrams{false};
bool quiet{false};
bool initialize{false};
bool allow_empty_diagrams{false};
std::optional<std::vector<std::string>> add_compile_flag;
std::optional<std::vector<std::string>> remove_compile_flag;
#if !defined(_WIN32)

View File

@@ -95,11 +95,17 @@ void generate_diagram_select_generator(const std::string &od,
using diagram_generator =
typename diagram_generator_t<DiagramConfig, GeneratorTag>::type;
std::stringstream buffer;
buffer << diagram_generator(
dynamic_cast<DiagramConfig &>(*diagram), *model);
// Only open the file after the diagram has been generated successfully
// in order not to overwrite previous diagram in case of failure
auto path = std::filesystem::path{od} /
fmt::format("{}.{}", name, GeneratorTag::extension);
std::ofstream ofs;
ofs.open(path, std::ofstream::out | std::ofstream::trunc);
ofs << diagram_generator(dynamic_cast<DiagramConfig &>(*diagram), *model);
ofs << buffer.str();
ofs.close();
@@ -258,11 +264,12 @@ void generate_diagrams(const std::vector<std::string> &diagram_names,
if (indicator)
indicator->complete(name);
}
catch (std::exception &e) {
catch (const std::exception &e) {
if (indicator)
indicator->fail(name);
LOG_ERROR(e.what());
LOG_ERROR(
"ERROR: Failed to generate diagram {}: {}", name, e.what());
}
};

View File

@@ -107,11 +107,19 @@ std::ostream &operator<<(
template <typename C, typename D>
void generator<C, D>::generate(std::ostream &ostr) const
{
const auto &config = generators::generator<C, D>::config();
const auto &model = generators::generator<C, D>::model();
if (!config.allow_empty_diagrams() && model.is_empty()) {
throw clanguml::error::empty_diagram_error{
"Diagram configuration resulted in empty diagram."};
}
nlohmann::json j;
j["name"] = generators::generator<C, D>::model().name();
j["diagram_type"] = to_string(generators::generator<C, D>::model().type());
if (generators::generator<C, D>::config().title) {
j["title"] = generators::generator<C, D>::config().title();
j["name"] = model.name();
j["diagram_type"] = to_string(model.type());
if (config.title) {
j["title"] = config.title();
}
generate_diagram(j);

View File

@@ -250,6 +250,13 @@ template <typename C, typename D>
void generator<C, D>::generate(std::ostream &ostr) const
{
const auto &config = generators::generator<C, D>::config();
const auto &model = generators::generator<C, D>::model();
if (!config.allow_empty_diagrams() && model.is_empty() &&
config.mermaid().before.empty() && config.mermaid().after.empty()) {
throw clanguml::error::empty_diagram_error{
"Diagram configuration resulted in empty diagram."};
}
update_context();

View File

@@ -21,6 +21,7 @@
#include "common/model/diagram_filter.h"
#include "common/model/relationship.h"
#include "config/config.h"
#include "error.h"
#include "util/error.h"
#include "util/util.h"
#include "version.h"
@@ -265,9 +266,16 @@ template <typename C, typename D>
void generator<C, D>::generate(std::ostream &ostr) const
{
const auto &config = generators::generator<C, D>::config();
const auto &model = generators::generator<C, D>::model();
update_context();
if (!config.allow_empty_diagrams() && model.is_empty() &&
config.puml().before.empty() && config.puml().after.empty()) {
throw clanguml::error::empty_diagram_error{
"Diagram configuration resulted in empty diagram."};
}
ostr << "@startuml" << '\n';
generate_title(ostr);

View File

@@ -164,6 +164,13 @@ public:
*/
virtual inja::json context() const = 0;
/**
* @brief Check whether the diagram is empty
*
* @return True, if diagram is empty
*/
virtual bool is_empty() const = 0;
private:
std::string name_;
std::unique_ptr<diagram_filter> filter_;

View File

@@ -69,6 +69,13 @@ public:
return {};
}
/**
* @brief Check whether the element view is empty
*
* @return True, if the view does not contain any elements
*/
bool is_empty() const { return elements_.empty(); }
private:
reference_vector<T> elements_;
};

View File

@@ -237,6 +237,7 @@ void inheritable_diagram_options::inherit(
parent.generate_condition_statements);
debug_mode.override(parent.debug_mode);
generate_metadata.override(parent.generate_metadata);
allow_empty_diagrams.override(parent.allow_empty_diagrams);
type_aliases.override(parent.type_aliases);
}

View File

@@ -577,6 +577,7 @@ struct inheritable_diagram_options {
"message_comment_width", clanguml::util::kDefaultMessageCommentWidth};
option<bool> debug_mode{"debug_mode", false};
option<bool> generate_metadata{"generate_metadata", true};
option<bool> allow_empty_diagrams{"allow_empty_diagrams", false};
protected:
// This is the relative path with respect to the `base_directory`,

View File

@@ -306,6 +306,7 @@ root:
query_driver: !optional string
add_compile_flags: !optional [string]
remove_compile_flags: !optional [string]
allow_empty_diagrams: !optional bool
diagram_templates: !optional diagram_templates_t
diagrams: !required map_t<string;diagram_t>
#

View File

@@ -820,6 +820,7 @@ template <> struct convert<config> {
get_option(node, rhs.using_module);
get_option(node, rhs.output_directory);
get_option(node, rhs.query_driver);
get_option(node, rhs.allow_empty_diagrams);
get_option(node, rhs.compilation_database_dir);
get_option(node, rhs.add_compile_flags);
get_option(node, rhs.remove_compile_flags);

View File

@@ -151,6 +151,9 @@ inja::json diagram::context() const
return ctx;
}
bool diagram::is_empty() const { return element_view<source_file>::is_empty(); }
} // namespace clanguml::include_diagram::model
namespace clanguml::common::model {

View File

@@ -131,6 +131,13 @@ public:
const common::model::namespace_ &ns) const override;
inja::json context() const override;
/**
* @brief Check whether the diagram is empty
*
* @return True, if diagram is empty
*/
bool is_empty() const override;
};
template <typename ElementT>

View File

@@ -73,6 +73,8 @@ inja::json diagram::context() const
return ctx;
}
bool diagram::is_empty() const { return element_view<package>::is_empty(); }
} // namespace clanguml::package_diagram::model
namespace clanguml::common::model {

View File

@@ -157,6 +157,13 @@ public:
*/
inja::json context() const override;
/**
* @brief Check whether the diagram is empty
*
* @return True, if diagram is empty
*/
bool is_empty() const override;
private:
/**
* @brief Add element using module as diagram path

View File

@@ -399,6 +399,11 @@ std::vector<message_chain_t> diagram::get_all_from_to_message_chains(
return message_chains_unique;
}
bool diagram::is_empty() const
{
return sequences_.empty() || participants_.empty();
}
void diagram::print() const
{
LOG_TRACE(" --- Participants ---");

View File

@@ -280,6 +280,13 @@ public:
*/
void finalize() override;
/**
* @brief Check whether the diagram is empty
*
* @return True, if diagram is empty
*/
bool is_empty() const override;
private:
/**
* This method checks the last messages in sequence (current_messages),

View File

@@ -36,4 +36,8 @@ class compilation_database_error : public std::runtime_error {
using std::runtime_error::runtime_error;
};
class empty_diagram_error : public std::runtime_error {
using std::runtime_error::runtime_error;
};
} // namespace clanguml::error