Merge pull request #3676 from martin-s/feature-graph

Dependency graph as new command
This commit is contained in:
Alexander Karatarakis 2018-06-20 00:34:13 -07:00 committed by GitHub
commit bf6708576c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 168 additions and 108 deletions

View File

@ -1,61 +1,166 @@
#include "pch.h"
#include <vcpkg/base/strings.h>
#include <vcpkg/base/system.h>
#include <vcpkg/base/util.h>
#include <vcpkg/commands.h>
#include <vcpkg/help.h>
#include <vcpkg/paragraphs.h>
namespace vcpkg::Commands::DependInfo
{
const CommandStructure COMMAND_STRUCTURE = {
Help::create_example_string(R"###(depend-info [pat])###"),
0,
1,
{},
nullptr,
};
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
{
Util::unused(args.parse_arguments(COMMAND_STRUCTURE));
std::vector<std::unique_ptr<SourceControlFile>> source_control_files =
Paragraphs::load_all_ports(paths.get_filesystem(), paths.ports);
if (args.command_arguments.size() == 1)
{
const std::string filter = args.command_arguments.at(0);
Util::erase_remove_if(source_control_files,
[&](const std::unique_ptr<SourceControlFile>& source_control_file) {
const SourceParagraph& source_paragraph = *source_control_file->core_paragraph;
if (Strings::case_insensitive_ascii_contains(source_paragraph.name, filter))
{
return false;
}
for (const Dependency& dependency : source_paragraph.depends)
{
if (Strings::case_insensitive_ascii_contains(dependency.name(), filter))
{
return false;
}
}
return true;
});
}
for (auto&& source_control_file : source_control_files)
{
const SourceParagraph& source_paragraph = *source_control_file->core_paragraph;
const auto s = Strings::join(", ", source_paragraph.depends, [](const Dependency& d) { return d.name(); });
System::println("%s: %s", source_paragraph.name, s);
}
Checks::exit_success(VCPKG_LINE_INFO);
}
}
#include "pch.h"
#include <vcpkg/base/strings.h>
#include <vcpkg/base/system.h>
#include <vcpkg/base/util.h>
#include <vcpkg/commands.h>
#include <vcpkg/help.h>
#include <vcpkg/paragraphs.h>
namespace vcpkg::Commands::DependInfo
{
constexpr StringLiteral OPTION_DOT = "--dot";
constexpr StringLiteral OPTION_DGML = "--dgml";
constexpr std::array<CommandSwitch, 2> DEPEND_SWITCHES = { {
{ OPTION_DOT, "Creates graph on basis of dot" },
{ OPTION_DGML, "Creates graph on basis of dgml" },
} };
const CommandStructure COMMAND_STRUCTURE = {
Help::create_example_string(R"###(depend-info [pat])###"),
0,
1,
{ DEPEND_SWITCHES,{} },
nullptr,
};
std::string replace_dashes_with_underscore(const std::string& input)
{
std::string output = input;
std::replace(output.begin(), output.end(), '-', '_');
return output;
}
std::string create_dot_as_string(
const std::vector<std::unique_ptr<SourceControlFile>>& source_control_files)
{
int empty_node_count = 0;
std::string s;
s.append("digraph G{ rankdir=LR; edge [minlen=3]; overlap=false;");
for (const auto& source_control_file : source_control_files)
{
const SourceParagraph& source_paragraph = *source_control_file->core_paragraph;
if (source_paragraph.depends.empty())
{
empty_node_count++;
continue;
}
const std::string name = replace_dashes_with_underscore(source_paragraph.name);
s.append(Strings::format("%s;", name));
for (const Dependency& d : source_paragraph.depends)
{
const std::string dependency_name = replace_dashes_with_underscore(d.name());
s.append(Strings::format("%s -> %s;", name, dependency_name));
}
}
s.append(Strings::format("empty [label=\"%d singletons...\"]; }", empty_node_count));
return s;
}
std::string create_dgml_as_string(
const std::vector<std::unique_ptr<SourceControlFile>>& source_control_files)
{
std::string s;
s.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
s.append("<DirectedGraph xmlns=\"http://schemas.microsoft.com/vs/2009/dgml\">");
std::string nodes, links;
for (const auto& source_control_file : source_control_files)
{
const SourceParagraph& source_paragraph = *source_control_file->core_paragraph;
const std::string name = source_paragraph.name;
nodes.append(Strings::format("<Node Id=\"%s\" />", name));
// Iterate over dependencies.
for (const Dependency& d : source_paragraph.depends)
{
links.append(Strings::format("<Link Source=\"%s\" Target=\"%s\" />", name, d.name()));
}
// Iterate over feature dependencies.
const std::vector<std::unique_ptr<FeatureParagraph>>& feature_paragraphs = source_control_file->feature_paragraphs;
for (const auto& feature_paragraph : feature_paragraphs)
{
for (const Dependency& d : feature_paragraph->depends)
{
links.append(Strings::format("<Link Source=\"%s\" Target=\"%s\" />", name, d.name()));
}
}
}
s.append(Strings::format("<Nodes>%s</Nodes>", nodes));
s.append(Strings::format("<Links>%s</Links>", links));
s.append("</DirectedGraph>");
return s;
}
std::string create_graph_as_string(
const std::unordered_set<std::string>& switches,
const std::vector<std::unique_ptr<SourceControlFile>>& source_control_files)
{
if (Util::Sets::contains(switches, OPTION_DOT))
{
return create_dot_as_string(source_control_files);
}
else if (Util::Sets::contains(switches, OPTION_DGML))
{
return create_dgml_as_string(source_control_files);
}
return "";
}
void perform_and_exit(const VcpkgCmdArguments& args, const VcpkgPaths& paths)
{
const ParsedArguments options = args.parse_arguments(COMMAND_STRUCTURE);
auto source_control_files = Paragraphs::load_all_ports(paths.get_filesystem(), paths.ports);
if (args.command_arguments.size() == 1)
{
const std::string filter = args.command_arguments.at(0);
Util::erase_remove_if(source_control_files,
[&](const std::unique_ptr<SourceControlFile>& source_control_file) {
const SourceParagraph& source_paragraph = *source_control_file->core_paragraph;
if (Strings::case_insensitive_ascii_contains(source_paragraph.name, filter))
{
return false;
}
for (const Dependency& dependency : source_paragraph.depends)
{
if (Strings::case_insensitive_ascii_contains(dependency.name(), filter))
{
return false;
}
}
return true;
});
}
if (!options.switches.empty())
{
const std::string graph_as_string = create_graph_as_string(options.switches, source_control_files);
System::println(graph_as_string);
Checks::exit_success(VCPKG_LINE_INFO);
}
for (auto&& source_control_file : source_control_files)
{
const SourceParagraph& source_paragraph = *source_control_file->core_paragraph;
const auto s = Strings::join(", ", source_paragraph.depends, [](const Dependency& d) { return d.name(); });
System::println("%s: %s", source_paragraph.name, s);
}
Checks::exit_success(VCPKG_LINE_INFO);
}
}

View File

@ -10,46 +10,9 @@
namespace vcpkg::Commands::Search
{
static constexpr StringLiteral OPTION_GRAPH = "--graph"; // TODO: This should find a better home, eventually
static constexpr StringLiteral OPTION_FULLDESC =
"--x-full-desc"; // TODO: This should find a better home, eventually
static std::string replace_dashes_with_underscore(const std::string& input)
{
std::string output = input;
std::replace(output.begin(), output.end(), '-', '_');
return output;
}
static std::string create_graph_as_string(
const std::vector<std::unique_ptr<SourceControlFile>>& source_control_files)
{
int empty_node_count = 0;
std::string s;
s.append("digraph G{ rankdir=LR; edge [minlen=3]; overlap=false;");
for (const auto& source_control_file : source_control_files)
{
const SourceParagraph& source_paragraph = *source_control_file->core_paragraph;
if (source_paragraph.depends.empty())
{
empty_node_count++;
continue;
}
const std::string name = replace_dashes_with_underscore(source_paragraph.name);
s.append(Strings::format("%s;", name));
for (const Dependency& d : source_paragraph.depends)
{
const std::string dependency_name = replace_dashes_with_underscore(d.name());
s.append(Strings::format("%s -> %s;", name, dependency_name));
}
}
s.append(Strings::format("empty [label=\"%d singletons...\"]; }", empty_node_count));
return s;
}
static void do_print(const SourceParagraph& source_paragraph, bool full_desc)
{
if (full_desc)
@ -80,8 +43,7 @@ namespace vcpkg::Commands::Search
}
}
static constexpr std::array<CommandSwitch, 2> SEARCH_SWITCHES = {{
{OPTION_GRAPH, "Open editor into the port-specific buildtree subfolder"},
static constexpr std::array<CommandSwitch, 1> SEARCH_SWITCHES = {{
{OPTION_FULLDESC, "Do not truncate long text"},
}};
@ -102,13 +64,6 @@ namespace vcpkg::Commands::Search
auto source_paragraphs = Paragraphs::load_all_ports(paths.get_filesystem(), paths.ports);
if (Util::Sets::contains(options.switches, OPTION_GRAPH))
{
const std::string graph_as_string = create_graph_as_string(source_paragraphs);
System::println(graph_as_string);
Checks::exit_success(VCPKG_LINE_INFO);
}
if (args.command_arguments.empty())
{
for (const auto& source_control_file : source_paragraphs)