From 9162aaf7c50459d284ac82436645b8cb68d9fe55 Mon Sep 17 00:00:00 2001 From: lijunru Date: Thu, 12 Jun 2025 21:17:36 +0800 Subject: [PATCH] Generate module config refactoring Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICETCQ Signed-off-by: lijunru --- ets2panda/bindings/src/arktsConfigGenerate.ts | 58 ------ ets2panda/bindings/src/build/buildMode.ts | 188 ------------------ ets2panda/bindings/src/buildConfigGenerate.ts | 79 ++++---- ets2panda/bindings/src/driver_helper.ts | 3 +- .../src/{build => }/generateArkTSConfig.ts | 134 +++++++++---- ets2panda/bindings/src/index.ts | 4 +- ets2panda/bindings/src/lsp_helper.ts | 171 ++++++++-------- ets2panda/bindings/src/preDefine.ts | 3 +- ets2panda/bindings/src/types.ts | 64 ++---- ets2panda/bindings/test/run_tests.ts | 24 +-- 10 files changed, 242 insertions(+), 486 deletions(-) delete mode 100644 ets2panda/bindings/src/arktsConfigGenerate.ts delete mode 100644 ets2panda/bindings/src/build/buildMode.ts rename ets2panda/bindings/src/{build => }/generateArkTSConfig.ts (58%) diff --git a/ets2panda/bindings/src/arktsConfigGenerate.ts b/ets2panda/bindings/src/arktsConfigGenerate.ts deleted file mode 100644 index 56f00052ed..0000000000 --- a/ets2panda/bindings/src/arktsConfigGenerate.ts +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { BuildMode } from './build/buildMode'; -import { BuildConfig } from './types'; -import { ModuleDescriptor, generateBuildConfigs } from './buildConfigGenerate'; -import { PANDA_SDK_PATH_FROM_SDK } from './preDefine'; - -import * as fs from 'fs'; -import * as path from 'path'; -import { PluginDriver } from './ui_plugins_driver'; - -function processBuildConfig(projectConfig: BuildConfig): BuildConfig { - let buildConfig: BuildConfig = { ...projectConfig }; - let buildSdkPath: string = buildConfig.buildSdkPath as string; - buildConfig.pandaSdkPath = buildConfig.pandaSdkPath ?? path.resolve(buildSdkPath, PANDA_SDK_PATH_FROM_SDK); - PluginDriver.getInstance().initPlugins(buildConfig); - return buildConfig; -} - -export function generateArkTsConfigByModules( - buildSdkPath: string, - projectRoot: string, - modules?: ModuleDescriptor[] -): void { - const allBuildConfig = generateBuildConfigs(buildSdkPath, projectRoot, modules); - let compileFileInfos: Record = {}; - const cacheDir = path.join(projectRoot, '.idea', '.deveco'); - const compileFileInfosPath = path.join(cacheDir, 'lsp_compileFileInfos.json'); - Object.keys(allBuildConfig).forEach((moduleName) => { - const moduleConfig = allBuildConfig[moduleName] as BuildConfig; - const processedConfig = processBuildConfig(moduleConfig); - - const buildMode = new BuildMode(processedConfig); - buildMode.generateArkTSConfig(compileFileInfos); - }); - try { - const jsonCompileFileInfos = JSON.stringify(compileFileInfos, null, 2); - if (!fs.existsSync(cacheDir)) { - fs.mkdirSync(cacheDir, { recursive: true }); - } - fs.writeFileSync(compileFileInfosPath, jsonCompileFileInfos, 'utf-8'); - } catch (err) { - console.error(`Failed to write compileFileInfos to ${compileFileInfosPath} with error: ${err}`); - } -} diff --git a/ets2panda/bindings/src/build/buildMode.ts b/ets2panda/bindings/src/build/buildMode.ts deleted file mode 100644 index d1be834b99..0000000000 --- a/ets2panda/bindings/src/build/buildMode.ts +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Copyright (c) 2025 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import * as path from 'path'; - -import { ABC_SUFFIX, ARKTSCONFIG_JSON_FILE, LANGUAGE_VERSION } from '../preDefine'; -import { changeFileExtension } from '../utils'; -import { BuildConfig, DependentModuleConfig, ModuleInfo, CompileFileInfo } from '../types'; -import { ArkTSConfigGenerator } from './generateArkTSConfig'; - -export class BuildMode { - buildConfig: BuildConfig; - entryFiles: Set; - compileFiles: Map; - cacheDir: string; - pandaSdkPath: string; - buildSdkPath: string; - packageName: string; - sourceRoots: string[]; - moduleRootPath: string; - moduleType: string; - dependentModuleList: DependentModuleConfig[]; - moduleInfos: Map; - declgenV1OutPath: string | undefined; - declgenBridgeCodePath: string | undefined; - hasMainModule: boolean; - - constructor(buildConfig: BuildConfig) { - this.buildConfig = buildConfig; - this.entryFiles = new Set(buildConfig.compileFiles as string[]); - this.cacheDir = buildConfig.cachePath as string; - this.pandaSdkPath = buildConfig.pandaSdkPath as string; - this.buildSdkPath = buildConfig.buildSdkPath as string; - this.packageName = buildConfig.packageName as string; - this.sourceRoots = buildConfig.sourceRoots as string[]; - this.moduleRootPath = buildConfig.moduleRootPath as string; - this.moduleType = buildConfig.moduleType as string; - this.dependentModuleList = buildConfig.dependentModuleList; - this.hasMainModule = buildConfig.hasMainModule; - - this.declgenV1OutPath = buildConfig.declgenV1OutPath as string | undefined; - this.declgenBridgeCodePath = buildConfig.declgenBridgeCodePath as string | undefined; - - this.moduleInfos = new Map(); - this.compileFiles = new Map(); - } - - private getDependentModules(moduleInfo: ModuleInfo): Map[] { - let dynamicDepModules: Map = new Map(); - let staticDepModules: Map = new Map(); - - if (moduleInfo.isMainModule) { - this.moduleInfos.forEach((module: ModuleInfo, packageName: string) => { - if (module.isMainModule) { - return; - } - module.language === LANGUAGE_VERSION.ARKTS_1_2 - ? staticDepModules.set(packageName, module) - : dynamicDepModules.set(packageName, module); - }); - return [dynamicDepModules, staticDepModules]; - } - - if (moduleInfo.dependencies) { - moduleInfo.dependencies.forEach((packageName: string) => { - let depModuleInfo: ModuleInfo | undefined = this.moduleInfos.get(packageName); - if (!depModuleInfo) { - console.error(`Module ${packageName} not found in moduleInfos`); - } else { - depModuleInfo.language === LANGUAGE_VERSION.ARKTS_1_2 - ? staticDepModules.set(packageName, depModuleInfo) - : dynamicDepModules.set(packageName, depModuleInfo); - } - }); - } - return [dynamicDepModules, staticDepModules]; - } - - private generateArkTSConfigForModules(compileFileInfos: Record): void { - let generator = ArkTSConfigGenerator.getGenerator(this.buildConfig, this.moduleInfos); - this.moduleInfos.forEach((moduleInfo: ModuleInfo, moduleRootPath: string) => { - for (const fileInfo of moduleInfo.compileFileInfos) { - compileFileInfos[fileInfo.filePath] = fileInfo.arktsConfigFile; - } - generator.writeArkTSConfigFile(moduleInfo); - }); - } - - private collectDepModuleInfos(): void { - this.moduleInfos.forEach((moduleInfo) => { - let [dynamicDepModules, staticDepModules] = this.getDependentModules(moduleInfo); - moduleInfo.dynamicDepModuleInfos = dynamicDepModules; - moduleInfo.staticDepModuleInfos = staticDepModules; - }); - } - - private collectModuleInfos(): void { - if (this.hasMainModule && (!this.packageName || !this.moduleRootPath || !this.sourceRoots)) { - console.error('Main module info from hvigor is not correct.'); - } - let mainModuleInfo: ModuleInfo = { - isMainModule: this.hasMainModule, - packageName: this.packageName, - moduleRootPath: this.moduleRootPath, - moduleType: this.moduleType, - sourceRoots: this.sourceRoots, - entryFile: '', - arktsConfigFile: path.resolve(this.cacheDir, this.packageName, ARKTSCONFIG_JSON_FILE), - dynamicDepModuleInfos: new Map(), - staticDepModuleInfos: new Map(), - compileFileInfos: [], - declgenV1OutPath: this.declgenV1OutPath, - declgenBridgeCodePath: this.declgenBridgeCodePath - }; - this.moduleInfos.set(this.packageName, mainModuleInfo); - this.dependentModuleList.forEach((module: DependentModuleConfig) => { - if (!module.packageName || !module.modulePath || !module.sourceRoots || !module.entryFile) { - console.error('Dependent module info from hvigor is not correct.'); - } - let moduleInfo: ModuleInfo = { - isMainModule: false, - packageName: module.packageName, - moduleRootPath: module.modulePath, - moduleType: module.moduleType, - sourceRoots: module.sourceRoots, - entryFile: module.entryFile, - arktsConfigFile: path.resolve(this.cacheDir, module.packageName, ARKTSCONFIG_JSON_FILE), - compileFileInfos: [], - dynamicDepModuleInfos: new Map(), - staticDepModuleInfos: new Map(), - declgenV1OutPath: undefined, - declgenBridgeCodePath: undefined, - language: module.language, - declFilesPath: module.declFilesPath, - dependencies: module.dependencies - }; - this.moduleInfos.set(module.packageName, moduleInfo); - }); - this.collectDepModuleInfos(); - } - - private collectCompileFiles(): void { - this.entryFiles.forEach((file: string) => { - for (const [packageName, moduleInfo] of this.moduleInfos) { - if (!file.startsWith(moduleInfo.moduleRootPath)) { - continue; - } - let filePathFromModuleRoot: string = path.relative(moduleInfo.moduleRootPath, file); - let filePathInCache: string = path.join(this.cacheDir, moduleInfo.packageName, filePathFromModuleRoot); - let abcFilePath: string = path.resolve(changeFileExtension(filePathInCache, ABC_SUFFIX)); - - let fileInfo: CompileFileInfo = { - filePath: file, - dependentFiles: [], - abcFilePath: abcFilePath, - arktsConfigFile: moduleInfo.arktsConfigFile, - packageName: moduleInfo.packageName - }; - moduleInfo.compileFileInfos.push(fileInfo); - this.compileFiles.set(file, fileInfo); - return; - } - console.error('File does not belong to any module in moduleInfos.'); - }); - } - - public generateModuleInfos(): void { - this.collectModuleInfos(); - this.collectCompileFiles(); - } - - public async generateArkTSConfig(compileFileInfos: Record): Promise { - this.generateModuleInfos(); - this.generateArkTSConfigForModules(compileFileInfos); - } -} diff --git a/ets2panda/bindings/src/buildConfigGenerate.ts b/ets2panda/bindings/src/buildConfigGenerate.ts index 478196c6aa..1da7a7038b 100644 --- a/ets2panda/bindings/src/buildConfigGenerate.ts +++ b/ets2panda/bindings/src/buildConfigGenerate.ts @@ -16,7 +16,7 @@ import * as fs from 'fs'; import * as path from 'path'; import * as JSON5 from 'json5'; -import { BuildConfig } from './types'; +import { BuildConfig, PathConfig } from './types'; export interface ModuleDescriptor { arktsversion: string; @@ -92,9 +92,8 @@ function getEtsFiles(modulePath: string): string[] { const files: string[] = []; const shouldSkipDirectory = (relativePath: string): boolean => { - const testDir1 = `src${path.sep}test`; - const testDir2 = `src${path.sep}ohosTest`; - return relativePath.startsWith(testDir1) || relativePath.startsWith(testDir2); + const filterList = [`src${path.sep}test`, `src${path.sep}ohosTest`, `build${path.sep}`, `oh_modules${path.sep}`]; + return filterList.some((directoryPrefix: string) => relativePath.startsWith(directoryPrefix)); }; const processEntry = (dir: string, entry: fs.Dirent): void => { @@ -150,15 +149,8 @@ function getModuleDependencies(modulePath: string, visited = new Set()): } }; - const resolveNestedDependencies = (deps: string[]): string[] => { - return deps.flatMap((depPath) => - visited.has(depPath) ? [] : [depPath, ...getModuleDependencies(depPath, visited)] - ); - }; - const dependencies = extractDependencies(); - const nestedDependencies = resolveNestedDependencies(dependencies); - return Array.from(new Set([...dependencies, ...nestedDependencies])); + return Array.from(new Set([...dependencies])); } function createMapEntryForPlugin(buildSdkPath: string, pluginName: string): string { @@ -175,67 +167,68 @@ function createPluginMap(buildSdkPath: string): Record { } export function generateBuildConfigs( - buildSdkPath: string, - projectRoot: string, + pathConfig: PathConfig, modules?: ModuleDescriptor[] ): Record { const allBuildConfigs: Record = {}; if (!modules) { - const buildProfilePath = path.join(projectRoot, 'build-profile.json5'); + const buildProfilePath = path.join(pathConfig.projectPath, 'build-profile.json5'); modules = getModulesFromBuildProfile(buildProfilePath); } const definedModules = modules; - const cacheDir = path.join(projectRoot, '.idea', '.deveco'); + + const enableDeclgen: Map = new Map(modules.map((module) => [module.name, false])); for (const module of definedModules) { const modulePath = module.srcPath; const compileFiles = new Set(getEtsFiles(modulePath)); - const pluginMap = createPluginMap(buildSdkPath); + const pluginMap = createPluginMap(pathConfig.buildSdkPath); // Get recursive dependencies const dependencies = getModuleDependencies(modulePath); for (const depPath of dependencies) { getEtsFiles(depPath).forEach((file) => compileFiles.add(file)); + const depModule = definedModules.find((m) => m.srcPath === depPath); + if (module.arktsversion === '1.1' && depModule?.arktsversion === '1.2') { + enableDeclgen.set(depModule.name, true); + } } allBuildConfigs[module.name] = { plugins: pluginMap, - arkts: {}, - arktsGlobal: {}, compileFiles: Array.from(compileFiles), packageName: module.name, moduleType: module.moduleType, - buildType: 'build', - buildMode: 'Debug', moduleRootPath: modulePath, - sourceRoots: ['./'], - hasMainModule: true, - loaderOutPath: path.join(modulePath, 'build', 'default', 'cache'), - cachePath: cacheDir, - buildSdkPath: buildSdkPath, + language: module.arktsversion, + buildSdkPath: pathConfig.buildSdkPath, + projectPath: pathConfig.projectPath, + declgenOutDir: pathConfig.declgenOutDir, enableDeclgenEts2Ts: false, - declgenDtsOutPath: path.join(modulePath, 'build', 'default', 'cache'), - declgenTsOutPath: path.join(modulePath, 'build', 'default', 'cache'), - dependentModuleList: dependencies.map((dep) => { + declFilesPath: + module.arktsversion === '1.1' + ? path.join(pathConfig.declgenOutDir, 'static', module.name, 'decl-fileInfo.json') + : undefined, + dependencies: dependencies.map((dep) => { const depModule = definedModules.find((m) => m.srcPath === dep); - return { - packageName: path.basename(dep), - moduleName: path.basename(dep), - moduleType: depModule ? depModule.moduleType : 'har', - modulePath: dep, - sourceRoots: ['./'], - entryFile: 'index.ets', - language: depModule ? depModule.arktsversion : '1.1' - }; + return depModule!.name; }) }; } - const outputPath = path.join(cacheDir, 'lsp_build_config.json'); - if (!fs.existsSync(cacheDir)) { - fs.mkdirSync(cacheDir, { recursive: true }); - } - fs.writeFileSync(outputPath, JSON.stringify(allBuildConfigs, null, 4)); + Object.entries(allBuildConfigs).forEach(([key, config]) => { + if (enableDeclgen.get(key) === true) { + config.enableDeclgenEts2Ts = true; + config.declgenV1OutPath = path.join(pathConfig.declgenOutDir, 'dynamic', 'declgenV1'); + config.declgenBridgeCodePath = path.join(pathConfig.declgenOutDir, 'dynamic', 'declgenBridgeCode'); + if (!fs.existsSync(config.declgenV1OutPath)) { + fs.mkdirSync(config.declgenV1OutPath, { recursive: true }); + } + if (!fs.existsSync(config.declgenBridgeCodePath)) { + fs.mkdirSync(config.declgenBridgeCodePath, { recursive: true }); + } + } + }); return allBuildConfigs; } diff --git a/ets2panda/bindings/src/driver_helper.ts b/ets2panda/bindings/src/driver_helper.ts index 2422969bf3..ad435334f1 100644 --- a/ets2panda/bindings/src/driver_helper.ts +++ b/ets2panda/bindings/src/driver_helper.ts @@ -80,8 +80,7 @@ export class DriverHelper { public generateTsDecl(declOutPath: string, etsOutPath: string, exportAll: boolean, isolated: boolean): void { let exportAll_: KBoolean = exportAll ? 1 : 0; let isolated_: KBoolean = isolated ? 1 : 0; - global.es2panda._GenerateTsDeclarationsFromContext(this._cfg.peer, declOutPath, etsOutPath, - exportAll_, isolated_); + global.es2panda._GenerateTsDeclarationsFromContext(this._cfg.peer, declOutPath, etsOutPath, exportAll_, isolated_); } } diff --git a/ets2panda/bindings/src/build/generateArkTSConfig.ts b/ets2panda/bindings/src/generateArkTSConfig.ts similarity index 58% rename from ets2panda/bindings/src/build/generateArkTSConfig.ts rename to ets2panda/bindings/src/generateArkTSConfig.ts index fe1dc108fc..2fb3668c8c 100644 --- a/ets2panda/bindings/src/build/generateArkTSConfig.ts +++ b/ets2panda/bindings/src/generateArkTSConfig.ts @@ -16,14 +16,20 @@ import * as path from 'path'; import * as fs from 'fs'; -import { changeFileExtension, ensurePathExists } from '../utils'; -import { BuildConfig, ModuleInfo } from '../types'; -import { LANGUAGE_VERSION, SYSTEM_SDK_PATH_FROM_SDK } from '../preDefine'; +import { changeFileExtension, ensurePathExists } from './utils'; +import { BuildConfig, ModuleInfo } from './types'; +import { + ARKTSCONFIG_JSON_FILE, + CACHE_DIR, + LANGUAGE_VERSION, + PANDA_SDK_PATH_FROM_SDK, + SYSTEM_SDK_PATH_FROM_SDK +} from './preDefine'; interface DynamicPathItem { language: string; declPath: string; - runtimeName: string; + ohmUrl: string; } interface ArkTSConfigObject { @@ -37,18 +43,17 @@ interface ArkTSConfigObject { }; } -export class ArkTSConfigGenerator { - private static instance: ArkTSConfigGenerator | undefined; +class ArkTSConfigGenerator { private stdlibStdPath: string; private stdlibEscompatPath: string; private systemSdkPath: string; - private moduleInfos: Map; + private moduleInfos: Record; private pathSection: Record; - private constructor(buildConfig: BuildConfig, moduleInfos: Map) { - let pandaStdlibPath: string = - buildConfig.pandaStdlibPath ?? path.resolve(buildConfig.pandaSdkPath!!, 'lib', 'stdlib'); + private constructor(buildConfig: BuildConfig, moduleInfos: Record) { + let pandaSdkPath = path.resolve(buildConfig.buildSdkPath, PANDA_SDK_PATH_FROM_SDK); + let pandaStdlibPath: string = path.resolve(pandaSdkPath, 'lib', 'stdlib'); this.stdlibStdPath = path.resolve(pandaStdlibPath, 'std'); this.stdlibEscompatPath = path.resolve(pandaStdlibPath, 'escompat'); this.systemSdkPath = path.resolve(buildConfig.buildSdkPath, SYSTEM_SDK_PATH_FROM_SDK); @@ -57,24 +62,10 @@ export class ArkTSConfigGenerator { this.pathSection = {}; } - public static getInstance(buildConfig?: BuildConfig, moduleInfos?: Map): ArkTSConfigGenerator { - if (!ArkTSConfigGenerator.instance) { - if (!buildConfig || !moduleInfos) { - throw new Error('buildConfig and moduleInfos is required for the first instantiation of ArkTSConfigGenerator.'); - } - ArkTSConfigGenerator.instance = new ArkTSConfigGenerator(buildConfig, moduleInfos); - } - return ArkTSConfigGenerator.instance; - } - - public static getGenerator(buildConfig: BuildConfig, moduleInfos: Map): ArkTSConfigGenerator { + public static getGenerator(buildConfig: BuildConfig, moduleInfos: Record): ArkTSConfigGenerator { return new ArkTSConfigGenerator(buildConfig, moduleInfos); } - public static destroyInstance(): void { - ArkTSConfigGenerator.instance = undefined; - } - private generateSystemSdkPathSection(pathSection: Record): void { function traverse(currentDir: string, relativePath: string = '', isExcludedDir: boolean = false): void { const items = fs.readdirSync(currentDir); @@ -109,7 +100,7 @@ export class ArkTSConfigGenerator { fs.existsSync(kitsPath) ? traverse(kitsPath) : console.error(`sdk path ${kitsPath} not exist.`); } - private getPathSection(): Record { + private getPathSection(moduleInfo: ModuleInfo): Record { if (Object.keys(this.pathSection).length !== 0) { return this.pathSection; } @@ -119,18 +110,18 @@ export class ArkTSConfigGenerator { this.generateSystemSdkPathSection(this.pathSection); - this.moduleInfos.forEach((moduleInfo: ModuleInfo, packageName: string) => { - if (moduleInfo.language === LANGUAGE_VERSION.ARKTS_1_2) { - this.pathSection[moduleInfo.packageName] = [path.resolve(moduleInfo.moduleRootPath, moduleInfo.sourceRoots[0])]; - } + Object.values(moduleInfo.staticDepModuleInfos).forEach((depModuleName: string) => { + let depModuleInfo = this.moduleInfos[depModuleName]; + this.pathSection[depModuleInfo.packageName] = [path.resolve(depModuleInfo.moduleRootPath)]; }); return this.pathSection; } private getDependenciesSection(moduleInfo: ModuleInfo, dependenciesSection: string[]): void { - let depModules: Map = moduleInfo.staticDepModuleInfos; - depModules.forEach((depModuleInfo: ModuleInfo) => { + let depModules: string[] = moduleInfo.staticDepModuleInfos; + depModules.forEach((depModuleName: string) => { + let depModuleInfo = this.moduleInfos[depModuleName]; dependenciesSection.push(depModuleInfo.arktsConfigFile); }); } @@ -142,9 +133,10 @@ export class ArkTSConfigGenerator { } private getDynamicPathSection(moduleInfo: ModuleInfo, dynamicPathSection: Record): void { - let depModules: Map = moduleInfo.dynamicDepModuleInfos; + let depModules: string[] = moduleInfo.dynamicDepModuleInfos; - depModules.forEach((depModuleInfo: ModuleInfo) => { + depModules.forEach((depModuleName: string) => { + let depModuleInfo = this.moduleInfos[depModuleName]; if (!depModuleInfo.declFilesPath || !fs.existsSync(depModuleInfo.declFilesPath)) { console.error(`Module ${moduleInfo.packageName} depends on dynamic module ${depModuleInfo.packageName}, but decl file not found on path ${depModuleInfo.declFilesPath}`); @@ -156,7 +148,7 @@ export class ArkTSConfigGenerator { dynamicPathSection[ohmurl] = { language: 'js', declPath: declFilesObject.files[file].declPath, - runtimeName: declFilesObject.files[file].ohmUrl + ohmUrl: declFilesObject.files[file].ohmUrl }; let absFilePath: string = path.resolve(depModuleInfo.moduleRootPath, file); @@ -169,15 +161,13 @@ export class ArkTSConfigGenerator { } public writeArkTSConfigFile(moduleInfo: ModuleInfo): void { - if (!moduleInfo.sourceRoots || moduleInfo.sourceRoots.length === 0) { - console.error('SourceRoots not set from hvigor.'); - } - let pathSection = this.getPathSection(); + let pathSection = this.getPathSection(moduleInfo); let dependenciesSection: string[] = []; + this.getDependenciesSection(moduleInfo, dependenciesSection); let dynamicPathSection: Record = {}; this.getDynamicPathSection(moduleInfo, dynamicPathSection); - let baseUrl: string = path.resolve(moduleInfo.moduleRootPath, moduleInfo.sourceRoots[0]); + let baseUrl: string = path.resolve(moduleInfo.moduleRootPath); let arktsConfig: ArkTSConfigObject = { compilerOptions: { package: moduleInfo.packageName, @@ -193,3 +183,67 @@ export class ArkTSConfigGenerator { fs.writeFileSync(moduleInfo.arktsConfigFile, JSON.stringify(arktsConfig, null, 2), 'utf-8'); } } + +function collectDepModuleInfos(moduleInfo: ModuleInfo, allBuildConfig: Record): void { + let dynamicDepModules: string[] = []; + let staticDepModules: string[] = []; + + if (moduleInfo.dependencies) { + moduleInfo.dependencies.forEach((moduleName: string) => { + let depModule = allBuildConfig[moduleName]; + depModule.language === LANGUAGE_VERSION.ARKTS_1_2 + ? staticDepModules.push(depModule.packageName) + : dynamicDepModules.push(depModule.packageName); + }); + } + moduleInfo.dynamicDepModuleInfos = dynamicDepModules; + moduleInfo.staticDepModuleInfos = staticDepModules; +} + +function collectModuleInfos(allBuildConfig: Record): Record { + let moduleInfos: Record = {}; + Object.values(allBuildConfig).forEach((buildConfig) => { + let moduleInfo = generateModuleInfo(allBuildConfig, buildConfig); + moduleInfos[moduleInfo.packageName] = moduleInfo; + }); + return moduleInfos; +} + +export function generateModuleInfo(allBuildConfig: Record, buildConfig: BuildConfig): ModuleInfo { + if (!buildConfig.packageName || !buildConfig.moduleRootPath) { + console.error('Main buildConfig info from hvigor is not correct.'); + } + let moduleInfo: ModuleInfo = { + packageName: buildConfig.packageName, + moduleRootPath: buildConfig.moduleRootPath, + moduleType: buildConfig.moduleType, + entryFile: buildConfig.packageName !== 'entry' ? path.join(buildConfig.moduleRootPath, 'Index.ets') : '', + arktsConfigFile: path.resolve(buildConfig.projectPath, CACHE_DIR, buildConfig.packageName, ARKTSCONFIG_JSON_FILE), + compileFiles: buildConfig.compileFiles, + declgenV1OutPath: buildConfig.declgenV1OutPath, + declgenBridgeCodePath: buildConfig.declgenBridgeCodePath, + staticDepModuleInfos: [], + dynamicDepModuleInfos: [], + language: buildConfig.language, + dependencies: buildConfig.dependencies, + declFilesPath: buildConfig.declFilesPath + }; + collectDepModuleInfos(moduleInfo, allBuildConfig); + return moduleInfo; +} + +export function generateArkTsConfigs(allBuildConfig: Record): Record { + let moduleInfos: Record = collectModuleInfos(allBuildConfig); + Object.keys(moduleInfos).forEach((filePath: string) => { + let packageName = moduleInfos[filePath].packageName; + let generator = ArkTSConfigGenerator.getGenerator(allBuildConfig[packageName], moduleInfos); + generator.writeArkTSConfigFile(moduleInfos[filePath]); + }); + let fileToModuleInfo: Record = {}; + Object.values(moduleInfos).forEach((moduleInfo: ModuleInfo) => { + moduleInfo.compileFiles.forEach((file: string) => { + fileToModuleInfo[file] = moduleInfo; + }); + }); + return fileToModuleInfo; +} diff --git a/ets2panda/bindings/src/index.ts b/ets2panda/bindings/src/index.ts index 7f57228711..e4f2f94151 100644 --- a/ets2panda/bindings/src/index.ts +++ b/ets2panda/bindings/src/index.ts @@ -14,7 +14,6 @@ */ export { Lsp } from './lsp_helper'; -export { DriverHelper } from './driver_helper'; export { Es2pandaContextState } from './generated/Es2pandaEnums'; export { LspCompletionInfo, @@ -26,6 +25,5 @@ export { LspQuickInfo, LspSymbolDisplayPart } from './lspNode'; -export { generateArkTsConfigByModules } from './arktsConfigGenerate'; export type { ModuleDescriptor } from './buildConfigGenerate'; -export type { TextDocumentChangeInfo } from './lsp_helper'; +export type { PathConfig, TextDocumentChangeInfo } from './types'; diff --git a/ets2panda/bindings/src/lsp_helper.ts b/ets2panda/bindings/src/lsp_helper.ts index 2995d1ef79..6caf51ed58 100644 --- a/ets2panda/bindings/src/lsp_helper.ts +++ b/ets2panda/bindings/src/lsp_helper.ts @@ -46,18 +46,28 @@ import { } from './lspNode'; import { passStringArray, unpackString } from './private'; import { Es2pandaContextState } from './generated/Es2pandaEnums'; -import { BuildConfig, Config, FileDepsInfo, Job, JobInfo, WorkerInfo } from './types'; +import { + BuildConfig, + Config, + FileDepsInfo, + Job, + JobInfo, + WorkerInfo, + ModuleInfo, + PathConfig, + TextDocumentChangeInfo +} from './types'; import { PluginDriver, PluginHook } from './ui_plugins_driver'; -import { ModuleDescriptor } from './buildConfigGenerate'; -import { generateArkTsConfigByModules } from './arktsConfigGenerate'; +import { ModuleDescriptor, generateBuildConfigs } from './buildConfigGenerate'; +import { generateArkTsConfigs, generateModuleInfo } from './generateArkTSConfig'; import * as fs from 'fs'; import * as path from 'path'; import { KNativePointer } from './InteropTypes'; import { Worker as ThreadWorker } from 'worker_threads'; -import { ensurePathExists, isMac } from './utils'; +import { ensurePathExists } from './utils'; import * as child_process from 'child_process'; -import { DECL_ETS_SUFFIX } from './preDefine'; +import { CACHE_DIR, DECL_ETS_SUFFIX } from './preDefine'; import * as crypto from 'crypto'; import * as os from 'os'; @@ -71,46 +81,52 @@ function initBuildEnv(): void { process.env.PATH = `${currentPath}${path.delimiter}${pandaLibPath}`; } -export interface TextDocumentChangeInfo { - newDoc: string; - rangeStart?: number; - rangeEnd?: number; - updateText?: string; -} - export class Lsp { private pandaLibPath: string; private pandaBinPath: string; - private fileNameToArktsconfig: Record; // Map - private moduleToBuildConfig: Record; // Map private getFileContent: (filePath: string) => string; private filesMap: Map; // Map - private projectPath: string; private cacheDir: string; private globalContextPtr?: KNativePointer; private globalConfig?: Config; private globalLspDriverHelper?: LspDriverHelper; private entryArkTsConfig: string; private fileDependencies: string; + private buildConfigs: Record; // Map + private moduleInfos: Record; // Map + private pathConfig: PathConfig; - constructor(projectPath: string, getContentCallback?: (filePath: string) => string) { + constructor(pathConfig: PathConfig, getContentCallback?: (filePath: string) => string, modules?: ModuleDescriptor[]) { initBuildEnv(); - this.cacheDir = path.join(projectPath, '.idea', '.deveco'); - let compileFileInfoPath = path.join(this.cacheDir, 'lsp_compileFileInfos.json'); + this.cacheDir = path.join(pathConfig.projectPath, CACHE_DIR); this.fileDependencies = path.join(this.cacheDir, 'file_dependencies.json'); this.entryArkTsConfig = path.join(this.cacheDir, 'entry', 'arktsconfig.json'); - this.projectPath = projectPath; this.pandaLibPath = process.env.PANDA_LIB_PATH ? process.env.PANDA_LIB_PATH : path.resolve(__dirname, '../../ets2panda/lib'); this.pandaBinPath = process.env.PANDA_BIN_PATH ? process.env.PANDA_BIN_PATH : path.resolve(__dirname, '../../ets2panda/bin'); - this.fileNameToArktsconfig = JSON.parse(fs.readFileSync(compileFileInfoPath, 'utf-8')); - let buildConfigPath = path.join(projectPath, '.idea', '.deveco', 'lsp_build_config.json'); - this.moduleToBuildConfig = JSON.parse(fs.readFileSync(buildConfigPath, 'utf-8')); this.filesMap = new Map(); this.getFileContent = getContentCallback || ((path: string): string => fs.readFileSync(path, 'utf8')); + this.buildConfigs = generateBuildConfigs(pathConfig, modules); + this.moduleInfos = generateArkTsConfigs(this.buildConfigs); + this.pathConfig = pathConfig; + PluginDriver.getInstance().initPlugins(Object.values(this.buildConfigs)[0]); + } + + // Partially update for new file + updateModuleInfos(module: ModuleDescriptor, newFilePath: String): void { + let buildConfig = this.buildConfigs[module.name]; + buildConfig.compileFiles.push(newFilePath.valueOf()); + let moduleInfo = generateModuleInfo(this.buildConfigs, buildConfig); + this.moduleInfos[newFilePath.valueOf()] = moduleInfo; + } + + // Full update for `Sync Now` + update(modules: ModuleDescriptor[]): void { + this.buildConfigs = generateBuildConfigs(this.pathConfig, modules); + this.moduleInfos = generateArkTsConfigs(this.buildConfigs); } modifyFilesMap(fileName: string, fileContent: TextDocumentChangeInfo): void { @@ -121,14 +137,6 @@ export class Lsp { this.filesMap.delete(fileName); } - updateConfig(buildSdkPath: string, modules?: ModuleDescriptor[]): void { - generateArkTsConfigByModules(buildSdkPath, this.projectPath, modules); - let compileFileInfoPath = path.join(this.projectPath, '.idea', '.deveco', 'lsp_compileFileInfos.json'); - this.fileNameToArktsconfig = JSON.parse(fs.readFileSync(compileFileInfoPath, 'utf-8')); - let buildConfigPath = path.join(this.projectPath, '.idea', '.deveco', 'lsp_build_config.json'); - this.moduleToBuildConfig = JSON.parse(fs.readFileSync(buildConfigPath, 'utf-8')); - } - private getFileSource(filePath: string): string { const getSource = this.filesMap.get(filePath) || this.getFileContent(filePath); if (!getSource) { @@ -137,20 +145,10 @@ export class Lsp { return getSource.replace(/\r\n/g, '\n'); } - private getModuleNameFromFilename(filePath: string): string { - const projectRoot = this.projectPath; - if (!filePath.startsWith(projectRoot)) { - return ''; - } - const relativePath = path.relative(projectRoot, filePath); - const parts = relativePath.split(path.sep); - return parts[0] || ''; - } - getDefinitionAtPosition(filename: String, offset: number): LspDefinitionData { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -169,7 +167,7 @@ export class Lsp { getSemanticDiagnostics(filename: String): LspDiagsNode { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -188,7 +186,7 @@ export class Lsp { getCurrentTokenValue(filename: String, offset: number): string { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -207,7 +205,7 @@ export class Lsp { getImplementationAtPosition(filename: String, offset: number): LspDefinitionData { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -226,7 +224,7 @@ export class Lsp { getFileReferences(filename: String): LspReferenceData[] { let lspDriverHelper = new LspDriverHelper(); let searchFilePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[searchFilePath]; + let arktsconfig = this.moduleInfos[searchFilePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, searchFilePath, this.pandaLibPath); const source = this.getFileSource(searchFilePath); @@ -240,11 +238,10 @@ export class Lsp { lspDriverHelper.destroyContext(localCtx); lspDriverHelper.destroyConfig(localCfg); let result: LspReferenceData[] = []; - let moduleName = path.basename(path.dirname(arktsconfig)); - let buildConfig: BuildConfig = this.moduleToBuildConfig[moduleName]; - for (let i = 0; i < buildConfig.compileFiles.length; i++) { - let filePath = path.resolve(buildConfig.compileFiles[i]); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let compileFiles = this.moduleInfos[searchFilePath].compileFiles; + for (let i = 0; i < compileFiles.length; i++) { + let filePath = path.resolve(compileFiles[i]); + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -270,7 +267,7 @@ export class Lsp { getReferencesAtPosition(filename: String, offset: number): LspReferenceData[] { let lspDriverHelper = new LspDriverHelper(); let searchFilePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[searchFilePath]; + let arktsconfig = this.moduleInfos[searchFilePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, searchFilePath, this.pandaLibPath); const source = this.getFileSource(searchFilePath); @@ -284,11 +281,10 @@ export class Lsp { lspDriverHelper.destroyContext(localCtx); lspDriverHelper.destroyConfig(localCfg); let result: LspReferenceData[] = []; - let moduleName = path.basename(path.dirname(arktsconfig)); - let buildConfig: BuildConfig = this.moduleToBuildConfig[moduleName]; - for (let i = 0; i < buildConfig.compileFiles.length; i++) { - let filePath = path.resolve(buildConfig.compileFiles[i]); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let compileFiles = this.moduleInfos[searchFilePath].compileFiles; + for (let i = 0; i < compileFiles.length; i++) { + let filePath = path.resolve(compileFiles[i]); + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -310,7 +306,7 @@ export class Lsp { getTypeHierarchies(filename: String, offset: number): LspTypeHierarchiesInfo | null { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -329,10 +325,10 @@ export class Lsp { } let result: LspTypeHierarchiesInfo[] = []; let moduleName = path.basename(path.dirname(arktsconfig)); - let buildConfig: BuildConfig = this.moduleToBuildConfig[moduleName]; + let buildConfig: BuildConfig = this.buildConfigs[moduleName]; for (let i = 0; i < buildConfig.compileFiles.length; i++) { let filePath = path.resolve(buildConfig.compileFiles[i]); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let searchCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -374,7 +370,7 @@ export class Lsp { getClassHierarchyInfo(filename: String, offset: number): LspClassHierarchy { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileContent(filePath).replace(/\r\n/g, '\n'); @@ -393,7 +389,7 @@ export class Lsp { getAliasScriptElementKind(filename: String, offset: number): LspCompletionEntryKind { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileContent(filePath).replace(/\r\n/g, '\n'); @@ -412,7 +408,7 @@ export class Lsp { getClassHierarchies(filename: String, offset: number): LspClassHierarchies { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileContent(filePath).replace(/\r\n/g, '\n'); @@ -435,7 +431,7 @@ export class Lsp { ): LspClassPropertyInfo { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileContent(filePath).replace(/\r\n/g, '\n'); @@ -454,7 +450,7 @@ export class Lsp { getOrganizeImports(filename: String): LspFileTextChanges { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -473,7 +469,7 @@ export class Lsp { findSafeDeleteLocation(filename: String, offset: number): LspSafeDeleteLocationInfo[] { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -488,10 +484,10 @@ export class Lsp { lspDriverHelper.destroyConfig(localCfg); let result: LspSafeDeleteLocationInfo[] = []; let moduleName = path.basename(path.dirname(arktsconfig)); - let buildConfig: BuildConfig = this.moduleToBuildConfig[moduleName]; + let buildConfig: BuildConfig = this.buildConfigs[moduleName]; for (let i = 0; i < buildConfig.compileFiles.length; i++) { let filePath = path.resolve(buildConfig.compileFiles[i]); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -513,7 +509,7 @@ export class Lsp { getCompletionEntryDetails(filename: String, offset: number, entryName: String): CompletionEntryDetails { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -532,7 +528,7 @@ export class Lsp { getApplicableRefactors(filename: String, kind: String, offset: number): LspApplicableRefactorInfo { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -551,7 +547,7 @@ export class Lsp { getClassConstructorInfo(filename: String, offset: number, properties: string[]): LspClassConstructorInfo { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -570,13 +566,13 @@ export class Lsp { getSyntacticDiagnostics(filename: String): LspDiagsNode { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); let localCtx = lspDriverHelper.createCtx(source, filePath, localCfg, this.globalContextPtr); - const moduleName = this.getModuleNameFromFilename(filePath); - const buildConfig = this.moduleToBuildConfig[moduleName]; + const packageName = this.moduleInfos[filePath].packageName; + const buildConfig = this.buildConfigs[packageName]; PluginDriver.getInstance().getPluginContext().setProjectConfig(buildConfig); PluginDriver.getInstance().getPluginContext().setContextPtr(localCtx); lspDriverHelper.proceedToState(localCtx, Es2pandaContextState.ES2PANDA_STATE_PARSED); @@ -592,7 +588,7 @@ export class Lsp { getSuggestionDiagnostics(filename: String): LspDiagsNode { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -611,7 +607,7 @@ export class Lsp { getQuickInfoAtPosition(filename: String, offset: number): LspQuickInfo { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -630,7 +626,7 @@ export class Lsp { getDocumentHighlights(filename: String, offset: number): LspDocumentHighlightsReferences { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -649,7 +645,7 @@ export class Lsp { getCompletionAtPosition(filename: String, offset: number): LspCompletionInfo { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); let source = this.getFileSource(filePath); @@ -678,7 +674,7 @@ export class Lsp { toLineColumnOffset(filename: String, offset: number): LspLineAndCharacter { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -697,7 +693,7 @@ export class Lsp { getSafeDeleteInfo(filename: String, position: number): boolean { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -716,7 +712,7 @@ export class Lsp { getSpanOfEnclosingComment(filename: String, offset: number, onlyMultiLine: boolean): LspTextSpan { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -735,7 +731,7 @@ export class Lsp { getCodeFixesAtPosition(filename: String, start: number, end: number, errorCodes: number[]): CodeFixActionInfo[] { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -763,7 +759,7 @@ export class Lsp { provideInlayHints(filename: String, span: TextSpan): LspInlayHint[] { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -786,7 +782,7 @@ export class Lsp { getSignatureHelpItems(filename: String, offset: number): LspSignatureHelpItems { let lspDriverHelper = new LspDriverHelper(); let filePath = path.resolve(filename.valueOf()); - let arktsconfig = this.fileNameToArktsconfig[filePath]; + let arktsconfig = this.moduleInfos[filePath].arktsConfigFile; let ets2pandaCmd = ets2pandaCmdPrefix.concat(arktsconfig); let localCfg = lspDriverHelper.createCfg(ets2pandaCmd, filePath, this.pandaLibPath); const source = this.getFileSource(filePath); @@ -806,7 +802,6 @@ export class Lsp { private getFileDependencies(inputs: string[], output: string): void { let depInputContent = ''; let outputFile: string = output; - let libPath = process.env.PANDA_SDK_PATH; let depAnalyzerPath: string = path.join(this.pandaBinPath, 'dependency_analyzer'); let depInputFile = path.join(this.cacheDir, 'depInput.txt'); inputs.forEach((file) => { @@ -929,7 +924,7 @@ export class Lsp { } private collectCompileJobs(jobs: Record, isValid: boolean = false): void { - let entryFileList: string[] = Object.keys(this.fileNameToArktsconfig); + let entryFileList: string[] = Object.keys(this.moduleInfos); this.getFileDependencies(entryFileList, this.fileDependencies); const data = fs.readFileSync(this.fileDependencies, 'utf-8'); let fileDepsInfo: FileDepsInfo = JSON.parse(data) as FileDepsInfo; @@ -1055,7 +1050,7 @@ export class Lsp { 'ets', '--arktsconfig', this.entryArkTsConfig, - Object.keys(this.fileNameToArktsconfig)[0] + Object.keys(this.moduleInfos)[0] ]; this.globalLspDriverHelper = new LspDriverHelper(); @@ -1196,7 +1191,7 @@ export class Lsp { filePath: job.fileList[0], arktsConfigFile: this.entryArkTsConfig, globalContextPtr: this.globalContextPtr!, - buildConfig: Object.values(this.moduleToBuildConfig)[0], + buildConfig: Object.values(this.buildConfigs)[0], isValid: job.isValid }; } @@ -1242,7 +1237,7 @@ export class Lsp { filePath: filename.valueOf(), arktsConfigFile: this.entryArkTsConfig, globalContextPtr: this.globalContextPtr!, - buildConfig: Object.values(this.moduleToBuildConfig)[0], + buildConfig: Object.values(this.buildConfigs)[0], isValid: true }; this.compileExternalProgram(jobInfo); diff --git a/ets2panda/bindings/src/preDefine.ts b/ets2panda/bindings/src/preDefine.ts index 392cee9a94..2a259b6439 100644 --- a/ets2panda/bindings/src/preDefine.ts +++ b/ets2panda/bindings/src/preDefine.ts @@ -15,8 +15,6 @@ export const ARKTSCONFIG_JSON_FILE: string = 'arktsconfig.json'; -export const ABC_SUFFIX: string = '.abc'; - export enum LANGUAGE_VERSION { ARKTS_1_2 = '1.2', ARKTS_1_1 = '1.1', @@ -26,3 +24,4 @@ export enum LANGUAGE_VERSION { 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 CACHE_DIR: string = './.idea/.deveco'; diff --git a/ets2panda/bindings/src/types.ts b/ets2panda/bindings/src/types.ts index ef956f40bf..6037880905 100644 --- a/ets2panda/bindings/src/types.ts +++ b/ets2panda/bindings/src/types.ts @@ -136,30 +136,19 @@ export interface PluginsConfig { [pluginName: string]: string; } -export interface BuildBaseConfig { - buildType: 'build' | 'preview' | 'hotreload' | 'coldreload'; - buildMode: 'Debug' | 'Release'; - hasMainModule: boolean; - arkts: object; - arktsGlobal: object; -} - export interface ModuleConfig { packageName: string; moduleType: string; moduleRootPath: string; - sourceRoots: string[]; + language: string; + declFilesPath?: string; + dependencies?: string[]; } export interface PathConfig { - loaderOutPath: string; - declgenDtsOutPath: string; - declgenTsOutPath: string; - cachePath: string; buildSdkPath: string; - pandaSdkPath?: string; // path to panda sdk lib/bin, for local test - pandaStdlibPath?: string; // path to panda sdk stdlib, for local test - abcLinkerPath?: string; + projectPath: string; + declgenOutDir: string; } export interface DeclgenConfig { @@ -168,52 +157,25 @@ export interface DeclgenConfig { declgenBridgeCodePath?: string; } -export interface LoggerConfig { - getHvigorConsoleLogger?: Function; -} - -export interface DependentModuleConfig { - packageName: string; - moduleName: string; - moduleType: string; - modulePath: string; - sourceRoots: string[]; - entryFile: string; - language: string; - declFilesPath?: string; - dependencies?: string[]; -} - -export interface BuildConfig extends BuildBaseConfig, DeclgenConfig, LoggerConfig, ModuleConfig, PathConfig { +export interface BuildConfig extends DeclgenConfig, ModuleConfig, PathConfig { plugins: PluginsConfig; compileFiles: string[]; - dependentModuleList: DependentModuleConfig[]; } // ProjectConfig ends -export interface CompileFileInfo { - filePath: string; - dependentFiles: string[]; - abcFilePath: string; - arktsConfigFile: string; - packageName: string; -} - export interface ModuleInfo { - isMainModule: boolean; packageName: string; moduleRootPath: string; moduleType: string; - sourceRoots: string[]; entryFile: string; arktsConfigFile: string; - compileFileInfos: CompileFileInfo[]; + compileFiles: string[]; declgenV1OutPath: string | undefined; declgenBridgeCodePath: string | undefined; + staticDepModuleInfos: string[]; + dynamicDepModuleInfos: string[]; + language: string; dependencies?: string[]; - staticDepModuleInfos: Map; - dynamicDepModuleInfos: Map; - language?: string; declFilesPath?: string; } @@ -245,3 +207,9 @@ export interface WorkerInfo { worker: ThreadWorker; isIdle: boolean; } +export interface TextDocumentChangeInfo { + newDoc: string; + rangeStart?: number; + rangeEnd?: number; + updateText?: string; +} diff --git a/ets2panda/bindings/test/run_tests.ts b/ets2panda/bindings/test/run_tests.ts index ecb0adaa16..4190cbac73 100644 --- a/ets2panda/bindings/test/run_tests.ts +++ b/ets2panda/bindings/test/run_tests.ts @@ -15,14 +15,7 @@ import path from 'path'; import fs from 'fs'; -import { - Lsp, - LspDefinitionData, - LspCompletionInfo, - LspDiagsNode, - ModuleDescriptor, - generateArkTsConfigByModules -} from '../src/index'; +import { Lsp, LspDefinitionData, LspCompletionInfo, LspDiagsNode, ModuleDescriptor, PathConfig } from '../src/index'; import { testCases } from './cases'; import { LspCompletionEntry } from '../src/lspNode'; @@ -336,13 +329,16 @@ if (require.main === module) { updateMode = true; } const testDir = path.resolve(process.argv[2]); - const buildSdkPath = path.join(testDir, 'ets', 'ets1.2'); - const projectRoot = path.join(testDir, 'testcases'); - const modules = getModules(projectRoot); + const pathConfig: PathConfig = { + buildSdkPath: path.join(testDir, 'ets', 'ets1.2'), + projectPath: path.join(testDir, 'testcases'), + declgenOutDir: '' + }; + + const modules = getModules(pathConfig.projectPath); - generateArkTsConfigByModules(buildSdkPath, projectRoot, modules); - const lsp = new Lsp(projectRoot); + const lsp = new Lsp(pathConfig, undefined, modules); - process.env.BINDINGS_PATH = path.join(buildSdkPath, 'build-tools', 'bindings'); + process.env.BINDINGS_PATH = path.join(pathConfig.buildSdkPath, 'build-tools', 'bindings'); runTests(testDir, lsp); } -- Gitee