@hadss/hmrouter-plugin
Version:
HMRouter Compiler Plugin
119 lines (118 loc) • 6.92 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 micromatch_1 = __importDefault(require("micromatch"));
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.context = context;
}
execute() {
framework_1.Logger.debug(this.context.node.getNodeName(), `Start to code generation...`);
this.context.generatedPaths = new Map();
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);
this.generatePageFile(result, pageSourceFile, this.matchedPath(pageSourceFile, this.context.config.customPageTemplate, this.context.config.getDefaultTplFilePath()));
}
});
}
generatePageFile(result, pageSourceFile, tempFilePath) {
const templateModel = this.prepareTemplateModel(result, pageSourceFile);
tempFilePath = this.determineTemplatePath(result, tempFilePath);
const generatedFilePath = this.generateFile(templateModel, tempFilePath);
framework_1.Logger.info(this.context.node.getNodeName(), `Builder ${templateModel.generatorViewName}.ets has been generated in ${generatedFilePath}`);
const uniqueKey = `${result.name}#${result.pageUrl}`;
this.context.generatedPaths.set(uniqueKey, generatedFilePath);
}
matchedPath(filePath, customPageTemplate, defaultTplFilePath) {
for (const template of customPageTemplate) {
if (micromatch_1.default.isMatch(filePath, template.srcPath)) {
framework_1.Logger.debug(this.context.node.getNodeName(), `${filePath} detected matching template: ${template.templatePath}`);
return framework_1.PluginFileUtil.pathResolve(this.context.config.configDir, template.templatePath);
}
}
return defaultTplFilePath;
}
prepareTemplateModel(analyzeResult, pageSourceFile) {
let importPath = this.context.config
.getRelativeBuilderPath(pageSourceFile)
.replaceAll(constants_1.FilePathConstants.FILE_SEPARATOR, constants_1.FilePathConstants.DELIMITER)
.replaceAll(constants_1.FilePathConstants.ETS_SUFFIX, '');
let generatorViewName = constants_1.PrefixConstants.VIEW_NAME_PREFIX + analyzeResult.name + framework_1.StringUtil.stringToHashCode(analyzeResult.pageUrl);
let templateData = this.context.getTemplateData(analyzeResult.name);
return {
pageUrl: analyzeResult.pageUrl,
componentName: analyzeResult.name,
dialog: !!analyzeResult.dialog,
isDefaultExport: !!templateData?.isDefaultExport,
importPath,
generatorViewName,
...templateData,
};
}
determineTemplatePath(analyzeResult, defaultTemplatePath) {
const libraryVersion = this.detectLibraryVersion();
framework_1.Logger.debug(this.context.node.getNodeName(), `Library version: ${libraryVersion}`);
const useV1Template = libraryVersion.startsWith(constants_1.VersionConstants.HMROUTER_VERSION_V1);
if (useV1Template) {
return framework_1.PluginFileUtil.pathResolve(__dirname, constants_1.FilePathConstants.PARENT_DELIMITER.repeat(3) + constants_1.TemplateConstants.V1_TEMPLATE);
}
else if (analyzeResult.useNavDst === true) {
return framework_1.PluginFileUtil.pathResolve(__dirname, constants_1.FilePathConstants.PARENT_DELIMITER.repeat(3), constants_1.TemplateConstants.CUSTOM_BUILDER_TEMPLATE);
}
return defaultTemplatePath;
}
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.generatorViewName);
framework_1.PluginFileUtil.ensureFileSync(generatorFilePath);
framework_1.PluginFileUtil.writeFileSync(generatorFilePath, templateStr);
return this.context.config
.getBuilderFilePath(templateModel.generatorViewName)
.replaceAll(constants_1.FilePathConstants.FILE_SEPARATOR, constants_1.FilePathConstants.DELIMITER);
}
detectLibraryVersion() {
let libraryVersion = '';
const possiblePaths = this.getLibraryPossiblePaths();
for (const packagePath of possiblePaths) {
try {
if (framework_1.PluginFileUtil.exist(packagePath)) {
const packageJson = framework_1.PluginFileUtil.readJson5(packagePath);
libraryVersion = packageJson[constants_1.VersionConstants.HMROUTER_VERSION_KEY] || '';
if (libraryVersion) {
break;
}
}
}
catch (error) {
}
}
return libraryVersion;
}
getLibraryPossiblePaths() {
const paths = [];
paths.push(framework_1.PluginFileUtil.pathResolve(this.context.config.modulePath, constants_1.FilePathConstants.OH_MODULE_PATH, constants_1.VersionConstants.HMROUTER_ORGANIZATION, constants_1.VersionConstants.HMROUTER_LIB_NAME, constants_1.FilePathConstants.OH_PACKAGE_FILE_NAME));
const projectRoot = framework_1.PluginStore.getInstance().get('projectFilePath');
if (projectRoot && projectRoot !== this.context.config.modulePath) {
paths.push(framework_1.PluginFileUtil.pathResolve(projectRoot, constants_1.FilePathConstants.OH_MODULE_PATH, constants_1.VersionConstants.HMROUTER_ORGANIZATION, constants_1.VersionConstants.HMROUTER_LIB_NAME, constants_1.FilePathConstants.OH_PACKAGE_FILE_NAME));
}
return paths;
}
}
exports.CodeGenerationProcessor = CodeGenerationProcessor;