@hadss/hmrouter-plugin 1.2.2 → 1.2.3-rc.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -16,12 +16,14 @@ HMRouter编译插件是一个基于hvigor的编译插件,用于:
16
16
 
17
17
  ### 工程级配置
18
18
 
19
+ ***插件版本建议和库的版本保持一致***
20
+
19
21
  在工程的`hvigor/hvigor-config.json`文件中添加路由编译插件:
20
22
 
21
23
  ```json5
22
24
  {
23
25
  "dependencies": {
24
- "@hadss/hmrouter-plugin": "^1.2.2" // 使用最新版本
26
+ "@hadss/hmrouter-plugin": "^1.2.3-rc.1" // 使用最新版本
25
27
  },
26
28
  // ...其余配置
27
29
  }
@@ -438,6 +440,14 @@ hvigor 5.7.3及以上
438
440
 
439
441
  ## 更新日志
440
442
 
443
+ ### 1.2.3-rc.1 (2026.01.15)
444
+
445
+ - [修复插件编译报错路由常量找不到问题](https://gitcode.com/openharmony-sig/ohrouter/issues/207)
446
+
447
+ ### 1.2.3-rc.0 (2025.12.11)
448
+
449
+ - [修复库版本与插件版本不匹配时编译报错的问题](https://gitcode.com/openharmony-sig/ohrouter/issues/250)
450
+
441
451
  ### 1.2.2 (2025.10.24)
442
452
 
443
453
  - [修复系统路由表合并异常的问题](https://gitcode.com/openharmony-sig/ohrouter/issues/215)
@@ -9,4 +9,5 @@ export declare class HMRouterPluginManager {
9
9
  static getInstance(): HMRouterPluginManager;
10
10
  registerPlugin(node: HvigorNode, pluginId: string, options: PluginParam): void;
11
11
  addModuleNode(node: HvigorNode, isIgnored: boolean): void;
12
+ private initializeArkAnalyzer;
12
13
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HMRouterPluginManager = void 0;
4
4
  const hvigor_1 = require("@ohos/hvigor");
5
+ const hvigor_ohos_plugin_1 = require("@ohos/hvigor-ohos-plugin");
5
6
  const PluginError_1 = require("./hmrouter_extension/error/PluginError");
6
7
  const HMRouterExtension_1 = require("./hmrouter_extension/HMRouterExtension");
7
8
  const framework_1 = require("./framework");
@@ -13,6 +14,7 @@ class HMRouterPluginManager {
13
14
  framework_1.Logger.info('', 'HMRouterPlugin starting...');
14
15
  (0, framework_1.registerPluginExtension)(new HMRouterExtension_1.HMRouterDefaultExtension());
15
16
  hvigor_1.hvigor.nodesEvaluated(() => {
17
+ this.initializeArkAnalyzer();
16
18
  this.moduleNodes.forEach((node) => {
17
19
  if (this.controllerMap.has(node.getNodeName())) {
18
20
  return;
@@ -29,6 +31,7 @@ class HMRouterPluginManager {
29
31
  this.controllerMap.forEach((controller) => {
30
32
  controller.complete();
31
33
  });
34
+ framework_1.ArkAnalyzerUtil.clear();
32
35
  HMRouterPluginManager.instance = null;
33
36
  framework_1.PluginStore.destroy();
34
37
  framework_1.ExtensionManager.destroy();
@@ -73,6 +76,18 @@ class HMRouterPluginManager {
73
76
  this.nodeIgnored.add(node.getNodeName());
74
77
  }
75
78
  }
79
+ initializeArkAnalyzer() {
80
+ if (this.moduleNodes.length === 0) {
81
+ return;
82
+ }
83
+ const firstNode = this.moduleNodes[0];
84
+ const appContext = firstNode.getParentNode()?.getContext(hvigor_ohos_plugin_1.OhosPluginId.OHOS_APP_PLUGIN);
85
+ if (appContext) {
86
+ const projectPath = appContext.getProjectPath();
87
+ framework_1.Logger.info('', `Initializing ArkAnalyzer for project: ${projectPath}`);
88
+ framework_1.ArkAnalyzerUtil.initialize(projectPath);
89
+ }
90
+ }
76
91
  }
77
92
  exports.HMRouterPluginManager = HMRouterPluginManager;
78
93
  HMRouterPluginManager.instance = null;
@@ -0,0 +1,24 @@
1
+ export declare class ArkAnalyzerUtil {
2
+ private static instance;
3
+ private scene;
4
+ private isInitialized;
5
+ private constructor();
6
+ static getInstance(): ArkAnalyzerUtil;
7
+ init(projectRoot: string): boolean;
8
+ isReady(): boolean;
9
+ findVariableFilePath(moduleName: string, variableName: string): string | undefined;
10
+ private findModuleSceneByName;
11
+ private getAllModuleScenes;
12
+ private findInModuleScene;
13
+ private findInAllFiles;
14
+ private isModuleMatch;
15
+ private findExportInFile;
16
+ private traceToDefinition;
17
+ private findFileByPath;
18
+ private normalizeModuleName;
19
+ clear(): void;
20
+ static isReady(): boolean;
21
+ static findVariableFilePath(moduleName: string, variableName: string): string | undefined;
22
+ static initialize(projectRoot: string): void;
23
+ static clear(): void;
24
+ }
@@ -0,0 +1,208 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ArkAnalyzerUtil = void 0;
7
+ const arkanalyzer_1 = require("arkanalyzer");
8
+ const Logger_1 = require("./Logger");
9
+ const path_1 = __importDefault(require("path"));
10
+ class ArkAnalyzerUtil {
11
+ constructor() {
12
+ this.scene = null;
13
+ this.isInitialized = false;
14
+ }
15
+ static getInstance() {
16
+ if (!ArkAnalyzerUtil.instance) {
17
+ ArkAnalyzerUtil.instance = new ArkAnalyzerUtil();
18
+ }
19
+ return ArkAnalyzerUtil.instance;
20
+ }
21
+ init(projectRoot) {
22
+ if (this.isInitialized) {
23
+ return true;
24
+ }
25
+ try {
26
+ this.scene = new arkanalyzer_1.Scene();
27
+ const options = {
28
+ supportFileExts: ['.ets', '.d.ets'],
29
+ projectName: 'HMRouterProject'
30
+ };
31
+ const config = new arkanalyzer_1.SceneConfig(options);
32
+ config.buildFromProjectDir(projectRoot);
33
+ this.scene.buildSceneFromProjectDir(config);
34
+ this.scene.buildScene4HarmonyProject();
35
+ this.scene.inferTypes();
36
+ this.isInitialized = true;
37
+ Logger_1.Logger.info('ArkAnalyzerUtil', 'Initialized successfully');
38
+ return true;
39
+ }
40
+ catch (e) {
41
+ Logger_1.Logger.error('ArkAnalyzerUtil', `Failed to initialize: ${e}`);
42
+ return false;
43
+ }
44
+ }
45
+ isReady() {
46
+ return this.isInitialized && this.scene !== null;
47
+ }
48
+ findVariableFilePath(moduleName, variableName) {
49
+ if (!this.scene) {
50
+ throw new Error('ArkAnalyzer scene not initialized');
51
+ }
52
+ const moduleScene = this.findModuleSceneByName(moduleName);
53
+ if (moduleScene) {
54
+ const result = this.findInModuleScene(moduleScene, variableName);
55
+ if (result) {
56
+ return result;
57
+ }
58
+ }
59
+ return this.findInAllFiles(moduleName, variableName);
60
+ }
61
+ findModuleSceneByName(moduleName) {
62
+ if (!this.scene) {
63
+ return undefined;
64
+ }
65
+ let moduleScene = this.scene.getModuleScene(moduleName);
66
+ if (moduleScene) {
67
+ return moduleScene;
68
+ }
69
+ const allScenes = this.getAllModuleScenes();
70
+ moduleScene = allScenes.find(scene => scene.getModuleName().toLowerCase() === moduleName.toLowerCase());
71
+ if (moduleScene) {
72
+ return moduleScene;
73
+ }
74
+ const normalizedModuleName = this.normalizeModuleName(moduleName);
75
+ if (normalizedModuleName !== moduleName) {
76
+ moduleScene = allScenes.find(scene => {
77
+ const sceneName = scene.getModuleName();
78
+ const normalizedSceneName = this.normalizeModuleName(sceneName);
79
+ return normalizedSceneName.toLowerCase() === normalizedModuleName.toLowerCase();
80
+ });
81
+ }
82
+ return moduleScene;
83
+ }
84
+ getAllModuleScenes() {
85
+ if (!this.scene) {
86
+ return [];
87
+ }
88
+ const scenes = [];
89
+ const files = this.scene.getFiles();
90
+ const seenScenes = new Set();
91
+ for (const file of files) {
92
+ const moduleScene = file.getModuleScene();
93
+ if (moduleScene && !seenScenes.has(moduleScene)) {
94
+ seenScenes.add(moduleScene);
95
+ scenes.push(moduleScene);
96
+ }
97
+ }
98
+ return scenes;
99
+ }
100
+ findInModuleScene(moduleScene, variableName) {
101
+ const filesMap = moduleScene.getModuleFilesMap();
102
+ for (const [, arkFile] of filesMap) {
103
+ const exportInfos = arkFile.getExportInfos();
104
+ for (const exportInfo of exportInfos) {
105
+ if (exportInfo.getExportClauseName() === variableName) {
106
+ return this.traceToDefinition(exportInfo, arkFile);
107
+ }
108
+ }
109
+ }
110
+ return undefined;
111
+ }
112
+ findInAllFiles(moduleName, variableName) {
113
+ const normalizedModuleName = this.normalizeModuleName(moduleName);
114
+ const files = this.scene.getFiles();
115
+ for (const arkFile of files) {
116
+ if (!this.isModuleMatch(arkFile, moduleName, normalizedModuleName)) {
117
+ continue;
118
+ }
119
+ const result = this.findExportInFile(arkFile, variableName);
120
+ if (result) {
121
+ return result;
122
+ }
123
+ }
124
+ return undefined;
125
+ }
126
+ isModuleMatch(arkFile, moduleName, normalizedModuleName) {
127
+ const fileModuleName = arkFile.getModuleName() || arkFile.getModuleScene()?.getModuleName() || '';
128
+ const normalizedFileModule = this.normalizeModuleName(fileModuleName);
129
+ if (fileModuleName === moduleName) {
130
+ return true;
131
+ }
132
+ if (fileModuleName.toLowerCase() === moduleName.toLowerCase()) {
133
+ return true;
134
+ }
135
+ if (normalizedFileModule.toLowerCase() === normalizedModuleName.toLowerCase()) {
136
+ return true;
137
+ }
138
+ return false;
139
+ }
140
+ findExportInFile(arkFile, variableName) {
141
+ const exportInfos = arkFile.getExportInfos();
142
+ for (const exportInfo of exportInfos) {
143
+ if (exportInfo.getExportClauseName() === variableName) {
144
+ return this.traceToDefinition(exportInfo, arkFile);
145
+ }
146
+ }
147
+ return undefined;
148
+ }
149
+ traceToDefinition(exportInfo, currentFile) {
150
+ const fromPath = exportInfo.getFrom();
151
+ if (!fromPath) {
152
+ return currentFile.getFilePath();
153
+ }
154
+ const currentDir = path_1.default.dirname(currentFile.getFilePath());
155
+ let targetPath = path_1.default.resolve(currentDir, fromPath);
156
+ if (!targetPath.endsWith('.ets') && !targetPath.endsWith('.d.ets')) {
157
+ targetPath += '.ets';
158
+ }
159
+ const targetFile = this.findFileByPath(targetPath);
160
+ if (!targetFile) {
161
+ return currentFile.getFilePath();
162
+ }
163
+ const variableName = exportInfo.getNameBeforeAs() || exportInfo.getExportClauseName();
164
+ const targetExports = targetFile.getExportInfos();
165
+ for (const targetExport of targetExports) {
166
+ if (targetExport.getExportClauseName() === variableName) {
167
+ return this.traceToDefinition(targetExport, targetFile);
168
+ }
169
+ }
170
+ return targetFile.getFilePath();
171
+ }
172
+ findFileByPath(filePath) {
173
+ const files = this.scene.getFiles();
174
+ for (const arkFile of files) {
175
+ if (arkFile.getFilePath() === filePath) {
176
+ return arkFile;
177
+ }
178
+ }
179
+ return undefined;
180
+ }
181
+ normalizeModuleName(moduleName) {
182
+ if (moduleName.startsWith('@')) {
183
+ const parts = moduleName.split('/');
184
+ return parts.length >= 2 ? parts[1] : moduleName;
185
+ }
186
+ return moduleName;
187
+ }
188
+ clear() {
189
+ if (this.scene) {
190
+ this.scene.dispose();
191
+ this.scene = null;
192
+ }
193
+ this.isInitialized = false;
194
+ }
195
+ static isReady() {
196
+ return ArkAnalyzerUtil.getInstance().isReady();
197
+ }
198
+ static findVariableFilePath(moduleName, variableName) {
199
+ return ArkAnalyzerUtil.getInstance().findVariableFilePath(moduleName, variableName);
200
+ }
201
+ static initialize(projectRoot) {
202
+ ArkAnalyzerUtil.getInstance().init(projectRoot);
203
+ }
204
+ static clear() {
205
+ ArkAnalyzerUtil.getInstance().clear();
206
+ }
207
+ }
208
+ exports.ArkAnalyzerUtil = ArkAnalyzerUtil;
@@ -5,3 +5,4 @@ export * from './ModuleTypeUtil';
5
5
  export * from './StringUtil';
6
6
  export * from './ObjectUtils';
7
7
  export * from './Logger';
8
+ export * from './ArkAnalyzerUtil';
@@ -21,3 +21,4 @@ __exportStar(require("./ModuleTypeUtil"), exports);
21
21
  __exportStar(require("./StringUtil"), exports);
22
22
  __exportStar(require("./ObjectUtils"), exports);
23
23
  __exportStar(require("./Logger"), exports);
24
+ __exportStar(require("./ArkAnalyzerUtil"), exports);
@@ -19,7 +19,7 @@ class HMRouterDefaultExtension extends framework_1.PluginExtension {
19
19
  framework_2.Logger.debug(this.name, 'module ignored: annotationAnalysis');
20
20
  return;
21
21
  }
22
- AnnotationAnalyzerRegistry_1.AnnotationAnalyzerRegistry.getInstance().initialize(context.config.modulePath);
22
+ AnnotationAnalyzerRegistry_1.AnnotationAnalyzerRegistry.getInstance().initialize();
23
23
  const analyzers = AnnotationAnalyzerRegistry_1.AnnotationAnalyzerRegistry.getInstance().getAnalyzers();
24
24
  for (const analyzer of analyzers) {
25
25
  analyzer.analyze(sourceFile, filePath, context);
@@ -6,7 +6,7 @@ export declare class AnnotationAnalyzerRegistry {
6
6
  private constantResolver;
7
7
  private constructor();
8
8
  static getInstance(): AnnotationAnalyzerRegistry;
9
- initialize(modulePath: string): void;
9
+ initialize(): void;
10
10
  registerAnalyzer(analyzer: AbstractAnnotationAnalyzer): void;
11
11
  getAnalyzers(): AbstractAnnotationAnalyzer[];
12
12
  getConstantResolver(): IConstantResolver;
@@ -15,11 +15,11 @@ class AnnotationAnalyzerRegistry {
15
15
  }
16
16
  return this.instance;
17
17
  }
18
- initialize(modulePath) {
18
+ initialize() {
19
19
  if (this.constantResolver) {
20
20
  return;
21
21
  }
22
- this.constantResolver = new ConstantResolver_1.ConstantResolver(modulePath);
22
+ this.constantResolver = new ConstantResolver_1.ConstantResolver();
23
23
  this.registerDefaultAnalyzers();
24
24
  }
25
25
  registerAnalyzer(analyzer) {
@@ -2,14 +2,10 @@ import { Expression, SourceFile } from 'ts-morph';
2
2
  import { IConstantResolver } from '../interface/IConstantResolver';
3
3
  export declare class ConstantResolver implements IConstantResolver {
4
4
  private importMapCache;
5
- private readonly modulePath;
6
- constructor(modulePath: string);
7
5
  resolveConstant(value: any, sourceFile: SourceFile, filePath: string): any;
8
6
  resolvePropertyValue(value: Expression, sourceFile: SourceFile, filePath: string): any;
9
7
  getVariableFilePath(variableName: string, sourceFile: SourceFile, filePath: string): string;
10
- getOtherModuleVariableFilePath(moduleName: string, variableName: string, modulePath: string): string;
8
+ getOtherModuleVariableFilePath(moduleName: string, variableName: string): string;
11
9
  private parsePrimitiveValue;
12
10
  private parseConstantValue;
13
- private parseCrossModuleVariable;
14
- getExportedVariables(sourceFile: SourceFile): string[];
15
11
  }
@@ -7,9 +7,8 @@ const constants_1 = require("../../constants");
7
7
  const ImportAnalyzer_1 = require("./ImportAnalyzer");
8
8
  const PluginError_1 = require("../../error/PluginError");
9
9
  class ConstantResolver {
10
- constructor(modulePath) {
10
+ constructor() {
11
11
  this.importMapCache = new Map();
12
- this.modulePath = modulePath;
13
12
  }
14
13
  resolveConstant(value, sourceFile, filePath) {
15
14
  if (value.type === 'constant') {
@@ -87,33 +86,18 @@ class ConstantResolver {
87
86
  if (!resultPath) {
88
87
  for (const [importPath, importNames] of importMap.entries()) {
89
88
  if (importNames.includes(variableName)) {
90
- resultPath = this.getOtherModuleVariableFilePath(importPath, variableName, this.modulePath);
89
+ resultPath = this.getOtherModuleVariableFilePath(importPath, variableName);
91
90
  break;
92
91
  }
93
92
  }
94
93
  }
95
94
  return resultPath;
96
95
  }
97
- getOtherModuleVariableFilePath(moduleName, variableName, modulePath) {
98
- let moduleFilePath = framework_1.PluginFileUtil.pathResolve(modulePath, constants_1.FilePathConstants.OH_MODULE_PATH, moduleName, constants_1.FilePathConstants.DEFAULT_SCAN_DIR);
99
- if (!framework_1.PluginFileUtil.exist(moduleFilePath)) {
100
- moduleFilePath = framework_1.PluginFileUtil.pathResolve(framework_1.PluginStore.getInstance().get('projectFilePath'), constants_1.FilePathConstants.OH_MODULE_PATH, moduleName, constants_1.FilePathConstants.DEFAULT_SCAN_DIR);
101
- }
102
- const variableCache = framework_1.PluginStore.getInstance().get('variableCache');
103
- if (!variableCache) {
104
- framework_1.PluginStore.getInstance().set('variableCache', new Map());
105
- }
106
- let variableMap;
107
- if (variableCache?.has(moduleName)) {
108
- variableMap = variableCache.get(moduleName);
109
- }
110
- else {
111
- variableMap = this.parseCrossModuleVariable(moduleFilePath);
112
- variableCache?.set(moduleName, variableMap);
113
- }
114
- for (let [key, value] of variableMap) {
115
- if (value.includes(variableName)) {
116
- return key;
96
+ getOtherModuleVariableFilePath(moduleName, variableName) {
97
+ if (framework_1.ArkAnalyzerUtil.isReady()) {
98
+ const filePath = framework_1.ArkAnalyzerUtil.findVariableFilePath(moduleName, variableName);
99
+ if (filePath) {
100
+ return filePath;
117
101
  }
118
102
  }
119
103
  throw PluginError_1.PluginError.create(PluginError_1.ErrorCode.UNKNOWN_VARIABLE, '', variableName);
@@ -177,41 +161,5 @@ class ConstantResolver {
177
161
  }
178
162
  return resultValue;
179
163
  }
180
- parseCrossModuleVariable(scanDir) {
181
- let sourceFiles = framework_1.TsAstUtil.project.addSourceFilesAtPaths(`${scanDir}/**/*.ets`);
182
- const exportMap = new Map();
183
- for (let sourceFile of sourceFiles) {
184
- const exportedNames = this.getExportedVariables(sourceFile);
185
- if (exportedNames.length > 0) {
186
- exportMap.set(sourceFile.getFilePath(), exportedNames);
187
- }
188
- }
189
- return exportMap;
190
- }
191
- getExportedVariables(sourceFile) {
192
- const exportSymbols = [];
193
- let exportKeywordNodes = sourceFile.getDescendantsOfKind(ts_morph_1.SyntaxKind.ExportKeyword);
194
- exportKeywordNodes.forEach((node) => {
195
- let parentNodeKind = node.getParent()?.getKind();
196
- switch (parentNodeKind) {
197
- case ts_morph_1.SyntaxKind.VariableStatement:
198
- let variableStatement = node.getParent()?.asKind(ts_morph_1.SyntaxKind.VariableStatement);
199
- let variableNames = variableStatement
200
- .getDeclarationList()
201
- .getDeclarations()
202
- .map((declaration) => {
203
- return declaration.getName();
204
- });
205
- exportSymbols.push(...variableNames);
206
- break;
207
- case ts_morph_1.SyntaxKind.ClassDeclaration:
208
- let classDeclaration = node.getParent();
209
- let className = classDeclaration?.asKind(ts_morph_1.SyntaxKind.ClassDeclaration)?.getName();
210
- exportSymbols.push(className);
211
- break;
212
- }
213
- });
214
- return exportSymbols;
215
- }
216
164
  }
217
165
  exports.ConstantResolver = ConstantResolver;
@@ -12,6 +12,7 @@ export default class FilePathConstants {
12
12
  static readonly CURRENT_DELIMITER = "./";
13
13
  static readonly OH_MODULE_PATH = "oh_modules";
14
14
  static readonly OH_PACKAGE_FILE_NAME = "oh-package.json5";
15
+ static readonly OH_PACKAGE_LOCK_FILE_NAME = "oh-package-lock.json5";
15
16
  static readonly LINE_BREAK = "\n";
16
17
  static readonly BUILD_PROFILE = "build-profile.json5";
17
18
  static readonly SDK_HOME = "default/openharmony/ets/oh-uni-package.json";
@@ -20,6 +20,7 @@ FilePathConstants.PARENT_DELIMITER = '../';
20
20
  FilePathConstants.CURRENT_DELIMITER = './';
21
21
  FilePathConstants.OH_MODULE_PATH = 'oh_modules';
22
22
  FilePathConstants.OH_PACKAGE_FILE_NAME = 'oh-package.json5';
23
+ FilePathConstants.OH_PACKAGE_LOCK_FILE_NAME = 'oh-package-lock.json5';
23
24
  FilePathConstants.LINE_BREAK = '\n';
24
25
  FilePathConstants.BUILD_PROFILE = 'build-profile.json5';
25
26
  FilePathConstants.SDK_HOME = 'default/openharmony/ets/oh-uni-package.json';
@@ -2,5 +2,6 @@ export default class TemplateConstants {
2
2
  static readonly CUSTOM_BUILDER_TEMPLATE = "template/customBuilder.ejs";
3
3
  static readonly VIEW_BUILDER_TEMPLATE = "template/viewBuilder.ejs";
4
4
  static readonly V1_TEMPLATE = "template/viewBuilder_v1.ejs";
5
+ static readonly V1_1_TEMPLATE = "template/viewBuilder_v1_1.ejs";
5
6
  static readonly WARP_BUILDER = "WrapBuilder";
6
7
  }
@@ -6,4 +6,5 @@ exports.default = TemplateConstants;
6
6
  TemplateConstants.CUSTOM_BUILDER_TEMPLATE = 'template/customBuilder.ejs';
7
7
  TemplateConstants.VIEW_BUILDER_TEMPLATE = 'template/viewBuilder.ejs';
8
8
  TemplateConstants.V1_TEMPLATE = 'template/viewBuilder_v1.ejs';
9
+ TemplateConstants.V1_1_TEMPLATE = 'template/viewBuilder_v1_1.ejs';
9
10
  TemplateConstants.WARP_BUILDER = 'WrapBuilder';
@@ -3,4 +3,6 @@ export default class VersionConstants {
3
3
  static readonly HMROUTER_LIB_NAME = "hmrouter";
4
4
  static readonly HMROUTER_VERSION_KEY = "version";
5
5
  static readonly HMROUTER_VERSION_V1 = "1.0.0";
6
+ static readonly HMROUTER_MIN_VERSION_FOR_NAV_HELPER = "1.1.0";
7
+ static readonly HMROUTER_MIN_VERSION_FOR_SDK_API = "1.2.0";
6
8
  }
@@ -7,3 +7,5 @@ VersionConstants.HMROUTER_ORGANIZATION = '@hadss';
7
7
  VersionConstants.HMROUTER_LIB_NAME = 'hmrouter';
8
8
  VersionConstants.HMROUTER_VERSION_KEY = 'version';
9
9
  VersionConstants.HMROUTER_VERSION_V1 = '1.0.0';
10
+ VersionConstants.HMROUTER_MIN_VERSION_FOR_NAV_HELPER = '1.1.0';
11
+ VersionConstants.HMROUTER_MIN_VERSION_FOR_SDK_API = '1.2.0';
@@ -11,8 +11,12 @@ export declare class CodeGenerationProcessor {
11
11
  private matchedPath;
12
12
  private prepareTemplateModel;
13
13
  private determineTemplatePath;
14
+ private determineTemplateType;
14
15
  private generateFile;
15
16
  private detectLibraryVersion;
16
17
  private detectLibraryVersion2;
18
+ private extractHMRouterVersion;
17
19
  private getLibraryPossiblePaths;
20
+ private parseVersion;
21
+ private compareVersions;
18
22
  }
@@ -125,14 +125,43 @@ class CodeGenerationProcessor {
125
125
  determineTemplatePath(analyzeResult, defaultTemplatePath) {
126
126
  const libraryVersion = this.detectLibraryVersion();
127
127
  framework_1.Logger.debug(this.context.node.getNodeName(), `Library version: ${libraryVersion}`);
128
- const useV1Template = libraryVersion.startsWith(constants_1.VersionConstants.HMROUTER_VERSION_V1);
129
- if (useV1Template) {
130
- return framework_1.PluginFileUtil.pathResolve(__dirname, constants_1.FilePathConstants.PARENT_DELIMITER.repeat(3) + constants_1.TemplateConstants.V1_TEMPLATE);
131
- }
132
- else if (analyzeResult.useNavDst === true) {
128
+ if (analyzeResult.useNavDst === true) {
133
129
  return framework_1.PluginFileUtil.pathResolve(__dirname, constants_1.FilePathConstants.PARENT_DELIMITER.repeat(3), constants_1.TemplateConstants.CUSTOM_BUILDER_TEMPLATE);
134
130
  }
135
- return defaultTemplatePath;
131
+ const templateType = this.determineTemplateType(libraryVersion);
132
+ if (templateType !== 'latest' && libraryVersion) {
133
+ framework_1.Logger.warn(this.context.node.getNodeName(), `[HMRouter Version Compatibility] Detected library version ${libraryVersion}, ` +
134
+ `which is below the minimum recommended version ${constants_1.VersionConstants.HMROUTER_MIN_VERSION_FOR_SDK_API}. ` +
135
+ `Automatically switched to compatible template (${templateType}). ` +
136
+ `Please upgrade @hadss/hmrouter to ${constants_1.VersionConstants.HMROUTER_MIN_VERSION_FOR_SDK_API} or higher for full feature support.`);
137
+ }
138
+ switch (templateType) {
139
+ case 'v1':
140
+ return framework_1.PluginFileUtil.pathResolve(__dirname, constants_1.FilePathConstants.PARENT_DELIMITER.repeat(3) + constants_1.TemplateConstants.V1_TEMPLATE);
141
+ case 'v1_1':
142
+ return framework_1.PluginFileUtil.pathResolve(__dirname, constants_1.FilePathConstants.PARENT_DELIMITER.repeat(3) + constants_1.TemplateConstants.V1_1_TEMPLATE);
143
+ case 'latest':
144
+ default:
145
+ return defaultTemplatePath;
146
+ }
147
+ }
148
+ determineTemplateType(libraryVersion) {
149
+ if (!libraryVersion) {
150
+ return 'v1';
151
+ }
152
+ const currentVersion = this.parseVersion(libraryVersion);
153
+ const v1_1Version = this.parseVersion(constants_1.VersionConstants.HMROUTER_MIN_VERSION_FOR_NAV_HELPER);
154
+ const v1_2Version = this.parseVersion(constants_1.VersionConstants.HMROUTER_MIN_VERSION_FOR_SDK_API);
155
+ if (!currentVersion || !v1_1Version || !v1_2Version) {
156
+ return 'v1';
157
+ }
158
+ if (this.compareVersions(currentVersion, v1_2Version) >= 0) {
159
+ return 'latest';
160
+ }
161
+ if (this.compareVersions(currentVersion, v1_1Version) >= 0) {
162
+ return 'v1_1';
163
+ }
164
+ return 'v1';
136
165
  }
137
166
  generateFile(templateModel, templatePath) {
138
167
  if (!framework_1.PluginFileUtil.exist(templatePath)) {
@@ -158,26 +187,78 @@ class CodeGenerationProcessor {
158
187
  }
159
188
  return libraryVersion;
160
189
  }
161
- detectLibraryVersion2(packagePath) {
162
- let libraryVersion = '';
190
+ detectLibraryVersion2(lockFilePath) {
163
191
  try {
164
- if (framework_1.PluginFileUtil.exist(packagePath)) {
165
- const packageJson = framework_1.PluginFileUtil.readJson5(packagePath);
166
- libraryVersion = packageJson[constants_1.VersionConstants.HMROUTER_VERSION_KEY] || '';
192
+ if (!framework_1.PluginFileUtil.exist(lockFilePath)) {
193
+ return '';
167
194
  }
195
+ const lockFileData = framework_1.PluginFileUtil.readJson5(lockFilePath);
196
+ return this.extractHMRouterVersion(lockFileData.packages);
168
197
  }
169
198
  catch (error) {
199
+ return '';
170
200
  }
171
- return libraryVersion;
201
+ }
202
+ extractHMRouterVersion(packages) {
203
+ if (!packages) {
204
+ return '';
205
+ }
206
+ const hmrouterPrefix = `${constants_1.VersionConstants.HMROUTER_ORGANIZATION}/${constants_1.VersionConstants.HMROUTER_LIB_NAME}@`;
207
+ for (const key of Object.keys(packages)) {
208
+ if (!key.startsWith(hmrouterPrefix)) {
209
+ continue;
210
+ }
211
+ const packageInfo = packages[key];
212
+ if (packageInfo && packageInfo[constants_1.VersionConstants.HMROUTER_VERSION_KEY]) {
213
+ return packageInfo[constants_1.VersionConstants.HMROUTER_VERSION_KEY];
214
+ }
215
+ }
216
+ return '';
172
217
  }
173
218
  getLibraryPossiblePaths() {
174
219
  const paths = [];
175
- paths.push(framework_1.PluginFileUtil.pathResolve(this.context.config.modulePath, constants_1.FilePathConstants.OH_MODULE_PATH, constants_1.VersionConstants.HMROUTER_ORGANIZATION, constants_1.VersionConstants.HMROUTER_LIB_NAME, constants_1.FilePathConstants.OH_PACKAGE_FILE_NAME));
220
+ paths.push(framework_1.PluginFileUtil.pathResolve(this.context.config.modulePath, constants_1.FilePathConstants.OH_PACKAGE_LOCK_FILE_NAME));
176
221
  const projectRoot = framework_1.PluginStore.getInstance().get('projectFilePath');
177
222
  if (projectRoot && projectRoot !== this.context.config.modulePath) {
178
- paths.push(framework_1.PluginFileUtil.pathResolve(projectRoot, constants_1.FilePathConstants.OH_MODULE_PATH, constants_1.VersionConstants.HMROUTER_ORGANIZATION, constants_1.VersionConstants.HMROUTER_LIB_NAME, constants_1.FilePathConstants.OH_PACKAGE_FILE_NAME));
223
+ paths.push(framework_1.PluginFileUtil.pathResolve(projectRoot, constants_1.FilePathConstants.OH_PACKAGE_LOCK_FILE_NAME));
179
224
  }
180
225
  return paths;
181
226
  }
227
+ parseVersion(version) {
228
+ const match = version.match(/^(\d+)\.(\d+)\.(\d+)(-.*)?/);
229
+ if (!match) {
230
+ return null;
231
+ }
232
+ return {
233
+ major: parseInt(match[1], 10),
234
+ minor: parseInt(match[2], 10),
235
+ patch: parseInt(match[3], 10),
236
+ preRelease: match[4] || '',
237
+ };
238
+ }
239
+ compareVersions(v1, v2) {
240
+ if (v1.major !== v2.major) {
241
+ return v1.major < v2.major ? -1 : 1;
242
+ }
243
+ if (v1.minor !== v2.minor) {
244
+ return v1.minor < v2.minor ? -1 : 1;
245
+ }
246
+ if (v1.patch !== v2.patch) {
247
+ return v1.patch < v2.patch ? -1 : 1;
248
+ }
249
+ if (v1.preRelease && !v2.preRelease) {
250
+ return -1;
251
+ }
252
+ if (!v1.preRelease && v2.preRelease) {
253
+ return 1;
254
+ }
255
+ if (v1.preRelease < v2.preRelease) {
256
+ return -1;
257
+ }
258
+ if (v1.preRelease > v2.preRelease) {
259
+ return 1;
260
+ }
261
+ return 0;
262
+ }
182
263
  }
183
264
  exports.CodeGenerationProcessor = CodeGenerationProcessor;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hadss/hmrouter-plugin",
3
- "version": "1.2.2",
3
+ "version": "1.2.3-rc.1",
4
4
  "description": "HMRouter Compiler Plugin",
5
5
  "main": "dist/Index.js",
6
6
  "scripts": {
@@ -21,7 +21,8 @@
21
21
  "dependencies": {
22
22
  "ejs": "^3.1.10",
23
23
  "micromatch": "^4.0.8",
24
- "ts-morph": "^23.0.0"
24
+ "ts-morph": "^23.0.0",
25
+ "arkanalyzer": "^1.0.79"
25
26
  },
26
27
  "devDependencies": {
27
28
  "@ohos/hvigor": "^5.8.9",
@@ -0,0 +1,51 @@
1
+ /*
2
+ * Copyright (c) 2024 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
+
16
+ // instrument ignore file
17
+ // auto-generated <%= componentName %>Builder.ets by HMRouter
18
+ import <%if(isDefaultExport) {%> <%= componentName %> <% } else {%> { <%= componentName %> } <% } %> from '<%= importPath %>'
19
+ import { NavDestinationHelper } from '@hadss/hmrouter';
20
+
21
+ @Builder
22
+ export function <%= componentName %>Builder(name: string, param: Object) {
23
+ <%= componentName %>Generated()
24
+ }
25
+
26
+ export const <%= componentName %>BuilderWrapBuilder = wrapBuilder(<%= componentName %>Builder)
27
+
28
+ @Component
29
+ export struct <%= componentName %>Generated {
30
+ private helper: NavDestinationHelper = new NavDestinationHelper(this);
31
+
32
+ build() {
33
+ NavDestination() {
34
+ <%= componentName %>()
35
+ }
36
+ <% if(dialog){ %>.mode(NavDestinationMode.DIALOG)<% } %>
37
+ .hideTitleBar(true)
38
+ .attributeModifier(this.helper.modifier)
39
+ .gestureModifier(this.helper.gestureModifier)
40
+ .onWillAppear(()=>{this.helper.onWillAppear()})
41
+ .onAppear(() => {this.helper.onAppear()})
42
+ .onWillShow(()=>{this.helper.onWillShow()})
43
+ .onShown(()=>{this.helper.onShown()})
44
+ .onWillHide(()=>{this.helper.onWillHide()})
45
+ .onHidden(()=>{this.helper.onHidden()})
46
+ .onWillDisappear(()=>{this.helper.onWillDisappear()})
47
+ .onDisAppear(()=>{this.helper.onDisAppear()})
48
+ .onReady((ctx)=>{this.helper.onReady(ctx)})
49
+ .onBackPressed(()=> this.helper.onBackPressed())
50
+ }
51
+ }