@hadss/hmrouter-plugin 1.0.0-rc.0
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/LICENSE +78 -0
- package/README.md +37 -0
- package/lib/HMRouterAnalyzer.js +223 -0
- package/lib/HMRouterHvigorPlugin.js +175 -0
- package/lib/HMRouterPluginConfig.js +31 -0
- package/lib/Index.js +201 -0
- package/lib/PluginModel.js +33 -0
- package/package.json +27 -0
- package/src/HMRouterAnalyzer.ts +253 -0
- package/src/HMRouterHvigorPlugin.ts +246 -0
- package/src/HMRouterPluginConfig.ts +44 -0
- package/src/Index.ts +218 -0
- package/src/PluginModel.ts +50 -0
- package/tsconfig.json +14 -0
- package/viewBuilder.tpl +97 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
Copyright (c) 2024 Huawei Device Co., Ltd. All rights reserved.
|
|
2
|
+
|
|
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
|
+
Apache License, Version 2.0
|
|
16
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
17
|
+
|
|
18
|
+
1. Definitions.
|
|
19
|
+
|
|
20
|
+
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
|
|
21
|
+
|
|
22
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
|
|
23
|
+
|
|
24
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
|
|
25
|
+
|
|
26
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
|
|
27
|
+
|
|
28
|
+
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
|
|
31
|
+
|
|
32
|
+
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
|
|
33
|
+
|
|
34
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
|
|
35
|
+
|
|
36
|
+
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
|
|
37
|
+
|
|
38
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
|
|
39
|
+
|
|
40
|
+
2. Grant of Copyright License.
|
|
41
|
+
|
|
42
|
+
Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
|
|
43
|
+
|
|
44
|
+
3. Grant of Patent License.
|
|
45
|
+
|
|
46
|
+
Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
|
|
47
|
+
|
|
48
|
+
4. Redistribution.
|
|
49
|
+
|
|
50
|
+
You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
|
|
51
|
+
1.You must give any other recipients of the Work or Derivative Works a copy of this License; and
|
|
52
|
+
2.You must cause any modified files to carry prominent notices stating that You changed the files; and
|
|
53
|
+
3.You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
|
|
54
|
+
4.If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
|
|
55
|
+
|
|
56
|
+
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
|
|
57
|
+
|
|
58
|
+
5. Submission of Contributions.
|
|
59
|
+
|
|
60
|
+
Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
|
|
61
|
+
|
|
62
|
+
6. Trademarks.
|
|
63
|
+
|
|
64
|
+
This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
|
|
65
|
+
|
|
66
|
+
7. Disclaimer of Warranty.
|
|
67
|
+
|
|
68
|
+
Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
|
|
69
|
+
|
|
70
|
+
8. Limitation of Liability.
|
|
71
|
+
|
|
72
|
+
In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
|
|
73
|
+
|
|
74
|
+
9. Accepting Warranty or Additional Liability.
|
|
75
|
+
|
|
76
|
+
While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
|
|
77
|
+
|
|
78
|
+
END OF TERMS AND CONDITIONS
|
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
## 编译插件配置
|
|
2
|
+
|
|
3
|
+
1. 修改项目的`hvigor/hvigor-config.json`文件,加入路由编译插件
|
|
4
|
+
```json5
|
|
5
|
+
{
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"@hadss/hmrouter-plugin": "^1.0.0-rc.0" // 使用npm仓版本号
|
|
8
|
+
},
|
|
9
|
+
// ...其他配置
|
|
10
|
+
}
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
2. 项目根目录或者模块目录创建路由编译插件配置文件`hmrouter_config.json`(推荐)
|
|
14
|
+
```json5
|
|
15
|
+
{
|
|
16
|
+
"scanDir": ["src/main/ets/components","src/main/ets/interceptors"],
|
|
17
|
+
// 如果不传则扫描src/main/ets目录,对代码进行全量扫描,建议配置可缩短编译耗时
|
|
18
|
+
"saveGeneratedFile": false, // 默认为false,改成true则不删除编译产物
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
3. 在模块中引入路由编译插件,修改`hvigorfile.ts`
|
|
23
|
+
```typescript
|
|
24
|
+
import { hapTasks } from '@ohos/hvigor-ohos-plugin';
|
|
25
|
+
import { hapPlugin } from "@hadss/hmrouter-plugin";
|
|
26
|
+
|
|
27
|
+
export default {
|
|
28
|
+
system: hapTasks,
|
|
29
|
+
plugins: [hapPlugin()]
|
|
30
|
+
}
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
> 如果是Har则使用`harPlugin()`, Hsp则使用`hspPlugin()`
|
|
34
|
+
|
|
35
|
+
## HMRouter使用
|
|
36
|
+
|
|
37
|
+
详见[@hadss/hmrouter](https://ohpm.openharmony.cn/#/cn/detail/@hadss%2Fhmrouter)
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.Analyzer = void 0;
|
|
21
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
22
|
+
const fs_1 = __importDefault(require("fs"));
|
|
23
|
+
const PluginModel_1 = require("./PluginModel");
|
|
24
|
+
const hvigor_1 = require("@ohos/hvigor");
|
|
25
|
+
const Index_1 = require("./Index");
|
|
26
|
+
class NodeInfo {
|
|
27
|
+
}
|
|
28
|
+
const Logger = hvigor_1.HvigorLogger.getLogger();
|
|
29
|
+
class Analyzer {
|
|
30
|
+
constructor(sourcePath, pluginConfig) {
|
|
31
|
+
this.customAnnotationExisted = false;
|
|
32
|
+
this.analyzeResultMap = new Map();
|
|
33
|
+
this.analyzeResult = {};
|
|
34
|
+
this.keywordPos = 0;
|
|
35
|
+
this.sourcePath = sourcePath;
|
|
36
|
+
this.pluginConfig = pluginConfig;
|
|
37
|
+
}
|
|
38
|
+
start() {
|
|
39
|
+
const sourceCode = fs_1.default.readFileSync(this.sourcePath, 'utf-8');
|
|
40
|
+
const sourceFile = typescript_1.default.createSourceFile(this.sourcePath, sourceCode, typescript_1.default.ScriptTarget.ES2021, false);
|
|
41
|
+
typescript_1.default.forEachChild(sourceFile, node => {
|
|
42
|
+
this.resolveNode(node);
|
|
43
|
+
// 解析完成后赋值存进Map
|
|
44
|
+
switch (this.analyzeResult.annotation) {
|
|
45
|
+
case 'HMRouter':
|
|
46
|
+
this.analyzeResultMap.set(this.analyzeResult.pageUrl, this.analyzeResult);
|
|
47
|
+
break;
|
|
48
|
+
case 'HMAnimator':
|
|
49
|
+
this.analyzeResultMap.set(this.analyzeResult.animatorName, this.analyzeResult);
|
|
50
|
+
break;
|
|
51
|
+
case 'HMInterceptor':
|
|
52
|
+
this.analyzeResultMap.set(this.analyzeResult.interceptorName, this.analyzeResult);
|
|
53
|
+
break;
|
|
54
|
+
case 'HMLifecycle':
|
|
55
|
+
this.analyzeResultMap.set(this.analyzeResult.lifecycleName, this.analyzeResult);
|
|
56
|
+
break;
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
resolveNode(node) {
|
|
61
|
+
if (typescript_1.default.isMissingDeclaration(node)) {
|
|
62
|
+
this.resolveMissingDeclaration(node);
|
|
63
|
+
}
|
|
64
|
+
else if (typescript_1.default.isClassDeclaration(node)) {
|
|
65
|
+
this.resolveClass(node);
|
|
66
|
+
}
|
|
67
|
+
else if (typescript_1.default.isDecorator(node)) {
|
|
68
|
+
this.resolveDecorator(node);
|
|
69
|
+
}
|
|
70
|
+
else if (typescript_1.default.isCallExpression(node)) {
|
|
71
|
+
this.resolveCallExpression(node);
|
|
72
|
+
}
|
|
73
|
+
else if (typescript_1.default.isExpressionStatement(node)) {
|
|
74
|
+
this.resolveExpression(node);
|
|
75
|
+
}
|
|
76
|
+
else if (typescript_1.default.isBlock(node)) {
|
|
77
|
+
this.resolveBlock(node);
|
|
78
|
+
}
|
|
79
|
+
else if (typescript_1.default.isPropertyAssignment(node)) {
|
|
80
|
+
return this.resolvePropertyAccess(node);
|
|
81
|
+
}
|
|
82
|
+
else if (typescript_1.default.isIdentifier(node)) {
|
|
83
|
+
return this.resolveIdentifier(node);
|
|
84
|
+
}
|
|
85
|
+
else if (typescript_1.default.isStringLiteral(node)) {
|
|
86
|
+
return this.resolveStringLiteral(node);
|
|
87
|
+
}
|
|
88
|
+
else if (node.kind === typescript_1.default.SyntaxKind.TrueKeyword) {
|
|
89
|
+
let info = new NodeInfo();
|
|
90
|
+
info.value = true;
|
|
91
|
+
return info;
|
|
92
|
+
}
|
|
93
|
+
else if (node.kind === typescript_1.default.SyntaxKind.FalseKeyword) {
|
|
94
|
+
let info = new NodeInfo();
|
|
95
|
+
info.value = false;
|
|
96
|
+
return info;
|
|
97
|
+
}
|
|
98
|
+
else if (typescript_1.default.isNumericLiteral(node)) {
|
|
99
|
+
return this.resolveNumericLiteral(node);
|
|
100
|
+
}
|
|
101
|
+
else if (typescript_1.default.isArrayLiteralExpression(node)) {
|
|
102
|
+
let interceptors = this.resolveArrayLiteral(node);
|
|
103
|
+
let info = new NodeInfo();
|
|
104
|
+
info.value = interceptors;
|
|
105
|
+
return info;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
resolveMissingDeclaration(node) {
|
|
109
|
+
this.analyzeResult = {};
|
|
110
|
+
node.forEachChild(child => this.resolveNode(child));
|
|
111
|
+
}
|
|
112
|
+
resolveClass(node) {
|
|
113
|
+
// 解析到类声明,先清空一次返回结果
|
|
114
|
+
this.analyzeResult = {};
|
|
115
|
+
node.modifiers?.forEach(modifier => {
|
|
116
|
+
// 遍历分析装饰器
|
|
117
|
+
this.resolveNode(modifier);
|
|
118
|
+
});
|
|
119
|
+
if (this.customAnnotationExisted) {
|
|
120
|
+
this.analyzeResult.name = node.name?.text;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
resolveDecorator(node) {
|
|
124
|
+
if (typescript_1.default.isCallExpression(node.expression)) {
|
|
125
|
+
const callExpression = node.expression;
|
|
126
|
+
if (typescript_1.default.isIdentifier(callExpression.expression)) {
|
|
127
|
+
this.switchIdentifier(callExpression);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
switchIdentifier(callExpression) {
|
|
132
|
+
const identifier = callExpression.expression;
|
|
133
|
+
if (this.pluginConfig.annotation.some(item => item === identifier.text)) {
|
|
134
|
+
this.customAnnotationExisted = true;
|
|
135
|
+
// 区分是什么装饰器,构造不同的返回类
|
|
136
|
+
switch (identifier.text) {
|
|
137
|
+
case 'HMRouter':
|
|
138
|
+
this.analyzeResult = new PluginModel_1.HMRouterResult();
|
|
139
|
+
this.analyzeResult.annotation = 'HMRouter';
|
|
140
|
+
break;
|
|
141
|
+
case 'HMAnimator':
|
|
142
|
+
this.analyzeResult = new PluginModel_1.HMAnimatorResult();
|
|
143
|
+
this.analyzeResult.annotation = 'HMAnimator';
|
|
144
|
+
break;
|
|
145
|
+
case 'HMInterceptor':
|
|
146
|
+
this.analyzeResult = new PluginModel_1.HMInterceptorResult();
|
|
147
|
+
this.analyzeResult.annotation = 'HMInterceptor';
|
|
148
|
+
break;
|
|
149
|
+
case 'HMLifecycle':
|
|
150
|
+
this.analyzeResult = new PluginModel_1.HMLifecycleResult();
|
|
151
|
+
this.analyzeResult.annotation = 'HMLifecycle';
|
|
152
|
+
break;
|
|
153
|
+
}
|
|
154
|
+
if (callExpression.arguments.length > 0) {
|
|
155
|
+
this.resolveCallExpression(callExpression);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
resolveCallExpression(node) {
|
|
160
|
+
let identifier = this.resolveNode(node.expression);
|
|
161
|
+
this.parseAnnotation(node.arguments, identifier);
|
|
162
|
+
}
|
|
163
|
+
resolveExpression(node) {
|
|
164
|
+
let identifier = this.resolveNode(node.expression);
|
|
165
|
+
if (identifier?.value === 'struct') {
|
|
166
|
+
this.keywordPos = node.end;
|
|
167
|
+
}
|
|
168
|
+
if (this.analyzeResult.annotation === 'HMRouter' && this.keywordPos === node.pos) {
|
|
169
|
+
this.analyzeResult.name = identifier?.value;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
resolveBlock(node) {
|
|
173
|
+
node.statements.forEach(statement => {
|
|
174
|
+
this.resolveNode(statement);
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
parseAnnotation(args, nodeInfo) {
|
|
178
|
+
if (this.pluginConfig.annotation.some(item => nodeInfo?.value === item)) {
|
|
179
|
+
args
|
|
180
|
+
.flatMap((e) => e.properties)
|
|
181
|
+
.forEach((e) => {
|
|
182
|
+
this.parseConfig(e, this.analyzeResult);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
parseConfig(node, result) {
|
|
187
|
+
let info = this.resolveNode(node);
|
|
188
|
+
Reflect.set(result, info?.value['key'], info?.value['value']);
|
|
189
|
+
}
|
|
190
|
+
resolveArrayLiteral(node) {
|
|
191
|
+
return node.elements.map(e => this.resolveNode(e)?.value);
|
|
192
|
+
}
|
|
193
|
+
resolvePropertyAccess(node) {
|
|
194
|
+
let propertyName = this.resolveNode(node.name)?.value;
|
|
195
|
+
let propertyValue = this.resolveNode(node.initializer)?.value;
|
|
196
|
+
let info = new NodeInfo();
|
|
197
|
+
info.value = { key: propertyName, value: propertyValue };
|
|
198
|
+
return info;
|
|
199
|
+
}
|
|
200
|
+
resolveNumericLiteral(node) {
|
|
201
|
+
let info = new NodeInfo();
|
|
202
|
+
info.value = Number(node.text);
|
|
203
|
+
return info;
|
|
204
|
+
}
|
|
205
|
+
resolveStringLiteral(node) {
|
|
206
|
+
let info = new NodeInfo();
|
|
207
|
+
info.value = node.text;
|
|
208
|
+
return info;
|
|
209
|
+
}
|
|
210
|
+
resolveIdentifier(node) {
|
|
211
|
+
if (node.escapedText === 'NavDestination' && this.analyzeResult.annotation === 'HMRouter') {
|
|
212
|
+
Logger.error('errorCode: 40000003, errorMsg: @HMRouter 修饰的组件不能包含 NavDestination in' +
|
|
213
|
+
this.analyzeResult.name);
|
|
214
|
+
(0, Index_1.deleteGeneratorFile)(this.pluginConfig);
|
|
215
|
+
throw new Error('NavDestination is not allowed in HMRouter' + this.sourcePath);
|
|
216
|
+
}
|
|
217
|
+
let info = new NodeInfo();
|
|
218
|
+
info.value = node.escapedText.toString();
|
|
219
|
+
return info;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
exports.Analyzer = Analyzer;
|
|
223
|
+
//# sourceMappingURL=HMRouterAnalyzer.js.map
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
exports.HMRouterHvigorPlugin = exports.RouterInfo = void 0;
|
|
21
|
+
const hvigor_1 = require("@ohos/hvigor");
|
|
22
|
+
const path_1 = __importDefault(require("path"));
|
|
23
|
+
const fs_1 = __importDefault(require("fs"));
|
|
24
|
+
const HMRouterAnalyzer_1 = require("./HMRouterAnalyzer");
|
|
25
|
+
const handlebars_1 = __importDefault(require("handlebars"));
|
|
26
|
+
const Index_1 = require("./Index");
|
|
27
|
+
const Logger = hvigor_1.HvigorLogger.getLogger();
|
|
28
|
+
class RouterInfo {
|
|
29
|
+
constructor(name, pageSourceFile, buildFunction, data = {}) {
|
|
30
|
+
this.name = name;
|
|
31
|
+
this.pageSourceFile = pageSourceFile;
|
|
32
|
+
this.buildFunction = buildFunction;
|
|
33
|
+
this.data = data;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
exports.RouterInfo = RouterInfo;
|
|
37
|
+
class HMRouterHvigorPlugin {
|
|
38
|
+
constructor(config) {
|
|
39
|
+
this.routerMap = [];
|
|
40
|
+
this.scanFiles = [];
|
|
41
|
+
this.HMRouterNum = 0;
|
|
42
|
+
this.config = config;
|
|
43
|
+
}
|
|
44
|
+
analyzeAnnotation() {
|
|
45
|
+
const templateModel = {
|
|
46
|
+
pageUrl: '',
|
|
47
|
+
importPath: '',
|
|
48
|
+
componentName: '',
|
|
49
|
+
dialog: false,
|
|
50
|
+
generatorViewName: ''
|
|
51
|
+
};
|
|
52
|
+
this.config.scanDir.forEach(scanDir => {
|
|
53
|
+
const scanPath = `${this.config.modulePath}/${scanDir}`;
|
|
54
|
+
this.deepScan(scanPath, '');
|
|
55
|
+
});
|
|
56
|
+
Logger.info(`扫描到${this.scanFiles.length}个文件`, this.scanFiles);
|
|
57
|
+
this.scanFiles.forEach(filePath => {
|
|
58
|
+
if (filePath.endsWith('.ets')) {
|
|
59
|
+
const importPath = path_1.default
|
|
60
|
+
.relative(`${this.config.modulePath}/${this.config.builderDir}`, filePath)
|
|
61
|
+
.replaceAll('\\', '/')
|
|
62
|
+
.replaceAll('.ets', '');
|
|
63
|
+
const analyzer = new HMRouterAnalyzer_1.Analyzer(filePath, this.config);
|
|
64
|
+
analyzer.start();
|
|
65
|
+
for (let [key, value] of analyzer.analyzeResultMap) {
|
|
66
|
+
value.module = this.config.moduleName;
|
|
67
|
+
let pageSourceFile = path_1.default.relative(this.config.modulePath, filePath).replaceAll('\\', '/');
|
|
68
|
+
this.pushRouterInfo(value, pageSourceFile, importPath, templateModel);
|
|
69
|
+
}
|
|
70
|
+
this.HMRouterNum = 0;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
stringToHashCode(str) {
|
|
75
|
+
let hash = 0;
|
|
76
|
+
if (str.length === 0)
|
|
77
|
+
return hash;
|
|
78
|
+
for (let i = 0; i < str.length; i++) {
|
|
79
|
+
const char = str.charCodeAt(i);
|
|
80
|
+
hash = (hash << 5) - hash + char;
|
|
81
|
+
hash |= 0; // 转换为32位整数
|
|
82
|
+
}
|
|
83
|
+
return hash;
|
|
84
|
+
}
|
|
85
|
+
pushHMRouter(value, importPath, templateModel) {
|
|
86
|
+
let analyzeResult = value;
|
|
87
|
+
templateModel.pageUrl = analyzeResult.pageUrl;
|
|
88
|
+
templateModel.importPath = importPath;
|
|
89
|
+
templateModel.componentName = analyzeResult.name;
|
|
90
|
+
templateModel.dialog = !!analyzeResult.dialog;
|
|
91
|
+
templateModel.generatorViewName = 'HM' + analyzeResult.name + this.stringToHashCode(analyzeResult.pageUrl);
|
|
92
|
+
this.routerMap.push(new RouterInfo(analyzeResult.pageUrl, `${this.config.builderDir}/${templateModel.generatorViewName}.ets`, `${templateModel.componentName}Builder`, value));
|
|
93
|
+
this.generateBuilder(templateModel);
|
|
94
|
+
}
|
|
95
|
+
pushRouterInfo(value, pageSourceFile, importPath, templateModel) {
|
|
96
|
+
switch (value.annotation) {
|
|
97
|
+
case 'HMRouter':
|
|
98
|
+
this.HMRouterNum++;
|
|
99
|
+
if (this.HMRouterNum > 1) {
|
|
100
|
+
Logger.error(`errorCode: 40000004, errorMsg: 文件${pageSourceFile}中存在多个HMRouter注解`);
|
|
101
|
+
(0, Index_1.deleteGeneratorFile)(this.config);
|
|
102
|
+
throw new Error(`文件${pageSourceFile}中存在多个HMRouter注解`);
|
|
103
|
+
}
|
|
104
|
+
this.pushHMRouter(value, importPath, templateModel);
|
|
105
|
+
break;
|
|
106
|
+
case 'HMAnimator':
|
|
107
|
+
this.routerMap.push(new RouterInfo(`__animator__${value.animatorName}`, pageSourceFile, '', value));
|
|
108
|
+
break;
|
|
109
|
+
case 'HMInterceptor':
|
|
110
|
+
this.routerMap.push(new RouterInfo(`__interceptor__${value.interceptorName}`, pageSourceFile, '', value));
|
|
111
|
+
break;
|
|
112
|
+
case 'HMLifecycle':
|
|
113
|
+
this.routerMap.push(new RouterInfo(`__lifecycle__${value.lifecycleName}`, pageSourceFile, '', value));
|
|
114
|
+
break;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
generateBuilder(templateModel) {
|
|
118
|
+
const builderPath = path_1.default.resolve(__dirname, '../' + this.config.builderTpl);
|
|
119
|
+
const tpl = fs_1.default.readFileSync(builderPath, { encoding: 'utf-8' });
|
|
120
|
+
const template = handlebars_1.default.compile(tpl);
|
|
121
|
+
const output = template(templateModel);
|
|
122
|
+
const routerBuilderDir = `${this.config.modulePath}/${this.config.builderDir}`;
|
|
123
|
+
if (!fs_1.default.existsSync(routerBuilderDir)) {
|
|
124
|
+
fs_1.default.mkdirSync(routerBuilderDir);
|
|
125
|
+
}
|
|
126
|
+
fs_1.default.writeFileSync(`${routerBuilderDir}/${templateModel.generatorViewName}.ets`, output, {
|
|
127
|
+
encoding: 'utf-8'
|
|
128
|
+
});
|
|
129
|
+
Logger.info(`Builder ${templateModel.generatorViewName}.ets has been generated in ${routerBuilderDir}`);
|
|
130
|
+
}
|
|
131
|
+
generateRouterMap() {
|
|
132
|
+
let set = new Set();
|
|
133
|
+
this.routerMap.forEach(item => {
|
|
134
|
+
if (set.has(item.name)) {
|
|
135
|
+
Logger.error(`errorCode: 40000001, errorMsg: 重复的pageUrl、拦截器、生命周期、动画--${item.name}`);
|
|
136
|
+
(0, Index_1.deleteGeneratorFile)(this.config);
|
|
137
|
+
throw new Error(`路由${item.name}重复`);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
set.add(item.name);
|
|
141
|
+
}
|
|
142
|
+
});
|
|
143
|
+
let routerMap = {
|
|
144
|
+
routerMap: this.routerMap.map(item => {
|
|
145
|
+
if (item.data && item.data.annotation) {
|
|
146
|
+
delete item.data.annotation;
|
|
147
|
+
// @ts-ignore
|
|
148
|
+
item.data = { data: JSON.stringify(item.data) };
|
|
149
|
+
}
|
|
150
|
+
return item;
|
|
151
|
+
})
|
|
152
|
+
};
|
|
153
|
+
const jsonOutput = JSON.stringify(routerMap, null, 2);
|
|
154
|
+
const routerMapDir = `${this.config.modulePath}/${this.config.routerMapDir}`;
|
|
155
|
+
if (!fs_1.default.existsSync(routerMapDir)) {
|
|
156
|
+
fs_1.default.mkdirSync(routerMapDir, { recursive: true });
|
|
157
|
+
}
|
|
158
|
+
fs_1.default.writeFileSync(`${routerMapDir}/hm_router_map.json`, jsonOutput, { encoding: 'utf-8' });
|
|
159
|
+
Logger.info(`hm_router_map.json has been generated in ${routerMapDir}`);
|
|
160
|
+
return this.routerMap;
|
|
161
|
+
}
|
|
162
|
+
deepScan(scanPath, filePath) {
|
|
163
|
+
if (fs_1.default.lstatSync(`${scanPath + filePath}`).isDirectory()) {
|
|
164
|
+
const files = fs_1.default.readdirSync(`${scanPath}${filePath}`);
|
|
165
|
+
files.forEach(file => {
|
|
166
|
+
this.deepScan(`${scanPath}${filePath}/`, file);
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
this.scanFiles.push(`${scanPath}${filePath}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
exports.HMRouterHvigorPlugin = HMRouterHvigorPlugin;
|
|
175
|
+
//# sourceMappingURL=HMRouterHvigorPlugin.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2024 Huawei Device Co., Ltd.
|
|
4
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
* you may not use this file except in compliance with the License.
|
|
6
|
+
* You may obtain a copy of the License at
|
|
7
|
+
*
|
|
8
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
*
|
|
10
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
* See the License for the specific language governing permissions and
|
|
14
|
+
* limitations under the License.
|
|
15
|
+
*/
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.HMRouterPluginConfig = void 0;
|
|
18
|
+
class HMRouterPluginConfig {
|
|
19
|
+
constructor(moduleName, modulePath, param) {
|
|
20
|
+
this.moduleName = moduleName;
|
|
21
|
+
this.modulePath = modulePath;
|
|
22
|
+
this.scanDir = param.scanDir ? param.scanDir : ['src/main/ets'];
|
|
23
|
+
this.routerMapDir = param.routerMapDir ? param.routerMapDir : 'src/main/resources/base/profile';
|
|
24
|
+
this.builderDir = param.builderDir ? param.builderDir : 'src/main/ets/generated';
|
|
25
|
+
this.annotation = ['HMRouter', 'HMAnimator', 'HMInterceptor', 'HMLifecycle'];
|
|
26
|
+
this.builderTpl = param.builderTpl ? param.builderTpl : 'viewBuilder.tpl';
|
|
27
|
+
this.saveGeneratedFile = !!param.saveGeneratedFile;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.HMRouterPluginConfig = HMRouterPluginConfig;
|
|
31
|
+
//# sourceMappingURL=HMRouterPluginConfig.js.map
|