UNPKG

@hadss/hmrouter-plugin

Version:

HMRouter Compiler Plugin

171 lines (170 loc) 8.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PluginExecutionController = void 0; const TaskManager_1 = require("./TaskManager"); const TaskStage_1 = require("./TaskStage"); const constants_1 = require("../constants"); const extension_1 = require("../extension"); const utils_1 = require("../utils"); const constants_2 = require("../../hmrouter_extension/constants"); class PluginExecutionController { constructor(node, moduleContext, moduleIgnored, moduleExtensions) { this.node = node; this.moduleContext = moduleContext; this.baseContext = this.initializeContext(); this.baseContext.moduleIgnored = moduleIgnored; this.taskManager = new TaskManager_1.TaskManager(this.baseContext, moduleExtensions); } start() { this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_INITIALIZE); this.registerHvigorTasks(); } complete() { this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_COMPLETION); } initializeContext() { const context = new extension_1.ExtensionContextImpl(this.node, this.moduleContext); context.scanFiles = []; return context; } registerHvigorTasks() { this.moduleContext.targets((target) => { const targetName = target.getTargetName(); this.taskManager.context.currentTarget = target; const filtered = this.filterScanFilesForTarget(targetName); this.baseContext.scanFiles = filtered; this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_ANNOTATION_ANALYSIS); this.registerCodeGenerationTask(targetName); this.registerObfuscationTask(targetName); this.registerBuildRouterMapFile(targetName); this.registerConfigUpdateTask(targetName); if ((0, utils_1.isHapModule)(this.node) || (0, utils_1.isHspModule)(this.node)) { this.registerCopyRouterMapTask(target); } }); } filterScanFilesForTarget(currentTargetName) { const buildProfileTargets = this.moduleContext.getBuildProfileOpt().targets || []; const targetOpt = buildProfileTargets.find((target) => target.name === currentTargetName); let absoluteSourceRoots; if (!targetOpt?.source?.sourceRoots || targetOpt.source.sourceRoots.length === 0) { absoluteSourceRoots = this.getAbsoluteSourceRoots([constants_1.TaskConstants.DEFAULT_SOURCE_ROOT]); } else { if (!targetOpt.source.sourceRoots.find((value) => value === constants_1.TaskConstants.DEFAULT_SOURCE_ROOT)) { targetOpt.source.sourceRoots.push(constants_1.TaskConstants.DEFAULT_SOURCE_ROOT); } absoluteSourceRoots = this.getAbsoluteSourceRoots(targetOpt.source.sourceRoots); } const masterFiles = this.baseContext.getModuleScanFiles(); return masterFiles.filter((filePath) => { const normalizedFilePath = utils_1.PluginFileUtil.normalize(filePath); return absoluteSourceRoots.some((sourceRootPath) => { return normalizedFilePath.startsWith(sourceRootPath); }); }); } getAbsoluteSourceRoots(sourceRoots) { const moduleRoot = this.moduleContext.getModulePath(); return sourceRoots.map((sourceRoot) => { const absolutePath = utils_1.PluginFileUtil.resolve(moduleRoot, sourceRoot); const normalizedPath = utils_1.PluginFileUtil.normalize(absolutePath); return normalizedPath.endsWith(utils_1.PluginFileUtil.sep) ? normalizedPath : normalizedPath + utils_1.PluginFileUtil.sep; }); } getAnalyzeRouterResults() { const results = []; this.baseContext.getAnalyzeResults().forEach((result) => { if (result.annotation === constants_2.AnnotationConstants.ROUTER_ANNOTATION) { results.push(result); } }); return results; } getRouterTemplatePaths(routers) { const pluginPath = utils_1.PluginFileUtil.pathResolve(__dirname, constants_2.FilePathConstants.PARENT_DELIMITER.repeat(3)); const customTemplates = new Set(); routers.forEach((result) => { if (result.templatePath?.indexOf(pluginPath) === -1) { customTemplates.add(result.templatePath); } }); return customTemplates; } registerCodeGenerationTask(targetName) { this.node.registerTask({ name: this.getTaskName(targetName, constants_1.TaskConstants.CODE_GENERATION_TASK), run: (taskContext) => { this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_CODE_GENERATION); }, input: (input) => { const results = this.getAnalyzeRouterResults(); const config = this.baseContext.config; const propertyVal = JSON.stringify(results) + JSON.stringify(config); input.property('analyzeResults', propertyVal); input.files(Array.from(this.getRouterTemplatePaths(results))); }, output: (output) => { const builderDir = this.baseContext.config.getBuilderDir(); output.file(builderDir); }, dependencies: [], postDependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PRE_BUILD)], }); } registerBuildRouterMapFile(targetName) { this.node.registerTask({ name: this.getTaskName(targetName, constants_1.TaskConstants.BUILD_ROUTER_MAP_TASK), run: () => { this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_ROUTER_MAP_BUILDING); }, input: (input) => { const config = this.baseContext.config; input.property('routerMap', JSON.stringify(this.baseContext.routerMap) + JSON.stringify(config)); input.files(Array.from(this.getRouterTemplatePaths(this.getAnalyzeRouterResults()))); }, output: (output) => { const builderDir = this.baseContext.config.getRouterMapDir(); output.file(builderDir); }, dependencies: [this.getTaskName(targetName, constants_1.TaskConstants.CODE_GENERATION_TASK)], postDependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PRE_BUILD)], }); } registerConfigUpdateTask(targetName) { this.node.registerTask({ name: this.getTaskName(targetName, constants_1.TaskConstants.CONFIG_UPDATE_TASK), run: () => { this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_CONFIG_UPDATE); }, dependencies: [this.getTaskName(targetName, constants_1.TaskConstants.BUILD_ROUTER_MAP_TASK)], postDependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PRE_BUILD)], }); } registerObfuscationTask(targetName) { this.node.registerTask({ name: this.getTaskName(targetName, constants_1.TaskConstants.GENERATE_OBFUSCATION_TASK), run: () => { this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_OBFUSCATION_PROCESS); }, dependencies: [this.getTaskName(targetName, constants_1.TaskConstants.CODE_GENERATION_TASK)], postDependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PRE_BUILD)], }); } registerCopyRouterMapTask(target) { const targetName = target.getTargetName(); this.node.registerTask({ name: this.getTaskName(targetName, constants_1.TaskConstants.COPY_ROUTER_MAP_TASK), run: () => { this.taskManager.context.currentTarget = target; this.taskManager.executeStage(TaskStage_1.TaskStage.AFTER_RESOURCE_PROCESS); }, dependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PROCESS_ROUTER_MAP)], postDependencies: [this.getTaskName(targetName, constants_1.TaskConstants.PROCESS_RESOURCE)], }); } getTaskName(targetName, taskConstant) { return targetName + taskConstant; } } exports.PluginExecutionController = PluginExecutionController;