Added cli options for cppidx generator
This commit is contained in:
240
src/main.cc
240
src/main.cc
@@ -16,14 +16,10 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "class_diagram/generators/plantuml/class_diagram_generator.h"
|
||||
#include "cli/cli_handler.h"
|
||||
#include "config/config.h"
|
||||
#include "common/generators/generators.h"
|
||||
#include "include_diagram/generators/plantuml/include_diagram_generator.h"
|
||||
#include "package_diagram/generators/plantuml/package_diagram_generator.h"
|
||||
#include "sequence_diagram/generators/plantuml/sequence_diagram_generator.h"
|
||||
#include "util/util.h"
|
||||
#include "version.h"
|
||||
|
||||
#ifdef ENABLE_BACKWARD_CPP
|
||||
#define BACKWARD_HAS_DW 1
|
||||
@@ -35,9 +31,6 @@
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
#include <cstring>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <future>
|
||||
#include <iostream>
|
||||
#include <util/thread_pool_executor.h>
|
||||
|
||||
@@ -49,65 +42,6 @@ backward::SignalHandling sh; // NOLINT
|
||||
|
||||
using namespace clanguml;
|
||||
|
||||
/**
|
||||
* Generate specific diagram identified by name
|
||||
*
|
||||
* @param od Diagram output directory
|
||||
* @param name Name of the diagram as specified in the config file
|
||||
* @param diagram Diagram model instance
|
||||
* @param db Compilation database
|
||||
* @param translation_units List of translation units to be used for this
|
||||
* diagram
|
||||
* @param verbose Logging level
|
||||
*/
|
||||
void generate_diagram(const std::string &od, const std::string &name,
|
||||
std::shared_ptr<clanguml::config::diagram> diagram,
|
||||
const clang::tooling::CompilationDatabase &db,
|
||||
const std::vector<std::string> &translation_units, bool verbose);
|
||||
|
||||
/**
|
||||
* Find translation units for diagrams.
|
||||
*
|
||||
* For each diagram to be generated, this function selects translation units
|
||||
* to be used for this diagram. The files are selected as an intersection
|
||||
* between all translation units found in the compilation database and the
|
||||
* `glob` patterns specified for each diagram in the configuration file.
|
||||
*
|
||||
* @param diagram_names List of diagram names to be generated
|
||||
* @param config Configuration instance
|
||||
* @param compilation_database_files All translation units in compilation
|
||||
* database
|
||||
* @param translation_units_map The output map containing translation
|
||||
* units for each diagram by name
|
||||
*/
|
||||
void find_translation_units_for_diagrams(
|
||||
const std::vector<std::string> &diagram_names,
|
||||
clanguml::config::config &config,
|
||||
const std::vector<std::string> &compilation_database_files,
|
||||
std::map<std::string, std::vector<std::string>> &translation_units_map);
|
||||
|
||||
/**
|
||||
* Generate diagrams.
|
||||
*
|
||||
* This function generates all diagrams specified in the configuration file
|
||||
* and in the command line.
|
||||
*
|
||||
* @param diagram_names List of diagram names to be generated
|
||||
* @param config Configuration instance
|
||||
* @param od Output directory where diagrams should be written
|
||||
* @param db Compilation database instance
|
||||
* @param verbose Verbosity level
|
||||
* @param thread_count Number of diagrams to be generated in parallel
|
||||
* @param translation_units_map List of translation units to be used for each
|
||||
* diagram
|
||||
*/
|
||||
void generate_diagrams(const std::vector<std::string> &diagram_names,
|
||||
clanguml::config::config &config, const std::string &od,
|
||||
const std::unique_ptr<clang::tooling::CompilationDatabase> &db, int verbose,
|
||||
unsigned int thread_count,
|
||||
const std::map<std::string, std::vector<std::string>>
|
||||
&translation_units_map);
|
||||
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
cli::cli_handler cli;
|
||||
@@ -138,173 +72,13 @@ int main(int argc, const char *argv[])
|
||||
// We have to generate the translation units list for each diagram before
|
||||
// scheduling tasks, because std::filesystem::current_path cannot be trusted
|
||||
// with multiple threads
|
||||
find_translation_units_for_diagrams(cli.diagram_names, cli.config,
|
||||
compilation_database_files, translation_units_map);
|
||||
|
||||
generate_diagrams(cli.diagram_names, cli.config,
|
||||
cli.effective_output_directory, db, cli.verbose, cli.thread_count,
|
||||
clanguml::common::generators::find_translation_units_for_diagrams(
|
||||
cli.diagram_names, cli.config, compilation_database_files,
|
||||
translation_units_map);
|
||||
|
||||
clanguml::common::generators::generate_diagrams(cli.diagram_names,
|
||||
cli.config, cli.effective_output_directory, db, cli.verbose,
|
||||
cli.thread_count, cli.generators, translation_units_map);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void generate_diagram(const std::string &od, const std::string &name,
|
||||
std::shared_ptr<clanguml::config::diagram> diagram,
|
||||
const clang::tooling::CompilationDatabase &db,
|
||||
const std::vector<std::string> &translation_units, bool verbose)
|
||||
{
|
||||
using clanguml::common::model::diagram_t;
|
||||
using clanguml::config::class_diagram;
|
||||
using clanguml::config::include_diagram;
|
||||
using clanguml::config::package_diagram;
|
||||
using clanguml::config::sequence_diagram;
|
||||
|
||||
auto path = std::filesystem::path{od} / fmt::format("{}.puml", name);
|
||||
std::ofstream ofs;
|
||||
ofs.open(path, std::ofstream::out | std::ofstream::trunc);
|
||||
|
||||
if (diagram->type() == diagram_t::kClass) {
|
||||
using diagram_config = class_diagram;
|
||||
using diagram_model = clanguml::class_diagram::model::diagram;
|
||||
using diagram_visitor =
|
||||
clanguml::class_diagram::visitor::translation_unit_visitor;
|
||||
|
||||
auto model =
|
||||
clanguml::common::generators::plantuml::generate<diagram_model,
|
||||
diagram_config, diagram_visitor>(db, diagram->name,
|
||||
dynamic_cast<diagram_config &>(*diagram), translation_units,
|
||||
verbose);
|
||||
|
||||
ofs << clanguml::class_diagram::generators::plantuml::generator(
|
||||
dynamic_cast<diagram_config &>(*diagram), *model);
|
||||
}
|
||||
else if (diagram->type() == diagram_t::kSequence) {
|
||||
using diagram_config = sequence_diagram;
|
||||
using diagram_model = clanguml::sequence_diagram::model::diagram;
|
||||
using diagram_visitor =
|
||||
clanguml::sequence_diagram::visitor::translation_unit_visitor;
|
||||
|
||||
auto model =
|
||||
clanguml::common::generators::plantuml::generate<diagram_model,
|
||||
diagram_config, diagram_visitor>(db, diagram->name,
|
||||
dynamic_cast<diagram_config &>(*diagram), translation_units,
|
||||
verbose);
|
||||
|
||||
ofs << clanguml::sequence_diagram::generators::plantuml::generator(
|
||||
dynamic_cast<clanguml::config::sequence_diagram &>(*diagram),
|
||||
*model);
|
||||
}
|
||||
else if (diagram->type() == diagram_t::kPackage) {
|
||||
using diagram_config = package_diagram;
|
||||
using diagram_model = clanguml::package_diagram::model::diagram;
|
||||
using diagram_visitor =
|
||||
clanguml::package_diagram::visitor::translation_unit_visitor;
|
||||
|
||||
auto model =
|
||||
clanguml::common::generators::plantuml::generate<diagram_model,
|
||||
diagram_config, diagram_visitor>(db, diagram->name,
|
||||
dynamic_cast<diagram_config &>(*diagram), translation_units,
|
||||
verbose);
|
||||
|
||||
ofs << clanguml::package_diagram::generators::plantuml::generator(
|
||||
dynamic_cast<diagram_config &>(*diagram), *model);
|
||||
}
|
||||
else if (diagram->type() == diagram_t::kInclude) {
|
||||
using diagram_config = include_diagram;
|
||||
using diagram_model = clanguml::include_diagram::model::diagram;
|
||||
using diagram_visitor =
|
||||
clanguml::include_diagram::visitor::translation_unit_visitor;
|
||||
|
||||
auto model =
|
||||
clanguml::common::generators::plantuml::generate<diagram_model,
|
||||
diagram_config, diagram_visitor>(db, diagram->name,
|
||||
dynamic_cast<diagram_config &>(*diagram), translation_units,
|
||||
verbose);
|
||||
|
||||
ofs << clanguml::include_diagram::generators::plantuml::generator(
|
||||
dynamic_cast<diagram_config &>(*diagram), *model);
|
||||
}
|
||||
|
||||
LOG_INFO("Written {} diagram to {}", name, path.string());
|
||||
|
||||
ofs.close();
|
||||
}
|
||||
|
||||
void generate_diagrams(const std::vector<std::string> &diagram_names,
|
||||
clanguml::config::config &config, const std::string &od,
|
||||
const std::unique_ptr<clang::tooling::CompilationDatabase> &db,
|
||||
const int verbose, const unsigned int thread_count,
|
||||
const std::map<std::string, std::vector<std::string>>
|
||||
&translation_units_map)
|
||||
{
|
||||
util::thread_pool_executor generator_executor{thread_count};
|
||||
std::vector<std::future<void>> futs;
|
||||
|
||||
for (const auto &[name, diagram] : config.diagrams) {
|
||||
// If there are any specific diagram names provided on the command line,
|
||||
// and this diagram is not in that list - skip it
|
||||
if (!diagram_names.empty() && !util::contains(diagram_names, name))
|
||||
continue;
|
||||
|
||||
const auto &valid_translation_units = translation_units_map.at(name);
|
||||
|
||||
if (valid_translation_units.empty()) {
|
||||
LOG_ERROR(
|
||||
"Diagram {} generation failed: no translation units found",
|
||||
name);
|
||||
continue;
|
||||
}
|
||||
|
||||
futs.emplace_back(generator_executor.add(
|
||||
[&od, &name = name, &diagram = diagram, db = std::ref(*db),
|
||||
translation_units = valid_translation_units, verbose]() {
|
||||
try {
|
||||
generate_diagram(
|
||||
od, name, diagram, db, translation_units, verbose != 0);
|
||||
}
|
||||
catch (std::runtime_error &e) {
|
||||
LOG_ERROR(e.what());
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
for (auto &fut : futs) {
|
||||
fut.get();
|
||||
}
|
||||
}
|
||||
|
||||
void find_translation_units_for_diagrams(
|
||||
const std::vector<std::string> &diagram_names,
|
||||
clanguml::config::config &config,
|
||||
const std::vector<std::string> &compilation_database_files,
|
||||
std::map<std::string, std::vector<std::string>> &translation_units_map)
|
||||
{
|
||||
for (const auto &[name, diagram] : config.diagrams) {
|
||||
// If there are any specific diagram names provided on the command line,
|
||||
// and this diagram is not in that list - skip it
|
||||
if (!diagram_names.empty() && !util::contains(diagram_names, name))
|
||||
continue;
|
||||
|
||||
// If glob is not defined use all translation units from the
|
||||
// compilation database
|
||||
if (!diagram->glob.has_value) {
|
||||
translation_units_map[name] = compilation_database_files;
|
||||
}
|
||||
// Otherwise, get all translation units matching the glob from diagram
|
||||
// configuration
|
||||
else {
|
||||
const std::vector<std::string> translation_units =
|
||||
diagram->get_translation_units();
|
||||
|
||||
std::vector<std::string> valid_translation_units{};
|
||||
std::copy_if(compilation_database_files.begin(),
|
||||
compilation_database_files.end(),
|
||||
std::back_inserter(valid_translation_units),
|
||||
[&translation_units](const auto &tu) {
|
||||
return util::contains(translation_units, tu);
|
||||
});
|
||||
|
||||
translation_units_map[name] = std::move(valid_translation_units);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user