@hadss/hmrouter-plugin 1.2.3-rc.1 → 1.2.3
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 +83 -2
- package/dist/HMRouterPluginManager.d.ts +1 -1
- package/dist/HMRouterPluginManager.js +17 -28
- package/dist/Index.js +6 -3
- package/dist/framework/constants/TaskConstants.d.ts +4 -1
- package/dist/framework/constants/TaskConstants.js +4 -1
- package/dist/framework/core/PluginExecutionController.d.ts +6 -1
- package/dist/framework/core/PluginExecutionController.js +70 -11
- package/dist/framework/core/TaskManager.d.ts +16 -0
- package/dist/framework/core/TaskManager.js +282 -8
- package/dist/framework/extension/context/ExtensionContextImpl.d.ts +3 -2
- package/dist/framework/extension/context/Interface.d.ts +1 -0
- package/dist/framework/utils/ModuleTypeUtil.d.ts +1 -0
- package/dist/framework/utils/ModuleTypeUtil.js +10 -1
- package/dist/framework/utils/ObjectUtils.d.ts +1 -1
- package/dist/framework/utils/PluginStore.d.ts +1 -1
- package/dist/framework/utils/StringUtil.d.ts +1 -0
- package/dist/framework/utils/StringUtil.js +13 -0
- package/dist/framework/utils/index.d.ts +0 -1
- package/dist/framework/utils/index.js +0 -1
- package/dist/hmrouter_extension/analyzer/RouterAnalyzer.js +1 -0
- package/dist/hmrouter_extension/analyzer/interface/IConstantResolver.d.ts +2 -0
- package/dist/hmrouter_extension/analyzer/utils/ConstantResolver.d.ts +11 -0
- package/dist/hmrouter_extension/analyzer/utils/ConstantResolver.js +139 -4
- package/dist/hmrouter_extension/config/HMRouterPluginConfig.d.ts +14 -8
- package/dist/hmrouter_extension/config/HMRouterPluginConfig.js +93 -23
- package/dist/hmrouter_extension/constants/FilePathConstants.d.ts +10 -0
- package/dist/hmrouter_extension/constants/FilePathConstants.js +10 -0
- package/dist/hmrouter_extension/constants/PluginConstants.d.ts +2 -0
- package/dist/hmrouter_extension/constants/PluginConstants.js +2 -0
- package/dist/hmrouter_extension/model/HMAnnotationModel.d.ts +1 -0
- package/dist/hmrouter_extension/model/TemplateModel.d.ts +1 -1
- package/dist/hmrouter_extension/processor/CodeGenerationProcessor.d.ts +1 -9
- package/dist/hmrouter_extension/processor/CodeGenerationProcessor.js +29 -148
- package/dist/hmrouter_extension/processor/CompletionProcessor.js +5 -5
- package/dist/hmrouter_extension/processor/ConfigUpdateProcessor.d.ts +1 -0
- package/dist/hmrouter_extension/processor/ConfigUpdateProcessor.js +29 -2
- package/dist/hmrouter_extension/processor/InitializerProcessor.js +0 -10
- package/dist/hmrouter_extension/processor/ObfuscationProcessor.js +10 -4
- package/dist/hmrouter_extension/processor/ResourceProcessor.d.ts +1 -0
- package/dist/hmrouter_extension/processor/ResourceProcessor.js +31 -19
- package/dist/hmrouter_extension/processor/RouterMapBuildingProcessor.d.ts +0 -3
- package/dist/hmrouter_extension/processor/RouterMapBuildingProcessor.js +0 -81
- package/package.json +2 -3
- package/dist/framework/utils/ArkAnalyzerUtil.d.ts +0 -24
- package/dist/framework/utils/ArkAnalyzerUtil.js +0 -208
|
@@ -1,9 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.TaskManager = void 0;
|
|
7
|
+
const micromatch_1 = __importDefault(require("micromatch"));
|
|
8
|
+
const constants_1 = require("../../hmrouter_extension/constants");
|
|
9
|
+
const PluginError_1 = require("../../hmrouter_extension/error/PluginError");
|
|
4
10
|
const extension_1 = require("../extension");
|
|
5
11
|
const utils_1 = require("../utils");
|
|
6
12
|
const TaskStage_1 = require("./TaskStage");
|
|
13
|
+
const annotationConfig = {
|
|
14
|
+
[constants_1.AnnotationConstants.ANIMATOR_ANNOTATION]: {
|
|
15
|
+
prefix: constants_1.PrefixConstants.ANIMATOR_PREFIX,
|
|
16
|
+
propertyName: 'animatorName',
|
|
17
|
+
resultType: 'HMAnimatorResult',
|
|
18
|
+
},
|
|
19
|
+
[constants_1.AnnotationConstants.INTERCEPTOR_ANNOTATION]: {
|
|
20
|
+
prefix: constants_1.PrefixConstants.INTERCEPTOR_PREFIX,
|
|
21
|
+
propertyName: 'interceptorName',
|
|
22
|
+
resultType: 'HMInterceptorResult',
|
|
23
|
+
},
|
|
24
|
+
[constants_1.AnnotationConstants.LIFECYCLE_ANNOTATION]: {
|
|
25
|
+
prefix: constants_1.PrefixConstants.LIFECYCLE_PREFIX,
|
|
26
|
+
propertyName: 'lifecycleName',
|
|
27
|
+
resultType: 'HMLifecycleResult',
|
|
28
|
+
},
|
|
29
|
+
[constants_1.AnnotationConstants.SERVICE_ANNOTATION]: {
|
|
30
|
+
prefix: constants_1.PrefixConstants.SERVICE_PREFIX,
|
|
31
|
+
propertyName: 'serviceName',
|
|
32
|
+
resultType: 'HMServiceResult',
|
|
33
|
+
},
|
|
34
|
+
[constants_1.AnnotationConstants.SERVICE_PROVIDE_ANNOTATION]: {
|
|
35
|
+
prefix: constants_1.PrefixConstants.SERVICE_PROVIDE_PREFIX,
|
|
36
|
+
propertyName: 'serviceName',
|
|
37
|
+
resultType: 'HMServiceResult',
|
|
38
|
+
},
|
|
39
|
+
};
|
|
7
40
|
class TaskManager {
|
|
8
41
|
constructor(context, moduleExtensions) {
|
|
9
42
|
this.extensionManager = extension_1.ExtensionManager.getInstance();
|
|
@@ -18,23 +51,38 @@ class TaskManager {
|
|
|
18
51
|
default:
|
|
19
52
|
this.getExtensions().forEach((extension) => {
|
|
20
53
|
const methodName = stage.toString();
|
|
21
|
-
|
|
54
|
+
const extensionMethods = {
|
|
55
|
+
'afterInitialize': extension.afterInitialize?.bind(extension),
|
|
56
|
+
'afterCodeGeneration': extension.afterCodeGeneration?.bind(extension),
|
|
57
|
+
'afterRouterMapBuilding': extension.afterRouterMapBuilding?.bind(extension),
|
|
58
|
+
'afterConfigUpdate': extension.afterConfigUpdate?.bind(extension),
|
|
59
|
+
'afterObfuscationProcess': extension.afterObfuscationProcess?.bind(extension),
|
|
60
|
+
'afterResourceProcess': extension.afterResourceProcess?.bind(extension),
|
|
61
|
+
'afterCompletion': extension.afterCompletion?.bind(extension)
|
|
62
|
+
};
|
|
63
|
+
const method = extensionMethods[methodName];
|
|
64
|
+
if (method) {
|
|
65
|
+
method(this.context);
|
|
66
|
+
}
|
|
22
67
|
});
|
|
23
68
|
break;
|
|
24
69
|
}
|
|
25
70
|
}
|
|
26
71
|
executeAnnotationAnalysis() {
|
|
72
|
+
this.routerContext = this.context;
|
|
27
73
|
const extensions = this.getExtensions();
|
|
28
|
-
this.
|
|
29
|
-
this.
|
|
74
|
+
this.routerContext.scanFiles.forEach((file) => {
|
|
75
|
+
this.routerContext.setCurrentFilePath(file);
|
|
30
76
|
const sourceFile = utils_1.TsAstUtil.getSourceFile(file);
|
|
31
77
|
extensions.forEach((extension) => {
|
|
32
78
|
if (extension.afterAnnotationAnalysis) {
|
|
33
|
-
extension.afterAnnotationAnalysis(sourceFile, file, this.
|
|
79
|
+
extension.afterAnnotationAnalysis(sourceFile, file, this.routerContext);
|
|
34
80
|
}
|
|
35
81
|
});
|
|
36
82
|
});
|
|
37
|
-
this.
|
|
83
|
+
this.routerContext.setCurrentFilePath('');
|
|
84
|
+
this.generateRouterFilePath();
|
|
85
|
+
this.buildRouterMap();
|
|
38
86
|
this.validateTemplateData();
|
|
39
87
|
}
|
|
40
88
|
getExtensions() {
|
|
@@ -54,17 +102,243 @@ class TaskManager {
|
|
|
54
102
|
}
|
|
55
103
|
validateTemplateData() {
|
|
56
104
|
const componentNames = new Set();
|
|
57
|
-
this.
|
|
105
|
+
this.routerContext.getAnalyzeResults().forEach((result) => {
|
|
58
106
|
if (result.name) {
|
|
59
107
|
componentNames.add(result.name);
|
|
60
108
|
}
|
|
61
109
|
});
|
|
62
|
-
const templateComponentNames = new Set(this.
|
|
110
|
+
const templateComponentNames = new Set(this.routerContext.getTemplateDataMap().keys());
|
|
63
111
|
templateComponentNames.forEach((name) => {
|
|
64
112
|
if (!componentNames.has(name)) {
|
|
65
|
-
utils_1.Logger.warn(this.
|
|
113
|
+
utils_1.Logger.warn(this.routerContext.node.getNodeName(), `Component ${name} has template data but was not found in analyze results.`);
|
|
66
114
|
}
|
|
67
115
|
});
|
|
68
116
|
}
|
|
117
|
+
generateRouterFilePath() {
|
|
118
|
+
this.routerContext.generatedPaths = new Map();
|
|
119
|
+
this.routerContext.getAnalyzeResults().forEach((analyzeResult) => {
|
|
120
|
+
if (analyzeResult.annotation === constants_1.AnnotationConstants.ROUTER_ANNOTATION) {
|
|
121
|
+
const relativeShortPath = this.routerContext.config.getRelativeShortSourcePath(analyzeResult);
|
|
122
|
+
const generatedFilePath = this.routerContext.config
|
|
123
|
+
.getBuilderFilePath(relativeShortPath)
|
|
124
|
+
.replaceAll(constants_1.FilePathConstants.FILE_SEPARATOR, constants_1.FilePathConstants.DELIMITER);
|
|
125
|
+
this.buildRouterTemplatePath(analyzeResult);
|
|
126
|
+
const uniqueKey = `${analyzeResult.name}#${analyzeResult.pageUrl}`;
|
|
127
|
+
this.routerContext.generatedPaths.set(uniqueKey, generatedFilePath);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
buildRouterMap() {
|
|
132
|
+
this.routerContext.getAnalyzeResults().forEach((result) => {
|
|
133
|
+
if (!result.sourceFilePath) {
|
|
134
|
+
throw PluginError_1.PluginError.create(PluginError_1.ErrorCode.ERROR_PAGE_SOURCE_FILE, this.routerContext.config.moduleName, result.sourceFilePath);
|
|
135
|
+
}
|
|
136
|
+
let pageSourceFile = this.routerContext.config
|
|
137
|
+
.getRelativeSourcePath(result.sourceFilePath)
|
|
138
|
+
.replaceAll(constants_1.FilePathConstants.FILE_SEPARATOR, constants_1.FilePathConstants.DELIMITER);
|
|
139
|
+
if (result.annotation === constants_1.AnnotationConstants.ROUTER_ANNOTATION) {
|
|
140
|
+
let pageUrl = result.pageUrl;
|
|
141
|
+
let uniqueKey = `${result.name}#${pageUrl}`;
|
|
142
|
+
let builderPath = this.routerContext.generatedPaths.get(uniqueKey) || '';
|
|
143
|
+
this.routerContext.routerMap.push({
|
|
144
|
+
name: pageUrl,
|
|
145
|
+
pageSourceFile: builderPath,
|
|
146
|
+
buildFunction: result.name + 'Builder',
|
|
147
|
+
customData: result,
|
|
148
|
+
});
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
this.useAnnotationConfig(result, pageSourceFile, this.routerContext);
|
|
152
|
+
});
|
|
153
|
+
this.readExistingRouterMap(this.routerContext);
|
|
154
|
+
}
|
|
155
|
+
useAnnotationConfig(result, pageSourceFile, context) {
|
|
156
|
+
const config = annotationConfig[result.annotation];
|
|
157
|
+
let nameValue;
|
|
158
|
+
switch (result.annotation) {
|
|
159
|
+
case constants_1.AnnotationConstants.ANIMATOR_ANNOTATION:
|
|
160
|
+
nameValue = result.animatorName;
|
|
161
|
+
break;
|
|
162
|
+
case constants_1.AnnotationConstants.INTERCEPTOR_ANNOTATION:
|
|
163
|
+
nameValue = result.interceptorName;
|
|
164
|
+
break;
|
|
165
|
+
case constants_1.AnnotationConstants.LIFECYCLE_ANNOTATION:
|
|
166
|
+
nameValue = result.lifecycleName;
|
|
167
|
+
break;
|
|
168
|
+
case constants_1.AnnotationConstants.SERVICE_ANNOTATION:
|
|
169
|
+
case constants_1.AnnotationConstants.SERVICE_PROVIDE_ANNOTATION:
|
|
170
|
+
nameValue = result.serviceName;
|
|
171
|
+
break;
|
|
172
|
+
default:
|
|
173
|
+
nameValue = result.name;
|
|
174
|
+
break;
|
|
175
|
+
}
|
|
176
|
+
const fullName = config.prefix + nameValue;
|
|
177
|
+
context.routerMap.push({
|
|
178
|
+
name: fullName,
|
|
179
|
+
pageSourceFile: pageSourceFile,
|
|
180
|
+
buildFunction: '',
|
|
181
|
+
customData: result,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
buildRouterTemplatePath(result) {
|
|
185
|
+
let pageSourceFile = this.routerContext.config
|
|
186
|
+
.getRelativeSourcePath(result.sourceFilePath)
|
|
187
|
+
.replaceAll(constants_1.FilePathConstants.FILE_SEPARATOR, constants_1.FilePathConstants.DELIMITER);
|
|
188
|
+
let tempFilePath = this.matchedPath(pageSourceFile, this.routerContext.config.customPageTemplate, this.routerContext.config.getDefaultTplFilePath());
|
|
189
|
+
tempFilePath = this.determineTemplatePath(result, tempFilePath);
|
|
190
|
+
result.templatePath = tempFilePath;
|
|
191
|
+
}
|
|
192
|
+
matchedPath(filePath, customPageTemplate, defaultTplFilePath) {
|
|
193
|
+
for (const template of customPageTemplate) {
|
|
194
|
+
if (micromatch_1.default.isMatch(filePath, template.srcPath)) {
|
|
195
|
+
utils_1.Logger.debug(this.routerContext.node.getNodeName(), `${filePath} detected matching template: ${template.templatePath}`);
|
|
196
|
+
return utils_1.PluginFileUtil.pathResolve(this.routerContext.config.configDir, template.templatePath);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return defaultTplFilePath;
|
|
200
|
+
}
|
|
201
|
+
readExistingRouterMap(context) {
|
|
202
|
+
const moduleJsonOpt = context.moduleContext.getModuleJsonOpt();
|
|
203
|
+
if (!moduleJsonOpt?.module.routerMap) {
|
|
204
|
+
utils_1.Logger.info(context.node.getNodeName(), `No routerMap found in the module configuration`);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const routerMapFileName = moduleJsonOpt.module.routerMap.split(':')[1];
|
|
208
|
+
if (routerMapFileName !== 'hm_router_map') {
|
|
209
|
+
const routerMapFilePath = context.config.getModuleRouterMapFilePath(routerMapFileName);
|
|
210
|
+
const routerMapObj = utils_1.PluginFileUtil.readJson5(routerMapFilePath);
|
|
211
|
+
const systemRouterMap = routerMapObj[constants_1.RouterMapConstants.ROUTER_MAP_KEY];
|
|
212
|
+
if (systemRouterMap && systemRouterMap.length > 0) {
|
|
213
|
+
context.routerMap.unshift(...systemRouterMap);
|
|
214
|
+
utils_1.Logger.info(context.node.getNodeName(), `System routerMap added to the current routerMap`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
determineTemplatePath(analyzeResult, defaultTemplatePath) {
|
|
219
|
+
const libraryVersion = this.detectLibraryVersion();
|
|
220
|
+
utils_1.Logger.debug(this.routerContext.node.getNodeName(), `Library version: ${libraryVersion}`);
|
|
221
|
+
if (analyzeResult.useNavDst === true) {
|
|
222
|
+
return utils_1.PluginFileUtil.pathResolve(__dirname, constants_1.FilePathConstants.PARENT_DELIMITER.repeat(3), constants_1.TemplateConstants.CUSTOM_BUILDER_TEMPLATE);
|
|
223
|
+
}
|
|
224
|
+
const templateType = this.determineTemplateType(libraryVersion);
|
|
225
|
+
if (templateType !== 'latest' && libraryVersion) {
|
|
226
|
+
utils_1.Logger.warn(this.routerContext.node.getNodeName(), `[HMRouter Version Compatibility] Detected library version ${libraryVersion}, ` +
|
|
227
|
+
`which is below the minimum recommended version ${constants_1.VersionConstants.HMROUTER_MIN_VERSION_FOR_SDK_API}. ` +
|
|
228
|
+
`Automatically switched to compatible template (${templateType}). ` +
|
|
229
|
+
`Please upgrade @hadss/hmrouter to ${constants_1.VersionConstants.HMROUTER_MIN_VERSION_FOR_SDK_API} or higher for full feature support.`);
|
|
230
|
+
}
|
|
231
|
+
switch (templateType) {
|
|
232
|
+
case 'v1':
|
|
233
|
+
return utils_1.PluginFileUtil.pathResolve(__dirname, constants_1.FilePathConstants.PARENT_DELIMITER.repeat(3) + constants_1.TemplateConstants.V1_TEMPLATE);
|
|
234
|
+
case 'v1_1':
|
|
235
|
+
return utils_1.PluginFileUtil.pathResolve(__dirname, constants_1.FilePathConstants.PARENT_DELIMITER.repeat(3) + constants_1.TemplateConstants.V1_1_TEMPLATE);
|
|
236
|
+
case 'latest':
|
|
237
|
+
default:
|
|
238
|
+
return defaultTemplatePath;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
determineTemplateType(libraryVersion) {
|
|
242
|
+
if (!libraryVersion) {
|
|
243
|
+
return 'v1';
|
|
244
|
+
}
|
|
245
|
+
const currentVersion = this.parseVersion(libraryVersion);
|
|
246
|
+
const v1_1Version = this.parseVersion(constants_1.VersionConstants.HMROUTER_MIN_VERSION_FOR_NAV_HELPER);
|
|
247
|
+
const v1_2Version = this.parseVersion(constants_1.VersionConstants.HMROUTER_MIN_VERSION_FOR_SDK_API);
|
|
248
|
+
if (!currentVersion || !v1_1Version || !v1_2Version) {
|
|
249
|
+
return 'v1';
|
|
250
|
+
}
|
|
251
|
+
if (this.compareVersions(currentVersion, v1_2Version) >= 0) {
|
|
252
|
+
return 'latest';
|
|
253
|
+
}
|
|
254
|
+
if (this.compareVersions(currentVersion, v1_1Version) >= 0) {
|
|
255
|
+
return 'v1_1';
|
|
256
|
+
}
|
|
257
|
+
return 'v1';
|
|
258
|
+
}
|
|
259
|
+
detectLibraryVersion() {
|
|
260
|
+
let libraryVersion = '';
|
|
261
|
+
const possiblePaths = this.getLibraryPossiblePaths();
|
|
262
|
+
for (const packagePath of possiblePaths) {
|
|
263
|
+
libraryVersion = this.detectLibraryVersion2(packagePath);
|
|
264
|
+
if (libraryVersion) {
|
|
265
|
+
break;
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return libraryVersion;
|
|
269
|
+
}
|
|
270
|
+
detectLibraryVersion2(lockFilePath) {
|
|
271
|
+
try {
|
|
272
|
+
if (!utils_1.PluginFileUtil.exist(lockFilePath)) {
|
|
273
|
+
return '';
|
|
274
|
+
}
|
|
275
|
+
const lockFileData = utils_1.PluginFileUtil.readJson5(lockFilePath);
|
|
276
|
+
return this.extractHMRouterVersion(lockFileData.packages);
|
|
277
|
+
}
|
|
278
|
+
catch (error) {
|
|
279
|
+
return '';
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
extractHMRouterVersion(packages) {
|
|
283
|
+
if (!packages) {
|
|
284
|
+
return '';
|
|
285
|
+
}
|
|
286
|
+
const hmrouterPrefix = `${constants_1.VersionConstants.HMROUTER_ORGANIZATION}/${constants_1.VersionConstants.HMROUTER_LIB_NAME}@`;
|
|
287
|
+
for (const key of Object.keys(packages)) {
|
|
288
|
+
if (!key.startsWith(hmrouterPrefix)) {
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
const packageInfo = packages[key];
|
|
292
|
+
if (packageInfo && packageInfo[constants_1.VersionConstants.HMROUTER_VERSION_KEY]) {
|
|
293
|
+
return packageInfo[constants_1.VersionConstants.HMROUTER_VERSION_KEY] ?? '';
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return '';
|
|
297
|
+
}
|
|
298
|
+
getLibraryPossiblePaths() {
|
|
299
|
+
const paths = [];
|
|
300
|
+
paths.push(utils_1.PluginFileUtil.pathResolve(this.routerContext.config.modulePath, constants_1.FilePathConstants.OH_PACKAGE_LOCK_FILE_NAME));
|
|
301
|
+
const projectRoot = utils_1.PluginStore.getInstance().get('projectFilePath');
|
|
302
|
+
if (projectRoot && projectRoot !== this.routerContext.config.modulePath) {
|
|
303
|
+
paths.push(utils_1.PluginFileUtil.pathResolve(projectRoot, constants_1.FilePathConstants.OH_PACKAGE_LOCK_FILE_NAME));
|
|
304
|
+
}
|
|
305
|
+
return paths;
|
|
306
|
+
}
|
|
307
|
+
parseVersion(version) {
|
|
308
|
+
const match = version.match(/^(\d+)\.(\d+)\.(\d+)(-.*)?/);
|
|
309
|
+
if (!match) {
|
|
310
|
+
return null;
|
|
311
|
+
}
|
|
312
|
+
return {
|
|
313
|
+
major: parseInt(match[1], 10),
|
|
314
|
+
minor: parseInt(match[2], 10),
|
|
315
|
+
patch: parseInt(match[3], 10),
|
|
316
|
+
preRelease: match[4] || '',
|
|
317
|
+
};
|
|
318
|
+
}
|
|
319
|
+
compareVersions(v1, v2) {
|
|
320
|
+
if (v1.major !== v2.major) {
|
|
321
|
+
return v1.major < v2.major ? -1 : 1;
|
|
322
|
+
}
|
|
323
|
+
if (v1.minor !== v2.minor) {
|
|
324
|
+
return v1.minor < v2.minor ? -1 : 1;
|
|
325
|
+
}
|
|
326
|
+
if (v1.patch !== v2.patch) {
|
|
327
|
+
return v1.patch < v2.patch ? -1 : 1;
|
|
328
|
+
}
|
|
329
|
+
if (v1.preRelease && !v2.preRelease) {
|
|
330
|
+
return -1;
|
|
331
|
+
}
|
|
332
|
+
if (!v1.preRelease && v2.preRelease) {
|
|
333
|
+
return 1;
|
|
334
|
+
}
|
|
335
|
+
if (v1.preRelease < v2.preRelease) {
|
|
336
|
+
return -1;
|
|
337
|
+
}
|
|
338
|
+
if (v1.preRelease > v2.preRelease) {
|
|
339
|
+
return 1;
|
|
340
|
+
}
|
|
341
|
+
return 0;
|
|
342
|
+
}
|
|
69
343
|
}
|
|
70
344
|
exports.TaskManager = TaskManager;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { HvigorNode } from '@ohos/hvigor';
|
|
2
2
|
import { OhosModuleContext, Target } from '@ohos/hvigor-ohos-plugin/src/plugin/context/plugin-context';
|
|
3
3
|
import { BaseAnalyzeResult, ExtensionContext } from './Interface';
|
|
4
|
+
import { RouterInfo } from '../../../hmrouter_extension/model';
|
|
4
5
|
export declare class ExtensionContextImpl implements ExtensionContext {
|
|
5
6
|
node: HvigorNode;
|
|
6
7
|
moduleContext: OhosModuleContext;
|
|
@@ -30,8 +31,8 @@ export declare class ExtensionContextImpl implements ExtensionContext {
|
|
|
30
31
|
set _currentView(v: BaseAnalyzeResult[]);
|
|
31
32
|
get _currentFilePath(): string | undefined;
|
|
32
33
|
set _currentFilePath(v: string | undefined);
|
|
33
|
-
get routerMap():
|
|
34
|
-
set routerMap(v:
|
|
34
|
+
get routerMap(): RouterInfo[];
|
|
35
|
+
set routerMap(v: RouterInfo[]);
|
|
35
36
|
get generatedPaths(): Map<string, string>;
|
|
36
37
|
set generatedPaths(v: Map<string, string>);
|
|
37
38
|
}
|
|
@@ -19,4 +19,5 @@ export interface ExtensionContext extends BaseExtensionContext {
|
|
|
19
19
|
getTemplateData(componentName: string): Record<string, any> | undefined;
|
|
20
20
|
getTemplateDataMap(): Map<string, Record<string, any>>;
|
|
21
21
|
addTemplateData(componentName: string, data: Record<string, any>): void;
|
|
22
|
+
setCurrentFilePath(filePath: string): void;
|
|
22
23
|
}
|
|
@@ -2,3 +2,4 @@ import { HvigorNode } from '@ohos/hvigor';
|
|
|
2
2
|
export declare function isHapModule(node: HvigorNode): boolean;
|
|
3
3
|
export declare function isHarModule(node: HvigorNode): boolean;
|
|
4
4
|
export declare function isHspModule(node: HvigorNode): boolean;
|
|
5
|
+
export declare function isModule(pluginId: string): boolean;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isHspModule = exports.isHarModule = exports.isHapModule = void 0;
|
|
3
|
+
exports.isModule = exports.isHspModule = exports.isHarModule = exports.isHapModule = void 0;
|
|
4
4
|
const hvigor_ohos_plugin_1 = require("@ohos/hvigor-ohos-plugin");
|
|
5
5
|
function isHapModule(node) {
|
|
6
6
|
return node.getAllPluginIds().includes(hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAP_PLUGIN);
|
|
@@ -14,3 +14,12 @@ function isHspModule(node) {
|
|
|
14
14
|
return node.getAllPluginIds().includes(hvigor_ohos_plugin_1.OhosPluginId.OHOS_HSP_PLUGIN);
|
|
15
15
|
}
|
|
16
16
|
exports.isHspModule = isHspModule;
|
|
17
|
+
function isModule(pluginId) {
|
|
18
|
+
return [
|
|
19
|
+
hvigor_ohos_plugin_1.OhosPluginId.OHOS_HSP_PLUGIN,
|
|
20
|
+
hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAR_PLUGIN,
|
|
21
|
+
hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAP_PLUGIN,
|
|
22
|
+
hvigor_ohos_plugin_1.OhosPluginId.OHOS_APP_PLUGIN
|
|
23
|
+
].includes(pluginId);
|
|
24
|
+
}
|
|
25
|
+
exports.isModule = isModule;
|
|
@@ -14,5 +14,18 @@ class StringUtil {
|
|
|
14
14
|
}
|
|
15
15
|
return hash;
|
|
16
16
|
}
|
|
17
|
+
static longestCommonPrefixOpt(strs) {
|
|
18
|
+
if (!strs || strs.length === 0) {
|
|
19
|
+
return '';
|
|
20
|
+
}
|
|
21
|
+
const shortestStr = strs.reduce((shortest, curr) => curr.length < shortest.length ? curr : shortest, strs[0]);
|
|
22
|
+
for (let i = 0; i < shortestStr.length; i++) {
|
|
23
|
+
const char = shortestStr[i];
|
|
24
|
+
if (!strs.every(str => str[i] === char)) {
|
|
25
|
+
return shortestStr.substring(0, i);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return shortestStr;
|
|
29
|
+
}
|
|
17
30
|
}
|
|
18
31
|
exports.StringUtil = StringUtil;
|
|
@@ -14,6 +14,7 @@ class RouterAnalyzer extends AbstractAnnotationAnalyzer_1.AbstractAnnotationAnal
|
|
|
14
14
|
}
|
|
15
15
|
analyze(sourceFile, filePath, context) {
|
|
16
16
|
framework_1.Logger.debug('', `Start to analyze source file: ${filePath}`);
|
|
17
|
+
this.constantResolver.setContext(context);
|
|
17
18
|
sourceFile.getChildrenOfKind(ts_morph_1.SyntaxKind.MissingDeclaration).forEach((node) => {
|
|
18
19
|
const componentName = this.extractComponentNameFromNode(node);
|
|
19
20
|
node.getChildrenOfKind(ts_morph_1.SyntaxKind.Decorator).forEach((decorator) => {
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { Expression, SourceFile } from 'ts-morph';
|
|
2
|
+
import { HMRouterExtensionContext } from '../../HMRouterExtensionContext';
|
|
2
3
|
export interface IConstantResolver {
|
|
4
|
+
setContext(context: HMRouterExtensionContext): void;
|
|
3
5
|
resolveConstant(value: any, sourceFile: SourceFile, filePath: string): any;
|
|
4
6
|
resolvePropertyValue(value: Expression, sourceFile: SourceFile, filePath: string): any;
|
|
5
7
|
getVariableFilePath(variableName: string, sourceFile: SourceFile, filePath: string): string;
|
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
import { Expression, SourceFile } from 'ts-morph';
|
|
2
2
|
import { IConstantResolver } from '../interface/IConstantResolver';
|
|
3
|
+
import { HMRouterExtensionContext } from '../../HMRouterExtensionContext';
|
|
3
4
|
export declare class ConstantResolver implements IConstantResolver {
|
|
4
5
|
private importMapCache;
|
|
6
|
+
private importMapCacheFilePath;
|
|
7
|
+
private context?;
|
|
8
|
+
setContext(context: HMRouterExtensionContext): void;
|
|
5
9
|
resolveConstant(value: any, sourceFile: SourceFile, filePath: string): any;
|
|
6
10
|
resolvePropertyValue(value: Expression, sourceFile: SourceFile, filePath: string): any;
|
|
7
11
|
getVariableFilePath(variableName: string, sourceFile: SourceFile, filePath: string): string;
|
|
8
12
|
getOtherModuleVariableFilePath(moduleName: string, variableName: string): string;
|
|
13
|
+
private findExportInFile;
|
|
14
|
+
private isFileExists;
|
|
15
|
+
private findLocalDefinition;
|
|
16
|
+
private findReExportedDeclaration;
|
|
17
|
+
private findReExport;
|
|
18
|
+
private shouldCheckReExport;
|
|
19
|
+
private findNamedExport;
|
|
9
20
|
private parsePrimitiveValue;
|
|
10
21
|
private parseConstantValue;
|
|
11
22
|
}
|
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.ConstantResolver = void 0;
|
|
4
7
|
const ts_morph_1 = require("ts-morph");
|
|
@@ -6,9 +9,14 @@ const framework_1 = require("../../../framework");
|
|
|
6
9
|
const constants_1 = require("../../constants");
|
|
7
10
|
const ImportAnalyzer_1 = require("./ImportAnalyzer");
|
|
8
11
|
const PluginError_1 = require("../../error/PluginError");
|
|
12
|
+
const path_1 = __importDefault(require("path"));
|
|
9
13
|
class ConstantResolver {
|
|
10
14
|
constructor() {
|
|
11
15
|
this.importMapCache = new Map();
|
|
16
|
+
this.importMapCacheFilePath = [];
|
|
17
|
+
}
|
|
18
|
+
setContext(context) {
|
|
19
|
+
this.context = context;
|
|
12
20
|
}
|
|
13
21
|
resolveConstant(value, sourceFile, filePath) {
|
|
14
22
|
if (value.type === 'constant') {
|
|
@@ -94,14 +102,141 @@ class ConstantResolver {
|
|
|
94
102
|
return resultPath;
|
|
95
103
|
}
|
|
96
104
|
getOtherModuleVariableFilePath(moduleName, variableName) {
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (
|
|
100
|
-
|
|
105
|
+
const dependencies = this.context?.moduleContext.getOhpmDependencyInfo() || {};
|
|
106
|
+
for (const [dependencyName, dependencyInfo] of Object.entries(dependencies)) {
|
|
107
|
+
if (moduleName.indexOf(dependencyName) === 0) {
|
|
108
|
+
const cacheImport = this.importMapCacheFilePath.find((f) => f.dependencyName === dependencyName &&
|
|
109
|
+
f.importConstantName === variableName && f.importPath === moduleName);
|
|
110
|
+
if (cacheImport) {
|
|
111
|
+
return cacheImport.importFullPath;
|
|
112
|
+
}
|
|
113
|
+
const isFullPathImport = dependencyName !== moduleName;
|
|
114
|
+
let dependencyFilePath = '';
|
|
115
|
+
if (isFullPathImport) {
|
|
116
|
+
dependencyFilePath = dependencyInfo.packagePath + moduleName.replace(dependencyName, '') + constants_1.FilePathConstants.ETS_SUFFIX;
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
const dependPackageFileName = framework_1.PluginFileUtil.readJson5(framework_1.PluginFileUtil.pathResolve(dependencyInfo.packagePath, constants_1.FilePathConstants.OH_PACKAGE_FILE_NAME));
|
|
120
|
+
dependencyFilePath = framework_1.PluginFileUtil.pathResolve(dependencyInfo.packagePath, dependPackageFileName.main || 'Index.ets');
|
|
121
|
+
}
|
|
122
|
+
const path = this.findExportInFile(dependencyFilePath, variableName);
|
|
123
|
+
if (path) {
|
|
124
|
+
this.importMapCacheFilePath.push({
|
|
125
|
+
dependencyName: dependencyName,
|
|
126
|
+
importPath: moduleName,
|
|
127
|
+
importConstantName: variableName,
|
|
128
|
+
importFullPath: path
|
|
129
|
+
});
|
|
130
|
+
return path;
|
|
131
|
+
}
|
|
101
132
|
}
|
|
102
133
|
}
|
|
103
134
|
throw PluginError_1.PluginError.create(PluginError_1.ErrorCode.UNKNOWN_VARIABLE, '', variableName);
|
|
104
135
|
}
|
|
136
|
+
findExportInFile(filePath, variableName) {
|
|
137
|
+
if (!this.isFileExists(filePath)) {
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
const sourceFile = framework_1.TsAstUtil.getSourceFile(filePath);
|
|
141
|
+
const localDefinitionPath = this.findLocalDefinition(sourceFile, filePath, variableName);
|
|
142
|
+
if (localDefinitionPath) {
|
|
143
|
+
return localDefinitionPath;
|
|
144
|
+
}
|
|
145
|
+
const reExportedPath = this.findReExportedDeclaration(sourceFile, filePath, variableName);
|
|
146
|
+
if (reExportedPath) {
|
|
147
|
+
return reExportedPath;
|
|
148
|
+
}
|
|
149
|
+
const namedExportPath = this.findNamedExport(sourceFile, filePath, variableName);
|
|
150
|
+
if (namedExportPath) {
|
|
151
|
+
return namedExportPath;
|
|
152
|
+
}
|
|
153
|
+
return undefined;
|
|
154
|
+
}
|
|
155
|
+
isFileExists(filePath) {
|
|
156
|
+
if (!framework_1.PluginFileUtil.exist(filePath)) {
|
|
157
|
+
const alternativePath = filePath.replace(constants_1.FilePathConstants.ETS_SUFFIX, constants_1.FilePathConstants.D_ETS_SUFFIX);
|
|
158
|
+
return framework_1.PluginFileUtil.exist(alternativePath);
|
|
159
|
+
}
|
|
160
|
+
return true;
|
|
161
|
+
}
|
|
162
|
+
findLocalDefinition(sourceFile, filePath, variableName) {
|
|
163
|
+
const localDefinitions = [
|
|
164
|
+
...sourceFile.getVariableDeclarations(),
|
|
165
|
+
...sourceFile.getClasses(),
|
|
166
|
+
...sourceFile.getInterfaces(),
|
|
167
|
+
].filter(node => node.getName() === variableName);
|
|
168
|
+
if (localDefinitions.length > 0) {
|
|
169
|
+
framework_1.Logger.debug('', `Object "${variableName}" is defined locally in file: ${filePath}`);
|
|
170
|
+
return filePath;
|
|
171
|
+
}
|
|
172
|
+
return undefined;
|
|
173
|
+
}
|
|
174
|
+
findReExportedDeclaration(sourceFile, filePath, variableName) {
|
|
175
|
+
const exportDeclarations = sourceFile.getExportDeclarations();
|
|
176
|
+
for (const exportDecl of exportDeclarations) {
|
|
177
|
+
const moduleSpecifier = exportDecl.getModuleSpecifierValue();
|
|
178
|
+
if (!moduleSpecifier) {
|
|
179
|
+
continue;
|
|
180
|
+
}
|
|
181
|
+
if (!this.shouldCheckReExport(exportDecl, variableName)) {
|
|
182
|
+
continue;
|
|
183
|
+
}
|
|
184
|
+
const originalDefinitionPath = this.findReExport(moduleSpecifier, filePath, variableName);
|
|
185
|
+
if (originalDefinitionPath) {
|
|
186
|
+
return originalDefinitionPath;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
for (const exportDecl of exportDeclarations) {
|
|
190
|
+
const moduleSpecifier = exportDecl.getModuleSpecifierValue();
|
|
191
|
+
if (!moduleSpecifier) {
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if (!exportDecl.isNamespaceExport()) {
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
const originalDefinitionPath = this.findReExport(moduleSpecifier, filePath, variableName);
|
|
198
|
+
if (originalDefinitionPath) {
|
|
199
|
+
return originalDefinitionPath;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return undefined;
|
|
203
|
+
}
|
|
204
|
+
findReExport(moduleSpecifier, filePath, variableName) {
|
|
205
|
+
const resolvedPath = framework_1.PluginFileUtil.pathResolve(path_1.default.dirname(filePath), moduleSpecifier + constants_1.FilePathConstants.ETS_SUFFIX);
|
|
206
|
+
const originalDefinitionPath = this.findExportInFile(resolvedPath, variableName);
|
|
207
|
+
if (originalDefinitionPath) {
|
|
208
|
+
framework_1.Logger.debug('', `Object "${variableName}" is re-exported from: ${resolvedPath}`);
|
|
209
|
+
return originalDefinitionPath;
|
|
210
|
+
}
|
|
211
|
+
return undefined;
|
|
212
|
+
}
|
|
213
|
+
shouldCheckReExport(exportDecl, variableName) {
|
|
214
|
+
const namedExports = exportDecl.getNamedExports();
|
|
215
|
+
if (namedExports.some(specifier => specifier.getAliasNode()?.getText() === variableName || specifier.getName() === variableName)) {
|
|
216
|
+
return true;
|
|
217
|
+
}
|
|
218
|
+
return false;
|
|
219
|
+
}
|
|
220
|
+
findNamedExport(sourceFile, filePath, variableName) {
|
|
221
|
+
const exportSpecifiers = sourceFile.getExportedDeclarations().get(variableName);
|
|
222
|
+
if (!exportSpecifiers || exportSpecifiers.length === 0) {
|
|
223
|
+
return undefined;
|
|
224
|
+
}
|
|
225
|
+
const exportSpecifier = exportSpecifiers[0];
|
|
226
|
+
const symbol = exportSpecifier.getSymbol();
|
|
227
|
+
if (!symbol) {
|
|
228
|
+
return undefined;
|
|
229
|
+
}
|
|
230
|
+
const definitions = symbol.getDeclarations();
|
|
231
|
+
for (const definition of definitions) {
|
|
232
|
+
const definitionFilePath = definition.getSourceFile().getFilePath();
|
|
233
|
+
if (definitionFilePath !== filePath) {
|
|
234
|
+
framework_1.Logger.debug('', `Object "${variableName}" is exported from: ${definitionFilePath}`);
|
|
235
|
+
return definitionFilePath;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return undefined;
|
|
239
|
+
}
|
|
105
240
|
parsePrimitiveValue(value) {
|
|
106
241
|
let propertyValue;
|
|
107
242
|
switch (value.getKind()) {
|