feat(daemon): generate editor configuration files over the Unix socket
This commit is contained in:
1 parent
72f4237af2
commit
e15e2cb2b4
9 files changed
+189
-9
No files matched your search
+81
-1
@@ -609,6 +609,65 @@ std::string compdb_export_project(const DaemonOptions &options, std::string_view
|
||||
return serialize_response(JsonValue{std::move(object)});
|
||||
}
|
||||
|
||||
// Publishes the compilation database and writes the requested editor configuration files.
|
||||
std::string ide_generate_project(const DaemonOptions &options, std::string_view profile_name,
|
||||
std::optional<CompilerFamily> compiler_override,
|
||||
std::string_view editor_name, bool force) {
|
||||
auto kind = Editor::parse_ide_kind(editor_name);
|
||||
if (!kind) {
|
||||
return error_response(kind.error().format());
|
||||
}
|
||||
auto evaluated =
|
||||
evaluate_project_graph(options, profile_name, compiler_override, "ide_generate");
|
||||
if (!evaluated) {
|
||||
return error_response(evaluated.error());
|
||||
}
|
||||
auto graph = lower_graph(options, *evaluated, {}, "ide_generate");
|
||||
if (!graph) {
|
||||
return error_response(graph.error());
|
||||
}
|
||||
auto published = publish_compdb(options, *graph);
|
||||
if (!published) {
|
||||
return error_response(std::format("ide_generate: {}", published.error()));
|
||||
}
|
||||
|
||||
const auto frontend = make_frontend_options(options);
|
||||
Editor::IdeConfigOptions ide;
|
||||
ide.project_root = options.project_root;
|
||||
ide.build_root = options.build_root;
|
||||
ide.sdk_include_dir = frontend.sdk_include_dir;
|
||||
ide.compdb_path = effective_compdb_path(options, *published);
|
||||
ide.editor = *kind;
|
||||
ide.force = force;
|
||||
std::error_code ec;
|
||||
ide.vbs_executable = std::filesystem::read_symlink("/proc/self/exe", ec);
|
||||
if (ec || ide.vbs_executable.empty()) {
|
||||
ide.vbs_executable = "vbs";
|
||||
}
|
||||
|
||||
auto report = Editor::generate_ide_configs(*graph, ide);
|
||||
if (!report) {
|
||||
return error_response(std::format("ide_generate: {}", report.error().format()));
|
||||
}
|
||||
|
||||
JsonValue::Array written;
|
||||
for (const auto &path : report->written_files) {
|
||||
written.emplace_back(path.string());
|
||||
}
|
||||
JsonValue::Array skipped;
|
||||
for (const auto &path : report->skipped_existing_files) {
|
||||
skipped.emplace_back(path.string());
|
||||
}
|
||||
JsonValue::Object object;
|
||||
object.emplace("ok", JsonValue{true});
|
||||
object.emplace("op", JsonValue{std::string{"ide_generate"}});
|
||||
object.emplace("profile", JsonValue{evaluated->context.name});
|
||||
object.emplace("editor", JsonValue{std::string{Editor::ide_kind_name(*kind)}});
|
||||
object.emplace("written", JsonValue{std::move(written)});
|
||||
object.emplace("skipped", JsonValue{std::move(skipped)});
|
||||
return serialize_response(JsonValue{std::move(object)});
|
||||
}
|
||||
|
||||
// Publishes the profile compilation database and returns the IDE query metadata JSON.
|
||||
std::string ide_query_project(const DaemonOptions &options, std::string_view profile_name,
|
||||
std::optional<CompilerFamily> compiler_override) {
|
||||
@@ -796,7 +855,7 @@ DaemonServer::Impl::DispatchResult DaemonServer::Impl::dispatch(std::string_view
|
||||
return {status_response(), false};
|
||||
}
|
||||
if (*name == "describe" || *name == "build" || *name == "ide_query" ||
|
||||
*name == "compdb_export") {
|
||||
*name == "ide_generate" || *name == "compdb_export") {
|
||||
std::string profile_name{"debug"};
|
||||
if (const auto entry = object->find("profile"); entry != object->end()) {
|
||||
const auto *value = std::get_if<std::string>(&entry->second.value);
|
||||
@@ -832,6 +891,27 @@ DaemonServer::Impl::DispatchResult DaemonServer::Impl::dispatch(std::string_view
|
||||
if (*name == "compdb_export") {
|
||||
return {compdb_export_project(options, profile_name, compiler_override), false};
|
||||
}
|
||||
if (*name == "ide_generate") {
|
||||
std::string editor_name{"all"};
|
||||
if (const auto entry = object->find("editor"); entry != object->end()) {
|
||||
const auto *value = std::get_if<std::string>(&entry->second.value);
|
||||
if (value == nullptr || value->empty()) {
|
||||
return {error_response("\"editor\" must be a non-empty string"), false};
|
||||
}
|
||||
editor_name = *value;
|
||||
}
|
||||
bool force = false;
|
||||
if (const auto entry = object->find("force"); entry != object->end()) {
|
||||
const auto *value = std::get_if<bool>(&entry->second.value);
|
||||
if (value == nullptr) {
|
||||
return {error_response("\"force\" must be a boolean"), false};
|
||||
}
|
||||
force = *value;
|
||||
}
|
||||
return {
|
||||
ide_generate_project(options, profile_name, compiler_override, editor_name, force),
|
||||
false};
|
||||
}
|
||||
|
||||
std::vector<std::string> selected_targets;
|
||||
if (const auto entry = object->find("targets"); entry != object->end()) {
|
||||
|
||||
+28
-5
@@ -1364,6 +1364,8 @@ struct DaemonCmdOptions {
|
||||
std::string profile_arg{"debug"};
|
||||
std::vector<std::string> targets;
|
||||
std::string jobs_arg{"0"};
|
||||
std::string editor_arg{"all"};
|
||||
bool force{false};
|
||||
std::string max_bytes_arg{"0"};
|
||||
std::string max_requests_arg{"0"};
|
||||
};
|
||||
@@ -1443,6 +1445,15 @@ int execute_daemon_command(std::string_view sub, const DaemonCmdOptions &opts) {
|
||||
}
|
||||
request_json =
|
||||
std::format(R"({{"op":"describe","profile":{}}})", Util::json_quote(opts.profile_arg));
|
||||
} else if (sub == "ide_generate") {
|
||||
if (opts.profile_arg.empty()) {
|
||||
std::cerr << "vbs daemon ide generate: --profile must not be empty\n";
|
||||
return 2;
|
||||
}
|
||||
request_json =
|
||||
std::format(R"({{"op":"ide_generate","profile":{},"editor":{},"force":{}}})",
|
||||
Util::json_quote(opts.profile_arg), Util::json_quote(opts.editor_arg),
|
||||
opts.force ? "true" : "false");
|
||||
} else if (sub == "compdb_export") {
|
||||
if (opts.profile_arg.empty()) {
|
||||
std::cerr << "vbs daemon compdb export: --profile must not be empty\n";
|
||||
@@ -1480,11 +1491,12 @@ int execute_daemon_command(std::string_view sub, const DaemonCmdOptions &opts) {
|
||||
|
||||
// Describe evaluates the whole project graph (descriptor compile, toolchain probe, lowering),
|
||||
// which can exceed the 2 s default; the daemon bounds evaluation at 10 s.
|
||||
const auto request_timeout = sub == "build" ? std::chrono::milliseconds{std::chrono::hours{1}}
|
||||
: sub == "describe" ? std::chrono::milliseconds{15000}
|
||||
: (sub == "ide_query" || sub == "compdb_export")
|
||||
? std::chrono::milliseconds{30000}
|
||||
: std::chrono::milliseconds{2000};
|
||||
const auto request_timeout =
|
||||
sub == "build" ? std::chrono::milliseconds{std::chrono::hours{1}}
|
||||
: sub == "describe" ? std::chrono::milliseconds{15000}
|
||||
: (sub == "ide_query" || sub == "ide_generate" || sub == "compdb_export")
|
||||
? std::chrono::milliseconds{30000}
|
||||
: std::chrono::milliseconds{2000};
|
||||
auto response = Daemon::request(socket_path, request_json, request_timeout);
|
||||
if (!response) {
|
||||
std::cerr << "vbs daemon: " << response.error().message() << '\n';
|
||||
@@ -2441,6 +2453,14 @@ int main(int argc, char **argv) {
|
||||
configure_daemon_common(daemon_ide_query_cmd);
|
||||
daemon_ide_query_cmd.option_default("--profile", "-p", "Profile to query",
|
||||
daemon_opts.profile_arg, "debug");
|
||||
auto &daemon_ide_generate_cmd =
|
||||
daemon_ide_cmd.subcommand("generate", "Write editor configuration files");
|
||||
configure_daemon_common(daemon_ide_generate_cmd);
|
||||
daemon_ide_generate_cmd
|
||||
.option_default("--profile", "-p", "Profile to evaluate", daemon_opts.profile_arg, "debug")
|
||||
.option_default("--editor", "-e", "Editor preset (all|clangd|vscode|clion|zed)",
|
||||
daemon_opts.editor_arg, "all")
|
||||
.flag("--force", "", "Overwrite existing IDE configuration files", daemon_opts.force);
|
||||
|
||||
const auto parsed = root.parse_detailed(argc, argv);
|
||||
if (parsed.is_help()) {
|
||||
@@ -2537,6 +2557,9 @@ int main(int argc, char **argv) {
|
||||
if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
|
||||
parsed.matched_command_path[2] == "query") {
|
||||
daemon_sub = "ide_query";
|
||||
} else if (daemon_sub == "ide" && parsed.matched_command_path.size() > 2 &&
|
||||
parsed.matched_command_path[2] == "generate") {
|
||||
daemon_sub = "ide_generate";
|
||||
} else if (daemon_sub == "compdb" && parsed.matched_command_path.size() > 2 &&
|
||||
parsed.matched_command_path[2] == "export") {
|
||||
daemon_sub = "compdb_export";
|
||||
|
||||
Reference in new issue
Block a user