@alvin917/jsbridge-plugin
Version:
jsbridge Compiler Plugin
151 lines (150 loc) • 7.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BridgeInfo = exports.BridgePluginHandler = void 0;
const hvigor_1 = require("@ohos/hvigor");
const BridgeConstant_1 = require("./BridgeConstant");
const BridgePluginAnalyzer_1 = require("./BridgePluginAnalyzer");
const analyzer_plugin_1 = require("@alvin917/analyzer-plugin");
class BridgePluginHandler extends analyzer_plugin_1.BasePluginHandler {
constructor() {
super(...arguments);
this.annotation = ['Bridge', 'Handler'];
this.bridgeInfos = [];
}
getConfigFileName() {
return BridgeConstant_1.BridgeConstant.CONFIG_FILE_NAME;
}
handle() {
this.context.targets((target) => {
const targetName = target.getTargetName();
this.node.registerTask({
name: `${targetName}@${BridgeConstant_1.BridgeConstant.BRIDGE_GEN_TASK_NAME}`,
run: () => {
this.runTask();
},
dependencies: [`${targetName}@PreBuild`],
postDependencies: [`${targetName}@MergeProfile`]
});
});
}
runTask() {
this.analyze();
this.generateBridgeMap();
this.generateBridgeRules();
this.updateModuleJsonOpt();
this.updateBuildProfileOpt();
}
analyze() {
if (this.scanFiles.length === 0)
return;
this.scanFiles.forEach((filePath) => {
const analyzer = new BridgePluginAnalyzer_1.BridgePluginAnalyzer(filePath, this.annotation);
analyzer.start();
for (let analyzeResult of analyzer.analyzeResultSet) {
const bridgeAnalyzeResult = { ...analyzeResult };
bridgeAnalyzeResult.package = this.config.packageName;
bridgeAnalyzeResult.module = this.config.moduleName;
let sourceFile = this.config.getRelativeSourcePath(filePath);
this.pushBridgeInfo(bridgeAnalyzeResult, sourceFile.replaceAll(analyzer_plugin_1.Constants.FILE_SEPARATOR, analyzer_plugin_1.Constants.DELIMITER));
}
});
}
generateBridgeMap() {
if (this.bridgeInfos.length == 0)
return;
let bridgeMap = {
bridgeMap: this.bridgeInfos.map((bridgeInfo) => {
return bridgeInfo;
})
};
const bridgeMapJsonStr = JSON.stringify(bridgeMap, null, 2);
const bridgeMapFilePath = this.config.getProfileFilePath(BridgeConstant_1.BridgeConstant.BRIDGE_MAP_NAME);
hvigor_1.FileUtil.ensureFileSync(bridgeMapFilePath);
hvigor_1.FileUtil.writeFileSync(bridgeMapFilePath, bridgeMapJsonStr);
}
generateBridgeRules() {
if (this.bridgeInfos.length == 0)
return;
let propertyNameRules = new Set();
propertyNameRules.add('-keep-property-name');
let fileNameRules = new Set();
fileNameRules.add('-keep-file-name');
this.bridgeInfos.forEach((bridgeInfo) => {
propertyNameRules.add(bridgeInfo.name);
propertyNameRules.add(bridgeInfo.handlerName);
let filePaths = bridgeInfo.sourceFile.split('.')[0]?.split(analyzer_plugin_1.Constants.DELIMITER);
filePaths?.forEach((filePath) => {
if (filePath != analyzer_plugin_1.Constants.SRC && filePath != analyzer_plugin_1.Constants.MAIN && filePath != analyzer_plugin_1.Constants.ETS) {
fileNameRules.add(filePath);
}
});
});
let bridgeRulesStr = [...fileNameRules, '\n', ...propertyNameRules].join('\n');
const bridgeRulesFilePath = this.config.getAbsoluteFilePath(BridgeConstant_1.BridgeConstant.BRIDGE_RULES_NAME);
hvigor_1.FileUtil.ensureFileSync(bridgeRulesFilePath);
hvigor_1.FileUtil.writeFileSync(bridgeRulesFilePath, bridgeRulesStr);
}
updateModuleJsonOpt() {
if (this.bridgeInfos.length == 0)
return;
const moduleJsonOpt = this.context.getModuleJsonOpt();
if (!moduleJsonOpt.module.metadata) {
moduleJsonOpt.module.metadata = [];
}
moduleJsonOpt.module.metadata.push({
name: BridgeConstant_1.BridgeConstant.BRIDGE_MAP_METADATA_NAME,
value: "bridge_map.json5 配置信息",
resource: BridgeConstant_1.BridgeConstant.BRIDGE_MAP_METADATA_RESOURCE
});
this.context.setModuleJsonOpt(moduleJsonOpt);
}
updateBuildProfileOpt() {
if (this.bridgeInfos.length == 0)
return;
const buildProfileOpt = this.context.getBuildProfileOpt();
if (!buildProfileOpt.buildOption) {
buildProfileOpt.buildOption = {};
}
if (!buildProfileOpt.buildOption.arkOptions) {
buildProfileOpt.buildOption.arkOptions = {};
}
if (!buildProfileOpt.buildOption.arkOptions.runtimeOnly) {
buildProfileOpt.buildOption.arkOptions.runtimeOnly = {};
}
if (!buildProfileOpt.buildOption.arkOptions.runtimeOnly.sources) {
buildProfileOpt.buildOption.arkOptions.runtimeOnly.sources = [];
}
const sources = buildProfileOpt.buildOption.arkOptions.runtimeOnly.sources;
this.bridgeInfos.forEach((bridgeInfo) => {
sources.push('./' + bridgeInfo.sourceFile);
});
let ruleOptions = buildProfileOpt.buildOptionSet?.find((buildOpt) => buildOpt.name === 'release');
ruleOptions.arkOptions.obfuscation.ruleOptions.files = [...ruleOptions.arkOptions.obfuscation.ruleOptions.files, `./${BridgeConstant_1.BridgeConstant.BRIDGE_RULES_NAME}`];
if (ruleOptions?.arkOptions?.obfuscation?.consumerFiles) {
ruleOptions.arkOptions.obfuscation.consumerFiles = [...ruleOptions.arkOptions.obfuscation.consumerFiles, `./${BridgeConstant_1.BridgeConstant.BRIDGE_RULES_NAME}`];
}
this.context.setBuildProfileOpt(buildProfileOpt);
}
pushBridgeInfo(analyzeResult, sourceFile) {
if (analyzeResult.annotation === 'Handler') {
const handlerName = analyzeResult.functionName;
if (this.bridgeInfos.some((bridgeInfo) => bridgeInfo.handlerName === handlerName)) {
throw new Error("确保Handler的别名以及方法名是唯一的");
}
const aliasName = Reflect.get(analyzeResult, "alias") ?? analyzeResult.functionName;
this.bridgeInfos.push(new BridgeInfo(analyzeResult.package, analyzeResult.module, analyzeResult.name, sourceFile, analyzeResult.functionName, aliasName));
}
}
}
exports.BridgePluginHandler = BridgePluginHandler;
class BridgeInfo {
constructor(packageName, moduleName, name, sourceFile, handlerName, alias) {
this.packageName = packageName;
this.moduleName = moduleName;
this.name = name;
this.sourceFile = sourceFile;
this.handlerName = handlerName;
this.alias = alias;
}
}
exports.BridgeInfo = BridgeInfo;