Skip to content

Commit 8d5c6b1

Browse files
committed
refactor: plugin documents and go plugin documents
Signed-off-by: peefy <xpf6677@163.com>
1 parent c804ba6 commit 8d5c6b1

File tree

4 files changed

+200
-136
lines changed

4 files changed

+200
-136
lines changed

docs/reference/plugin/overview.md

Lines changed: 100 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,102 @@ sidebar_position: 1
44

55
# Introduction
66

7-
KCL provides plugin support through a plugin agent and auxiliary command line tools, and the KCL plugin framework supports different general-purpose languages to develop plugins. Here we take Python as an example to briefly explain the use of plugins.
7+
KCL provides plugin support through a plugin agent and auxiliary command line tools, and the KCL plugin framework supports different general-purpose languages to develop plugins. The KCL plugin framework currently supports the development of plugins in Python and Go languages.
88

99
KCL plugin Git repository: [https://github.com/kcl-lang/kcl-plugin](https://github.com/kcl-lang/kcl-plugin)
1010

11-
## 0. Prerequisites
11+
## Use Go to Write Plugins
12+
13+
### 0. Prerequisites
14+
15+
Using the KCL Go plugin requires the presence of `Go 1.21+` in your `PATH` and add the dependency of KCL Go SDK
16+
17+
### 1. Hello Plugin
18+
19+
Write the following Go code and add the dependency of the hello plugin
20+
21+
```go
22+
package main
23+
24+
import (
25+
"fmt"
26+
27+
"kcl-lang.io/kcl-go/pkg/kcl"
28+
"kcl-lang.io/kcl-go/pkg/native" // Import the native API
29+
_ "kcl-lang.io/kcl-go/pkg/plugin/hello_plugin" // Import the hello plugin
30+
)
31+
32+
func main() {
33+
// Note we use `native.MustRun` here instead of `kcl.MustRun`, because it needs the cgo feature.
34+
yaml := native.MustRun("main.k", kcl.WithCode(code)).GetRawYamlResult()
35+
fmt.Println(yaml)
36+
}
37+
38+
const code = `
39+
import kcl_plugin.hello
40+
41+
name = "kcl"
42+
three = hello.add(1,2) # hello.add is written by Go
43+
`
44+
```
45+
46+
In KCL code, the `hello` plugin can be imported via `import kcl_plugin.hello`. The output result is
47+
48+
```yaml
49+
name: kcl
50+
three: 3
51+
```
52+
53+
### 2. Plugin Directory Structure
54+
55+
The KCL Go plugin is essentially a simple Go project, mainly containing the Go file `api.go` for the plugin code, which defines the registration and implementation functions of the plugin.
56+
57+
```go
58+
package hello_plugin
59+
60+
import (
61+
"kcl-lang.io/kcl-go/pkg/plugin"
62+
)
63+
64+
func init() {
65+
plugin.RegisterPlugin(plugin.Plugin{
66+
Name: "hello",
67+
MethodMap: map[string]plugin.MethodSpec{
68+
"add": {
69+
Body: func(args *plugin.MethodArgs) (*plugin.MethodResult, error) {
70+
v := args.IntArg(0) + args.IntArg(1)
71+
return &plugin.MethodResult{V: v}, nil
72+
},
73+
},
74+
},
75+
})
76+
}
77+
```
78+
79+
### 3. Test Plugin
80+
81+
Write a file called `api_test.go` to perform unit testing on plugin functions.
82+
83+
```go
84+
package hello_plugin
85+
86+
import (
87+
"testing"
88+
89+
"kcl-lang.io/kcl-go/pkg/plugin"
90+
)
91+
92+
func TestPluginAdd(t *testing.T) {
93+
result_json := plugin.Invoke("kcl_plugin.hello.add", []interface{}{111, 22}, nil)
94+
if result_json != "133" {
95+
t.Fatal(result_json)
96+
}
97+
}
98+
```
99+
100+
## Use Python to Write Plugins
101+
102+
### 0. Prerequisites
12103

13104
Using the KCL Python plugin requires the presence of `Python 3.7+` in your `PATH`, install the KCL python SDK and set the plugin path.
14105

@@ -18,7 +109,7 @@ alias kcl-plugin="python3 -m kclvm.tools.plugin"
18109
export KCL_PLUGINS_ROOT=~/.kcl/plugins
19110
```
20111

21-
## 1. Hello Plugin
112+
### 1. Hello Plugin
22113

23114
KCL plugins are installed in the `plugins` subdirectory of KCL (usually installed in the `$HOME/.kcl/plugins` directory), or set through the `$KCL_PLUGINS_ROOT` environment variable. Besides, the `plugins` directory could also be placed at the `pwd` path. KCL plugins are managed in the Git repository: [https://github.com/kcl-lang/kcl-plugin](https://github.com/kcl-lang/kcl-plugin), we can clone the repository for development.
24115

@@ -55,7 +146,7 @@ name: kcl
55146
three: 3
56147
```
57148

58-
## 2. `kcl-plugin` Command
149+
### 2. `kcl-plugin` Command
59150

60151
`kcl-plugin` is a plugin helper command line tool, the command line help is as follows:
61152

@@ -80,7 +171,7 @@ optional arguments:
80171
- The `gendoc` subcommand is used to update the API documentation of all plugins.
81172
- The `test` subcommand is used to test specified plugins.
82173

83-
## 3. Plugin Information and Documentation
174+
### 3. Plugin Information and Documentation
84175

85176
Enter `kcl-plugin info hello` to view the `hello` plugin information:
86177

@@ -104,7 +195,7 @@ $ kcl-plugin info hello
104195

105196
The information of the plugin mainly includes the name and version information of the plugin, and the function information provided by the plugin. This information is consistent with the automatically generated `api.md` file in the plugin directory (regenerate the `api.md` file for all plugins via `kcl-plugin gendoc` when the plugin API document changes).
106197

107-
## 4. Plugin Directory Structure
198+
### 4. Plugin Directory Structure
108199

109200
The directory structure of the plugin is as follows (replace `/Users/kcl_user` with the local `$HOME` path):
110201

@@ -141,7 +232,7 @@ Where `INFO` specifies the name of the plugin, a brief description, a detailed d
141232

142233
> Note: KCL plugins are implemented in an independent pure Python code file, and plugins cannot directly call each other.
143234

144-
## 5. Create Plugin
235+
### 5. Create Plugin
145236

146237
A plugin can be created with the `kcl-plugin init` command:
147238

@@ -154,7 +245,7 @@ hi: hi doc - 0.0.1
154245

155246
The `kcl-plugin init` command will construct a new plugin from the built-in template, and then we can view the created plugin information with the `kcl-plugin list` command.
156247

157-
## 6. Remove Plugin
248+
### 6. Remove Plugin
158249

159250
KCL plugins are located in the `plugins` subdirectory of KCL (usually installed in the `$HOME/.kcl/plugins` directory).
160251
We can query the plugin installation directory with the command `kcl-plugin info`.
@@ -173,7 +264,7 @@ $ tree /Users/kcl_user/.kcl/plugins/
173264
$
174265
```
175266

176-
## 7. Test Plugin
267+
### 7. Test Plugin
177268

178269
There is a `plugin_test.py` file in the plugin directory, which is the unit test file of the plugin (based on the `pytest` testing framework). Also placed in the `_test` directory are plugin integration tests for KCL files. The `plugin_test.py` unit test is required, and the KCL integration tests in the `_test` directory can be added as needed.
179270

docs/reference/plugin/project_context.md

Lines changed: 0 additions & 59 deletions
This file was deleted.

i18n/zh-CN/docusaurus-plugin-content-docs/current/reference/plugin/overview.md

Lines changed: 100 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,102 @@ sidebar_position: 1
44

55
# 简介
66

7-
KCL 是声明式配置策略语言,对于不方便通过配置直接描述的复杂的业务逻辑可以通过通用的编程语言开发 KCL 插件对语言进行扩展。KCL 支持通过通用语言开发插件,KCL 程序导入插件中的函数。KCL 通过插件运行时和辅助的命令行工具提供插件支持。KCL 插件框架支持多种不同的通用语言开发插件,这里我们以 Python 为例简单说明插件的使用
7+
KCL 是声明式配置策略语言,对于不方便通过配置直接描述的复杂的业务逻辑可以通过通用的编程语言开发 KCL 插件对语言进行扩展。KCL 支持通过通用语言开发插件,KCL 程序导入插件中的函数。KCL 通过插件运行时和辅助的命令行工具提供插件支持。KCL 插件框架目前支持 Python 语言和 Go 语言开发插件
88

99
插件的 Git 仓库: [https://github.com/kcl-lang/kcl-plugin](https://github.com/kcl-lang/kcl-plugin)
1010

11-
## 0. 前置依赖
11+
## 使用 Go 编写插件
12+
13+
### 0. 前置依赖
14+
15+
使用 KCL Go 插件需要您的 `PATH` 中存在 `Go 1.21+` 并在 Go 代码中添加 KCL Go SDK 的依赖
16+
17+
### 1. 你好插件
18+
19+
编写如下 Go 代码并添加你好插件的依赖
20+
21+
```go
22+
package main
23+
24+
import (
25+
"fmt"
26+
27+
"kcl-lang.io/kcl-go/pkg/kcl"
28+
"kcl-lang.io/kcl-go/pkg/native" // Import the native API
29+
_ "kcl-lang.io/kcl-go/pkg/plugin/hello_plugin" // Import the hello plugin
30+
)
31+
32+
func main() {
33+
// Note we use `native.MustRun` here instead of `kcl.MustRun`, because it needs the cgo feature.
34+
yaml := native.MustRun("main.k", kcl.WithCode(code)).GetRawYamlResult()
35+
fmt.Println(yaml)
36+
}
37+
38+
const code = `
39+
import kcl_plugin.hello
40+
41+
name = "kcl"
42+
three = hello.add(1,2) # hello.add is written by Go
43+
`
44+
```
45+
46+
在 KCL 代码中,可以通过 `kcl_plugin.hello` 导入 `hello` 插件。运行上述代码可以得到如下输出结果
47+
48+
```yaml
49+
name: kcl
50+
three: 3
51+
```
52+
53+
### 2. 插件结构
54+
55+
KCL Go 插件本质上是一个简单的 Go 工程,主要包含插件代码的 Go 文件 `api.go`,其中定义了插件的注册以及实现函数
56+
57+
```go
58+
package hello_plugin
59+
60+
import (
61+
"kcl-lang.io/kcl-go/pkg/plugin"
62+
)
63+
64+
func init() {
65+
plugin.RegisterPlugin(plugin.Plugin{
66+
Name: "hello",
67+
MethodMap: map[string]plugin.MethodSpec{
68+
"add": {
69+
Body: func(args *plugin.MethodArgs) (*plugin.MethodResult, error) {
70+
v := args.IntArg(0) + args.IntArg(1)
71+
return &plugin.MethodResult{V: v}, nil
72+
},
73+
},
74+
},
75+
})
76+
}
77+
```
78+
79+
### 3. 插件测试
80+
81+
编写 `api_test.go` 文件对插件函数进行单元测试
82+
83+
```go
84+
package hello_plugin
85+
86+
import (
87+
"testing"
88+
89+
"kcl-lang.io/kcl-go/pkg/plugin"
90+
)
91+
92+
func TestPluginAdd(t *testing.T) {
93+
result_json := plugin.Invoke("kcl_plugin.hello.add", []interface{}{111, 22}, nil)
94+
if result_json != "133" {
95+
t.Fatal(result_json)
96+
}
97+
}
98+
```
99+
100+
## 使用 Python 编写插件
101+
102+
### 0. 前置依赖
12103

13104
使用 KCL Python 插件需要您的 `PATH` 中存在 `Python3.7+` 并安装 KCL Python SDK, 设置插件存放路径。
14105

@@ -18,7 +109,7 @@ alias kcl-plugin="python3 -m kclvm.tools.plugin"
18109
export KCL_PLUGINS_ROOT=~/.kcl/plugins
19110
```
20111

21-
## 1. 你好插件
112+
### 1. 你好插件
22113

23114
KCL 插件在 KCL 的 `plugins` 子目录(通常安装在 `$HOME/.kcl/plugins` 目录),或者通过 `$KCL_PLUGINS_ROOT` 环境变量设置(环境变量优先级更高)。此外,`plugins` 插件目录还可以放在执行 KCL 命令的 `pwd` 路径或者父路径中。对于插件开发人员,插件都在 Git 仓库管理: [https://github.com/kcl-lang/kcl-plugin](https://github.com/kcl-lang/kcl-plugin) ,可以将插件仓库克隆到该目录进行开发。
24115

@@ -55,7 +146,7 @@ name: kcl
55146
three: 3
56147
```
57148

58-
## 2. `kcl-plugin` 辅助命令
149+
### 2. `kcl-plugin` 辅助命令
59150

60151
`kcl-plugin` 是提供的插件辅助工具,命令行帮助如下:
61152

@@ -76,7 +167,7 @@ optional arguments:
76167

77168
其中 `list` 子命令用于查看插件列表;`info` 用户查看插件目录和每个插件的信息;`init` 可以用户初始化新插件;`gendoc` 更新全部插件的 API 文档;`test` 测试指定的插件。
78169

79-
## 3. 插件信息和文档
170+
### 3. 插件信息和文档
80171

81172
输入 `kcl-plugin info hello` 查看 `hello` 插件信息:
82173

@@ -100,7 +191,7 @@ $ kcl-plugin info hello
100191

101192
插件的信息主要包含插件的名字和版本信息,插件提供的函数信息。该信息和插件目录中自动生成的 `api.md` 文件是一致的(插件 API 变化时通过 `kcl-plugin gendoc` 为全部的插件重新生成 `api.md` 文件)。
102193

103-
## 4. 插件的目录结构
194+
### 4. 插件的目录结构
104195

105196
插件的目录结构如下(将其中的 `/Users/kcl_user` 替换成本地的 `$HOME` 路径):
106197

@@ -137,7 +228,7 @@ def add(a: int, b: int) -> int:
137228

138229
其中 `INFO` 指明了插件的名字、概要说明、详细说明和版本信息。而所有名字以字母开头的函数是插件给 KCL 提供的函数,因此 KCL 中可以直接调用 `add` 函数。
139230

140-
## 5. 创建一个插件
231+
### 5. 创建一个插件
141232

142233
通过 `kcl-plugin init` 命令可以创建一个插件示例:
143234

@@ -150,7 +241,7 @@ hi: hi doc - 0.0.1
150241
151242
`kcl-plugin init` 命令会以内置的模板构造一个新的插件,然后通过 `kcl-plugin list` 命令可以查看到新创建的插件。
152243
153-
## 6. 插件的删除
244+
### 6. 插件的删除
154245
155246
KCL 插件在 KCL 的 `plugins` 子目录(通常安装在 `$HOME/.kcl/plugins` 目录)。
156247
可以通过命令 `kcl-plugin info` 查询插件安装目录。
@@ -169,7 +260,7 @@ $ tree /Users/kcl_user/.kcl/plugins/
169260
$
170261
```
171262

172-
## 7. 插件的测试
263+
### 7. 插件的测试
173264

174265
插件是独立的纯 Python 文件实现,插件目录下有个 `plugin_test.py` 文件是插件的单元测试文件(基于 pytest 测试框架)。此外在 `_test` 目录下放置的是 KCL 文件的插件集成测试。`plugin_test.py` 单元测试是必须的,`_test` 目录下的 KCL 集成测试可以根据情况添加。
175266

0 commit comments

Comments
 (0)