1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import * as path from 'path'; 17import * as fs from 'fs'; 18import { BuildConfig, DependentModuleConfig } from '../types'; 19import { toUnixPath } from '../utils'; 20import { ETS_1_1, ETS_1_1_INTEROP, LANGUAGE_VERSION } from '../pre_define'; 21import { readFirstLineSync } from '../utils' 22 23export class FileManager { 24 private static instance: FileManager | undefined = undefined; 25 static arkTSModuleMap: Map<string, DependentModuleConfig> = new Map(); 26 static staticApiPath: Set<string> = new Set(); 27 static dynamicApiPath: Set<string> = new Set(); 28 static buildConfig: BuildConfig; 29 private constructor() { } 30 static init(buildConfig: BuildConfig): void { 31 if (FileManager.instance === undefined) { 32 FileManager.instance = new FileManager(); 33 FileManager.initLanguageVersionFromDependentModuleMap(buildConfig.dependentModuleList); 34 FileManager.initSDK(new Set(buildConfig.externalApiPaths), buildConfig.buildSdkPath); 35 FileManager.buildConfig = buildConfig; 36 } 37 } 38 39 static getInstance(): FileManager { 40 if (!FileManager.instance) { 41 FileManager.instance = new FileManager(); 42 } 43 return FileManager.instance; 44 } 45 46 static cleanFileManagerObject(): void { 47 if (this.instance) { 48 this.instance = undefined; 49 } 50 } 51 52 static initSDK(externalApiPath: Set<string>, buildSDKPath: string): void { 53 externalApiPath?.forEach(path => { 54 FileManager.staticApiPath.add(toUnixPath(path)); 55 }) 56 57 const etsPath = path.resolve(buildSDKPath, '../'); 58 59 FileManager.dynamicApiPath.add(toUnixPath(path.resolve(etsPath, ETS_1_1))); 60 FileManager.dynamicApiPath.add(toUnixPath(path.resolve(etsPath, ETS_1_1_INTEROP))); 61 } 62 63 private static initLanguageVersionFromDependentModuleMap( 64 dependentModuleList: DependentModuleConfig[] 65 ): void { 66 const convertedMap = new Map<string, DependentModuleConfig>(); 67 dependentModuleList.forEach(module => { 68 const convertedModule: DependentModuleConfig = { 69 ...module, 70 modulePath: toUnixPath(module.modulePath), 71 declgenV1OutPath: module.declgenV1OutPath ? toUnixPath(module.declgenV1OutPath) : undefined, 72 declgenBridgeCodePath: module.declgenBridgeCodePath ? toUnixPath(module.declgenBridgeCodePath) : undefined, 73 declFilesPath: module.declFilesPath ? toUnixPath(module.declFilesPath) : undefined, 74 }; 75 convertedMap.set(module.packageName, convertedModule); 76 }) 77 78 this.arkTSModuleMap = convertedMap; 79 } 80 private static isFirstLineUseStatic(filePath: string): boolean { 81 const firstLine = readFirstLineSync(filePath); 82 return firstLine === "'use static'"; 83 } 84 85 getLanguageVersionByFilePath(filePath: string): string { 86 const path = toUnixPath(filePath); 87 for (const apiPath of FileManager.staticApiPath) { 88 if (path.startsWith(apiPath)) { 89 return LANGUAGE_VERSION.ARKTS_1_2; 90 } 91 } 92 for (const apiPath of FileManager.dynamicApiPath) { 93 if (path.startsWith(apiPath)) { 94 return LANGUAGE_VERSION.ARKTS_1_1; 95 } 96 } 97 if (FileManager.buildConfig.compileFiles?.includes(filePath)) { 98 return LANGUAGE_VERSION.ARKTS_1_2; 99 } 100 for (const [pkgName, moduleInfo] of FileManager.arkTSModuleMap) { 101 if (!path.startsWith(moduleInfo.modulePath)) { 102 continue; 103 } 104 if (moduleInfo.language !== LANGUAGE_VERSION.ARKTS_HYBRID) { 105 return moduleInfo.language; 106 } 107 /** 108 * when process hybrid hsp or har we can't get info of 1.1, 109 * only by module decl-fileinfo.json or `'use static'` 110 */ 111 if (FileManager.isFirstLineUseStatic(filePath)) { 112 return LANGUAGE_VERSION.ARKTS_1_2; 113 } 114 } 115 return LANGUAGE_VERSION.ARKTS_1_1; 116 } 117}