From 39502c04c7fb6211e4490ced5304093aafb6209b Mon Sep 17 00:00:00 2001 From: Roger-luo Date: Fri, 25 Sep 2020 11:57:59 -0400 Subject: [PATCH] support custom completion script --- src/tools/build.jl | 47 +++++++++++++++++++++++++++++++++++++++------- src/tools/path.jl | 2 ++ 2 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/tools/build.jl b/src/tools/build.jl index a924ce60..52e3f2de 100644 --- a/src/tools/build.jl +++ b/src/tools/build.jl @@ -147,17 +147,33 @@ end function install_completion(m::Module, configs::Configurations.Comonicon) completions_dir = expanduser(joinpath(configs.install.path, "completions")) sh = detect_shell() - sh === nothing && return + @info "SHELL detected: $sh" + if !ispath(completions_dir) + mkpath(completions_dir) + end + completion_file = joinpath(completions_dir, "_" * configs.name) + # 1. check if there are custom completion scripts + custom_completions = PATH.deps(m, "completions") + if ispath(custom_completions) + for each in readdir(custom_completions) + if each == "$sh.completion" + @info "custom completion script found: $each" + cp( + joinpath(custom_completions, each), completion_file; + force=true, + follow_symlinks=true, + ) + return + end + end + end + + # 2. auto generate if nothing found @info "generating auto-completion script for $sh" script = completion_script(sh, m) script === nothing && return - if !ispath(completions_dir) - mkpath(completions_dir) - end - - completion_file = joinpath(completions_dir, "_" * configs.name) @info "writing to $completion_file" write(completion_file, script) return @@ -297,7 +313,24 @@ function build_completion(m::Module, configs::Configurations.Comonicon) mkpath(completion_dir) end - for sh in ["zsh"] + custom_completions = PATH.deps(m, "completions") + shell_with_custom_completion = String[] + if ispath(custom_completions) + for each in readdir(custom_completions) + if endswith(each, ".completion") + @info "custom completion found: $each" + cp( + joinpath(custom_completions, each), joinpath(completion_dir, each); + force=true, + follow_symlinks=true, + ) + push!(shell_with_custom_completion, each[1:end-11]) + end + end + end + + # auto generate supported completions + for sh in setdiff(["zsh"], shell_with_custom_completion) script = completion_script(sh, m) script === nothing && continue write(joinpath(completion_dir, "$sh.completion"), script) diff --git a/src/tools/path.jl b/src/tools/path.jl index 82162d90..4b7ef04f 100644 --- a/src/tools/path.jl +++ b/src/tools/path.jl @@ -13,10 +13,12 @@ function project(m::Module, xs...) end project(xs...) = project(Comonicon, xs...) +deps(m::Module, xs...) = project(m, "deps", xs...) sysimg() = "libcomonicon.$(Libdl.dlext)" sysimg(name) = "lib$name.$(Libdl.dlext)" + """ default_exename()