Fixed handling of relative paths in configuration files (#69)

This commit is contained in:
Bartek Kryza
2023-01-21 18:00:21 +01:00
parent b3701fa810
commit 78cfa630e5
99 changed files with 221 additions and 207 deletions

View File

@@ -123,16 +123,18 @@ std::string inheritable_diagram_options::simplify_template_type(
return full_name;
}
std::vector<std::string> diagram::get_translation_units(
const std::filesystem::path &root_directory) const
std::vector<std::string> diagram::get_translation_units() const
{
std::vector<std::string> translation_units{};
for (const auto &g : glob()) {
const auto matches = glob::glob(g, root_directory);
std::string glob_path =
fmt::format("{}/{}", relative_to().string(), g.c_str());
auto matches = glob::glob(glob_path, true, false);
for (const auto &match : matches) {
const auto path =
std::filesystem::canonical(root_directory / match);
const auto path = std::filesystem::canonical(relative_to() / match);
translation_units.emplace_back(path.string());
}
}

View File

@@ -170,8 +170,7 @@ struct diagram : public inheritable_diagram_options {
virtual common::model::diagram_t type() const = 0;
std::vector<std::string> get_translation_units(
const std::filesystem::path &root_directory) const;
std::vector<std::string> get_translation_units() const;
void initialize_type_aliases();
@@ -268,7 +267,8 @@ YAML::Emitter &operator<<(YAML::Emitter &out, const option<T> &o)
return out;
}
config load(const std::string &config_file);
config load(const std::string &config_file,
std::optional<bool> paths_relative_to_pwd = {});
} // namespace config
namespace common::model {

View File

@@ -571,7 +571,30 @@ template <> struct convert<config> {
} // namespace YAML
namespace clanguml::config {
config load(const std::string &config_file)
namespace {
void resolve_option_path(YAML::Node &doc, const std::string &option)
{
std::filesystem::path relative_to_path{
doc["relative_to"].as<std::string>()};
assert(relative_to_path.is_absolute());
std::filesystem::path option_path{doc[option].as<std::string>()};
if (option_path.is_absolute())
return;
option_path = relative_to_path / option_path.string();
option_path = option_path.lexically_normal();
option_path.make_preferred();
doc[option] = option_path.string();
}
} // namespace
config load(
const std::string &config_file, std::optional<bool> paths_relative_to_pwd)
{
try {
YAML::Node doc = YAML::LoadFile(config_file);
@@ -583,6 +606,32 @@ config load(const std::string &config_file)
doc.force_insert(
"__parent_path", config_file_path.parent_path().string());
//
// If no relative_to path is specified in the config, make all paths
// resolvable against the parent directory of the .clang-uml config
// file, or against the $PWD if it was specified so in the command
// line
//
if (!doc["relative_to"]) {
bool paths_relative_to_config_file = true;
if (doc["paths_relative_to_config_file"] &&
!doc["paths_relative_to_config_file"].as<bool>())
paths_relative_to_config_file = false;
if (paths_relative_to_pwd && *paths_relative_to_pwd)
paths_relative_to_config_file = false;
if (paths_relative_to_config_file)
doc["relative_to"] = config_file_path.parent_path().string();
else
doc["relative_to"] = std::filesystem::current_path().string();
}
//
// Resolve common path-like config options relative to `relative_to`
//
resolve_option_path(doc, "output_directory");
resolve_option_path(doc, "compilation_database_dir");
// If the current directory is also a git repository,
// load some config values, which can be included in the
// generated diagrams