1/*
2 * Copyright (c) 2023 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 path from 'path';
17import { NumberConstant } from './Constant';
18import { kitData } from '../../kit.json';
19import { fileContent } from '../../subsystem.json';
20
21export class FunctionUtils {
22  /**
23   * 判断文件路径是否为ArkUI,返回ArkUI或文件名
24   *
25   * @param fileFilePath 文件路径
26   * @returns { string } 返回ArkUI或文件名
27   */
28  static getPackageName(fileFilePath: string): string {
29    const packageName =
30      fileFilePath.indexOf('component\\ets\\') >= 0 || fileFilePath.indexOf('component/ets/') >= 0 ?
31        'ArkUI' :
32        path.basename(fileFilePath).replace(/@|.d.ts$/g, '');
33    return packageName;
34  }
35
36  static handleSyscap(syscap: string): string {
37    const syscapArr: Array<string> = syscap.split('.');
38    let syscapField: string = '';
39
40    switch (syscapArr[1]) {
41      case 'MiscServices':
42        syscapField = syscapArr[NumberConstant.SYSCAP_KEY_FIELD_INDEX];
43        break;
44      case 'Communication':
45        if (splitSubsystem.has(syscapArr[NumberConstant.SYSCAP_KEY_FIELD_INDEX])) {
46          syscapField = syscapArr[NumberConstant.SYSCAP_KEY_FIELD_INDEX];
47          break;
48        } else {
49          syscapField = syscapArr[1];
50          break;
51        }
52      default:
53        syscapField = syscapArr[1];
54    }
55    return syscapField;
56  }
57
58  static readSubsystemFile(): SubSystemData {
59    const subsystemMap: Map<string, string> = new Map();
60    const fileNameMap: Map<string, string> = new Map();
61
62    fileContent.forEach((content: SubSystemInfo) => {
63      subsystemMap.set(content.syscap, content.subsystem);
64      fileNameMap.set(content.syscap, content.fileName);
65    });
66    return {
67      subsystemMap: subsystemMap,
68      fileNameMap: fileNameMap,
69    };
70  }
71
72  /**
73   * 遍历kit配置文件
74   * 
75   * @returns 
76   */
77  static readKitFile(): KitData {
78    const subsystemMap: Map<string, string> = new Map();
79    const kitNameMap: Map<string, string> = new Map();
80    const filePathSet: Set<string> = new Set();
81    kitData.forEach((subSystemInfo: KitInfo) => {
82      subsystemMap.set(subSystemInfo.filePath, subSystemInfo.subSystem);
83      kitNameMap.set(subSystemInfo.filePath, subSystemInfo.kitName);
84      filePathSet.add(subSystemInfo.filePath);
85    });
86    return { subsystemMap, kitNameMap, filePathSet };
87  }
88}
89
90/**
91 * 被拆分开的子系统
92 */
93const splitSubsystem: Set<string> = new Set(['Bluetooth', 'NetManager']);
94
95class SubSystemInfo {
96  syscap: string = '';
97  subsystem: string = '';
98  fileName: string = '';
99}
100
101/**
102 * 读取子系统配置文件返回的数据格式
103 */
104type SubSystemData = {
105  subsystemMap: Map<string, string>;
106  fileNameMap: Map<string, string>;
107};
108
109/**
110 * 读取kit配置文件返回的数据格式
111 */
112export type KitData = {
113  subsystemMap: Map<string, string>;
114  kitNameMap: Map<string, string>;
115  filePathSet: Set<string>;
116};
117
118/**
119 * kit配置文件里的信息项
120 */
121class KitInfo {
122  filePath: string = '';
123  subSystem: string = '';
124  kitName: string = '';
125}
126