feat(ide): export a portable VS Code extension; publish Void/VbsVsCode
This commit is contained in:
1 parent
399b52c31c
commit
12a52bbd0d
9 files changed
+278
-2
No files matched your search
@@ -1150,6 +1150,140 @@ query_compdb_entries(const std::filesystem::path &compdb_path, const CompdbQuery
|
||||
|
||||
namespace {
|
||||
|
||||
Void::Result::Result<IdePluginReport, Void::Result::Error>
|
||||
export_ide_plugin_assets(const IdePluginOptions &options) {
|
||||
using Util::json_array;
|
||||
using Util::json_quote;
|
||||
|
||||
std::error_code ec;
|
||||
const auto output = options.output_dir.lexically_normal();
|
||||
std::filesystem::create_directories(output, ec);
|
||||
if (ec) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::io_error,
|
||||
std::format("cannot create plugin directory '{}': {}", output.string(), ec.message()))};
|
||||
}
|
||||
|
||||
IdePluginReport report;
|
||||
const auto write_asset =
|
||||
[&](const std::filesystem::path &path,
|
||||
std::string_view content) -> Void::Result::Result<void, Void::Result::Error> {
|
||||
if (!options.force && std::filesystem::exists(path, ec)) {
|
||||
report.skipped_existing_files.push_back(path);
|
||||
return {};
|
||||
}
|
||||
auto written = Platform::atomic_write_file(path, content, false);
|
||||
if (!written) {
|
||||
return std::unexpected{written.error()};
|
||||
}
|
||||
report.written_files.push_back(path);
|
||||
return {};
|
||||
};
|
||||
|
||||
const std::string project = options.project_root.lexically_normal().string();
|
||||
const std::string build = options.build_root.lexically_normal().string();
|
||||
|
||||
struct PluginCommand {
|
||||
std::string id;
|
||||
std::string title;
|
||||
std::vector<std::string> args;
|
||||
};
|
||||
const std::vector<PluginCommand> commands = {
|
||||
{"vbs.build", "VBS: Build", {"build"}},
|
||||
{"vbs.test", "VBS: Test", {"test"}},
|
||||
{"vbs.install", "VBS: Install", {"install", "--prefix", "build/install"}},
|
||||
{"vbs.package",
|
||||
"VBS: Package (installer)",
|
||||
{"install", "--prefix", "build/install", "--archive", "build/install/installer.sh",
|
||||
"--archive-format", "sh"}},
|
||||
{"vbs.daemonStart", "VBS: Start Daemon", {"daemon", "run"}},
|
||||
{"vbs.daemonStatus", "VBS: Daemon Status", {"daemon", "status"}},
|
||||
};
|
||||
|
||||
const std::string package_json =
|
||||
"{\n"
|
||||
" \"name\": \"vbs-tools\",\n"
|
||||
" \"displayName\": \"VoidBuildSystem\",\n"
|
||||
" \"description\": \"Build, test, install and package with VoidBuildSystem.\",\n"
|
||||
" \"version\": \"0.1.0\",\n"
|
||||
" \"publisher\": \"void\",\n"
|
||||
" \"license\": \"MIT\",\n"
|
||||
" \"engines\": {\"vscode\": \"^1.80.0\"},\n"
|
||||
" \"categories\": [\"Other\"],\n"
|
||||
" \"activationEvents\": [],\n"
|
||||
" \"main\": \"./extension.js\",\n"
|
||||
" \"contributes\": {\n \"commands\": " +
|
||||
json_array(commands,
|
||||
[](const PluginCommand &command) {
|
||||
return "{\"command\":" + json_quote(command.id) +
|
||||
",\"title\":" + json_quote(command.title) + '}';
|
||||
}) +
|
||||
"\n }\n}\n";
|
||||
|
||||
std::string extension_js =
|
||||
"// Generated by `vbs ide plugin`. Requires the `vbs` executable in PATH or VBS_BIN.\n"
|
||||
"const vscode = require('vscode');\n"
|
||||
"const vbs = process.env.VBS_BIN || \"vbs\";\n\n"
|
||||
"function projectArgs() {\n"
|
||||
" const folders = vscode.workspace.workspaceFolders;\n"
|
||||
" return folders && folders.length ? ['--project', folders[0].uri.fsPath] : [];\n"
|
||||
"}\n\n"
|
||||
"function runTerminal(name, args) {\n"
|
||||
" const terminal = vscode.window.createTerminal({ name });\n"
|
||||
" terminal.show(true);\n"
|
||||
" const full = [...args, ...projectArgs()];\n"
|
||||
" terminal.sendText([vbs, ...full].map((a) => (a.includes(' ') ? `'${a}'` : a)).join(' '));\n"
|
||||
"}\n\n"
|
||||
"const commands = [\n";
|
||||
for (const auto &command : commands) {
|
||||
extension_js += " [" + json_quote(command.id) + ", " + json_quote(command.title) + ", " +
|
||||
json_array(command.args, json_quote) + "],\n";
|
||||
}
|
||||
extension_js += "];\n\n"
|
||||
"function activate(context) {\n"
|
||||
" for (const [id, title, args] of commands) {\n"
|
||||
" context.subscriptions.push(vscode.commands.registerCommand(id, () => "
|
||||
"runTerminal(title, args)));\n"
|
||||
" }\n"
|
||||
"}\n\n"
|
||||
"function deactivate() {}\n\n"
|
||||
"module.exports = { activate, deactivate };\n";
|
||||
|
||||
const std::string readme = std::format(
|
||||
"# VoidBuildSystem editor extension\n\n"
|
||||
"Generated by `vbs ide plugin` for the project at `{}`.\n\n"
|
||||
"## Commands\n\n{}\n\n"
|
||||
"## Install\n\n"
|
||||
"```sh\ncd {} && npm install -g @vscode/vsce && vsce package\n"
|
||||
"code --install-extension vbs-tools-0.1.0.vsix\n```\n\n"
|
||||
"Set `VBS_BIN` if `vbs` is not on `PATH`.\n",
|
||||
project,
|
||||
[&] {
|
||||
std::string list;
|
||||
for (const auto &command : commands) {
|
||||
list += "- `" + command.title + "` (`" + command.id + "`)\n";
|
||||
}
|
||||
return list;
|
||||
}(),
|
||||
output.string());
|
||||
|
||||
if (options.editor != IdeKind::VsCode) {
|
||||
return std::unexpected{Void::System::make_system_error(
|
||||
Void::System::SystemErrorCode::unsupported,
|
||||
"editor plugin assets are currently generated for vscode only")};
|
||||
}
|
||||
|
||||
for (const auto &[name, content] :
|
||||
std::vector<std::pair<std::string, std::string>>{{"package.json", package_json},
|
||||
{"extension.js", extension_js},
|
||||
{"README.md", readme}}) {
|
||||
if (auto written = write_asset(output / name, content); !written) {
|
||||
return std::unexpected{written.error()};
|
||||
}
|
||||
}
|
||||
return report;
|
||||
}
|
||||
|
||||
std::string file_api_target_type(TargetKind kind) {
|
||||
switch (kind) {
|
||||
case TargetKind::Executable:
|
||||
@@ -1390,4 +1524,9 @@ std::string format_ide_query_json(const Graph::BuildGraph &graph,
|
||||
return out;
|
||||
}
|
||||
|
||||
Void::Result::Result<IdePluginReport, Void::Result::Error>
|
||||
generate_ide_plugin_assets(const IdePluginOptions &options) {
|
||||
return export_ide_plugin_assets(options);
|
||||
}
|
||||
|
||||
} // namespace Void::Build::Editor
|
||||
+39
-1
@@ -2676,12 +2676,13 @@ struct IdeCmdOptions {
|
||||
std::string profile_name{"debug"};
|
||||
std::optional<std::string> compiler_arg;
|
||||
std::string editor_name{"all"};
|
||||
std::optional<std::string> plugin_output_arg;
|
||||
bool force{false};
|
||||
};
|
||||
|
||||
int execute_ide_command(std::string_view sub, const IdeCmdOptions &opts) {
|
||||
Editor::IdeKind ide_kind = Editor::IdeKind::All;
|
||||
if (sub == "generate") {
|
||||
if (sub == "generate" || sub == "plugin") {
|
||||
auto parsed_kind = Editor::parse_ide_kind(opts.editor_name);
|
||||
if (!parsed_kind) {
|
||||
std::cerr << "vbs ide: " << parsed_kind.error().message() << '\n';
|
||||
@@ -2690,6 +2691,37 @@ int execute_ide_command(std::string_view sub, const IdeCmdOptions &opts) {
|
||||
ide_kind = *parsed_kind;
|
||||
}
|
||||
|
||||
if (sub == "plugin") {
|
||||
if (!opts.plugin_output_arg || opts.plugin_output_arg->empty()) {
|
||||
std::cerr << "vbs ide plugin: --output is required\n";
|
||||
return 2;
|
||||
}
|
||||
const std::filesystem::path project_root{opts.project_arg};
|
||||
const std::filesystem::path build_root =
|
||||
opts.build_arg ? std::filesystem::path{*opts.build_arg} : (project_root / "build");
|
||||
Editor::IdePluginOptions plugin_opts;
|
||||
plugin_opts.project_root = project_root;
|
||||
plugin_opts.build_root = build_root;
|
||||
plugin_opts.editor = ide_kind;
|
||||
plugin_opts.output_dir = *opts.plugin_output_arg;
|
||||
plugin_opts.force = opts.force;
|
||||
if (auto exe = Platform::current_executable_path(); exe) {
|
||||
plugin_opts.vbs_executable = *exe;
|
||||
}
|
||||
auto report = Editor::generate_ide_plugin_assets(plugin_opts);
|
||||
if (!report) {
|
||||
std::cerr << "vbs ide plugin: " << report.error().message() << '\n';
|
||||
return 1;
|
||||
}
|
||||
for (const auto &written : report->written_files) {
|
||||
std::cout << "wrote " << written.string() << '\n';
|
||||
}
|
||||
for (const auto &skipped : report->skipped_existing_files) {
|
||||
std::cout << "preserved " << skipped.string() << " (pass --force to overwrite)\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::optional<CompilerFamily> compiler_override;
|
||||
if (opts.compiler_arg) {
|
||||
try {
|
||||
@@ -3318,6 +3350,12 @@ int main(int argc, char **argv) {
|
||||
ide_opts.compiler_arg);
|
||||
add_log_flags(p);
|
||||
};
|
||||
auto &ide_plugin_cmd = ide_cmd.subcommand("plugin", "Export editor extension assets (vscode)");
|
||||
configure_ide_common(ide_plugin_cmd);
|
||||
ide_plugin_cmd
|
||||
.option_default("--editor", "-e", "Editor preset (vscode)", ide_opts.editor_name, "vscode")
|
||||
.option("--output", "-o", "Output directory", ide_opts.plugin_output_arg)
|
||||
.flag("--force", "", "Overwrite existing plugin files", ide_opts.force);
|
||||
auto &ide_gen_cmd =
|
||||
ide_cmd.subcommand("generate", "Generate .clangd, .vscode/, .zed/, and .idea/ IDE configs");
|
||||
configure_ide_common(ide_gen_cmd);
|
||||
|
||||
Reference in new issue
Block a user