• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 - 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 */
15import Logger, { LOG_LEVEL, LOG_MODULE_TYPE } from 'arkanalyzer/lib/utils/logger';
16import { Command, OptionValues } from 'commander';
17
18const logger = Logger.getLogger(LOG_MODULE_TYPE.HOMECHECK, 'Utils');
19
20export class Utils {
21    /**
22     * 解析命令行选项
23     * @param args 命令行参数数组
24     * @returns 解析后的选项值
25     */
26    static parseCliOptions(args: string[]): OptionValues {
27        logger.info('Parse cli options.');
28        const program = new Command();
29        return this.getCliOptions(program, args);
30    }
31
32    /**
33     * 获取命令行选项
34     * @param program Command 对象
35     * @param args 命令行参数数组
36     * @returns 选项值对象
37     */
38    static getCliOptions(program: Command, args: string[]): OptionValues {
39        program
40            .option('--configPath <configPath>', 'rule config path')
41            .option('--projectConfigPath <projectConfigPath>', 'project config path')
42            .option('--depGraphOutputDir <depGraphOutputDir>', 'output directory of dependency graph')
43            .parse(args);
44        return program.opts();
45    }
46
47    /**
48     * 设置日志路径
49     * @param logPath 日志路径
50     */
51    static setLogPath(logPath: string): void {
52        Logger.configure(logPath, LOG_LEVEL.INFO, LOG_LEVEL.INFO);
53    }
54
55    /**
56     * 设置日志信息,包含路径、arkanalyzer日志级别、homecheck日志级别
57     */
58    static setLogConfig(logPath: string, aaLevel: LOG_LEVEL, hcLevel: LOG_LEVEL): void {
59        Logger.configure(logPath, aaLevel, hcLevel);
60    }
61
62    /**
63     * 获取枚举类型的值
64     * @param value - 枚举值,可以是字符串或数字
65     * @param enumType - 枚举类型
66     * @returns 枚举值对应的枚举类型值
67     */
68    static getEnumValues(value: string | number, enumType: any): any {
69        const key = Object.keys(enumType).find(k => k.toLowerCase() === value || enumType[k as string] === value);
70        return enumType[key as string];
71    }
72
73    /**
74     * 按行号和列号对键值对进行排序
75     * @param keyA 格式为 "行号%列号%规则ID" 的字符串
76     * @param keyB 格式为 "行号%列号%规则ID" 的字符串
77     * @returns 排序比较结果
78     */
79    public static sortByLineAndColumn(keyA: string, keyB: string): number {
80        const [lineA, colA] = keyA.split('%', 2).map(Number);
81        const [lineB, colB] = keyB.split('%', 2).map(Number);
82        if (lineA !== lineB) {
83            return lineA - lineB;
84        }
85        return colA - colB;
86    }
87}
88
89export type WarnInfo = {
90    line: number,
91    startCol: number,
92    endCol: number,
93    filePath: string
94};
95