@hadss/hmrouter-plugin
Version:
HMRouter Compiler Plugin
146 lines (145 loc) • 7.39 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CodeGenerationProcessor = void 0;
const ejs_1 = __importDefault(require("ejs"));
const framework_1 = require("../../framework");
const PluginError_1 = require("../error/PluginError");
const constants_1 = require("../constants");
class CodeGenerationProcessor {
constructor(context) {
this.sdkVersion = 0;
this.context = context;
}
execute() {
this.sdkVersion = this.getCompileSdkVersion();
framework_1.Logger.debug(this.context.node.getNodeName(), `Start to code generation...`);
const generatedFilePaths = [];
this.context.getAnalyzeResults().forEach((result) => {
if (result.annotation === constants_1.AnnotationConstants.ROUTER_ANNOTATION) {
if (!result.sourceFilePath) {
throw PluginError_1.PluginError.create(PluginError_1.ErrorCode.ERROR_PAGE_SOURCE_FILE, this.context.config.moduleName, result.sourceFilePath);
}
let pageSourceFile = this.context.config
.getRelativeSourcePath(result.sourceFilePath)
.replaceAll(constants_1.FilePathConstants.FILE_SEPARATOR, constants_1.FilePathConstants.DELIMITER);
const generatePageFilePath = this.generatePageFile(result, pageSourceFile);
generatedFilePaths.push(generatePageFilePath);
}
});
this.deleteOtherFile(generatedFilePaths);
}
extractVersion(compatibleSdkVersion) {
if (typeof compatibleSdkVersion === 'number') {
return compatibleSdkVersion;
}
else {
const match = compatibleSdkVersion.match(/\((\d+)\)/);
if (match && match[1]) {
return parseInt(match[1], 10);
}
const num = parseFloat(compatibleSdkVersion);
if (!isNaN(num) && num > constants_1.FilePathConstants.DEFAULT_VALUE) {
return num;
}
return constants_1.FilePathConstants.DEFAULT_VALUE;
}
}
getCompileSdkVersion() {
let json5FilePath = '';
let sdkVersion = constants_1.FilePathConstants.DEFAULT_VALUE;
let arrData = ['DEVECO_SDK_HOME', 'HOS_SDK_HOME', 'OHOS_SDK_HOME'];
for (let index = 0; index <= 4; index++) {
if (index < 3 && process.env[arrData[index]]) {
json5FilePath = framework_1.PluginFileUtil.pathResolve(process.env[arrData[index]], constants_1.FilePathConstants.SDK_HOME);
}
else if (index === 3) {
json5FilePath = framework_1.PluginFileUtil.pathResolve(framework_1.PluginStore.getInstance().get('projectFilePath'), constants_1.FilePathConstants.BUILD_PROFILE);
}
else if (index === 4) {
json5FilePath = framework_1.PluginFileUtil.pathResolve(framework_1.PluginStore.getInstance().get('projectFilePath'), constants_1.FilePathConstants.BUILD_PROFILE);
}
const sdkVer = this.getSdkVersion(json5FilePath, index);
if (sdkVer > constants_1.FilePathConstants.DEFAULT_VALUE) {
sdkVersion = sdkVer;
break;
}
}
return sdkVersion;
}
getSdkVersion(json5FilePath, index) {
if (framework_1.PluginFileUtil.exist(json5FilePath)) {
const data = framework_1.PluginFileUtil.readJson5(json5FilePath);
if (index < 3) {
return this.extractVersion(data.apiVersion);
}
else if (index === 3) {
if (data.app.products[0].compileSdkVersion) {
return this.extractVersion(data.app.products[0].compileSdkVersion);
}
}
else if (index === 4) {
if (data.app.products[0].compatibleSdkVersion) {
return this.extractVersion(data.app.products[0].compatibleSdkVersion);
}
}
}
return constants_1.FilePathConstants.DEFAULT_VALUE;
}
generatePageFile(result, pageSourceFile) {
const templateModel = this.prepareTemplateModel(result, pageSourceFile);
const generatedFilePath = this.generateFile(templateModel, result.templatePath ?? '');
framework_1.Logger.info(this.context.node.getNodeName(), `Builder ${result.sourceFilePath}.ets has been generated in ${generatedFilePath}`);
return generatedFilePath;
}
deleteOtherFile(generatedFilePaths) {
framework_1.Logger.debug(this.context.node.getNodeName(), `Start to delete other file...`);
const buildDir = this.context.config.getBuilderDir();
if (!framework_1.PluginFileUtil.exist(buildDir)) {
framework_1.Logger.debug(this.context.node.getNodeName(), `${buildDir} does not exist`);
return;
}
const files = framework_1.PluginFileUtil.readdirSync(buildDir, { recursive: true, encoding: null });
framework_1.Logger.debug(this.context.node.getNodeName(), `Files in ${buildDir}: ${files}`);
for (const file of files) {
const absoluteFilePath = framework_1.PluginFileUtil.join(buildDir, file);
framework_1.Logger.debug(this.context.node.getNodeName(), `Delete other file: ${absoluteFilePath}`);
const isExist = generatedFilePaths.findIndex((path) => path.indexOf(absoluteFilePath) >= 0);
if (isExist < 0) {
framework_1.PluginFileUtil.rmSync(absoluteFilePath);
}
}
}
prepareTemplateModel(analyzeResult, pageSourceFile) {
const relativeShortPath = this.context.config.getRelativeShortSourcePath(analyzeResult);
let importPath = this.context.config
.getRelativeBuilderPath(pageSourceFile, relativeShortPath)
.replaceAll(constants_1.FilePathConstants.FILE_SEPARATOR, constants_1.FilePathConstants.DELIMITER)
.replaceAll(constants_1.FilePathConstants.ETS_SUFFIX, '');
let templateData = this.context.getTemplateData(analyzeResult.name);
return {
sdkVersion: this.sdkVersion,
pageUrl: analyzeResult.pageUrl,
componentName: analyzeResult.name,
dialog: !!analyzeResult.dialog,
isDefaultExport: !!templateData?.isDefaultExport,
importPath,
relativeShortPath,
...templateData,
};
}
generateFile(templateModel, templatePath) {
if (!framework_1.PluginFileUtil.exist(templatePath)) {
throw PluginError_1.PluginError.create(PluginError_1.ErrorCode.TEMPLATE_NOT_FOUND, this.context.config.moduleName, templatePath);
}
const tpl = framework_1.PluginFileUtil.readFileSync(templatePath).toString();
const templateStr = ejs_1.default.render(tpl, templateModel);
const generatorFilePath = this.context.config.getGeneratedFilePath(templateModel.relativeShortPath);
framework_1.PluginFileUtil.ensureFileSync(generatorFilePath);
framework_1.PluginFileUtil.writeFileSync(generatorFilePath, templateStr);
return generatorFilePath;
}
}
exports.CodeGenerationProcessor = CodeGenerationProcessor;