Commit 574f90d95648401ee811ba82cf39c8ff1b8cd87b
1 parent
f07ec768
fix(infra): 兼容代码生成快照的 Windows 换行
- 代码生成尾逗号清理兼容 CRLF 换行 - 快照断言统一 CRLF/CR 为 LF,并忽略文件末尾换行 - 避免使用 trimEnd,防止误删模板中有意义的尾随空格
Showing
2 changed files
with
9 additions
and
6 deletions
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngine.java
| ... | ... | @@ -459,7 +459,7 @@ public class CodegenEngine { |
| 459 | 459 | private String prettyCode(String content, String vmPath) { |
| 460 | 460 | // Vue 界面:去除字段后面多余的 , 逗号,解决前端的 Pretty 代码格式检查的报错(需要排除 vben5、vue3_admin_uniapp) |
| 461 | 461 | if (!StrUtil.containsAny(vmPath, "vben5", "vue3_admin_uniapp")) { |
| 462 | - content = content.replaceAll(",\\r?\\n(\\s*)}", "\n$1}"); | |
| 462 | + content = content.replaceAll(",\\r?\\n}", "\n}").replaceAll(",\\r?\\n }", "\n }"); | |
| 463 | 463 | } |
| 464 | 464 | // Vue 界面:去除多的 dateFormatter,只有一个的情况下,说明没使用到 |
| 465 | 465 | if (StrUtil.count(content, "dateFormatter") == 1) { |
| ... | ... | @@ -482,8 +482,7 @@ public class CodegenEngine { |
| 482 | 482 | if (StrUtil.count(content, "DICT_TYPE.") == 0) { |
| 483 | 483 | content = StrUtils.removeLineContains(content, "DICT_TYPE"); |
| 484 | 484 | } |
| 485 | - // 统一换行符为 LF,避免 Windows 下模板引擎输出 CRLF 导致前端 ESLint/Prettier 报错 | |
| 486 | - return content.replace("\r\n", "\n"); | |
| 485 | + return content; | |
| 487 | 486 | } |
| 488 | 487 | |
| 489 | 488 | private Map<String, Object> initBindingMap(DbType dbType, CodegenTableDO table, List<CodegenColumnDO> columns, | ... | ... |
yudao-module-infra/src/test/java/cn/iocoder/yudao/module/infra/service/codegen/inner/CodegenEngineAbstractTest.java
| ... | ... | @@ -114,11 +114,15 @@ public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest { |
| 114 | 114 | }); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | + /** | |
| 118 | + * 统一换行符并忽略文件末尾换行,避免 Windows/Unix 落盘差异导致快照断言失败 | |
| 119 | + */ | |
| 117 | 120 | private static String normalizeLineEndings(String content) { |
| 118 | 121 | if (content == null) { |
| 119 | 122 | return null; |
| 120 | 123 | } |
| 121 | - return StrUtil.trimEnd(content.replace("\r\n", "\n").replace('\r', '\n')); | |
| 124 | + content = content.replace("\r\n", "\n").replace('\r', '\n'); | |
| 125 | + return content.replaceAll("\\n+\\z", ""); | |
| 122 | 126 | } |
| 123 | 127 | |
| 124 | 128 | // ==================== 调试专用 ==================== |
| ... | ... | @@ -154,10 +158,10 @@ public abstract class CodegenEngineAbstractTest extends BaseMockitoUnitTest { |
| 154 | 158 | + '/' + StrUtil.subBefore(lastFilePath, '.', true); |
| 155 | 159 | asserts.add(MapUtil.<String, String>builder().put("filePath", filePath) |
| 156 | 160 | .put("contentPath", contentPath).build()); |
| 157 | - FileUtil.writeUtf8String(normalizeLineEndings(fileContent), basePath + "/" + contentPath); | |
| 161 | + FileUtil.writeUtf8String(fileContent, basePath + "/" + contentPath); | |
| 158 | 162 | }); |
| 159 | 163 | // 写入 assert.json 文件 |
| 160 | - FileUtil.writeUtf8String(normalizeLineEndings(JsonUtils.toJsonPrettyString(asserts)), basePath + "/assert.json"); | |
| 164 | + FileUtil.writeUtf8String(JsonUtils.toJsonPrettyString(asserts), basePath + "/assert.json"); | |
| 161 | 165 | } |
| 162 | 166 | |
| 163 | 167 | } | ... | ... |