Skip to content

Commit

Permalink
build(neovim_plugin): replace lemmy-help with gen_vimdoc.lua for help…
Browse files Browse the repository at this point in the history
… file generation (closes #44)
  • Loading branch information
marcuscaisey committed Jun 16, 2024
1 parent d32d526 commit 2e2f105
Show file tree
Hide file tree
Showing 17 changed files with 2,888 additions and 301 deletions.
1 change: 0 additions & 1 deletion .plzconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ preloadsubincludes = ///cc//build_defs:c

[BuildConfig]
nvim-tool = //third_party/neovim:toolchain|nvim
lemmy-help-tool = //third_party/neovim:lemmy_help

[Plugin "go"]
Target = //plugins:go
Expand Down
4 changes: 0 additions & 4 deletions BUILD
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
neovim_plugin(
name = "please",
help_opts = [
"--layout mini",
"--prefix-func",
],
help_srcs = [
"//lua:lua",
"//lua/please:logging",
Expand Down
98 changes: 25 additions & 73 deletions build_defs/neovim.build_defs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# TODO: download neovim, lemmy-help by default if not configured in .plzconfig

subinclude("///shell//build_defs:shell")

def neovim_toolchain(name:str, version:str, hashes:list=[], visibility:list=["PUBLIC"]):
Expand Down Expand Up @@ -56,21 +54,29 @@ def neovim_toolchain(name:str, version:str, hashes:list=[], visibility:list=["PU
building_description = "Installing...",
)

def neovim_plugin(name:str, srcs:list=[], deps:list=[], help_srcs:list=[], help_opts:list=[], visibility:list=["PUBLIC"]):
def neovim_plugin(name:str, srcs:list=[], deps:list=[], help_srcs:list=[], visibility:list=["PUBLIC"]):
"""
Defines two Neovim plugin targets:
Defines a Neovim plugin.

Two targets are generated by this rule:
1. <name> which collects the plugin's files for use by dependent neovim_lua_module and neovim_test targets.
2. <name>_help_gen, a runnable target which generates a help file from Lua srcs using lemmy-help and copies it
into the doc directory.
2. <name>_help_gen_check, a runnable target which checks whether the help file needs to be regenerated. This is
intended to be run in CI.
2. help_gen, a runnable target which generates a help file from Lua srcs and writes it into the doc
directory.

To use this rule you must have nvim-tool configured under the [BuildConfig] section in your .plzconfig.
nvim-tool is the nvim binary used to run the test file. This can either be a path or the |nvim entry point
which is generated by the neovim_toolchain build rule.

For example:

[BuildConfig]
nvim-tool = //third_party/neovim:toolchain|nvim

Args:
name: Name of the build target.
srcs: Sources of the plugin. Optional.
deps: Dependencies of the plugin. These should typically be neovim_lua_module targets. Optional.
help_srcs: Lua sources to generate the help file from. Optional.
help_opts: Options to pass to lemmy-help when generating the help file. Optional.
visibility: Visibility specification. Defaults to public.
"""
runtimepath = package_name() or "."
Expand All @@ -81,45 +87,23 @@ def neovim_plugin(name:str, srcs:list=[], deps:list=[], help_srcs:list=[], help_
)

if help_srcs:
lemmy_help = check_config(
key = "LEMMY_HELP_TOOL",
nvim = check_config(
key = "NVIM_TOOL",
section = "BuildConfig",
rule = "neovim_plugin",
example = "//third_party/neovim:toolchain|nvim",
)

joined_opts = " ".join(help_opts)
help_file_rule = genrule(
name = tag(name, "help"),
srcs = help_srcs,
outs = [f"{name}.txt"],
tools = [lemmy_help],
cmd = f"$TOOL {joined_opts} $SRCS > $OUT",
)

help_file_path = f"doc/{name}.txt"
nvim_path = nvim
data = []
if looks_like_build_label(nvim):
nvim_path = f"$(out_location {nvim})"
data.append(nvim)
help_gen = sh_cmd(
name = "help_gen",
deps = [help_file_rule],
cmd = f"mkdir -p doc && cp $(out_location {help_file_rule}) {help_file_path}",
)

sh_test(
name = "help_gen_test",
src = text_file(
name = "test_script",
content = "\n".join([
f"if ! diff --unified --color=always {help_file_path} $(location {help_file_rule}); then",
f' echo {help_file_path} needs to be regenerated with "plz run //{help_gen}"',
" exit 1",
"fi",
]),
deps = [help_file_rule],
),
data = [
help_file_rule,
help_file_path,
],
srcs = help_srcs,
data = data,
cmd = f"mkdir -p doc && {nvim_path} -l tools/gen_vimdoc/gen_vimdoc.lua {name}.txt $SRCS",
)

return filegroup(
Expand Down Expand Up @@ -322,35 +306,3 @@ cc_shared_object(
],
visibility = visibility,
)

def neovim_lemmy_help(name:str, version:str, hashes:list=[], visibility:list=["PUBLIC"]):
"""
Downloads lemmy-help from GitHub.

To use this rule, add the following to your .plzconfig:

[BuildConfig]
lemmy-help-tool = //...:<name>

Args:
name: Name of the target.
version: The version of lemmy-help to download.
hashes: A list of possible hashes for the downloaded archive. Optional.
visibility: Visibility specification. Defaults to public.
"""
urls_by_os = {
"darwin": f"https://github.com/numToStr/lemmy-help/releases/download/v{version}/lemmy-help-x86_64-apple-darwin.tar.gz",
"linux": f"https://github.com/numToStr/lemmy-help/releases/download/v{version}/lemmy-help-x86_64-unknown-linux-gnu.tar.gz",
}
if CONFIG.HOSTOS not in urls_by_os:
fail(f'neovim_lemmy_help rule is not supported on OS: "{CONFIG.HOSTOS}"')

return remote_file(
name = name,
url = urls_by_os[CONFIG.HOSTOS],
extract = True,
exported_files = ["lemmy-help"],
binary = True,
hashes = hashes,
visibility = visibility,
)
Loading

0 comments on commit 2e2f105

Please sign in to comment.