feat(daemon): scaffold projects on request
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 4s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 9s

This commit is contained in:
faumaray committed 2026-12-04 03:50:09 +03:00
1 parent 2c6b60453a
commit 7945db9497
13 files changed
+365 -130

No files matched your search

+38 -128
View File
@@ -3,6 +3,7 @@
#include <Void/Build/Bridge/CMakeBridge.hpp>
#include <Void/Build/Config/BuildTree.hpp>
#include <Void/Build/Config/Frontend.hpp>
#include <Void/Build/Config/Scaffold.hpp>
#include <Void/Build/Daemon/Daemon.hpp>
#include <Void/Build/Editor/CompDb.hpp>
#include <Void/Build/Execution/Scheduler.hpp>
@@ -1538,6 +1539,11 @@ struct DaemonCmdOptions {
std::optional<std::string> prefix_arg;
std::optional<std::string> input_arg;
std::optional<std::string> output_dir_arg;
std::string init_dir_arg{"."};
std::optional<std::string> init_name_arg;
bool init_library{false};
bool init_with_toml{false};
bool init_force{false};
std::string package_version_arg{"0.1.0"};
std::optional<std::string> file_filter_arg;
std::string limit_arg{"50"};
@@ -1645,6 +1651,13 @@ int execute_daemon_command(std::string_view sub, const DaemonCmdOptions &opts) {
request_json = opts.pack_output_arg ? std::format(R"({{"op":"pack","output":{}}})",
Util::json_quote(*opts.pack_output_arg))
: R"({"op":"pack"})";
} else if (sub == "init") {
request_json = std::format(
R"({{"op":"init","dir":{},"library":{},"with_toml":{},"force":{}{}}})",
Util::json_quote(opts.init_dir_arg), opts.init_library ? "true" : "false",
opts.init_with_toml ? "true" : "false", opts.init_force ? "true" : "false",
opts.init_name_arg ? std::format(",\"name\":{}", Util::json_quote(*opts.init_name_arg))
: std::string{});
} else if (sub == "gen_tests") {
if (!opts.input_arg || !opts.output_dir_arg) {
std::cerr << "vbs daemon gen-tests: --input and --output-dir are required\n";
@@ -1897,136 +1910,22 @@ struct InitCmdOptions {
};
int execute_init_command(const InitCmdOptions &opts) {
std::error_code ec;
const std::filesystem::path target_dir = std::filesystem::path{opts.dir_arg}.lexically_normal();
std::filesystem::create_directories(target_dir, ec);
if (ec) {
std::cerr << "vbs init: cannot create directory '" << target_dir.string()
<< "': " << ec.message() << '\n';
return 1;
Config::ScaffoldOptions options;
options.target_dir = std::filesystem::path{opts.dir_arg};
options.name = opts.name_arg;
options.library = opts.library;
options.with_toml = opts.with_toml;
options.force = opts.force;
auto report = Config::scaffold_project(options);
if (!report) {
std::cerr << report.error().message() << '\n';
return report.error().kind() == Void::Result::ErrorKind::conflict ? 2 : 1;
}
const auto canon = std::filesystem::weakly_canonical(target_dir, ec);
std::string raw_name =
(opts.name_arg && !opts.name_arg->empty()) ? *opts.name_arg : canon.filename().string();
if (raw_name.empty()) {
raw_name = "app";
}
std::string ident;
for (const char c : raw_name) {
if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') ||
c == '_') {
ident.push_back(c);
} else if (c == '-' || c == ' ') {
ident.push_back('_');
}
}
if (ident.empty() || (ident.front() >= '0' && ident.front() <= '9')) {
ident = "vbs_" + ident;
}
const auto build_cpp = target_dir / "build.cpp";
if (!opts.force && std::filesystem::exists(build_cpp, ec)) {
std::cerr << "vbs init: '" << build_cpp.string()
<< "' already exists (pass --force to overwrite)\n";
return 2;
}
auto write_checked = [&](const std::filesystem::path &p, std::string_view content) -> bool {
auto w = Platform::atomic_write_file(p, content, false);
if (!w) {
std::cerr << "vbs init: failed writing '" << p.string() << "': " << w.error().message()
<< '\n';
return false;
}
return true;
};
std::string build_src =
"#include <Void/Build.hpp>\n\nvoid build(Void::Build::Project &project) {\n";
if (opts.with_toml) {
build_src += " project.dependencies_file(\"vbs.deps.toml\");\n\n";
}
if (opts.library) {
build_src += " auto lib = project.static_library(\"" + raw_name +
"\");\n"
" lib.cxx_standard(Void::Build::CxxStandard::Cxx26)\n"
" .sources({\"src/" +
ident +
".cpp\"})\n"
" .include_directory(\"include\", Void::Build::Visibility::Public)\n"
" .install(\"lib\");\n"
" project.install_directory(\"include\", \"include\");\n\n"
" auto t = project.test(\"" +
ident +
"_tests\");\n"
" t.cxx_standard(Void::Build::CxxStandard::Cxx26)\n"
" .sources({\"tests/test_main.cpp\"})\n"
" .link(lib);\n";
} else {
build_src += " auto lib = project.static_library(\"" + ident +
"_lib\");\n"
" lib.cxx_standard(Void::Build::CxxStandard::Cxx26)\n"
" .sources({\"src/" +
ident +
".cpp\"})\n"
" .include_directory(\"include\", Void::Build::Visibility::Public);\n\n"
" auto app = project.executable(\"" +
raw_name +
"\");\n"
" app.cxx_standard(Void::Build::CxxStandard::Cxx26)\n"
" .sources({\"src/main.cpp\"})\n"
" .link(lib)\n"
" .install(\"bin\");\n\n"
" auto t = project.test(\"" +
ident +
"_tests\");\n"
" t.cxx_standard(Void::Build::CxxStandard::Cxx26)\n"
" .sources({\"tests/test_main.cpp\"})\n"
" .link(lib);\n";
}
build_src += "}\n";
if (!write_checked(build_cpp, build_src)) {
return 1;
}
if (!write_checked(target_dir / "include" / (ident + ".hpp"),
"#pragma once\n\nnamespace " + ident + " {\nint answer();\n}\n")) {
return 1;
}
if (!write_checked(target_dir / "src" / (ident + ".cpp"),
"#include \"" + ident + ".hpp\"\n\nnamespace " + ident +
" {\nint answer() { return 42; }\n}\n")) {
return 1;
}
if (!opts.library) {
if (!write_checked(target_dir / "src" / "main.cpp",
"#include \"" + ident +
".hpp\"\n#include <iostream>\n\nint main() {\n std::cout << \"" +
raw_name + ": answer=\" << " + ident +
"::answer() << '\\n';\n return 0;\n}\n")) {
return 1;
}
}
if (!write_checked(target_dir / "tests" / "test_main.cpp",
"#include \"" + ident + ".hpp\"\n\nint main() { return " + ident +
"::answer() == 42 ? 0 : 1; }\n")) {
return 1;
}
if (!write_checked(target_dir / ".gitignore",
"/build/\n/.vbs/\n/compile_commands.json\n.env\n")) {
return 1;
}
if (opts.with_toml) {
if (!write_checked(target_dir / "vbs.deps.toml",
"# Declarative dependency manifest for " + raw_name + "\n")) {
return 1;
}
}
Void::Log::info("Init", "Initialized VBS {} project '{}' in '{}'",
opts.library ? "library" : "application", raw_name, target_dir.string());
std::cout << "initialized " << raw_name << " in " << target_dir.string() << '\n';
opts.library ? "library" : "application", report->project_name,
report->target_dir.string());
std::cout << "initialized " << report->project_name << " in " << report->target_dir.string()
<< '\n';
return 0;
}
@@ -2831,6 +2730,17 @@ int main(int argc, char **argv) {
daemon_install_cmd.option("--prefix", "", "Install prefix (required)", daemon_opts.prefix_arg)
.option_default("--package-version", "", "Exported package version",
daemon_opts.package_version_arg, "0.1.0");
auto &daemon_init_cmd =
daemon_cmd.subcommand("init", "Scaffold a new VBS project inside the project root");
configure_daemon_common(daemon_init_cmd);
daemon_init_cmd
.option_default("--dir", "", "Target directory (relative to the project root)",
daemon_opts.init_dir_arg, ".")
.option("--name", "-n", "Project/target name (defaults to the directory name)",
daemon_opts.init_name_arg)
.flag("--library", "", "Scaffold a static library project", daemon_opts.init_library)
.flag("--with-toml", "", "Include a vbs.deps.toml manifest", daemon_opts.init_with_toml)
.flag("--force", "", "Overwrite an existing build.cpp", daemon_opts.init_force);
auto &daemon_gen_tests_cmd = daemon_cmd.subcommand(
"gen-tests", "Generate a native Void::Testing registration unit via the daemon");
configure_daemon_common(daemon_gen_tests_cmd);