feat(daemon): scaffold projects on request
This commit is contained in:
1 parent
2c6b60453a
commit
7945db9497
13 files changed
+365
-130
No files matched your search
@@ -4,6 +4,7 @@
|
||||
#include <Void/Build/BootstrapConfig.hpp>
|
||||
#include <Void/Build/Config/BuildTree.hpp>
|
||||
#include <Void/Build/Config/Frontend.hpp>
|
||||
#include <Void/Build/Config/Scaffold.hpp>
|
||||
#include <Void/Build/Editor/CompDb.hpp>
|
||||
#include <Void/Build/Execution/Scheduler.hpp>
|
||||
#include <Void/Build/Graph/Graph.hpp>
|
||||
@@ -1885,6 +1886,69 @@ DaemonServer::Impl::DispatchResult DaemonServer::Impl::dispatch(std::string_view
|
||||
if (*name == "ide_configs") {
|
||||
return {ide_configs_project(options), false};
|
||||
}
|
||||
if (*name == "init") {
|
||||
std::string dir_value;
|
||||
if (const auto entry = object->find("dir"); entry != object->end()) {
|
||||
const auto *value = std::get_if<std::string>(&entry->second.value);
|
||||
if (value == nullptr || value->empty() || value->size() > 1024) {
|
||||
return {error_response("\"dir\" must be a bounded non-empty string"), false};
|
||||
}
|
||||
dir_value = *value;
|
||||
}
|
||||
std::error_code ec;
|
||||
const auto project_root = std::filesystem::weakly_canonical(options.project_root, ec);
|
||||
const std::filesystem::path raw_dir =
|
||||
dir_value.empty() ? std::filesystem::path{"."} : std::filesystem::path{dir_value};
|
||||
const auto target_dir = std::filesystem::weakly_canonical(
|
||||
raw_dir.is_absolute() ? raw_dir : project_root / raw_dir, ec);
|
||||
if (ec || !Void::Build::Platform::path_is_within(project_root, target_dir)) {
|
||||
return {error_response("init directory must stay inside the project root"), false};
|
||||
}
|
||||
|
||||
Config::ScaffoldOptions scaffold;
|
||||
scaffold.target_dir = target_dir;
|
||||
scaffold.library = false;
|
||||
scaffold.force = false;
|
||||
const auto read_flag = [&](std::string_view key, bool &out) -> bool {
|
||||
const auto entry = object->find(std::string{key});
|
||||
if (entry == object->end()) {
|
||||
return true;
|
||||
}
|
||||
const auto *value = std::get_if<bool>(&entry->second.value);
|
||||
if (value == nullptr) {
|
||||
return false;
|
||||
}
|
||||
out = *value;
|
||||
return true;
|
||||
};
|
||||
if (!read_flag("library", scaffold.library) ||
|
||||
!read_flag("with_toml", scaffold.with_toml) || !read_flag("force", scaffold.force)) {
|
||||
return {error_response("init flags must be booleans"), false};
|
||||
}
|
||||
if (const auto entry = object->find("name"); entry != object->end()) {
|
||||
const auto *value = std::get_if<std::string>(&entry->second.value);
|
||||
if (value == nullptr || value->empty()) {
|
||||
return {error_response("\"name\" must be a non-empty string"), false};
|
||||
}
|
||||
scaffold.name = *value;
|
||||
}
|
||||
|
||||
auto report = Config::scaffold_project(scaffold);
|
||||
if (!report) {
|
||||
return {error_response(report.error().format()), false};
|
||||
}
|
||||
JsonValue::Array written;
|
||||
for (const auto &path : report->written_files) {
|
||||
written.emplace_back(path.string());
|
||||
}
|
||||
JsonValue::Object object;
|
||||
object.emplace("ok", JsonValue{true});
|
||||
object.emplace("op", JsonValue{std::string{"init"}});
|
||||
object.emplace("dir", JsonValue{report->target_dir.string()});
|
||||
object.emplace("name", JsonValue{report->project_name});
|
||||
object.emplace("written", JsonValue{std::move(written)});
|
||||
return {serialize_response(JsonValue{std::move(object)}), false};
|
||||
}
|
||||
if (*name == "gen_tests") {
|
||||
const auto input_entry = object->find("input");
|
||||
const auto output_entry = object->find("output_dir");
|
||||
|
||||
Reference in new issue
Block a user