• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 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 */
15
16import { CheckEntry, checkEntryBuilder } from '../../utils/common/CheckEntry';
17import { RuleConfig } from '../../model/RuleConfig';
18import { ProjectConfig } from '../../model/ProjectConfig';
19import { Utils } from '../../utils/common/Utils';
20import { CheckerStorage } from '../../utils/common/CheckerStorage';
21import Logger, { LOG_MODULE_TYPE } from 'arkanalyzer/lib/utils/logger';
22import { FileUtils } from '../../utils/common/FileUtils';
23import { DefaultMessage } from '../../model/Message';
24import { FileIssues } from '../../model/Defects';
25import { CallGraphHelper, DVFGHelper, GlobalCallGraphHelper } from '../../checker/migration/Utils';
26
27const logger = Logger.getLogger(LOG_MODULE_TYPE.HOMECHECK, 'MigrationTool');
28
29export class MigrationTool {
30    private checkEntry: CheckEntry;
31
32    constructor(ruleConfig: any, projectConfig: any) {
33        this.checkEntry = new CheckEntry();
34        // 解析规则配置文件
35        this.checkEntry.ruleConfig = new RuleConfig(ruleConfig);
36        // 解析项目配置文件
37        this.checkEntry.projectConfig = new ProjectConfig(projectConfig);
38    }
39
40    public async buildCheckEntry(): Promise<Boolean> {
41        // 日志配置
42        const logPath = this.checkEntry.projectConfig.logPath;
43        Utils.setLogConfig(logPath.length === 0 ? './HomeCheck.log' : logPath,
44            this.checkEntry.projectConfig.arkAnalyzerLogLevel,
45            this.checkEntry.projectConfig.logLevel);
46        logger.info(`buildCheckEntry start`);
47        // api版本配置
48        CheckerStorage.getInstance().setApiVersion(this.checkEntry.projectConfig.apiVersion);
49        // product配置
50        CheckerStorage.getInstance().setProduct(this.checkEntry.projectConfig.product);
51        // 设置指定文件检查,不设置默认检查所有文件
52        this.checkEntry.setCheckFileList(FileUtils.getFileInfoFromFileList(this.checkEntry.projectConfig.fileOrFolderToCheck));
53
54        // 外部没有建立消息通道,使用默认通道
55        if (!this.checkEntry.message) {
56            this.checkEntry.message = new DefaultMessage();
57        }
58
59        // 前处理
60        if (!await checkEntryBuilder(this.checkEntry)) {
61            return false;
62        }
63        logger.info(`buildCheckEntry end`);
64        return true;
65    }
66
67    public async start(): Promise<FileIssues[]> {
68        logger.info(`MigrationTool run start`);
69        await this.checkEntry.runAll();
70
71        let result = this.checkEntry.sortIssues();
72        this.dispose();
73        logger.info(`MigrationTool run end`);
74        return result;
75    }
76
77    private dispose(): void {
78        CallGraphHelper.dispose();
79        GlobalCallGraphHelper.dispose();
80        DVFGHelper.dispose();
81        CheckerStorage.dispose();
82        this.checkEntry.scene.dispose();
83    }
84}