• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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 */
15import path from 'path';
16import fs from 'fs';
17import { RUNTIME_OS_OH } from '../../pre_define';
18
19/**
20 * configure syscapInfo to this.share.projectConfig
21 *
22 * @param config this.share.projectConfig
23 */
24export function configureSyscapInfo(config: any): void {
25  config.deviceTypesMessage = config.deviceTypes.join(',');
26  const deviceDir: string = path.resolve(__dirname, '../../../../../api/device-define/');
27  const deviceInfoMap: Map<string, string[]> = new Map();
28  const syscaps: Array<string[]> = [];
29  let allSyscaps: string[] = [];
30  config.deviceTypes.forEach((deviceType: string) => {
31    collectOhSyscapInfos(deviceType, deviceDir, deviceInfoMap);
32  });
33  if (config.runtimeOS !== RUNTIME_OS_OH) {
34    collectExternalSyscapInfos(config.externalApiPaths, config.deviceTypes, deviceInfoMap);
35  }
36  deviceInfoMap.forEach((value: string[]) => {
37    syscaps.push(value);
38    allSyscaps = allSyscaps.concat(value);
39  });
40  const intersectNoRepeatTwice = (arrs: Array<string[]>) => {
41    return arrs.reduce(function (prev: string[], cur: string[]) {
42      return Array.from(
43        new Set(
44          cur.filter((item: string) => {
45            return prev.includes(item);
46          })
47        )
48      );
49    });
50  };
51  let syscapIntersection: string[] = [];
52  if (config.deviceTypes.length === 1 || syscaps.length === 1) {
53    syscapIntersection = syscaps[0];
54  } else if (syscaps.length > 1) {
55    syscapIntersection = intersectNoRepeatTwice(syscaps);
56  }
57  config.syscapIntersectionSet = new Set(syscapIntersection);
58  config.syscapUnionSet = new Set(allSyscaps);
59}
60
61function collectOhSyscapInfos(deviceType: string, deviceDir: string, deviceInfoMap: Map<string, string[]>) {
62  let syscapFilePath: string = '';
63  if (deviceType === 'phone') {
64    syscapFilePath = path.resolve(deviceDir, 'default.json');
65  } else {
66    syscapFilePath = path.resolve(deviceDir, deviceType + '.json');
67  }
68  if (fs.existsSync(syscapFilePath)) {
69    const content: object = JSON.parse(fs.readFileSync(syscapFilePath, 'utf-8'));
70    if (deviceInfoMap.get(deviceType)) {
71      deviceInfoMap.set(deviceType, deviceInfoMap.get(deviceType).concat(content['SysCaps']));
72    } else {
73      deviceInfoMap.set(deviceType, content['SysCaps']);
74    }
75  }
76}
77
78function collectExternalSyscapInfos(
79  externalApiPaths: string[],
80  deviceTypes: string[],
81  deviceInfoMap: Map<string, string[]>
82) {
83  const externalDeviceDirs: string[] = [];
84  externalApiPaths.forEach((externalApiPath: string) => {
85    const externalDeviceDir: string = path.resolve(externalApiPath, './api/device-define');
86    if (fs.existsSync(externalDeviceDir)) {
87      externalDeviceDirs.push(externalDeviceDir);
88    }
89  });
90  externalDeviceDirs.forEach((externalDeviceDir: string) => {
91    deviceTypes.forEach((deviceType: string) => {
92      let syscapFilePath: string = '';
93      const files: string[] = fs.readdirSync(externalDeviceDir);
94      files.forEach((fileName: string) => {
95        if (fileName.startsWith(deviceType)) {
96          syscapFilePath = path.resolve(externalDeviceDir, fileName);
97          if (fs.existsSync(syscapFilePath)) {
98            const content: object = JSON.parse(fs.readFileSync(syscapFilePath, 'utf-8'));
99            if (deviceInfoMap.get(deviceType)) {
100              deviceInfoMap.set(deviceType, deviceInfoMap.get(deviceType).concat(content['SysCaps']));
101            } else {
102              deviceInfoMap.set(deviceType, content['SysCaps']);
103            }
104          }
105        }
106      });
107    });
108  });
109}
110