You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
104
+
Using the KCL Python plugin requires the presence of `Python 3.7+` in your `PATH` and install the KCL python SDK.
105
105
106
106
```shell
107
-
python3 -m pip install kclvm
108
-
alias kcl-plugin="python3 -m kclvm.tools.plugin"
109
-
export KCL_PLUGINS_ROOT=~/.kcl/plugins
107
+
python3 -m pip kcl_lib
110
108
```
111
109
112
110
### 1. Hello Plugin
113
111
114
-
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.
115
-
116
-
Enter the `kcl-plugin info` command to view the plugin directory (replace `/Users/kcl_user` with the local `$HOME` path):
117
-
118
-
```shell
119
-
$ kcl-plugin info
120
-
# plugin_root: /Users/kcl_user/.kcl/plugins
121
-
```
122
-
123
-
View the list of plugins with the `kcl-plugin list` subcommand:
124
-
125
-
```shell
126
-
$ kcl-plugin list
127
-
hello: hello doc - 0.0.1
128
-
```
129
-
130
-
Where `hello` is an example builtin plugin (do not modify the plugin).
131
-
132
-
In KCL code, the `hello` plugin can be imported via `import kcl_plugin.hello`. `main.k` code is as follows:
112
+
Write the following Python code and add the the plugin named `my_plugin`.
133
113
134
114
```python
135
-
import kcl_plugin.hello
115
+
import kcl_lib.plugin as plugin
116
+
import kcl_lib.api as api
136
117
137
-
name = "kcl"
138
-
three = hello.add(1,2)
139
-
```
118
+
plugin.register_plugin("my_plugin", {"add": lambda x, y: x + y})
140
119
141
-
The output result is
120
+
def main():
121
+
result = api.API().exec_program(
122
+
api.ExecProgram_Args(k_filename_list=["test.k"])
123
+
)
124
+
assert result.yaml_result == "result: 2"
142
125
143
-
```shell
144
-
$ python3 -m kclvm main.k
145
-
name: kcl
146
-
three: 3
126
+
main()
147
127
```
148
128
149
-
### 2. `kcl-plugin` Command
129
+
The content of `test.k` are:
150
130
151
-
`kcl-plugin` is a plugin helper command line tool, the command line help is as follows:
- The `list` subcommand is used to view the list of plugins.
169
-
- The `info` subcommand is used to view the plugin directory and information about each plugin.
170
-
- The `init` subcommand is used to initialize new plugins.
171
-
- The `gendoc` subcommand is used to update the API documentation of all plugins.
172
-
- The `test` subcommand is used to test specified plugins.
137
+
## Use Java to Write Plugins
173
138
174
-
### 3. Plugin Information and Documentation
139
+
### 0. Prerequisites
175
140
176
-
Enter `kcl-plugin info hello` to view the `hello` plugin information:
141
+
Using the KCL Java plugin requires the presence of `Java 8+` in your `PATH` and install the KCL Java SDK.
177
142
178
-
```shell
179
-
$ kcl-plugin info hello
180
-
{
181
-
"name": "hello",
182
-
"describe": "hello doc",
183
-
"long_describe": "long describe",
184
-
"version": "0.0.1",
185
-
"method": {
186
-
"add": "add two numbers, and return result",
187
-
"foo": "no doc",
188
-
"list_append": "no doc",
189
-
"say_hello": "no doc",
190
-
"tolower": "no doc",
191
-
"update_dict": "no doc"
192
-
}
193
-
}
194
-
```
143
+
### 1. Hello Plugin
195
144
196
-
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).
145
+
Write the following Java code and add the the plugin named `my_plugin`.
197
146
198
-
### 4. Plugin Directory Structure
147
+
```java
148
+
package com.kcl;
199
149
200
-
The directory structure of the plugin is as follows (replace `/Users/kcl_user` with the local `$HOME` path):
150
+
import com.kcl.api.API;
151
+
import com.kcl.api.Spec.ExecProgram_Args;
152
+
import com.kcl.api.Spec.ExecProgram_Result;
201
153
202
-
```shell
203
-
$ tree /Users/kcl_user/.kcl/plugins/
204
-
/Users/kcl_user/.kcl/plugins/
205
-
├── _examples
206
-
├── _test
207
-
└── hello
208
-
├── api.md
209
-
├── plugin.py
210
-
└── plugin_test.py
211
-
$
212
-
```
154
+
import java.util.Collections;
213
155
214
-
The `_examples` directory is the sample code of the plugin, the `_test` directory is the KCL test code of the plugin, and the other directories starting with letters are ordinary plugins. The content of the plugin is as follows:
156
+
public class PluginTest {
157
+
public static void main(String[] mainArgs) throws Exception {
Where `INFO` specifies the name of the plugin, a brief description, a detailed description and version information. And all the functions whose names start with letters are the functions provided by the plugin, so the `add` function can be called directly in KCL.
232
-
233
-
> Note: KCL plugins are implemented in an independent pure Python code file, and plugins cannot directly call each other.
234
-
235
-
### 5. Create Plugin
236
-
237
-
A plugin can be created with the `kcl-plugin init` command:
238
-
239
168
```
240
-
$ kcl-plugin init hi
241
-
$ kcl-plugin list
242
-
hello: hello doc - 0.0.1
243
-
hi: hi doc - 0.0.1
244
-
```
245
-
246
-
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.
247
-
248
-
### 6. Remove Plugin
249
169
250
-
KCL plugins are located in the `plugins` subdirectory of KCL (usually installed in the `$HOME/.kcl/plugins` directory).
251
-
We can query the plugin installation directory with the command `kcl-plugin info`.
252
-
253
-
```shell
254
-
$ kcl-plugin info
255
-
/Users/kcl_user/.kcl/plugins/
256
-
$ tree /Users/kcl_user/.kcl/plugins/
257
-
/Users/kcl_user/.kcl/plugins/
258
-
├── _examples
259
-
├── _test
260
-
└── hello -- Delete this directory to delete the hello plugin
261
-
├── api.md
262
-
├── plugin.py
263
-
└── plugin_test.py
264
-
$
265
-
```
170
+
The content of `test.k` are:
266
171
267
-
### 7. Test Plugin
268
-
269
-
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.
270
-
271
-
Unit tests for plugins can be executed via `kcl-plugin test`:
172
+
```python
173
+
import kcl_plugin.my_plugin
272
174
273
-
```shell
274
-
$ kcl-plugin test hello
275
-
============================= test session starts ==============================
276
-
platform darwin -- Python 3.7.6+, pytest-5.3.5, py-1.9.0, pluggy-0.13.1
277
-
rootdir: /Users/kcl_user
278
-
collected 5 items
279
-
.kcl/plugins/hello/plugin_test.py ..... [100%]
280
-
============================== 5 passed in 0.03s ===============================
281
-
$
175
+
result = my_plugin.add(1, 1)
282
176
```
283
-
284
-
Integration tests can be tested by executing the `python3 -m pytest` command in the `_test` directory.
0 commit comments