Repository: comonicon/Comonicon.jl
Pull request #172 (2022-02-03 10:04:13)

support plugin

codecov[bot]: # [Codecov](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#172](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (1293109) into [master](https://codecov.io/gh/comonicon/Comonicon.jl/commit/a2d05526a888099732c861c2ee0d0a30140f41c5?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (a2d0552) will **decrease** coverage by `28.92%`.
> The diff coverage is `0.00%`.

[![Impacted file tree graph](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172/graphs/tree.svg?width=650&height=150&src=pr&token=zZjCxCiFTY&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)

```diff
@@            Coverage Diff             @@
##           master    #172       +/-   ##
==========================================
- Coverage   30.21%   1.28%   -28.93%     
==========================================
  Files          18      18               
  Lines        1241    1242        +1     
==========================================
- Hits          375      16      -359     
- Misses        866    1226      +360     
```


| [Impacted Files](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [src/codegen/julia.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2NvZGVnZW4vanVsaWEuamw=) | `0.00% <0.00%> (-20.00%)` | :arrow_down: |
| [src/configs.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2NvbmZpZ3Muamw=) | `0.00% <ø> (-70.67%)` | :arrow_down: |
| [src/builder/app.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2J1aWxkZXIvYXBwLmps) | `0.00% <0.00%> (-100.00%)` | :arrow_down: |
| [src/builder/sysimg.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2J1aWxkZXIvc3lzaW1nLmps) | `0.00% <0.00%> (-83.08%)` | :arrow_down: |
| [src/codegen/zsh.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2NvZGVnZW4venNoLmps) | `0.00% <0.00%> (-80.36%)` | :arrow_down: |
| [src/builder/cli.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2J1aWxkZXIvY2xpLmps) | `0.00% <0.00%> (-79.42%)` | :arrow_down: |
| [src/builder/install.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2J1aWxkZXIvaW5zdGFsbC5qbA==) | `0.00% <0.00%> (-78.27%)` | :arrow_down: |
| [src/builder/tarball.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2J1aWxkZXIvdGFyYmFsbC5qbA==) | `0.00% <0.00%> (-57.15%)` | :arrow_down: |
| [src/frontend/utils.jl](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-c3JjL2Zyb250ZW5kL3V0aWxzLmps) | `0.00% <0.00%> (-34.79%)` | :arrow_down: |
| ... and [2 more](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Last update [a2d0552...1293109](https://codecov.io/gh/comonicon/Comonicon.jl/pull/172?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None).


Patch:
diff --git a/src/codegen/julia.jl b/src/codegen/julia.jl
index 646a5e1..4bacec7 100644
--- a/src/codegen/julia.jl
+++ b/src/codegen/julia.jl
@@ -9,7 +9,8 @@ help_str(x; color = true) = sprint(print_cmd, x; context = :color => color)
 Base.@kwdef struct Configs
     color::Bool = true
     static::Bool = true
-    dash::Bool = false
+    dash::Bool = true
+    plugin::Bool = false
 end
 
 function print_help_str(x, configs::Configs)
@@ -73,6 +74,7 @@ function emit(cmd::Entry, configs::Configs=Configs(), ptr::Int = 1)
         args = [Expr(:kw, :(ARGS::Vector{String}), :ARGS)],
         body = quote
             $(emit_scan_version(cmd))
+            $(configs.plugin ? emit_plugin_lookup(cmd) : nothing)
             $(emit_body(cmd.root, configs, ptr))
         end,
     )
@@ -88,6 +90,19 @@ function emit_scan_version(cmd::Entry)
     end
 end
 
+function emit_plugin_lookup(cmd::Entry)
+    quote
+        if length(ARGS) ≥ 1
+            path = Sys.which(cmd.root.name * "-" * ARGS[1])
+            path === nothing && @goto no_plugin_found
+
+            p = run(ignorestatus(`$path $(ARGS[2:end])`))
+            return p.exitcode
+        end
+        @label no_plugin_found
+    end
+end
+
 function emit_body(cmd::NodeCommand, configs::Configs, ptr::Int = 1)
     nargs_assert = quote
         if length(ARGS) < $ptr
diff --git a/src/configs.jl b/src/configs.jl
index 4b60d9a..4b7a67d 100644
--- a/src/configs.jl
+++ b/src/configs.jl
@@ -176,15 +176,18 @@ Configs for Command execution.
 
 # Keywords
 
-- `color`: whether print with color in help page.
-- `static`: whether genrate help info at compile time
-    (the format won't be adaptive to displaysize anymore)
-- `dash`: whether parse `--` seperator.
+- `color`: whether print with color in help page, default is `true`.
+- `static`: whether genrate help info at compile time,
+    the format won't be adaptive to displaysize anymore,
+    if `true`, default is `true`.
+- `dash`: whether parse `--` seperator, default is `true`.
+- `plugin`: parse `<main CLI name>-<plugin>` for CLI plugin, default is `false`.
 """
 @option struct Command
     color::Bool = true
     static::Bool = true
     dash::Bool = true
+    plugin::Bool = false
 end
 
 """


Test patch:
diff --git a/test/codegen/codegen.jl b/test/codegen/codegen.jl
index 47147a7..7f3861f 100644
--- a/test/codegen/codegen.jl
+++ b/test/codegen/codegen.jl
@@ -8,6 +8,7 @@ using Test
     include("julia/options.jl")
     include("julia/vararg.jl")
     include("julia/exception.jl")
+    include("julia/plugin.jl")
 end
 
 @testset "ZSHCompletions.emit" begin
diff --git a/test/codegen/julia/node-cmd3 b/test/codegen/julia/node-cmd3
new file mode 100755
index 0000000..976e75b
--- /dev/null
+++ b/test/codegen/julia/node-cmd3
@@ -0,0 +1,3 @@
+#!/usr/bin/sh
+
+echo $@
\ No newline at end of file
diff --git a/test/codegen/julia/plugin.jl b/test/codegen/julia/plugin.jl
new file mode 100644
index 0000000..8b8b2ac
--- /dev/null
+++ b/test/codegen/julia/plugin.jl
@@ -0,0 +1,31 @@
+module TestPlugin
+
+using Test
+using Comonicon.AST
+using Comonicon.JuliaExpr
+using Comonicon.JuliaExpr: emit, emit_body, emit_norm_body, emit_dash_body
+
+function called()
+    @test false
+end
+
+
+cmd = Entry(;
+    version = v"1.2.0",
+    root = NodeCommand(;
+        name = "node",
+        subcmds = Dict(
+            "cmd1" => LeafCommand(; fn = called, name = "cmd1"),
+            "cmd2" => LeafCommand(; fn = called, name = "cmd2"),
+        ),
+    ),
+)
+
+eval(emit(cmd, JuliaExpr.Configs(plugin=true)))
+end
+
+using Comonicon
+withenv("PATH"=>"$(pkgdir(Comonicon, "test", "codegen", "julia")):$(ENV["PATH"])") do
+    Sys.which("node-cmd3")
+    @test TestPlugin.command_main(["cmd3"]) == 0
+end