Compare commits
4 Commits
fix-handli
...
refactor-u
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
674441f3cd | ||
|
|
40194eb9b5 | ||
|
|
a404a3070d | ||
|
|
c5e3b7e71d |
@@ -301,6 +301,7 @@ root:
|
||||
#
|
||||
compilation_database_dir: !optional string
|
||||
output_directory: !optional string
|
||||
query_driver: !optional string
|
||||
add_compile_flags: !optional [string]
|
||||
remove_compile_flags: !optional [string]
|
||||
diagram_templates: !optional diagram_templates_t
|
||||
|
||||
@@ -815,6 +815,7 @@ template <> struct convert<config> {
|
||||
get_option(node, rhs.using_namespace);
|
||||
get_option(node, rhs.using_module);
|
||||
get_option(node, rhs.output_directory);
|
||||
get_option(node, rhs.query_driver);
|
||||
get_option(node, rhs.compilation_database_dir);
|
||||
get_option(node, rhs.add_compile_flags);
|
||||
get_option(node, rhs.remove_compile_flags);
|
||||
|
||||
@@ -274,6 +274,7 @@ YAML::Emitter &operator<<(YAML::Emitter &out, const config &c)
|
||||
|
||||
out << c.compilation_database_dir;
|
||||
out << c.output_directory;
|
||||
out << c.query_driver;
|
||||
out << c.add_compile_flags;
|
||||
out << c.remove_compile_flags;
|
||||
|
||||
|
||||
140
src/util/util.cc
140
src/util/util.cc
@@ -28,29 +28,70 @@ namespace clanguml::util {
|
||||
|
||||
static const auto WHITESPACE = " \n\r\t\f\v";
|
||||
|
||||
namespace {
|
||||
class pipe_t {
|
||||
public:
|
||||
explicit pipe_t(const std::string &command, int &result)
|
||||
: result_{result}
|
||||
,
|
||||
#if defined(__linux) || defined(__unix) || defined(__APPLE__)
|
||||
pipe_{popen(command.c_str(), "r")}
|
||||
#elif defined(_WIN32)
|
||||
pipe_{_popen(command.c_str(), "r")}
|
||||
#endif
|
||||
{
|
||||
}
|
||||
|
||||
~pipe_t() { reset(); }
|
||||
|
||||
operator bool() const { return pipe_ != nullptr; }
|
||||
|
||||
FILE *get() const { return pipe_; }
|
||||
|
||||
void reset()
|
||||
{
|
||||
if (pipe_ == nullptr)
|
||||
return;
|
||||
|
||||
#if defined(__linux) || defined(__unix) || defined(__APPLE__)
|
||||
result_ = pclose(pipe_);
|
||||
#elif defined(_WIN32)
|
||||
result_ = _pclose(pipe_);
|
||||
#endif
|
||||
pipe_ = nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
int &result_;
|
||||
FILE *pipe_;
|
||||
};
|
||||
} // namespace
|
||||
|
||||
std::string get_process_output(const std::string &command)
|
||||
{
|
||||
constexpr size_t kBufferSize{1024};
|
||||
std::array<char, kBufferSize> buffer{};
|
||||
std::string result;
|
||||
std::string output;
|
||||
int result{EXIT_FAILURE};
|
||||
|
||||
#if defined(__linux) || defined(__unix) || defined(__APPLE__)
|
||||
std::unique_ptr<FILE, decltype(&pclose)> pipe(
|
||||
popen(command.c_str(), "r"), pclose);
|
||||
#elif defined(_WIN32)
|
||||
std::unique_ptr<FILE, decltype(&_pclose)> pipe(
|
||||
_popen(command.c_str(), "r"), _pclose);
|
||||
#endif
|
||||
pipe_t pipe{command, result};
|
||||
|
||||
if (!pipe) {
|
||||
throw std::runtime_error("popen() failed!");
|
||||
}
|
||||
|
||||
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
|
||||
result += buffer.data();
|
||||
output += buffer.data();
|
||||
}
|
||||
|
||||
return result;
|
||||
pipe.reset();
|
||||
|
||||
if (result != EXIT_SUCCESS) {
|
||||
throw std::runtime_error(
|
||||
fmt::format("External command '{}' failed: {}", command, output));
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
void check_process_output(const std::string &command)
|
||||
@@ -59,21 +100,7 @@ void check_process_output(const std::string &command)
|
||||
std::array<char, kBufferSize> buffer{};
|
||||
int result{EXIT_FAILURE};
|
||||
std::string output;
|
||||
auto finalize = [&result](FILE *f) {
|
||||
#if defined(__linux) || defined(__unix) || defined(__APPLE__)
|
||||
result = pclose(f);
|
||||
#elif defined(_WIN32)
|
||||
result = _pclose(f);
|
||||
#endif
|
||||
};
|
||||
|
||||
#if defined(__linux) || defined(__unix) || defined(__APPLE__)
|
||||
std::unique_ptr<FILE, decltype(finalize)> pipe(
|
||||
popen(command.c_str(), "r"), finalize);
|
||||
#elif defined(_WIN32)
|
||||
std::unique_ptr<FILE, decltype(finalize)> pipe(
|
||||
_popen(command.c_str(), "r"), finalize);
|
||||
#endif
|
||||
pipe_t pipe{command, result};
|
||||
|
||||
if (!pipe) {
|
||||
throw std::runtime_error("popen() failed!");
|
||||
@@ -121,48 +148,59 @@ bool is_git_repository()
|
||||
if (!env.empty())
|
||||
return true;
|
||||
|
||||
return contains(
|
||||
trim(get_process_output("git rev-parse --git-dir")), ".git");
|
||||
std::string output;
|
||||
|
||||
try {
|
||||
output = get_process_output("git rev-parse --git-dir");
|
||||
}
|
||||
catch (std::runtime_error &e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return contains(trim(output), ".git");
|
||||
}
|
||||
|
||||
std::string run_git_command(
|
||||
const std::string &cmd, const std::string &env_override)
|
||||
{
|
||||
auto env = get_env(env_override);
|
||||
|
||||
if (!env.empty())
|
||||
return env;
|
||||
|
||||
std::string output;
|
||||
|
||||
try {
|
||||
output = get_process_output(cmd);
|
||||
}
|
||||
catch (std::runtime_error &e) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return trim(output);
|
||||
}
|
||||
|
||||
std::string get_git_branch()
|
||||
{
|
||||
auto env = get_env("CLANGUML_GIT_BRANCH");
|
||||
|
||||
if (!env.empty())
|
||||
return env;
|
||||
|
||||
return trim(get_process_output("git rev-parse --abbrev-ref HEAD"));
|
||||
return run_git_command(
|
||||
"git rev-parse --abbrev-ref HEAD", "CLANGUML_GIT_BRANCH");
|
||||
}
|
||||
|
||||
std::string get_git_revision()
|
||||
{
|
||||
auto env = get_env("CLANGUML_GIT_REVISION");
|
||||
|
||||
if (!env.empty())
|
||||
return env;
|
||||
|
||||
return trim(get_process_output("git describe --tags --always"));
|
||||
return run_git_command(
|
||||
"git describe --tags --always", "CLANGUML_GIT_REVISION");
|
||||
}
|
||||
|
||||
std::string get_git_commit()
|
||||
{
|
||||
auto env = get_env("CLANGUML_GIT_COMMIT");
|
||||
|
||||
if (!env.empty())
|
||||
return env;
|
||||
|
||||
return trim(get_process_output("git rev-parse HEAD"));
|
||||
return run_git_command("git rev-parse HEAD", "CLANGUML_GIT_COMMIT");
|
||||
}
|
||||
|
||||
std::string get_git_toplevel_dir()
|
||||
{
|
||||
auto env = get_env("CLANGUML_GIT_TOPLEVEL_DIR");
|
||||
|
||||
if (!env.empty())
|
||||
return env;
|
||||
|
||||
return trim(get_process_output("git rev-parse --show-toplevel"));
|
||||
return run_git_command(
|
||||
"git rev-parse --show-toplevel", "CLANGUML_GIT_TOPLEVEL_DIR");
|
||||
}
|
||||
|
||||
std::string get_os_name()
|
||||
|
||||
@@ -32,6 +32,7 @@ TEST_CASE("Test config simple", "[unit-test]")
|
||||
|
||||
auto cfg = clanguml::config::load("./test_config_data/simple.yml");
|
||||
|
||||
CHECK(cfg.query_driver() == "g++");
|
||||
CHECK(cfg.diagrams.size() == 1);
|
||||
auto &diagram = *cfg.diagrams["class_main"];
|
||||
CHECK(diagram.type() == clanguml::common::model::diagram_t::kClass);
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
compilation_database_dir: debug
|
||||
# The directory where *.puml files will be generated
|
||||
output_directory: docs/diagrams
|
||||
# Query compiler frontend for compilation flags
|
||||
query_driver: .
|
||||
# Set this as default for all diagrams
|
||||
generate_method_arguments: none
|
||||
# Enable generation of hyperlinks to diagram elements
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
compilation_database_dir: debug
|
||||
comment_parser: clang
|
||||
output_directory: output
|
||||
query_driver: g++
|
||||
generate_template_argument_dependencies: false
|
||||
diagrams:
|
||||
class_main:
|
||||
|
||||
Reference in New Issue
Block a user