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 */ 15 16import {DependsGraph, DependsNode} from 'arkanalyzer/lib/core/graph/DependsGraph'; 17import {GraphPrinter} from 'arkanalyzer'; 18 19export enum ModuleCategory { 20 ENTRY = 0, 21 FEATURE = 1, 22 HAR = 2, 23 HSP = 3, 24 THIRD_PARTY_PACKAGE = 4, 25 TAGGED_PACKAGE = 5, 26 UNKNOWN = -1, 27} 28 29export interface ModuleCategoryType { 30 name: string; 31 id: number; 32} 33 34export function getComponentCategories(): ModuleCategoryType[] { 35 return Object.entries(ModuleCategory) 36 .filter((e) => !isNaN(e[0] as any)) 37 .map((e) => ({name: e[1] as string, id: parseInt(e[0])})); 38} 39 40export interface Module { 41 id?: number; 42 name: string; 43 version?: number; 44 files?: Set<string>; 45 kind: ModuleCategory; 46 tag?: string; 47 originPath?: string;//for .har, .tgz, .hsp 48} 49 50export interface ModuleEdgeAttr { 51 kind: 0; 52} 53 54export class ModuleDepsGraph extends DependsGraph<Module, ModuleEdgeAttr> { 55 public constructor() { 56 super(); 57 } 58 59 public toJson(): { nodes: Module[]; edges: any[]; categories: {} } { 60 return { 61 categories: getComponentCategories(), 62 nodes: Array.from(this.getNodesIter()).map((value) => { 63 let module = (value as DependsNode<Module>).getNodeAttr() as Module; 64 module.id = (value as DependsNode<Module>).getID(); 65 return module; 66 }), 67 edges: Array.from(this.edgesMap.values()).map((value) => { 68 return { 69 source: value.getSrcID(), 70 target: value.getDstID() 71 }; 72 }), 73 }; 74 } 75 76 public dump(): string { 77 return new GraphPrinter<this>(this).dump(); 78 } 79 80 getGraphName(): string { 81 return 'Module Dependency Graph'; 82 } 83} 84