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 FileCategory { 20 FILE = 0, 21 PKG = 1, 22 SO = 2, 23 UNKNOWN = -1, 24} 25 26export interface FileCategoryType { 27 name: string; 28 id: number; 29} 30 31export function getComponentCategories(): FileCategoryType[] { 32 return Object.entries(FileCategory) 33 .filter((e) => !isNaN(e[0] as any)) 34 .map((e) => ({name: e[1] as string, id: parseInt(e[0])})); 35} 36 37export interface File { 38 id?: number; 39 name: string; 40 version?: number; 41 files?: Set<string>; 42 kind: FileCategory; 43 tag?: string; 44} 45 46export interface ImportInfo4Dep { 47 importClauseName: string; 48 importType: string; 49 importFrom?: string; 50 nameBeforeAs?: string; 51 isDefault?: boolean; 52} 53 54export interface FileEdgeAttr { 55 kind: 0; 56 // import clause name + import type, import info 57 attr: Map<string, ImportInfo4Dep>; 58} 59 60export class FileDepsGraph extends DependsGraph<File, FileEdgeAttr> { 61 public constructor() { 62 super(); 63 } 64 65 addImportInfo2Edge(edge: FileEdgeAttr, importInfo: ImportInfo4Dep): void { 66 const attrKey = `${importInfo.importClauseName} + ${importInfo.importType}`; 67 edge.attr.set(attrKey, importInfo); 68 } 69 70 public toJson(): { nodes: File[]; edges: any[]; categories: {} } { 71 return { 72 categories: getComponentCategories(), 73 nodes: Array.from(this.getNodesIter()).map((value) => { 74 let pkg = (value as DependsNode<File>).getNodeAttr() as File; 75 pkg.id = (value as DependsNode<File>).getID(); 76 return pkg; 77 }), 78 edges: Array.from(this.edgesMap.values()).map((value) => { 79 return { 80 source: value.getSrcID(), 81 target: value.getDstID(), 82 attr: Array.from(value.getEdgeAttr().attr.values()) 83 }; 84 }), 85 }; 86 } 87 88 public dump(): string { 89 return new GraphPrinter<this>(this).dump(); 90 } 91 92 getGraphName(): string { 93 return 'File Dependency Graph'; 94 } 95} 96