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'; 17import { factory } from './factory'; 18import { isAnnotation } from '../../common/arkts-utils'; 19import { CustomComponentNames } from '../utils'; 20 21export enum EntryWrapperNames { 22 ENTRY_FUNC = 'entry', 23 WRAPPER_CLASS_NAME = '__EntryWrapper', 24 ENTRY_STORAGE_ANNOTATION_KEY = 'storage', 25 ENTRY_STORAGE_LOCAL_STORAGE_PROPERTY_NAME = '_entry_local_storage_', 26 ENTRY_DEFAULT_IMPORT = 'arkui.UserView', 27 ENTRY_POINT_CLASS_NAME = 'EntryPoint', 28} 29 30/** 31 * @deprecated 32 */ 33export class EntryHandler { 34 private entryDefClassName: Set<string>; 35 36 private static instance: EntryHandler; 37 38 private constructor() { 39 this.entryDefClassName = new Set<string>(); 40 } 41 42 public static getInstance(): EntryHandler { 43 if (!this.instance) { 44 this.instance = new EntryHandler(); 45 } 46 return this.instance; 47 } 48 49 public rememberEntryFunction(classname: string): void { 50 this.entryDefClassName.add(classname); 51 } 52 53 public createEntryWrapper(): arkts.ClassDeclaration[] { 54 let result: arkts.ClassDeclaration[] = []; 55 this.entryDefClassName.forEach((classname) => { 56 result.push(factory.generateEntryWrapper(classname)); 57 }); 58 return result; 59 } 60} 61 62export function isEntryWrapperClass(node: arkts.AstNode): node is arkts.ClassDeclaration { 63 if (!arkts.isClassDeclaration(node)) return false; 64 const className = node?.definition?.ident?.name; 65 if (!className) return false; 66 return className === EntryWrapperNames.WRAPPER_CLASS_NAME; 67} 68 69/** 70 * find `{storage: "<name>"}` in `@Entry({storage: "<name>"})` (i.e. annotation's properties). 71 * 72 * @param node class definition node 73 */ 74export function findEntryWithStorageInClassAnnotations(node: arkts.ClassDefinition): arkts.ClassProperty | undefined { 75 const annotation = node.annotations.find((anno) => { 76 if (!isAnnotation(anno, CustomComponentNames.ENTRY_ANNOTATION_NAME)) return false; 77 const property = anno.properties?.at(0); 78 if (!property || !arkts.isClassProperty(property)) return false; 79 if (!property.key || !arkts.isIdentifier(property.key)) return false; 80 if (!property.value || !arkts.isStringLiteral(property.value)) return false; 81 return property.key.name === EntryWrapperNames.ENTRY_STORAGE_ANNOTATION_KEY; 82 }); 83 return annotation?.properties?.at(0) as arkts.ClassProperty | undefined; 84} 85