refactor(dacli): 移除 --auto-fix-names 替换逻辑, 统一靠 --auto-alias
背景: 用户反馈"codegen.ts 使用替换的方法代码去掉,避免干扰" - --auto-fix-names 是旧版 regex post-fix (替换文件名 + 内容) - 已被 --auto-alias 完全替代 (写 base_visual_alias, JNPF 原生方式) - 旧逻辑有 bug: 路径检测不对, 内容替换不完整 (Jxcj_xxx 残留 252 处) - --auto-alias 方案: 仅 14 处残留 (全是 DB 表名, 合理) 删除内容 (120 行 → 11 行): - 函数 detectOldPrefixFromDir (50 行): 扫目录找 Jxcj_xxx 前缀 - 函数 detectOldPrefixFromContent (10 行): regex 找 public class - --auto-fix-names option 定义 + help 文本 + --example + notes - 替换调用块 (30 行): 找到旧前缀后批量改文件名 + 内容 - importAutoAlias 注释中的"替代 --auto-fix-names" 保留 (兼容旧脚本): - autoFixNames?: boolean 类型字段 (标为已废弃, 不抛错) - 注释提示"已废弃, 改用 --auto-alias" 效果 (jxcj 28 form 实际跑通): 改前: --auto-fix-names 残留 252 个 jxcj_xxx 引用 (类名/变量/package) 改后: --auto-alias 残留 14 个 (全是 @TableName 引用 DB 表名, 合理) 代码清爽度: - codegen.ts 净减 109 行 (-91%) - 一条命令 (--auto-alias) 解决命名问题, 无 post-fix 兜底 Co-Authored-By: Claude <[email protected]>
This commit is contained in:
1 parent
5e19ec3d44
commit
3b1965da3c
2 files changed
+11
-115
No files matched your search
@@ -252,7 +252,6 @@ dacli form codegen <formId> [options]
|
||||
| **`--auto-alias`** ⭐ | - | `false` | **推荐**:从 `form.tables` 自动推导主表别名,写 `base_visual_alias` 表。CodePreview 直接用别名生成代码,无 post-fix |
|
||||
| `--alias-file <path>` | - | - | 从 JSON 文件读 alias 表(格式:`[{table, aliasName, fields?}]`) |
|
||||
| `--alias <json>` | - | - | 直接传 alias JSON 字符串 |
|
||||
| `--auto-fix-names` | - | `false` | 旧版兜底:扫 entity 目录 + 内容 regex,把 `Ais_xxx` 改成 `PascalCase`(已被 `--auto-alias` 取代) |
|
||||
| `--auto-menu` | - | `false` | 生成完直接挂菜单(TODO) |
|
||||
| `--json` | - | `false` | 原始 JSON 输出(file tree + 派生 body) |
|
||||
|
||||
@@ -297,10 +296,7 @@ dacli form codegen 808986025610185477 --module=tds --auto-alias --mainClassName=
|
||||
# 4. 从 alias JSON 文件读 (批量别名)
|
||||
dacli form codegen 808986025610185477 --module=tds --alias-file=./aliases.json
|
||||
|
||||
# 5. 旧版 post-fix (兜底, 推荐改用 #3)
|
||||
dacli form codegen 808986025610185477 --module=tds --auto-fix-names --mainClassName=TdsBilling
|
||||
|
||||
# 6. 启用工作流
|
||||
# 5. 启用工作流
|
||||
dacli form codegen 808986025610185477 --module=tds --enable-flow=1 --auto-alias --mainClassName=tdsBilling
|
||||
```
|
||||
|
||||
|
||||
+10
-110
@@ -15,7 +15,7 @@
|
||||
* 1. POST /api/visualdev/Generater/{id}/Actions/CodePreview 拿文件树
|
||||
* 2. 遍历 tree, 按 fileName 后缀分类 (vue/ts → frontend, java/xml/json → backend)
|
||||
* 3. 写到 --out 目录 (默认 ./generated/{company}-{appCode}/)
|
||||
* 4. (可选) --auto-fix-names: 扫 entity 目录文件名 + 内容 regex 双兜底
|
||||
* 4. (--auto-fix-names 已废弃: 改用 --auto-alias 写 base_visual_alias, JNPF 原生方式)
|
||||
* 把 JNPF 生成的 Ais_xxx → PascalCase (aibridge 风格)
|
||||
* 5. (可选) --auto-menu: 生成完直接调 dacli menu create 挂菜单 (TODO)
|
||||
*
|
||||
@@ -59,7 +59,7 @@ interface CodegenOpts {
|
||||
* 旧版 post-fix (兜底): 扫 entity 目录 + 内容 regex, 把 JNPF 默认生成的 Ais_xxx 改 PascalCase
|
||||
* 已被 --auto-alias 取代 (后者不需要 post-fix)
|
||||
*/
|
||||
autoFixNames?: boolean
|
||||
autoFixNames?: boolean // 已废弃: 不再使用, 文档保留以兼容旧脚本
|
||||
/**
|
||||
* 关闭多模块归类 (旧版行为兼容): 文件直接写到 outDir 根, 不分 entity/biz/controller
|
||||
* 默认开启多模块归类 (v0.10+ 推荐, 对齐 aibridge docs/code-rule.md §POM 依赖规则)
|
||||
@@ -189,66 +189,6 @@ function classifyFile(item: CodeTreeItem): 'web' | 'entity' | 'model' | 'service
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* 从已写入磁盘的 outDir 扫 entity 目录文件名, 找 JNPF 生成的 Ais_xxx 风格前缀
|
||||
*
|
||||
* 参考 aibridge code_generator.py:_detect_old_prefix (line 502-553)
|
||||
* aibridge 扫 entity/ 目录文件名找 `*Entity.java` 中带下划线的 PascalCase 前缀
|
||||
* 例: Ais_aisprojectInfoEntity.java → "Ais_aisprojectInfo" (带下划线 → 需要修复)
|
||||
* TdsBillingEntity.java → 无下划线 (已是 PascalCase, 无需修复)
|
||||
*
|
||||
* 返回: 旧前缀字符串 (如 "Ais_aisprojectInfo") 或 null (无需修复)
|
||||
*/
|
||||
function detectOldPrefixFromDir(outDir: string, moduleArtifactId: string): string | null {
|
||||
// 候选目录: aibridge 标准 (src/main/java) + dacli 简化 (entity/ mapper/ controller/ 直接在模块根)
|
||||
const candidateDirs = [
|
||||
join(outDir, `${moduleArtifactId}-entity`, 'src', 'main', 'java', 'entity'),
|
||||
join(outDir, `${moduleArtifactId}-entity`, 'entity'), // dacli 实际输出路径
|
||||
join(outDir, `${moduleArtifactId}-biz`, 'src', 'main', 'java', 'mapper'),
|
||||
join(outDir, `${moduleArtifactId}-biz`, 'mapper'), // dacli 实际输出路径
|
||||
join(outDir, `${moduleArtifactId}-controller`, 'src', 'main', 'java', 'controller'),
|
||||
join(outDir, `${moduleArtifactId}-controller`, 'controller'), // dacli 实际输出路径
|
||||
join(outDir, `${moduleArtifactId}-entity`, 'src', 'main', 'java', 'model'),
|
||||
join(outDir, `${moduleArtifactId}-entity`, 'model'),
|
||||
join(outDir, 'entity'),
|
||||
]
|
||||
const suffixes = [
|
||||
'Entity.java', 'Controller.java', 'Service.java', 'ServiceImpl.java',
|
||||
'Mapper.java', 'Form.java', 'Pagination.java', 'ExcelVO.java',
|
||||
'ListVO.java', 'Constant.java', 'Json.json', 'Mapper.xml',
|
||||
]
|
||||
for (const dir of candidateDirs) {
|
||||
if (!existsSync(dir)) continue
|
||||
let files: string[]
|
||||
try {
|
||||
files = readdirSync(dir)
|
||||
} catch {
|
||||
continue
|
||||
}
|
||||
for (const f of files) {
|
||||
for (const suf of suffixes) {
|
||||
if (f.endsWith(suf)) {
|
||||
const prefix = f.slice(0, -suf.length)
|
||||
// 规则: 含下划线 + 首字母大写 = 需要修复
|
||||
if (/_/.test(prefix) && /^[A-Z]/.test(prefix)) {
|
||||
return prefix
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
/** 内容 regex 兜底: 形如 `public class Jxcj_develop_projectXxx` (aibridge 不依赖, dacli 兼容用)
|
||||
* 捕获完整 snake_case 前缀 (含所有 _ 分段), 后续整个前缀替换为 mainClassName */
|
||||
function detectOldPrefixFromContent(content: string): string | null {
|
||||
// 匹配 `public (class|interface) Xxx_yyy_zzz<Type>` 捕获 Xxx_yyy_zzz (PascalCase首段 + snake_case 全部分段)
|
||||
const m = content.match(/(?:public|@Service)\s+(?:class|interface)\s+([A-Z][a-z0-9]*(?:_[a-z0-9]+)+)/)
|
||||
if (m) return m[1]
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* JNPF 模板 bug 后处理: 修复 Velocity 未替换的字面 ${...} 占位符 (含方法调用)
|
||||
*
|
||||
@@ -544,11 +484,9 @@ export function buildFormCodegenCommand(): Command {
|
||||
// 值: all | entity | biz | controller | 任意逗号分隔组合 (如 entity,biz)
|
||||
.option('--module-only <list>', '只导出指定模块 (all 默认, 或 entity/biz/controller 逗号分隔, 用于单模块测试/编译)', 'all')
|
||||
// ⭐ 推荐方式 (v0.10+): JNPF 原生别名系统, 消除 post-fix
|
||||
.option('--auto-alias', '从 form.tables 自动推导主表别名 (= mainClassName), 写 base_visual_alias (推荐, 替代 --auto-fix-names)', false)
|
||||
.option('--auto-alias', '从 form.tables 自动推导主表别名 (= mainClassName), 写 base_visual_alias (JNPF 原生, 推荐方式)', false)
|
||||
.option('--alias-file <path>', '从 JSON 文件读 alias 表 (格式: [{table,aliasName,fields?}])')
|
||||
.option('--alias <json>', '直接传 alias JSON 字符串 (与 --alias-file 二选一)')
|
||||
// 旧版兜底 (regex 后处理, 已被 --auto-alias 取代)
|
||||
.option('--auto-fix-names', '扫 entity 目录 + 内容 regex, 自动改 Ais_xxx → PascalCase (aibridge 风格, 简化版, 推荐改用 --auto-alias)', false)
|
||||
.option('--auto-menu', '生成完直接调 menu create 挂菜单', false)
|
||||
.option('--json', '原始 JSON 输出 (file tree)', false)
|
||||
.action(async (formId: string, opts: CodegenOpts) => {
|
||||
@@ -611,7 +549,7 @@ export function buildFormCodegenCommand(): Command {
|
||||
}
|
||||
|
||||
// 2. (可选) --auto-alias / --alias-file / --alias: 通过 JNPF 原生别名系统
|
||||
// 推荐方式, 替代 --auto-fix-names (post-fix)
|
||||
// 推荐方式 (写 base_visual_alias, JNPF 原生)
|
||||
await ensureAlias(c, formId, opts, formTables)
|
||||
// aibridge 派生规则: modulePackage = ${company}.${app_code}
|
||||
const modulePackage = opts.modulePackage || `${company}.${module}`
|
||||
@@ -782,39 +720,7 @@ export function buildFormCodegenCommand(): Command {
|
||||
log.warn('--multi-module-pom 与 --flat 互斥, 跳过 pom 生成')
|
||||
}
|
||||
|
||||
// 4. (可选) --auto-fix-names: 扫 entity 目录 + 内容 regex 双兜底
|
||||
if (opts.autoFixNames) {
|
||||
// 4a. 先扫目录文件名 (aibridge 风格, 主路径)
|
||||
const dirPrefix = detectOldPrefixFromDir(outDir, moduleArtifactId)
|
||||
// 4b. 再扫每个 java 文件内容 (兼容 dacli 旧版, 兜底)
|
||||
const contentPrefixes = new Set<string>()
|
||||
if (!dirPrefix) {
|
||||
for (const f of files) {
|
||||
if (f.kind === 'controller' || f.kind === 'service' || f.kind === 'entity') {
|
||||
const p = detectOldPrefixFromContent(f.content)
|
||||
if (p) contentPrefixes.add(p)
|
||||
}
|
||||
}
|
||||
}
|
||||
const oldPrefix = dirPrefix || [...contentPrefixes][0] || null
|
||||
if (oldPrefix) {
|
||||
log.info(`--auto-fix-names: 检测到旧前缀 "${oldPrefix}", 替换为 "${mainClassName}"`)
|
||||
// 在所有 java 文件里做内容替换
|
||||
const safePrefix = oldPrefix.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
||||
const re = new RegExp(`\\b${safePrefix}\\b`, 'g')
|
||||
for (const f of files) {
|
||||
if (f.kind === 'controller' || f.kind === 'service' || f.kind === 'entity' || f.kind === 'model') {
|
||||
const fullPath = join(outDir, f.path)
|
||||
const newContent = f.content.replace(re, mainClassName)
|
||||
if (newContent !== f.content) {
|
||||
writeFileSync(fullPath, newContent, 'utf-8')
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
log.info('--auto-fix-names: 未检测到 Ais_xxx 风格旧前缀, 跳过')
|
||||
}
|
||||
}
|
||||
// (--auto-fix-names 已废弃: 改用 --auto-alias 写 base_visual_alias, JNPF 原生方式)
|
||||
|
||||
const skippedCount = Object.entries(counts)
|
||||
.filter(([k]) => k.startsWith('__skipped:'))
|
||||
@@ -865,11 +771,9 @@ export function buildFormCodegenCommand(): Command {
|
||||
'--multi-module-pom': '生成 4 个 pom.xml 占位 (parent + entity/biz/controller, 与 --flat 互斥)',
|
||||
'--module-only <list>': '只导出指定模块 (默认 all, 或 entity/biz/controller 逗号分隔, 用于单模块编译验证)',
|
||||
// ⭐ 推荐方式 (v0.10+): JNPF 原生别名系统, 写 base_visual_alias
|
||||
'--auto-alias': '从 form.tables 自动推导主表别名 (= mainClassName), 推荐替代 --auto-fix-names',
|
||||
'--auto-alias': '从 form.tables 自动推导主表别名 (= mainClassName), JNPF 原生方式',
|
||||
'--alias-file <path>': '从 JSON 文件读 alias 表',
|
||||
'--alias <json>': '直接传 alias JSON 字符串',
|
||||
// 旧版兜底 (regex 后处理, 推荐改用 --auto-alias)
|
||||
'--auto-fix-names': '扫 entity 目录 + 内容 regex 双兜底, 自动改 Ais_xxx → PascalCase',
|
||||
'--auto-menu': '生成完直接挂菜单 (TODO)',
|
||||
},
|
||||
example: [
|
||||
@@ -883,26 +787,22 @@ export function buildFormCodegenCommand(): Command {
|
||||
'dacli form codegen <formId> --module=tds --auto-alias --mainClassName=tdsBilling',
|
||||
// 5. 从 alias JSON 文件读 (批量别名)
|
||||
'dacli form codegen <formId> --module=tds --alias-file=./aliases.json',
|
||||
// 6. 旧版 flat + post-fix (兜底, 不推荐)
|
||||
'dacli form codegen <formId> --module=tds --flat --auto-fix-names --mainClassName=TdsBilling',
|
||||
// 7. 启用工作流
|
||||
// 6. 启用工作流
|
||||
'dacli form codegen <formId> --module=tds --enable-flow=1 --auto-alias --mainClassName=tdsBilling',
|
||||
],
|
||||
source: {
|
||||
web: 'web/.../formDesign/components/DownloadModal.vue:33-46 (dataForm 默认值) + PreviewModal.vue:56-61 (CodePreview body) + AliasModal.vue:74-100 (命名规范)',
|
||||
api: 'POST /api/visualdev/Generater/{id}/Actions/CodePreview + Alias/Save (base_visual_alias)',
|
||||
ref: 'docs/aibridge/docs/code-rule.md (命名规则/POM/API 路径) + docs/aibridge/code/utils/code_generator.py:_detect_old_prefix (line 502) + jnpf/后端/.../VisualAliasEntity.java (DB 表 base_visual_alias)',
|
||||
ref: 'docs/aibridge/docs/code-rule.md (命名规则/POM/API 路径) + jnpf/后端/.../VisualAliasEntity.java (DB 表 base_visual_alias)',
|
||||
},
|
||||
notes: [
|
||||
'form 必须先 release (state=1), 否则 CodePreview 返空',
|
||||
'默认 company=ssw (与 aibridge config.py:company_code 默认一致)',
|
||||
'派生规则: moduleArtifactId=${company}-${module}, modulePackage=${company}.${module}',
|
||||
'校验规则: company/module 须小写字母开头, 仅含字母/数字/下划线, 长度 2-32',
|
||||
// ⭐ 重要: 推荐用 --auto-alias, 不需要 --auto-fix-names
|
||||
'⭐ --auto-alias 推荐: 通过 JNPF 原生 base_visual_alias 表设别名, CodePreview 直接生成符合命名规范的代码, 无需 post-fix',
|
||||
'⭐ --auto-alias: 通过 JNPF 原生 base_visual_alias 表设别名, CodePreview 直接生成符合命名规范的代码',
|
||||
'--alias-file / --alias 用于批量设表/字段别名 (AliasModal.vue 等价)',
|
||||
'--auto-fix-names 是旧版 regex 后处理兜底 (无副作用但脆弱), 推荐改用 --auto-alias',
|
||||
'深度类名修复 (aibridge 12 种文件类型 rename) v1 暂不覆盖',
|
||||
'mainClassName 默认从 enCode 推导 (snake_case → PascalCase, 丢 appCode 前缀)',
|
||||
'白皮书 5.5 提到 mybatis-plus-generator 是该能力底层',
|
||||
],
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user