From 5a3f1427371fa1d2d9681ff3ce69006b2c299676 Mon Sep 17 00:00:00 2001 From: lijunru Date: Wed, 13 Aug 2025 16:15:57 +0800 Subject: [PATCH] Interop sdk alias Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICS6ZI Signed-off-by: lijunru --- .../src/common/arkTSConfigGenerator.ts | 104 ++++++++++++------ ets2panda/bindings/src/common/preDefine.ts | 1 + ets2panda/bindings/src/common/types.ts | 1 + .../bindings/src/lsp/generateBuildConfig.ts | 10 +- 4 files changed, 83 insertions(+), 33 deletions(-) diff --git a/ets2panda/bindings/src/common/arkTSConfigGenerator.ts b/ets2panda/bindings/src/common/arkTSConfigGenerator.ts index 7e94bf25f9..a7dc4f2e90 100644 --- a/ets2panda/bindings/src/common/arkTSConfigGenerator.ts +++ b/ets2panda/bindings/src/common/arkTSConfigGenerator.ts @@ -23,7 +23,8 @@ import { LANGUAGE_VERSION, PANDA_SDK_PATH_FROM_SDK, SYSTEM_SDK_PATH_FROM_SDK } f interface DependencyItem { language: string; path: string; - ohmUrl: string; + ohmUrl?: string; + alias?: string[]; } interface ArkTSConfigObject { @@ -42,6 +43,7 @@ export class ArkTSConfigGenerator { private stdlibEscompatPath: string; private systemSdkPath: string; private externalApiPath: string; + private interopApiPath: string; private moduleInfos: Record; private pathSection: Record; @@ -53,6 +55,7 @@ export class ArkTSConfigGenerator { this.stdlibEscompatPath = path.resolve(pandaStdlibPath, 'escompat'); this.systemSdkPath = path.resolve(buildConfig.buildSdkPath, SYSTEM_SDK_PATH_FROM_SDK); this.externalApiPath = buildConfig.externalApiPath !== undefined ? buildConfig.externalApiPath : ''; + this.interopApiPath = buildConfig.interopApiPath !== undefined ? buildConfig.interopApiPath : ''; this.moduleInfos = moduleInfos; this.pathSection = {}; @@ -76,46 +79,66 @@ export class ArkTSConfigGenerator { ArkTSConfigGenerator.instance = undefined; } - private generateSystemSdkPathSection(pathSection: Record): void { - function traverse( - currentDir: string, - relativePath: string = '', - isExcludedDir: boolean = false, - allowedExtensions: string[] = ['.d.ets'] - ): void { - const items = fs.readdirSync(currentDir); - for (const item of items) { - const itemPath = path.join(currentDir, item); - const stat = fs.statSync(itemPath); - const isAllowedFile = allowedExtensions.some((ext) => item.endsWith(ext)); - if (stat.isFile() && !isAllowedFile) { - continue; - } + private traverse( + pathSection: Record, + currentDir: string, + prefix: string = '', + isInteropSdk: boolean = false, + relativePath: string = '', + isExcludedDir: boolean = false, + allowedExtensions: string[] = ['.d.ets'] + ): void { + const items = fs.readdirSync(currentDir); + for (const item of items) { + const itemPath = path.join(currentDir, item); + const stat = fs.statSync(itemPath); + const isAllowedFile = allowedExtensions.some((ext) => item.endsWith(ext)); + const separator = isInteropSdk ? '/' : '.'; + if (stat.isFile() && !isAllowedFile) { + continue; + } - if (stat.isFile()) { - const basename = path.basename(item, '.d.ets'); - const key = isExcludedDir ? basename : relativePath ? `${relativePath}.${basename}` : basename; - pathSection[key] = [changeFileExtension(itemPath, '', '.d.ets')]; - } - if (stat.isDirectory()) { - // For files under api dir excluding arkui/runtime-api dir, - // fill path section with `"pathFromApi.subdir.fileName" = [${absolute_path_to_file}]`; - // For @koalaui files under arkui/runtime-api dir, - // fill path section with `"fileName" = [${absolute_path_to_file}]`. - const isCurrentDirExcluded = path.basename(currentDir) === 'arkui' && item === 'runtime-api'; - const newRelativePath = isCurrentDirExcluded ? '' : relativePath ? `${relativePath}.${item}` : item; - traverse(path.resolve(currentDir, item), newRelativePath, isCurrentDirExcluded || isExcludedDir); - } + if (stat.isFile()) { + const basename = path.basename(item, '.d.ets'); + const key = isExcludedDir ? basename : relativePath ? `${relativePath}${separator}${basename}` : basename; + pathSection[prefix + key] = isInteropSdk + ? { + language: 'js', + path: itemPath, + ohmUrl: '', + alias: [key] + } + : [changeFileExtension(itemPath, '', '.d.ets')]; + } + if (stat.isDirectory()) { + // For files under api dir excluding arkui/runtime-api dir, + // fill path section with `"pathFromApi.subdir.fileName" = [${absolute_path_to_file}]`; + // For @koalaui files under arkui/runtime-api dir, + // fill path section with `"fileName" = [${absolute_path_to_file}]`. + const isCurrentDirExcluded = path.basename(currentDir) === 'arkui' && item === 'runtime-api'; + const newRelativePath = isCurrentDirExcluded ? '' : relativePath ? `${relativePath}${separator}${item}` : item; + this.traverse( + pathSection, + path.resolve(currentDir, item), + prefix, + isInteropSdk, + newRelativePath, + isCurrentDirExcluded || isExcludedDir + ); } } + } + private generateSystemSdkPathSection(pathSection: Record): void { let directoryNames: string[] = ['api', 'arkts', 'kits']; directoryNames.forEach((dir) => { let systemSdkPath = path.resolve(this.systemSdkPath, dir); let externalApiPath = path.resolve(this.externalApiPath, dir); - fs.existsSync(systemSdkPath) ? traverse(systemSdkPath) : console.warn(`sdk path ${systemSdkPath} not exist.`); + fs.existsSync(systemSdkPath) + ? this.traverse(pathSection, systemSdkPath) + : console.warn(`sdk path ${systemSdkPath} not exist.`); fs.existsSync(externalApiPath) - ? traverse(externalApiPath) + ? this.traverse(pathSection, externalApiPath) : console.warn(`sdk path ${externalApiPath} not exist.`); }); } @@ -199,7 +222,24 @@ export class ArkTSConfigGenerator { }); } + private generateSystemSdkDependenciesSection(dependencySection: Record): void { + let directoryNames: string[] = ['api', 'arkts', 'kits', 'component']; + directoryNames.forEach((dirName) => { + const basePath = path.resolve(this.interopApiPath, dirName); + if (!fs.existsSync(basePath)) { + console.warn(`interop sdk path ${basePath} not exist.`); + return; + } + if (dirName === 'component') { + this.traverse(dependencySection, basePath, 'component/', true); + } else { + this.traverse(dependencySection, basePath, 'dynamic/', true); + } + }); + } + private getDependenciesSection(moduleInfo: ModuleInfo, dependencySection: Record): void { + this.generateSystemSdkDependenciesSection(dependencySection); let depModules: string[] = moduleInfo.dynamicDepModuleInfos; depModules.forEach((depModuleName: string) => { let depModuleInfo = this.moduleInfos[depModuleName]; diff --git a/ets2panda/bindings/src/common/preDefine.ts b/ets2panda/bindings/src/common/preDefine.ts index 79f470bd73..30bec4a221 100644 --- a/ets2panda/bindings/src/common/preDefine.ts +++ b/ets2panda/bindings/src/common/preDefine.ts @@ -25,6 +25,7 @@ export const DECL_ETS_SUFFIX: string = '.d.ets'; export const PANDA_SDK_PATH_FROM_SDK: string = './build-tools/ets2panda'; export const SYSTEM_SDK_PATH_FROM_SDK: string = './'; export const EXTERNAL_API_PATH_FROM_SDK: string = '../../../hms/ets/ets1.2'; +export const INTEROP_API_PATH_FROM_SDK: string = '../ets1.1/build-tools/interop'; export const DEFAULT_CACHE_DIR: string = './.idea/.deveco'; export const ETS_SUFFIX: string = '.ets'; export const TS_SUFFIX: string = '.ts'; diff --git a/ets2panda/bindings/src/common/types.ts b/ets2panda/bindings/src/common/types.ts index 918cc29bd0..4a5c80a36f 100644 --- a/ets2panda/bindings/src/common/types.ts +++ b/ets2panda/bindings/src/common/types.ts @@ -165,6 +165,7 @@ export interface PathConfig { cacheDir?: string; externalApiPath?: string; aceModuleJsonPath?: string; + interopApiPath?: string; } export interface DeclgenConfig { diff --git a/ets2panda/bindings/src/lsp/generateBuildConfig.ts b/ets2panda/bindings/src/lsp/generateBuildConfig.ts index 67d98d2578..4786a41809 100644 --- a/ets2panda/bindings/src/lsp/generateBuildConfig.ts +++ b/ets2panda/bindings/src/lsp/generateBuildConfig.ts @@ -17,7 +17,12 @@ import * as fs from 'fs'; import * as path from 'path'; import * as JSON5 from 'json5'; import { BuildConfig, PathConfig } from '../common/types'; -import { DEFAULT_CACHE_DIR, EXTERNAL_API_PATH_FROM_SDK, LANGUAGE_VERSION } from '../common/preDefine'; +import { + DEFAULT_CACHE_DIR, + EXTERNAL_API_PATH_FROM_SDK, + INTEROP_API_PATH_FROM_SDK, + LANGUAGE_VERSION +} from '../common/preDefine'; import { getFileLanguageVersion } from '../common/utils'; export interface ModuleDescriptor { @@ -232,6 +237,9 @@ export function generateBuildConfigs( externalApiPath: pathConfig.externalApiPath ? pathConfig.externalApiPath : path.resolve(pathConfig.buildSdkPath, EXTERNAL_API_PATH_FROM_SDK), + interopApiPath: pathConfig.interopApiPath + ? pathConfig.interopApiPath + : path.resolve(pathConfig.buildSdkPath, INTEROP_API_PATH_FROM_SDK), cacheDir: pathConfig.cacheDir !== undefined ? pathConfig.cacheDir : path.join(pathConfig.projectPath, DEFAULT_CACHE_DIR), declFilesPath: -- Gitee