feat(daemon): list editor generation history
This commit is contained in:
1 parent
ba682e8bd1
commit
41a74bc925
8 files changed
+217
-2
No files matched your search
@@ -22,6 +22,7 @@
|
||||
#include <Void/System/Path.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
@@ -64,6 +65,7 @@ constexpr std::size_t kMaxBuildTargets = 64U;
|
||||
constexpr std::size_t kMaxCompdbArguments = 256U;
|
||||
constexpr std::size_t kMaxCompdbArgumentBytes = 1024U;
|
||||
constexpr std::size_t kMaxCompdbEntries = 500U;
|
||||
constexpr std::size_t kMaxCompdbGenerations = 128U;
|
||||
constexpr std::size_t kMaxPendingChanges = 4096U;
|
||||
constexpr std::int64_t kMaxWatchWaitMs = 5000;
|
||||
constexpr std::int64_t kDefaultWatchWaitMs = 2000;
|
||||
@@ -950,6 +952,109 @@ std::string graph_project(const DaemonOptions &options, std::string_view profile
|
||||
",\"graph\":" + Util::json_quote(rendered) + '}';
|
||||
}
|
||||
|
||||
// Lists immutable editor generations for a profile, including the active selection.
|
||||
std::string compdb_generations_project(const DaemonOptions &options,
|
||||
std::string_view profile_name) {
|
||||
const bool valid_profile =
|
||||
!profile_name.empty() && profile_name.size() <= 128 &&
|
||||
std::ranges::all_of(profile_name, [](char c) {
|
||||
return std::isalnum(static_cast<unsigned char>(c)) || c == '.' || c == '_' || c == '-';
|
||||
});
|
||||
if (!valid_profile) {
|
||||
return error_response("compdb_generations: profile must be 1-128 chars of [A-Za-z0-9._-]");
|
||||
}
|
||||
|
||||
const auto profile_dir = options.build_root / "profiles" / std::string{profile_name} / "editor";
|
||||
const auto generation_root = profile_dir / "generations";
|
||||
std::string active_generation;
|
||||
auto selection = Editor::read_selection_state(options.project_root);
|
||||
if (selection && selection->has_value()) {
|
||||
active_generation = (*selection)->selected_generation_id;
|
||||
}
|
||||
|
||||
// The per-profile status record describes the most recently published generation.
|
||||
std::string status_generation;
|
||||
std::string status_value{"unknown"};
|
||||
std::string status_diagnostic;
|
||||
std::int64_t status_entries = 0;
|
||||
if (auto text = Void::FileSystem::read_file(profile_dir / "status.json"); text) {
|
||||
if (auto parsed = Void::Json::Strict::parse(*text); parsed) {
|
||||
if (const auto *object = std::get_if<JsonValue::Object>(&parsed->value)) {
|
||||
const auto read_string = [&](std::string_view key, std::string &out) {
|
||||
if (const auto it = object->find(key); it != object->end()) {
|
||||
if (const auto *value = std::get_if<std::string>(&it->second.value)) {
|
||||
out = *value;
|
||||
}
|
||||
}
|
||||
};
|
||||
read_string("generation_id", status_generation);
|
||||
read_string("status", status_value);
|
||||
read_string("diagnostic", status_diagnostic);
|
||||
if (const auto it = object->find("entry_count"); it != object->end()) {
|
||||
if (const auto *value = std::get_if<std::int64_t>(&it->second.value)) {
|
||||
status_entries = *value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
JsonValue::Array generations;
|
||||
std::error_code ec;
|
||||
if (std::filesystem::is_directory(generation_root, ec)) {
|
||||
std::vector<std::filesystem::path> directories;
|
||||
for (const auto &entry : std::filesystem::directory_iterator(generation_root, ec)) {
|
||||
if (entry.is_directory()) {
|
||||
directories.push_back(entry.path());
|
||||
}
|
||||
}
|
||||
std::ranges::sort(directories);
|
||||
for (const auto &directory : directories) {
|
||||
if (generations.size() >= kMaxCompdbGenerations) {
|
||||
break;
|
||||
}
|
||||
const auto compdb = directory / "compile_commands.json";
|
||||
if (!std::filesystem::is_regular_file(compdb, ec)) {
|
||||
continue;
|
||||
}
|
||||
const std::string generation_id = directory.filename().string();
|
||||
const bool is_status_generation = generation_id == status_generation;
|
||||
JsonValue::Object generation;
|
||||
generation.emplace("id", JsonValue{generation_id});
|
||||
generation.emplace("active", JsonValue{generation_id == active_generation});
|
||||
generation.emplace(
|
||||
"status", JsonValue{is_status_generation ? status_value : std::string{"archived"}});
|
||||
generation.emplace("diagnostic",
|
||||
JsonValue{is_status_generation ? status_diagnostic : std::string{}});
|
||||
|
||||
std::int64_t entry_count = is_status_generation ? status_entries : 0;
|
||||
if (!is_status_generation) {
|
||||
if (auto text = Void::FileSystem::read_file(compdb); text) {
|
||||
if (auto parsed = Void::Json::Strict::parse(*text); parsed) {
|
||||
if (const auto *array = std::get_if<JsonValue::Array>(&parsed->value)) {
|
||||
entry_count = static_cast<std::int64_t>(array->size());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
generation.emplace("entries", JsonValue{entry_count});
|
||||
generations.emplace_back(std::move(generation));
|
||||
}
|
||||
}
|
||||
|
||||
JsonValue::Object object;
|
||||
object.emplace("ok", JsonValue{true});
|
||||
object.emplace("op", JsonValue{std::string{"compdb_generations"}});
|
||||
object.emplace("profile", JsonValue{std::string{profile_name}});
|
||||
if (active_generation.empty()) {
|
||||
object.emplace("active", JsonValue{nullptr});
|
||||
} else {
|
||||
object.emplace("active", JsonValue{active_generation});
|
||||
}
|
||||
object.emplace("generations", JsonValue{std::move(generations)});
|
||||
return serialize_response(JsonValue{std::move(object)});
|
||||
}
|
||||
|
||||
// Reports which editor configuration files are present for the project.
|
||||
std::string ide_configs_project(const DaemonOptions &options) {
|
||||
struct ConfigEntry {
|
||||
@@ -1502,6 +1607,17 @@ DaemonServer::Impl::DispatchResult DaemonServer::Impl::dispatch(std::string_view
|
||||
if (*name == "ide_configs") {
|
||||
return {ide_configs_project(options), false};
|
||||
}
|
||||
if (*name == "compdb_generations") {
|
||||
const auto entry = object->find("profile");
|
||||
if (entry == object->end()) {
|
||||
return {error_response("compdb_generations requires \"profile\""), false};
|
||||
}
|
||||
const auto *value = std::get_if<std::string>(&entry->second.value);
|
||||
if (value == nullptr || value->empty() || value->size() > kMaxDescribeProfileBytes) {
|
||||
return {error_response("\"profile\" must be a bounded non-empty string"), false};
|
||||
}
|
||||
return {compdb_generations_project(options, *value), false};
|
||||
}
|
||||
if (*name == "compdb_entries") {
|
||||
std::string file_filter;
|
||||
if (const auto entry = object->find("file"); entry != object->end()) {
|
||||
|
||||
@@ -1569,6 +1569,13 @@ int execute_daemon_command(std::string_view sub, const DaemonCmdOptions &opts) {
|
||||
request_json = R"({"op":"workspace_members"})";
|
||||
} else if (sub == "ide_configs") {
|
||||
request_json = R"({"op":"ide_configs"})";
|
||||
} else if (sub == "compdb_generations") {
|
||||
if (opts.profile_arg.empty()) {
|
||||
std::cerr << "vbs daemon compdb generations: --profile must not be empty\n";
|
||||
return 2;
|
||||
}
|
||||
request_json = std::format(R"({{"op":"compdb_generations","profile":{}}})",
|
||||
Util::json_quote(opts.profile_arg));
|
||||
} else if (sub == "compdb_entries") {
|
||||
request_json =
|
||||
std::format(R"({{"op":"compdb_entries","limit":{}{}}})", opts.limit_arg,
|
||||
@@ -2646,6 +2653,11 @@ int main(int argc, char **argv) {
|
||||
daemon_opts.jobs_arg, "0");
|
||||
auto &daemon_compdb_cmd =
|
||||
daemon_cmd.subcommand("compdb", "Editor compilation database served by the daemon");
|
||||
auto &daemon_compdb_generations_cmd = daemon_compdb_cmd.subcommand(
|
||||
"generations", "List immutable editor generations for a profile");
|
||||
configure_daemon_common(daemon_compdb_generations_cmd);
|
||||
daemon_compdb_generations_cmd.option_default("--profile", "-p", "Profile to inspect",
|
||||
daemon_opts.profile_arg, "debug");
|
||||
auto &daemon_compdb_entries_cmd = daemon_compdb_cmd.subcommand(
|
||||
"entries", "Query bounded compile_commands entries for editor extensions");
|
||||
configure_daemon_common(daemon_compdb_entries_cmd);
|
||||
@@ -2839,6 +2851,9 @@ int main(int argc, char **argv) {
|
||||
} else if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
|
||||
parsed.matched_command_path[2] == "configs") {
|
||||
daemon_sub = "ide_configs";
|
||||
} else if (daemon_sub == "compdb" && parsed.matched_command_path.size() > 2 &&
|
||||
parsed.matched_command_path[2] == "generations") {
|
||||
daemon_sub = "compdb_generations";
|
||||
}
|
||||
return execute_daemon_command(daemon_sub, daemon_opts);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user