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 * as arkts from '@koalaui/libarkts'; 17 18// This is the same plugin-context in the build-system. 19export class PluginContext { 20 private ast: arkts.EtsScript | undefined; 21 private program: arkts.Program | undefined; 22 private projectConfig: ProjectConfig | undefined; 23 private contextPtr: number | undefined; 24 25 constructor() { 26 this.ast = undefined; 27 this.program = undefined; 28 this.projectConfig = undefined; 29 this.contextPtr = undefined; 30 } 31 32 /** 33 * @deprecated 34 */ 35 public setArkTSAst(ast: arkts.EtsScript): void { 36 this.ast = ast; 37 } 38 39 /** 40 * @deprecated 41 */ 42 public getArkTSAst(): arkts.EtsScript | undefined { 43 return this.ast; 44 } 45 46 /** 47 * @deprecated 48 */ 49 public setArkTSProgram(program: arkts.Program): void { 50 this.program = program; 51 } 52 53 /** 54 * @deprecated 55 */ 56 public getArkTSProgram(): arkts.Program | undefined { 57 return this.program; 58 } 59 60 public setProjectConfig(projectConfig: ProjectConfig): void { 61 throw new Error('do not set projectConfig!'); 62 } 63 64 public getProjectConfig(): ProjectConfig | undefined { 65 return this.projectConfig; 66 } 67 68 public setContextPtr(ptr: number): void { 69 this.contextPtr = ptr; 70 } 71 72 public getContextPtr(): number | undefined { 73 return this.contextPtr; 74 } 75} 76 77export interface ProjectConfig { 78 bundleName: string; 79 moduleName: string; 80 cachePath: string; 81 frameworkMode?: string; 82} 83 84export type PluginHandlerFunction = () => void; 85 86export type PluginHandlerObject = { 87 order: 'pre' | 'post' | undefined; 88 handler: PluginHandlerFunction; 89}; 90 91export type PluginHandler = PluginHandlerFunction | PluginHandlerObject; 92 93export interface Plugins { 94 name: string; 95 afterNew?: PluginHandler; 96 parsed?: PluginHandler; 97 scopeInited?: PluginHandler; 98 checked?: PluginHandler; 99 lowered?: PluginHandler; 100 asmGenerated?: PluginHandler; 101 binGenerated?: PluginHandler; 102 clean?: PluginHandler; 103} 104 105export type PluginState = keyof Omit<Plugins, 'name'>; 106 107export type PluginExecutor = { 108 name: string; 109 handler: PluginHandlerFunction; 110}; 111