• 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 */
15
16import fs from 'fs';
17import path from 'path';
18import { ArkFile } from '../core/model/ArkFile';
19import { DotFilePrinter } from './DotPrinter';
20import { SourceFilePrinter } from './source/SourceFilePrinter';
21import { Printer } from './Printer';
22import { JsonPrinter } from './JsonPrinter';
23import { ArkIRFilePrinter } from './arkir/ArkIRFilePrinter';
24import { Scene } from '../Scene';
25import { PrinterOptions, setPrinterOptions } from './base/BasePrinter';
26
27/**
28 * @example
29 * // dump method IR to ts source
30 * let method: Method = xx;
31 * let srcPrinter = new SourceMethodPrinter(method);
32 * PrinterBuilder.dump(srcPrinter, 'output.ts');
33 *
34 *
35 * // dump method cfg to dot
36 * let dotPrinter = new DotMethodPrinter(method);
37 * PrinterBuilder.dump(dotPrinter, 'output.dot');
38 *
39 * // dump project
40 * let printer = new PrinterBuilder('output');
41 * for (let f of scene.getFiles()) {
42 *     printer.dumpToTs(f);
43 * }
44 *
45 * @category save
46 */
47export class PrinterBuilder {
48    outputDir: string;
49    constructor(outputDir: string = '') {
50        this.outputDir = outputDir;
51    }
52
53    public static dump(source: Printer, output: string): void {
54        fs.writeFileSync(output, source.dump());
55    }
56
57    protected getOutputDir(arkFile: ArkFile): string {
58        if (this.outputDir === '') {
59            return path.join(arkFile.getProjectDir(), '..', 'output');
60        } else {
61            return path.join(this.outputDir);
62        }
63    }
64
65    public dumpToDot(arkFile: ArkFile, output: string | undefined = undefined): void {
66        let filename = output;
67        if (filename === undefined) {
68            filename = path.join(this.getOutputDir(arkFile), arkFile.getName() + '.dot');
69        }
70        fs.mkdirSync(path.dirname(filename), { recursive: true });
71
72        let printer: Printer = new DotFilePrinter(arkFile);
73        PrinterBuilder.dump(printer, filename as string);
74    }
75
76    public dumpToTs(arkFile: ArkFile, output: string | undefined = undefined): void {
77        let filename = output;
78        if (filename === undefined) {
79            filename = path.join(this.getOutputDir(arkFile), arkFile.getName());
80        }
81        if (path.extname(filename) === '') {
82            filename += '.ts';
83        }
84        fs.mkdirSync(path.dirname(filename), { recursive: true });
85
86        let printer: Printer = new SourceFilePrinter(arkFile);
87        PrinterBuilder.dump(printer, filename);
88    }
89
90    public dumpToJson(arkFile: ArkFile, output: string | undefined = undefined): void {
91        let filename = output;
92        if (filename === undefined) {
93            filename = path.join(this.getOutputDir(arkFile), arkFile.getName() + '.json');
94        }
95        fs.mkdirSync(path.dirname(filename), { recursive: true });
96
97        let printer: Printer = new JsonPrinter(arkFile);
98        PrinterBuilder.dump(printer, filename);
99    }
100
101    public dumpToIR(arkFile: ArkFile, output: string | undefined = undefined): void {
102        let filename = output;
103        if (filename === undefined) {
104            filename = path.join(this.getOutputDir(arkFile), arkFile.getName());
105        }
106
107        filename += '.ir';
108
109        fs.mkdirSync(path.dirname(filename), { recursive: true });
110
111        let printer: Printer = new ArkIRFilePrinter(arkFile);
112        PrinterBuilder.dump(printer, filename);
113    }
114}
115
116/**
117 * @example
118 * // dump scene
119 * let scenePrinter = new ScenePrinter(scene, 'output');
120 * scenePrinter.dumpToTs();
121 * scenePrinter.dumpToIR();
122 *
123 * @category save
124 */
125export class ScenePrinter {
126    scene: Scene;
127    outputDir: string;
128    printer: PrinterBuilder;
129
130    constructor(scene: Scene, outputDir: string, option?: PrinterOptions) {
131        this.scene = scene;
132        this.outputDir = outputDir;
133        this.printer = new PrinterBuilder(outputDir);
134        if (option) {
135            setPrinterOptions(option);
136        }
137    }
138
139    public dumpToDot(): void {
140        for (let f of this.scene.getFiles()) {
141            this.printer.dumpToDot(f);
142        }
143    }
144
145    public dumpToTs(): void {
146        for (let f of this.scene.getFiles()) {
147            let relativePath = path.relative(f.getProjectDir(), f.getFilePath());
148            this.printer.dumpToTs(f, path.join(this.outputDir, relativePath));
149        }
150    }
151
152    public dumpToJson(): void {
153        for (let f of this.scene.getFiles()) {
154            this.printer.dumpToJson(f);
155        }
156    }
157
158    public dumpToIR(): void {
159        for (let f of this.scene.getFiles()) {
160            this.printer.dumpToIR(f);
161        }
162    }
163}
164