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

+168
View File
@@ -0,0 +1,168 @@
#include <Void/Build/Config/Scaffold.hpp>
#include <Void/Build/Platform/FileOps.hpp>
#include <Void/System/Types.hpp>
#include <format>
#include <string_view>
namespace Void::Build::Config {
namespace {
using Void::Result::Error;
using Void::Result::Result;
Error make_error(Void::System::SystemErrorCode code, std::string message) {
return Void::System::make_system_error(code, std::move(message));
}
} // namespace
Result<ScaffoldReport, Error> scaffold_project(const ScaffoldOptions &options) {
std::error_code ec;
const std::filesystem::path target_dir = options.target_dir.lexically_normal();
std::filesystem::create_directories(target_dir, ec);
if (ec) {
return std::unexpected{make_error(Void::System::SystemErrorCode::io_error,
std::format("vbs init: cannot create directory '{}': {}",
target_dir.string(), ec.message()))};
}
const auto canon = std::filesystem::weakly_canonical(target_dir, ec);
std::string raw_name =
(options.name && !options.name->empty()) ? *options.name : 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 (!options.force && std::filesystem::exists(build_cpp, ec)) {
return std::unexpected{
make_error(Void::System::SystemErrorCode::already_exists,
std::format("vbs init: '{}' already exists (pass --force to overwrite)",
build_cpp.string()))};
}
ScaffoldReport report;
report.target_dir = target_dir;
report.project_name = raw_name;
report.identifier = ident;
auto write_checked = [&](const std::filesystem::path &path,
std::string_view content) -> Result<void, Error> {
auto written = Platform::atomic_write_file(path, content, false);
if (!written) {
return std::unexpected{
make_error(Void::System::SystemErrorCode::io_error,
std::format("vbs init: failed writing '{}': {}", path.string(),
written.error().message()))};
}
report.written_files.push_back(path);
return {};
};
std::string build_src =
"#include <Void/Build.hpp>\n\nvoid build(Void::Build::Project &project) {\n";
if (options.with_toml) {
build_src += " project.dependencies_file(\"vbs.deps.toml\");\n\n";
}
if (options.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 (auto written = write_checked(build_cpp, build_src); !written) {
return std::unexpected{written.error()};
}
if (auto written =
write_checked(target_dir / "include" / (ident + ".hpp"),
"#pragma once\n\nnamespace " + ident + " {\nint answer();\n}\n");
!written) {
return std::unexpected{written.error()};
}
if (auto written = write_checked(target_dir / "src" / (ident + ".cpp"),
"#include \"" + ident + ".hpp\"\n\nnamespace " + ident +
" {\nint answer() { return 42; }\n}\n");
!written) {
return std::unexpected{written.error()};
}
if (!options.library) {
if (auto written = 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");
!written) {
return std::unexpected{written.error()};
}
}
if (auto written = write_checked(target_dir / "tests" / "test_main.cpp",
"#include \"" + ident + ".hpp\"\n\nint main() { return " +
ident + "::answer() == 42 ? 0 : 1; }\n");
!written) {
return std::unexpected{written.error()};
}
if (auto written = write_checked(target_dir / ".gitignore",
"/build/\n/.vbs/\n/compile_commands.json\n.env\n");
!written) {
return std::unexpected{written.error()};
}
if (options.with_toml) {
if (auto written =
write_checked(target_dir / "vbs.deps.toml",
"# Declarative dependency manifest for " + raw_name + "\n");
!written) {
return std::unexpected{written.error()};
}
}
return report;
}
} // namespace Void::Build::Config