129 lines
5.6 KiB
JavaScript
129 lines
5.6 KiB
JavaScript
/**
|
||
* Service方法体转换器(主协调器)
|
||
*
|
||
* 职责:协调各个子转换器,将Java Service方法体转换为TypeScript
|
||
*
|
||
* 核心功能:
|
||
* 1. 提取Java方法体
|
||
* 2. 调用各个专业转换器进行转换
|
||
* 3. 后处理清理
|
||
* 4. 分析需要的imports
|
||
*/
|
||
|
||
// 导入全局映射器(新)
|
||
const JavaToV1Mapper = require('./java-to-v1-mapper');
|
||
|
||
// 导入各个转换器(保留用于特定场景)
|
||
const BasicSyntaxConverter = require('./syntax/basic-syntax.converter');
|
||
const ConfigConverter = require('./utils/config.converter');
|
||
const FileConverter = require('./utils/file.converter');
|
||
const StringConverter = require('./utils/string.converter');
|
||
const CollectionConverter = require('./utils/collection.converter');
|
||
|
||
const PostProcessor = require('./post-processor');
|
||
|
||
class ServiceMethodConverter {
|
||
constructor() {
|
||
// 初始化全局映射器(优先使用)
|
||
this.v1Mapper = new JavaToV1Mapper();
|
||
|
||
// 初始化特定转换器(用于特殊场景)
|
||
this.basicSyntax = new BasicSyntaxConverter();
|
||
this.config = new ConfigConverter();
|
||
this.file = new FileConverter();
|
||
this.string = new StringConverter();
|
||
this.collection = new CollectionConverter();
|
||
|
||
this.postProcessor = new PostProcessor();
|
||
}
|
||
|
||
/**
|
||
* 转换Java方法体为TypeScript
|
||
*
|
||
* @param {string} javaMethodBody - Java方法体代码
|
||
* @param {object} context - 上下文信息(Service类信息、依赖等)
|
||
* @returns {string} 转换后的TypeScript方法体
|
||
*/
|
||
convertMethodBody(javaMethodBody, context = {}) {
|
||
if (!javaMethodBody || javaMethodBody.trim() === '') {
|
||
return ' // TODO: 实现业务逻辑\n return null;';
|
||
}
|
||
|
||
let tsBody = javaMethodBody;
|
||
|
||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||
// 【核心转换】使用全局映射器(Java → V1框架)
|
||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||
this.v1Mapper.resetImports();
|
||
tsBody = this.v1Mapper.convert(tsBody);
|
||
|
||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||
// 【补充转换】特定场景处理
|
||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||
tsBody = this.basicSyntax.convert(tsBody); // Lambda、for-each等语法
|
||
tsBody = this.config.convert(tsBody); // WebAppEnvs/GlobalConfig → AppConfigService
|
||
tsBody = this.file.convert(tsBody); // 文件操作补充转换
|
||
tsBody = this.string.convert(tsBody); // String工具补充
|
||
tsBody = this.collection.convert(tsBody); // Collection工具补充
|
||
|
||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||
// 【后处理】清理与格式化
|
||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||
tsBody = this.postProcessor.process(tsBody);
|
||
|
||
// 添加缩进
|
||
tsBody = tsBody.split('\n').map(line => ' ' + line).join('\n');
|
||
|
||
return tsBody;
|
||
}
|
||
|
||
/**
|
||
* 分析需要的imports
|
||
*
|
||
* @param {string} convertedBody - 转换后的TypeScript代码
|
||
* @returns {object} 需要导入的模块
|
||
*/
|
||
analyzeImports(convertedBody) {
|
||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||
// 【核心】从全局映射器获取imports
|
||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||
const v1Imports = this.v1Mapper.getImports();
|
||
|
||
const imports = {
|
||
nestjs: new Set(v1Imports.nestjs),
|
||
boot: new Set(v1Imports.boot),
|
||
typeorm: new Set(v1Imports.typeorm),
|
||
nodeModules: new Set(v1Imports.nodeModules)
|
||
};
|
||
|
||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||
// 【补充】从特定转换器获取额外imports
|
||
// ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
|
||
|
||
// AppConfigService (from config.converter.js)
|
||
const configImports = this.config.analyzeImports(convertedBody);
|
||
configImports.forEach(imp => imports.boot.add(imp));
|
||
|
||
// StringUtils (from string.converter.js)
|
||
const stringImports = this.string.analyzeImports(convertedBody);
|
||
stringImports.forEach(imp => imports.boot.add(imp));
|
||
|
||
// Collection utils (from collection.converter.js)
|
||
const collectionImports = this.collection.analyzeImports(convertedBody);
|
||
collectionImports.forEach(imp => imports.boot.add(imp));
|
||
|
||
// Node modules (from file.converter.js)
|
||
const nodeModules = this.file.analyzeNodeModules(convertedBody);
|
||
nodeModules.forEach(mod => imports.nodeModules.add(mod));
|
||
|
||
return {
|
||
nestjs: Array.from(imports.nestjs),
|
||
boot: Array.from(imports.boot),
|
||
typeorm: Array.from(imports.typeorm),
|
||
nodeModules: Array.from(imports.nodeModules)
|
||
};
|
||
}
|
||
}
|
||
|
||
module.exports = ServiceMethodConverter;
|