From 5402ff966f611cbacfdbc733d5da7d7321964e7c Mon Sep 17 00:00:00 2001 From: yangbo_404 Date: Fri, 25 Jul 2025 15:56:40 +0800 Subject: [PATCH] add external api check hook Signed-off-by: yangbo_404 --- compiler/main.js | 19 ++++++ .../fast_build/system_api/api_check_utils.ts | 58 ++++++++++++++++++- 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/compiler/main.js b/compiler/main.js index 5d657d2c1..045c28dd6 100644 --- a/compiler/main.js +++ b/compiler/main.js @@ -79,6 +79,7 @@ let sdkConfigPrefix = 'ohos|system|kit|arkts'; let ohosSystemModulePaths = []; let ohosSystemModuleSubDirPaths = []; let allModulesPaths = []; +let externalApiCheckPlugin = new Map(); function initProjectConfig(projectConfig) { initProjectPathConfig(projectConfig); @@ -794,6 +795,9 @@ function collectExternalModules(sdkPaths) { if (!sdkConfig.apiPath) { continue; } + if (sdkConfig.apiCheckPlugin && sdkConfig.apiCheckPlugin.length > 0) { + collectExternalApiCheckPlugin(sdkConfig, sdkPath); + } let externalApiPathArray = []; if (Array.isArray(sdkConfig.apiPath)) { externalApiPathArray = sdkConfig.apiPath; @@ -818,6 +822,19 @@ function collectExternalModules(sdkPaths) { } } +function collectExternalApiCheckPlugin(sdkConfig, sdkPath) { + const pluginConfigs = sdkConfig.apiCheckPlugin; + pluginConfigs.forEach(config => { + const pluginPrefix = sdkConfig.prefix + '/' + config.tag; + config.path = path.resolve(sdkPath, config.path); + if (externalApiCheckPlugin.get(pluginPrefix)) { + externalApiCheckPlugin.set(pluginPrefix, externalApiCheckPlugin.get(pluginPrefix).push(...pluginConfigs)); + } else { + externalApiCheckPlugin.set(pluginPrefix, [...pluginConfigs]); + } + }); +} + function readHspResource() { if (aceBuildJson.hspResourcesMap) { projectConfig.hspResourcesMap = true; @@ -1119,6 +1136,7 @@ function resetMain() { ohosSystemModulePaths = []; ohosSystemModuleSubDirPaths = []; allModulesPaths = []; + externalApiCheckPlugin = new Map(); } function resetAbilityConfig() { @@ -1209,3 +1227,4 @@ exports.resetGlobalProgram = resetGlobalProgram; exports.setEntryArrayForObf = setEntryArrayForObf; exports.getPackageJsonEntryPath = getPackageJsonEntryPath; exports.setIntentEntryPages = setIntentEntryPages; +exports.externalApiCheckPlugin = externalApiCheckPlugin; diff --git a/compiler/src/fast_build/system_api/api_check_utils.ts b/compiler/src/fast_build/system_api/api_check_utils.ts index 64c268fa3..cf5f7d8f5 100644 --- a/compiler/src/fast_build/system_api/api_check_utils.ts +++ b/compiler/src/fast_build/system_api/api_check_utils.ts @@ -24,7 +24,8 @@ import { ohosSystemModulePaths, systemModules, allModulesPaths, - ohosSystemModuleSubDirPaths + ohosSystemModuleSubDirPaths, + externalApiCheckPlugin } from '../../../main'; import { LogType, @@ -562,13 +563,66 @@ function checkSinceValue(jsDocTags: readonly ts.JSDocTag[], config: ts.JsDocNode if (!isCompliantSince(minSince) || !isCompliantSince(compatibleSdkVersion)) { return false; } - if (hasSince && comparePointVersion(compatibleSdkVersion.toString(), minSince) === -1) { + const sourceFile: ts.SourceFile = currentNode.getSourceFile(); + if (!sourceFile) { + return false; + } + const apiFilePath: string = sourceFile.fileName; + const pluginKey: string = getPluginKey(apiFilePath, SINCE_TAG_NAME); + const needExtrenalApiCheck: boolean = isExternalApiCheck(pluginKey); + if (hasSince && needExtrenalApiCheck ? externalApiCheck(pluginKey, compatibleSdkVersion.toString(), minSince) : + comparePointVersion(compatibleSdkVersion.toString(), minSince) === -1) { config.message = SINCE_TAG_CHECK_ERROER.replace('$SINCE1', minSince).replace('$SINCE2', compatibleSdkVersion); return true; } return false; } +/** + * 是否需要使用拓展SDK自定义校验接口 + * @param { string } pluginKey prefix/tagname + * @returns { boolean } + */ +function isExternalApiCheck(pluginKey: string): boolean { + if (externalApiCheckPlugin.get(pluginKey)) { + return true; + } + return false; +} + +/** + * 获取externalApiCheckPlugin的key + * @param { string } apiFilePath + * @param { string } tagName + * @returns { string } + */ +function getPluginKey(apiFilePath: string, tagName: string): string { + const apiFileName: string = path.basename(apiFilePath); + const apiPrefix: string = apiFileName.split('.')[0]; + const pluginKey: string = apiPrefix + '/' + tagName; + return pluginKey; +} + +/** + * 调用拓展SDK自定义校验接口 + * @param { string } pluginKey + * @param { string } tagValue + * @param { string } targetValue + * @returns { boolean } + */ +function externalApiCheck(pluginKey: string, tagValue: string, targetValue: string): boolean { + const extrenalPlugins = externalApiCheckPlugin.get(pluginKey); + for (let i = 0; i < extrenalPlugins.length; i++) { + const extrenalPlugin = extrenalPlugins[i]; + const extrenalMethod = require(extrenalPlugin.path)[extrenalPlugin.functionName]; + const checkResult: boolean = extrenalMethod(tagValue, targetValue); + if (checkResult) { + return true; + } + } + return false; +} + /** * Confirm compliance since * Only major version can be passed in, such as "19"; -- Gitee