FreeKit CLI
FreeKit 代码脚手架工具,基于 Scriban 模板生成全套代码。
功能概览
| 功能 | 说明 |
|---|---|
| 模块生成 | 一键生成 Controller、Service、Entity |
| 代码模板 | 基于 Scriban 模板引擎 |
| 风格统一 | 保持项目代码风格一致 |
安装
dotnet pack src/BuildingBlocks/IGeekFan.FreeKit.CLI
dotnet tool install --global --add-source src/BuildingBlocks/IGeekFan.FreeKit.CLI/nupkg FreeKit.CLI --version 0.0.1
快速开始
本地运行
dotnet run --project src/BuildingBlocks/IGeekFan.FreeKit.CLI -- scaffold --help
使用命令
freekit scaffold --help
命令参数
| 参数 | 说明 | 必填 |
|---|---|---|
--profile | 模板风格(cmskit/platform) | 是 |
--group | 功能分组(Content/Nav 等) | 是 |
--base-directory | 模块基础目录 | 是 |
--project-name | 项目名称 | 是 |
--entity-file-path | 实体文件路径 | 是 |
--output-directory | 输出目录 | 是 |
--template-path | 自定义模板路径 | 否 |
使用示例
CmsKit 风格
freekit scaffold ^
--profile cmskit ^
--group Content ^
--base-directory src\Services\CmsKit\FreeKit.CmsKit ^
--project-name FreeKit.CmsKit ^
--entity-file-path Models\Classify.cs ^
--output-directory D:\code-scaffolding
Platform 风格
freekit scaffold ^
--profile platform ^
--group Nav ^
--base-directory src\Services\Platform\FreeKit.Platform ^
--project-name FreeKit.Platform ^
--entity-file-path Application\Nav\NavItems\NavItem.cs ^
--output-directory D:\code-scaffolding
支持的 Profile
| Profile | 说明 | 模板位置 |
|---|---|---|
cmskit | CmsKit 内容管理风格 | Templates/cmskit/ |
platform | Platform 工具平台风格 | Templates/platform/ |
生成的文件
脚手架会根据实体文件生成以下文件:
| 文件 | 说明 |
|---|---|
Controllers/ | API 控制器 |
Application/ | 应用服务 |
Domain/ | 领域服务 |
Models/ | 实体定义 |
Contracts/ | 权限定义 |
自定义模板
使用自定义模板
freekit scaffold ^
--profile cmskit ^
--group Content ^
--base-directory src\Services\CmsKit\FreeKit.CmsKit ^
--project-name FreeKit.CmsKit ^
--entity-file-path Models\Article.cs ^
--output-directory D:\scaffolding ^
--template-path D:\my-templates
模板语法
模板使用 Scriban 语法,支持以下变量:
| 变量 | 说明 |
|---|---|
{{ProjectInfo.FullName}} | 项目全名 |
{{EntityInfo.Name}} | 实体名称 |
{{EntityInfo.Properties}} | 实体属性列表 |
{{GroupInfo.Name}} | 分组名称 |
配置文件
可以通过 appsettings.json 配置默认参数:
{
"SettingOptions": {
"Profile": "cmskit",
"Group": "Content",
"BaseDirectory": "src/Services/CmsKit/FreeKit.CmsKit",
"ProjectName": "FreeKit.CmsKit"
}
}
适用场景
| 场景 | 命令示例 |
|---|---|
| 新建 CmsKit 模块 | freekit scaffold --profile cmskit --group Content ... |
| 新建 Platform 模块 | freekit scaffold --profile platform --group Nav ... |
| 生成实体相关代码 | freekit scaffold --entity-file-path Models/Article.cs ... |
| 批量生成 | 在脚本中循环调用 |
故障排查
| 问题 | 解决方案 |
|---|---|
| 命令未找到 | 检查 dotnet tool 路径 |
| 模板错误 | 检查模板语法 |
| 输出目录不存在 | 创建输出目录 |
| 实体文件找不到 | 检查实体文件路径 |