Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions framework/fit/python/fit_cli/commands/build_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import json
import shutil
from pathlib import Path
from fit_cli.utils.build import calculate_checksum, parse_python_file

def generate_tools_json(base_dir: Path, plugin_name: str):
"""生成 tools.json"""
src_dir = base_dir / plugin_name / "src"
if not src_dir.exists():
print(f"❌ 未找到插件目录 {src_dir}")
return None

tools_json = {
"version": "1.0.0",
"definitionGroups": [],
"toolGroups": []
}
# 遍历src目录下的所有.py文件
for py_file in src_dir.glob("**/*.py"):
# 跳过__init__.py文件
if py_file.name == "__init__.py":
continue
# 解析 Python 文件
definition_group, tool_groups = parse_python_file(py_file)
if definition_group is not None:
tools_json["definitionGroups"].append(definition_group)
if len(tool_groups) > 0:
tools_json["toolGroups"].extend(tool_groups)

path = base_dir / "tools.json"
path.write_text(json.dumps(tools_json, indent=2, ensure_ascii=False), encoding="utf-8")
print(f"✅ 已生成 {path}")
return tools_json


def generate_plugin_json(base_dir: Path, plugin_name: str):
"""生成 plugin.json"""
tar_path = base_dir / f"{plugin_name}.tar"
if not tar_path.exists():
print(f"❌ TAR 文件 {tar_path} 不存在,请先打包源代码")
return None
# 计算 TAR 文件的 SHA256
checksum = calculate_checksum(tar_path)
plugin_json = {
"checksum": checksum,
"name": plugin_name,
"description": f"{plugin_name} 插件",
"type": "python",
"uniqueness": {
"name": plugin_name
}
}
path = base_dir / "plugin.json"
path.write_text(json.dumps(plugin_json, indent=2, ensure_ascii=False), encoding="utf-8")
print(f"✅ 已生成 {path}")
return plugin_json


def make_plugin_tar(base_dir: Path, plugin_name: str):
"""打包源代码为 tar 格式"""
tar_path = base_dir / f"{plugin_name}.tar"
plugin_dir = base_dir / plugin_name

shutil.make_archive(str(tar_path.with_suffix("")), "tar", plugin_dir)
print(f"✅ 已生成打包文件 {tar_path}")


def run(args):
"""build 命令入口"""
base_dir = Path("plugin") / args.name
plugin_name = args.name

if not base_dir.exists():
print(f"❌ 插件目录 {base_dir} 不存在,请先运行 fit_cli init {args.name}")
return

# 打包源代码
make_plugin_tar(base_dir, plugin_name)

# 生成 JSON
generate_tools_json(base_dir, plugin_name)
generate_plugin_json(base_dir, plugin_name)
2 changes: 1 addition & 1 deletion framework/fit/python/fit_cli/commands/init_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def create_file(path: Path, content: str = "", overwrite: bool = False):
def generate_plugin_structure(plugin_name: str):
"""生成插件目录和文件结构"""
base_dir = Path("plugin") / plugin_name
src_dir = base_dir / "src"
src_dir = base_dir / plugin_name / "src"

# 创建目录
create_directory(base_dir)
Expand Down
40 changes: 40 additions & 0 deletions framework/fit/python/fit_cli/commands/package_cmd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import zipfile
from pathlib import Path

def package_to_zip(plugin_name: str):
"""将 build 生成的文件打包为 zip"""
base_dir = Path("plugin") / plugin_name

# 待打包的文件列表
files_to_zip = [
base_dir / f"{plugin_name}.tar",
base_dir / "tools.json",
base_dir / "plugin.json"
]

# 检查文件是否存在
missing_files = [str(f) for f in files_to_zip if not f.exists()]
if missing_files:
print(f"❌ 缺少以下文件,请先执行 build 命令:{', '.join(missing_files)}")
return None

# 打包文件
zip_path = base_dir.parent / f"{plugin_name}.zip"
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for file in files_to_zip:
zipf.write(file, arcname=file.name)

print(f"✅ 已生成打包文件:{zip_path}")
return zip_path


def run(args):
"""package 命令入口"""
plugin_name = args.name
base_dir = Path("plugin") / plugin_name

if not base_dir.exists():
print(f"❌ 插件目录 {base_dir} 不存在,请先运行 init 和 build 命令")
return

package_to_zip(plugin_name)
12 changes: 12 additions & 0 deletions framework/fit/python/fit_cli/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import argparse
from fit_cli.commands import init_cmd
from fit_cli.commands import build_cmd
from fit_cli.commands import package_cmd

def main():
parser = argparse.ArgumentParser(description="FIT Framework CLI 插件开发工具")
Expand All @@ -10,6 +12,16 @@ def main():
parser_init.add_argument("name", help="插件目录名称")
parser_init.set_defaults(func=init_cmd.run)

# build
parser_build = subparsers.add_parser("build", help="构建插件,生成 tools.json / plugin.json")
parser_build.add_argument("name", help="插件目录名称")
parser_build.set_defaults(func=build_cmd.run)

# package
parser_package = subparsers.add_parser("package", help="将插件文件打包")
parser_package.add_argument("name", help="插件目录名称")
parser_package.set_defaults(func=package_cmd.run)

args = parser.parse_args()

if hasattr(args, "func"):
Expand Down
39 changes: 37 additions & 2 deletions framework/fit/python/fit_cli/readme.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,49 @@
# FIT CLI 工具

FIT CLI 工具是基于 **FIT Framework** 的命令行开发工具,提供插件初始化、打包、发布等功能,帮助用户快速开发和管理 FIT 插件。
FIT CLI 工具是基于 **FIT Framework** 的命令行开发工具,提供插件初始化、构建、打包等功能,帮助用户快速开发和管理 FIT 插件。

---

## 使用方式

FIT CLI 支持 3 个核心子命令:init(初始化)、build(构建)、package(打包),以下是详细说明。

### init
Comment thread
surpercodehang marked this conversation as resolved.

以 framework/fit/python 为项目根目录,运行:

```bash
python -m fit_cli init %{your_plugin_name}
```
将会在 plugin 目录中创建 %{your_plugin_name} 目录,并生成插件模板。
· 参数:%{your_plugin_name} - 自定义插件名称

会在 plugin 目录中创建 %{your_plugin_name} 目录,包含源代码目录、示例插件函数等。

### build
Comment thread
CodeCasterX marked this conversation as resolved.

在完成插件的开发后,执行
Comment thread
surpercodehang marked this conversation as resolved.
```bash
python -m fit_cli build %{your_plugin_name}
Comment thread
surpercodehang marked this conversation as resolved.
```
· 参数:%{your_plugin_name} - 自定义插件名称

解析插件源代码,在 plugin 目录中生成 %{your_plugin_name}.tar 文件,包含插件的所有源代码,并生成 tools.json 和 plugin.json 文件。

开发者可根据自己的需要,修改完善tools.json 和 plugin.json 文件。

### package

在完成插件的构建后,执行
```bash
python -m fit_cli package %{your_plugin_name}
Comment thread
surpercodehang marked this conversation as resolved.
```
· 参数:%{your_plugin_name} - 自定义插件名称

将 %{your_plugin_name}.tar 文件、tools.json 和 plugin.json 文件打包为 zip 文件。

---

## 注意事项

1. 在运行 init, build 或 package 子命令前,请先切换至 framework/fit/python 项目根目录下。
2. 更多详细信息和使用说明,可参考 https://github.com/ModelEngine-Group/fit-framework 官方仓库。
Empty file.
Loading