Skip to content

Commit ef32e25

Browse files
authored
新增python的FIT插件开发CLI工具 (#278)
* cli init * 修改init_cmd * 修改变量为${xxx}格式等 * 删除多余空行 * 删除多余空行
1 parent 30a311d commit ef32e25

File tree

6 files changed

+101
-0
lines changed

6 files changed

+101
-0
lines changed

framework/fit/python/fit_cli/__init__.py

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from .main import main
2+
3+
if __name__ == "__main__":
4+
main()

framework/fit/python/fit_cli/commands/__init__.py

Whitespace-only changes.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
from pathlib import Path
2+
3+
TEMPLATE_PLUGIN = '''from fitframework.api.decorators import fitable # 引入 Fit for Python 框架核心接口
4+
5+
# - 修改 ${genericable_id} / ${fitable_id} 为唯一的 ID
6+
# - 建议和插件功能相关,并且 GenericableId 必须全局唯一,通用的全局唯一方式可以采用域名的方式,例如:com.A.B
7+
@fitable("${genericable_id}", "${fitable_id}") # 指定可供调用函数的 genericable id 和 fitable id
8+
def hello(name: str) -> str: # 定义可供调用的函数,特别注意需要提供函数类型签名,可参照文档
9+
# - https://github.com/ModelEngine-Group/fit-framework/blob/main/docs/framework/fit/overview/00.%20%E6%A6%82%E8%BF%B0.md
10+
# - https://github.com/ModelEngine-Group/fit-framework/blob/main/docs/framework/fit/python/%E8%A2%AB%E6%B3%A8%E8%A7%A3%E5%87%BD%E6%95%B0%E7%AD%BE%E5%90%8D%E8%A7%84%E8%8C%83.md
11+
"""
12+
一个简单的 FIT 插件示例函数
13+
14+
修改函数名和参数
15+
- 函数名(hello)应根据功能调整,例如 concat, multiply
16+
- 参数(name: str)可以增加多个,类型也可以是 int, float 等
17+
"""
18+
19+
return f"Hello, {name}!" # 提供函数实现逻辑
20+
21+
# 关于插件开发其他内容可参考官方文档:https://github.com/ModelEngine-Group/fit-framework/tree/main/docs/framework/fit/python
22+
'''
23+
24+
def create_directory(path: Path):
25+
"""创建目录(如果不存在)"""
26+
if not path.exists():
27+
path.mkdir(parents=True)
28+
return path
29+
30+
31+
def create_file(path: Path, content: str = "", overwrite: bool = False):
32+
"""创建文件,支持写入内容"""
33+
if path.exists() and not overwrite:
34+
print(f"⚠️ 文件 {path} 已存在,未覆盖。")
35+
return
36+
path.write_text(content, encoding="utf-8") if content else path.touch()
37+
38+
39+
def generate_plugin_structure(plugin_name: str):
40+
"""生成插件目录和文件结构"""
41+
base_dir = Path("plugin") / plugin_name
42+
src_dir = base_dir / "src"
43+
44+
# 创建目录
45+
create_directory(base_dir)
46+
create_directory(src_dir)
47+
48+
# 创建 __init__.py
49+
init_file = src_dir / "__init__.py"
50+
create_file(init_file)
51+
52+
# 创建 plugin.py
53+
plugin_file = src_dir / "plugin.py"
54+
create_file(plugin_file, TEMPLATE_PLUGIN)
55+
56+
print(f"✅ 已创建目录 {base_dir} ")
57+
58+
59+
def run(args):
60+
"""命令入口"""
61+
generate_plugin_structure(args.name)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import argparse
2+
from fit_cli.commands import init_cmd
3+
4+
def main():
5+
parser = argparse.ArgumentParser(description="FIT Framework CLI 插件开发工具")
6+
subparsers = parser.add_subparsers(dest="command")
7+
8+
# init
9+
parser_init = subparsers.add_parser("init", help="生成插件模板")
10+
parser_init.add_argument("name", help="插件目录名称")
11+
parser_init.set_defaults(func=init_cmd.run)
12+
13+
args = parser.parse_args()
14+
15+
if hasattr(args, "func"):
16+
args.func(args)
17+
else:
18+
parser.print_help()
19+
20+
21+
if __name__ == "__main__":
22+
main()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# FIT CLI 工具
2+
3+
FIT CLI 工具是基于 **FIT Framework** 的命令行开发工具,提供插件初始化、打包、发布等功能,帮助用户快速开发和管理 FIT 插件。
4+
5+
---
6+
7+
## 使用方式
8+
9+
以 framework/fit/python 为项目根目录,运行:
10+
11+
```bash
12+
python -m fit_cli init %{your_plugin_name}
13+
```
14+
将会在 plugin 目录中创建 %{your_plugin_name} 目录,并生成插件模板。

0 commit comments

Comments
 (0)