• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 fs from 'fs';
18import { FileUtils } from './FileUtils';
19import { NumberConstant } from './Constant';
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 subsystemFilePath: string = path.join(FileUtils.getBaseDirName(), 'subsystem.json');
60    const fileContent: Array<SubSystemInfo> = JSON.parse(fs.readFileSync(subsystemFilePath, 'utf-8'));
61    const subsystemMap: Map<string, string> = new Map();
62    const fileNameMap: Map<string, string> = new Map();
63
64    fileContent.forEach((content: SubSystemInfo) => {
65      subsystemMap.set(content.syscap, content.subsystem);
66      fileNameMap.set(content.syscap, content.fileName);
67    });
68    return {
69      subsystemMap: subsystemMap,
70      fileNameMap: fileNameMap,
71    };
72  }
73}
74
75/**
76 * 被拆分开的子系统
77 */
78const splitSubsystem: Set<string> = new Set(['Bluetooth', 'NetManager']);
79
80class SubSystemInfo {
81  syscap: string = '';
82  subsystem: string = '';
83  fileName: string = '';
84}
85
86/**
87 * 读取子系统配置文件返回的数据格式
88 */
89type SubSystemData = {
90  subsystemMap: Map<string, string>;
91  fileNameMap: Map<string, string>;
92};
93