refactor(editor): share the compdb entry query with a new CLI command
This commit is contained in:
1 parent
af735c28ff
commit
f5eb489e79
9 files changed
+221
-64
No files matched your search
+22
-63
@@ -1626,75 +1626,34 @@ std::string compdb_entries_project(const DaemonOptions &options, std::string_vie
|
||||
"compdb_entries: no compilation database is published; run compdb export first");
|
||||
}
|
||||
|
||||
auto text = Void::FileSystem::read_file(compdb_path);
|
||||
if (!text) {
|
||||
return error_response(std::format("compdb_entries: {}", text.error().format()));
|
||||
Editor::CompdbQuery query;
|
||||
query.file_filter = file_filter;
|
||||
query.limit = limit;
|
||||
auto queried = Editor::query_compdb_entries(compdb_path, query);
|
||||
if (!queried) {
|
||||
return error_response(std::format("compdb_entries: {}", queried.error().format()));
|
||||
}
|
||||
auto parsed = Void::Json::Strict::parse(*text);
|
||||
if (!parsed) {
|
||||
return error_response("compdb_entries: compilation database is not valid JSON");
|
||||
}
|
||||
const auto *entries = std::get_if<JsonValue::Array>(&parsed->value);
|
||||
if (entries == nullptr) {
|
||||
return error_response("compdb_entries: compilation database must be a JSON array");
|
||||
}
|
||||
|
||||
JsonValue::Array matched;
|
||||
std::size_t total = 0;
|
||||
for (const auto &entry : *entries) {
|
||||
const auto *object = std::get_if<JsonValue::Object>(&entry.value);
|
||||
if (object == nullptr) {
|
||||
continue;
|
||||
}
|
||||
const auto file_it = object->find("file");
|
||||
if (file_it == object->end()) {
|
||||
continue;
|
||||
}
|
||||
const auto *file = std::get_if<std::string>(&file_it->second.value);
|
||||
if (file == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (!file_filter.empty() &&
|
||||
!std::string_view{*file}.ends_with(std::string_view{file_filter})) {
|
||||
continue;
|
||||
}
|
||||
++total;
|
||||
if (matched.size() >= limit) {
|
||||
continue;
|
||||
}
|
||||
for (const auto &entry : queried->entries) {
|
||||
JsonValue::Object projected;
|
||||
projected.emplace("file", JsonValue{*file});
|
||||
const auto copy_string = [&](std::string_view field_name) {
|
||||
if (const auto it = object->find(field_name); it != object->end()) {
|
||||
if (const auto *value = std::get_if<std::string>(&it->second.value)) {
|
||||
projected.emplace(std::string{field_name}, JsonValue{*value});
|
||||
}
|
||||
}
|
||||
};
|
||||
copy_string("directory");
|
||||
copy_string("output");
|
||||
copy_string("command");
|
||||
if (const auto it = object->find("arguments"); it != object->end()) {
|
||||
if (const auto *arguments = std::get_if<JsonValue::Array>(&it->second.value)) {
|
||||
JsonValue::Array bounded;
|
||||
for (const auto &argument : *arguments) {
|
||||
const auto *value = std::get_if<std::string>(&argument.value);
|
||||
if (value == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (bounded.size() >= kMaxCompdbArguments) {
|
||||
projected.emplace("arguments_truncated", JsonValue{true});
|
||||
break;
|
||||
}
|
||||
bounded.emplace_back(value->size() > kMaxCompdbArgumentBytes
|
||||
? value->substr(0, kMaxCompdbArgumentBytes)
|
||||
: *value);
|
||||
}
|
||||
projected.emplace("arguments", JsonValue{std::move(bounded)});
|
||||
}
|
||||
projected.emplace("file", JsonValue{entry.file});
|
||||
if (!entry.directory.empty()) {
|
||||
projected.emplace("directory", JsonValue{entry.directory});
|
||||
}
|
||||
if (!entry.output.empty()) {
|
||||
projected.emplace("output", JsonValue{entry.output});
|
||||
}
|
||||
if (!entry.command.empty()) {
|
||||
projected.emplace("command", JsonValue{entry.command});
|
||||
}
|
||||
JsonValue::Array arguments;
|
||||
for (const auto &argument : entry.arguments) {
|
||||
arguments.emplace_back(argument);
|
||||
}
|
||||
projected.emplace("arguments", JsonValue{std::move(arguments)});
|
||||
matched.emplace_back(std::move(projected));
|
||||
}
|
||||
const std::size_t total = queried->total;
|
||||
|
||||
JsonValue::Object object;
|
||||
object.emplace("ok", JsonValue{true});
|
||||
|
||||
@@ -1043,6 +1043,85 @@ generate_ide_configs(const Graph::BuildGraph &graph, const IdeConfigOptions &opt
|
||||
return report;
|
||||
}
|
||||
|
||||
Void::Result::Result<CompdbQueryResult, Void::Result::Error>
|
||||
query_compdb_entries(const std::filesystem::path &compdb_path, const CompdbQuery &query) {
|
||||
constexpr std::size_t kMaxArguments = 256;
|
||||
constexpr std::size_t kMaxArgumentBytes = 1024;
|
||||
|
||||
auto text = Void::FileSystem::read_file(compdb_path);
|
||||
if (!text) {
|
||||
return std::unexpected{text.error()};
|
||||
}
|
||||
auto parsed = Void::Json::Strict::parse(*text);
|
||||
if (!parsed) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("compilation database '{}' is not valid JSON", compdb_path.string()))};
|
||||
}
|
||||
const auto *entries = std::get_if<Void::Json::Strict::Value::Array>(&parsed->value);
|
||||
if (!entries) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
"compilation database must be a JSON array")};
|
||||
}
|
||||
|
||||
CompdbQueryResult result;
|
||||
result.compdb_path = compdb_path;
|
||||
for (const auto &entry : *entries) {
|
||||
const auto *object = std::get_if<Void::Json::Strict::Value::Object>(&entry.value);
|
||||
if (!object) {
|
||||
continue;
|
||||
}
|
||||
const auto file_it = object->find("file");
|
||||
if (file_it == object->end()) {
|
||||
continue;
|
||||
}
|
||||
const auto *file = std::get_if<std::string>(&file_it->second.value);
|
||||
if (file == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (!query.file_filter.empty() &&
|
||||
!std::string_view{*file}.ends_with(query.file_filter)) {
|
||||
continue;
|
||||
}
|
||||
++result.total;
|
||||
if (result.entries.size() >= query.limit) {
|
||||
continue;
|
||||
}
|
||||
CompdbEntry projected;
|
||||
projected.file = *file;
|
||||
const auto read = [&](std::string_view name, std::string &out) {
|
||||
if (const auto it = object->find(std::string{name}); it != object->end()) {
|
||||
if (const auto *value = std::get_if<std::string>(&it->second.value)) {
|
||||
out = *value;
|
||||
}
|
||||
}
|
||||
};
|
||||
read("directory", projected.directory);
|
||||
read("output", projected.output);
|
||||
read("command", projected.command);
|
||||
if (const auto it = object->find("arguments"); it != object->end()) {
|
||||
if (const auto *arguments =
|
||||
std::get_if<Void::Json::Strict::Value::Array>(&it->second.value)) {
|
||||
for (const auto &argument : *arguments) {
|
||||
const auto *value = std::get_if<std::string>(&argument.value);
|
||||
if (value == nullptr) {
|
||||
continue;
|
||||
}
|
||||
if (projected.arguments.size() >= kMaxArguments) {
|
||||
break;
|
||||
}
|
||||
projected.arguments.push_back(value->size() > kMaxArgumentBytes
|
||||
? value->substr(0, kMaxArgumentBytes)
|
||||
: *value);
|
||||
}
|
||||
}
|
||||
}
|
||||
result.entries.push_back(std::move(projected));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string format_ide_query_json(const Graph::BuildGraph &graph,
|
||||
const std::filesystem::path &compdb_path,
|
||||
const std::filesystem::path &sdk_include_dir) {
|
||||
|
||||
@@ -1201,6 +1201,71 @@ int execute_tokens_command(std::string_view sub, const TokensCmdOptions &opts) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct CompdbEntriesCmdOptions {
|
||||
std::string project_arg;
|
||||
std::optional<std::string> build_arg;
|
||||
std::optional<std::string> file_arg;
|
||||
std::string limit_arg{"50"};
|
||||
};
|
||||
|
||||
int execute_compdb_entries_command(const CompdbEntriesCmdOptions &opts) {
|
||||
int parsed_limit = 0;
|
||||
if (!Void::Str::to_int(opts.limit_arg, parsed_limit) || parsed_limit < 0 ||
|
||||
parsed_limit > 500) {
|
||||
std::cerr << "vbs compdb entries: invalid --limit value '" << opts.limit_arg << "'\n";
|
||||
return 2;
|
||||
}
|
||||
const auto limit = static_cast<std::size_t>(parsed_limit);
|
||||
const std::filesystem::path project_root{opts.project_arg};
|
||||
const std::filesystem::path build_root =
|
||||
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
||||
|
||||
auto selection = Editor::read_selection_state(project_root);
|
||||
if (!selection) {
|
||||
std::cerr << "vbs compdb entries: " << selection.error().message() << '\n';
|
||||
return 1;
|
||||
}
|
||||
std::filesystem::path compdb_path;
|
||||
if (selection->has_value()) {
|
||||
compdb_path = (*selection)->selected_compdb_path;
|
||||
} else {
|
||||
std::error_code ec;
|
||||
const auto root_alias = build_root / "compile_commands.json";
|
||||
if (std::filesystem::exists(root_alias, ec)) {
|
||||
compdb_path = root_alias;
|
||||
}
|
||||
}
|
||||
if (compdb_path.empty()) {
|
||||
std::cerr << "vbs compdb entries: no compilation database is published; "
|
||||
"run 'vbs compdb export' first\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
Editor::CompdbQuery query;
|
||||
query.file_filter = opts.file_arg.value_or(std::string{});
|
||||
query.limit = limit;
|
||||
auto queried = Editor::query_compdb_entries(compdb_path, query);
|
||||
if (!queried) {
|
||||
std::cerr << "vbs compdb entries: " << queried.error().message() << '\n';
|
||||
return 1;
|
||||
}
|
||||
std::cout << "# " << queried->compdb_path.string() << " (" << queried->total
|
||||
<< " matching, showing " << queried->entries.size() << ")\n";
|
||||
for (const auto &entry : queried->entries) {
|
||||
std::cout << entry.file << '\n';
|
||||
if (!entry.arguments.empty()) {
|
||||
std::cout << " ";
|
||||
for (const auto &argument : entry.arguments) {
|
||||
std::cout << argument << ' ';
|
||||
}
|
||||
std::cout << '\n';
|
||||
} else if (!entry.command.empty()) {
|
||||
std::cout << " " << entry.command << '\n';
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct VerifyCmdOptions {
|
||||
std::optional<std::string> name_arg;
|
||||
std::optional<std::string> version_arg;
|
||||
@@ -2518,6 +2583,7 @@ int main(int argc, char **argv) {
|
||||
TokensCmdOptions tokens_opts;
|
||||
LockCmdOptions lock_opts;
|
||||
VerifyCmdOptions verify_opts;
|
||||
CompdbEntriesCmdOptions compdb_entries_opts;
|
||||
publish_opts.project_arg = cwd;
|
||||
BridgeCmdOptions bridge_opts;
|
||||
GcCmdOptions gc_opts;
|
||||
@@ -2681,6 +2747,18 @@ int main(int argc, char **argv) {
|
||||
publish_binary_opts.token_env, "VBS_REGISTRY_TOKEN");
|
||||
add_log_flags(publish_binary_cmd, false); // `-v` is `--version` on publish-binary.
|
||||
|
||||
auto &compdb_entries_root_cmd =
|
||||
root.subcommand("compdb-entries", "Query bounded entries from the published compile database");
|
||||
compdb_entries_root_cmd
|
||||
.option_default("--project", "-C", "Project directory", compdb_entries_opts.project_arg,
|
||||
cwd)
|
||||
.option("--build-dir", "-B", "Build directory", compdb_entries_opts.build_arg)
|
||||
.option("--file", "", "Only entries whose source path ends with this value",
|
||||
compdb_entries_opts.file_arg)
|
||||
.option_default("--limit", "", "Maximum entries (0-500)", compdb_entries_opts.limit_arg,
|
||||
"50");
|
||||
add_log_flags(compdb_entries_root_cmd);
|
||||
|
||||
auto &verify_cmd =
|
||||
root.subcommand("verify", "Verify a detached Ed25519 signature over a package artifact");
|
||||
verify_cmd
|
||||
@@ -3143,6 +3221,9 @@ int main(int argc, char **argv) {
|
||||
if (cmd == "verify") {
|
||||
return execute_verify_command(verify_opts);
|
||||
}
|
||||
if (cmd == "compdb-entries") {
|
||||
return execute_compdb_entries_command(compdb_entries_opts);
|
||||
}
|
||||
if (cmd == "bridge") {
|
||||
return execute_bridge_command(bridge_opts);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user