/* * Copyright (c) 2024 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 path from 'path'; import fs from 'fs'; import { RUNTIME_OS_OH } from '../../pre_define'; /** * configure syscapInfo to this.share.projectConfig * * @param config this.share.projectConfig */ export function configureSyscapInfo(config: any): void { config.deviceTypesMessage = config.deviceTypes.join(','); const deviceDir: string = path.resolve(__dirname, '../../../../../api/device-define/'); const deviceInfoMap: Map = new Map(); const syscaps: Array = []; let allSyscaps: string[] = []; config.deviceTypes.forEach((deviceType: string) => { collectOhSyscapInfos(deviceType, deviceDir, deviceInfoMap); }); if (config.runtimeOS !== RUNTIME_OS_OH) { collectExternalSyscapInfos(config.externalApiPaths, config.deviceTypes, deviceInfoMap); } deviceInfoMap.forEach((value: string[]) => { syscaps.push(value); allSyscaps = allSyscaps.concat(value); }); const intersectNoRepeatTwice = (arrs: Array) => { return arrs.reduce(function (prev: string[], cur: string[]) { return Array.from( new Set( cur.filter((item: string) => { return prev.includes(item); }) ) ); }); }; let syscapIntersection: string[] = []; if (config.deviceTypes.length === 1 || syscaps.length === 1) { syscapIntersection = syscaps[0]; } else if (syscaps.length > 1) { syscapIntersection = intersectNoRepeatTwice(syscaps); } config.syscapIntersectionSet = new Set(syscapIntersection); config.syscapUnionSet = new Set(allSyscaps); } function collectOhSyscapInfos(deviceType: string, deviceDir: string, deviceInfoMap: Map) { let syscapFilePath: string = ''; if (deviceType === 'phone') { syscapFilePath = path.resolve(deviceDir, 'default.json'); } else { syscapFilePath = path.resolve(deviceDir, deviceType + '.json'); } if (fs.existsSync(syscapFilePath)) { const content: object = JSON.parse(fs.readFileSync(syscapFilePath, 'utf-8')); if (deviceInfoMap.get(deviceType)) { deviceInfoMap.set(deviceType, deviceInfoMap.get(deviceType).concat(content['SysCaps'])); } else { deviceInfoMap.set(deviceType, content['SysCaps']); } } } function collectExternalSyscapInfos( externalApiPaths: string[], deviceTypes: string[], deviceInfoMap: Map ) { const externalDeviceDirs: string[] = []; externalApiPaths.forEach((externalApiPath: string) => { const externalDeviceDir: string = path.resolve(externalApiPath, './api/device-define'); if (fs.existsSync(externalDeviceDir)) { externalDeviceDirs.push(externalDeviceDir); } }); externalDeviceDirs.forEach((externalDeviceDir: string) => { deviceTypes.forEach((deviceType: string) => { let syscapFilePath: string = ''; const files: string[] = fs.readdirSync(externalDeviceDir); files.forEach((fileName: string) => { if (fileName.startsWith(deviceType)) { syscapFilePath = path.resolve(externalDeviceDir, fileName); if (fs.existsSync(syscapFilePath)) { const content: object = JSON.parse(fs.readFileSync(syscapFilePath, 'utf-8')); if (deviceInfoMap.get(deviceType)) { deviceInfoMap.set(deviceType, deviceInfoMap.get(deviceType).concat(content['SysCaps'])); } else { deviceInfoMap.set(deviceType, content['SysCaps']); } } } }); }); }); }