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
@@ -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
|
||||
@@ -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");
|
||||
|
||||
+38
-128
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user