@hadss/hmrouter-plugin 1.0.0-rc.5 → 1.0.0-rc.9

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.
@@ -4,18 +4,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.HMRouterHvigorPlugin = void 0;
7
- const fs_1 = __importDefault(require("fs"));
8
- const handlebars_1 = __importDefault(require("handlebars"));
9
- const hvigor_1 = require("@ohos/hvigor");
7
+ const micromatch_1 = __importDefault(require("micromatch"));
8
+ const ejs_1 = __importDefault(require("ejs"));
10
9
  const PluginModel_1 = require("./common/PluginModel");
11
- const HMRouterAnalyzer_1 = require("./HMRouterAnalyzer");
12
10
  const Logger_1 = require("./common/Logger");
13
- const Constant_1 = require("./common/Constant");
11
+ const CommonConstants_1 = __importDefault(require("./constants/CommonConstants"));
12
+ const FileUtil_1 = __importDefault(require("./utils/FileUtil"));
13
+ const StringUtil_1 = require("./utils/StringUtil");
14
+ const HMRouterAnalyzer_1 = require("./HMRouterAnalyzer");
14
15
  class HMRouterHvigorPlugin {
15
16
  constructor(config) {
16
17
  this.routerMap = [];
17
18
  this.scanFiles = [];
18
- this.HMRouterNum = 0;
19
+ this.analyzerController = new HMRouterAnalyzer_1.AnalyzerController();
19
20
  this.config = config;
20
21
  }
21
22
  analyzeAnnotation() {
@@ -25,71 +26,20 @@ class HMRouterHvigorPlugin {
25
26
  });
26
27
  Logger_1.Logger.info(`Scanned ${this.scanFiles.length} files`, this.scanFiles);
27
28
  this.scanFiles.forEach((filePath) => {
28
- if (filePath.endsWith(Constant_1.HMRouterPluginConstant.VIEW_NAME_SUFFIX)) {
29
- const analyzer = new HMRouterAnalyzer_1.Analyzer(filePath, this.config);
30
- analyzer.start();
31
- for (let analyzeResult of analyzer.analyzeResultSet) {
32
- analyzeResult.module = this.config.moduleName;
33
- let pageSourceFile = this.config.getRelativeSourcePath(filePath);
34
- this.pushRouterInfo(analyzeResult, pageSourceFile.replaceAll(Constant_1.HMRouterPluginConstant.FILE_SEPARATOR, Constant_1.HMRouterPluginConstant.DELIMITER));
35
- }
36
- this.HMRouterNum = 0;
29
+ if (filePath.endsWith(CommonConstants_1.default.VIEW_NAME_SUFFIX)) {
30
+ this.analyzerController.analyzeFile(filePath, this.config);
37
31
  }
38
32
  });
39
- }
40
- pushRouterInfo(analyzeResult, pageSourceFile) {
41
- switch (analyzeResult.annotation) {
42
- case Constant_1.HMRouterPluginConstant.ROUTER_ANNOTATION:
43
- this.HMRouterNum++;
44
- if (this.HMRouterNum > 1) {
45
- Logger_1.Logger.error(Logger_1.PluginError.ERR_REPEAT_ANNOTATION, pageSourceFile);
46
- throw new Error(`文件${pageSourceFile}中存在多个HMRouter注解`);
47
- }
48
- let [generatorFilePath, componentName] = this.generateBuilder(analyzeResult, pageSourceFile);
49
- generatorFilePath = generatorFilePath.replaceAll(Constant_1.HMRouterPluginConstant.FILE_SEPARATOR, Constant_1.HMRouterPluginConstant.DELIMITER);
50
- this.routerMap.push(new PluginModel_1.RouterInfo(analyzeResult.pageUrl, generatorFilePath, `${componentName}Builder`, analyzeResult));
51
- break;
52
- case Constant_1.HMRouterPluginConstant.ANIMATOR_ANNOTATION:
53
- let animatorName = Constant_1.HMRouterPluginConstant.ANIMATOR_PREFIX + analyzeResult.animatorName;
54
- this.routerMap.push(new PluginModel_1.RouterInfo(animatorName, pageSourceFile, '', analyzeResult));
55
- break;
56
- case Constant_1.HMRouterPluginConstant.INTERCEPTOR_ANNOTATION:
57
- let interceptorName = Constant_1.HMRouterPluginConstant.INTERCEPTOR_PREFIX + analyzeResult.interceptorName;
58
- this.routerMap.push(new PluginModel_1.RouterInfo(interceptorName, pageSourceFile, '', analyzeResult));
59
- break;
60
- case Constant_1.HMRouterPluginConstant.LIFECYCLE_ANNOTATION:
61
- let lifecycleName = Constant_1.HMRouterPluginConstant.LIFECYCLE_PREFIX + analyzeResult.lifecycleName;
62
- this.routerMap.push(new PluginModel_1.RouterInfo(lifecycleName, pageSourceFile, '', analyzeResult));
63
- break;
64
- case Constant_1.HMRouterPluginConstant.SERVICE_ANNOTATION:
65
- let serviceName = Constant_1.HMRouterPluginConstant.SERVICE_PREFIX + analyzeResult.serviceName;
66
- this.routerMap.push(new PluginModel_1.RouterInfo(serviceName, pageSourceFile, '', analyzeResult));
67
- break;
68
- }
69
- }
70
- generateBuilder(analyzeResult, pageSourceFile) {
71
- let importPath = this.config
72
- .getRelativeBuilderPath(pageSourceFile)
73
- .replaceAll(Constant_1.HMRouterPluginConstant.FILE_SEPARATOR, Constant_1.HMRouterPluginConstant.DELIMITER)
74
- .replaceAll(Constant_1.HMRouterPluginConstant.VIEW_NAME_SUFFIX, '');
75
- let generatorViewName = Constant_1.HMRouterPluginConstant.VIEW_NAME_PREFIX + analyzeResult.name + this.stringToHashCode(analyzeResult.pageUrl);
76
- const templateModel = new PluginModel_1.TemplateModel(analyzeResult.pageUrl, importPath, analyzeResult.name, !!analyzeResult.dialog, generatorViewName);
77
- const templateFilePath = this.config.getTplFilePath();
78
- const tpl = hvigor_1.FileUtil.readFileSync(templateFilePath).toString();
79
- const template = handlebars_1.default.compile(tpl);
80
- const output = template(templateModel);
81
- const generatorFilePath = this.config.getGeneratedFilePath(templateModel.generatorViewName);
82
- hvigor_1.FileUtil.ensureFileSync(generatorFilePath);
83
- hvigor_1.FileUtil.writeFileSync(generatorFilePath, output);
84
- Logger_1.Logger.info(`Builder ${templateModel.generatorViewName}.ets has been generated in ${generatorFilePath}`);
85
- return [this.config.getBuilderFilePath(templateModel.generatorViewName), templateModel.componentName];
33
+ this.analyzerController.getAnalyzeResultSet().forEach((analyzerResult) => {
34
+ this.pushRouterInfo(analyzerResult);
35
+ });
86
36
  }
87
37
  generateRouterMap() {
88
38
  let set = new Set();
89
39
  this.routerMap.forEach((item) => {
90
40
  if (set.has(item.name)) {
91
41
  Logger_1.Logger.error(Logger_1.PluginError.ERR_DUPLICATE_NAME, item.name);
92
- throw new Error(`路由${item.name}重复`);
42
+ throw new Error(`Route page name: ${item.name} is duplicated`);
93
43
  }
94
44
  else {
95
45
  set.add(item.name);
@@ -99,20 +49,79 @@ class HMRouterHvigorPlugin {
99
49
  routerMap: this.routerMap.map((item) => {
100
50
  if (item.customData && item.customData.annotation) {
101
51
  delete item.customData.annotation;
52
+ delete item.customData.pageSourceFile;
102
53
  }
103
54
  return item;
104
55
  }),
105
56
  };
106
57
  const routerMapJsonStr = JSON.stringify(routerMap, null, 2);
107
58
  const routerMapFilePath = this.config.getRouterMapDir();
108
- hvigor_1.FileUtil.ensureFileSync(routerMapFilePath);
109
- hvigor_1.FileUtil.writeFileSync(routerMapFilePath, routerMapJsonStr);
59
+ FileUtil_1.default.ensureFileSync(routerMapFilePath);
60
+ FileUtil_1.default.writeFileSync(routerMapFilePath, routerMapJsonStr);
110
61
  Logger_1.Logger.info(`hm_router_map.json has been generated in ${routerMapFilePath}`);
111
62
  }
63
+ matchedPath(filePath, customPageTemplate, defaultTplFilePath) {
64
+ for (const template of customPageTemplate) {
65
+ if (micromatch_1.default.isMatch(filePath, template.srcPath)) {
66
+ return FileUtil_1.default.pathResolve(this.config.configDir, template.templatePath);
67
+ }
68
+ }
69
+ return defaultTplFilePath;
70
+ }
71
+ pushRouterInfo(analyzeResult) {
72
+ let pageSourceFile = this.config
73
+ .getRelativeSourcePath(analyzeResult.pageSourceFile)
74
+ .replaceAll(CommonConstants_1.default.FILE_SEPARATOR, CommonConstants_1.default.DELIMITER);
75
+ switch (analyzeResult.annotation) {
76
+ case CommonConstants_1.default.ROUTER_ANNOTATION:
77
+ let generatorFilePath = this.generateBuilder(analyzeResult, pageSourceFile, this.matchedPath(pageSourceFile, this.config.customPageTemplate, this.config.getDefaultTplFilePath()));
78
+ let pageUrl = analyzeResult.pageUrl;
79
+ this.routerMap.push(new PluginModel_1.RouterInfo(pageUrl, generatorFilePath, analyzeResult.name + 'Builder', analyzeResult));
80
+ break;
81
+ case CommonConstants_1.default.ANIMATOR_ANNOTATION:
82
+ let animatorName = CommonConstants_1.default.ANIMATOR_PREFIX + analyzeResult.animatorName;
83
+ this.routerMap.push(new PluginModel_1.RouterInfo(animatorName, pageSourceFile, '', analyzeResult));
84
+ break;
85
+ case CommonConstants_1.default.INTERCEPTOR_ANNOTATION:
86
+ let interceptorName = CommonConstants_1.default.INTERCEPTOR_PREFIX + analyzeResult.interceptorName;
87
+ this.routerMap.push(new PluginModel_1.RouterInfo(interceptorName, pageSourceFile, '', analyzeResult));
88
+ break;
89
+ case CommonConstants_1.default.LIFECYCLE_ANNOTATION:
90
+ let lifecycleName = CommonConstants_1.default.LIFECYCLE_PREFIX + analyzeResult.lifecycleName;
91
+ this.routerMap.push(new PluginModel_1.RouterInfo(lifecycleName, pageSourceFile, '', analyzeResult));
92
+ break;
93
+ case CommonConstants_1.default.SERVICE_ANNOTATION:
94
+ let serviceName = CommonConstants_1.default.SERVICE_PREFIX + analyzeResult.serviceName;
95
+ this.routerMap.push(new PluginModel_1.RouterInfo(serviceName, pageSourceFile, '', analyzeResult));
96
+ break;
97
+ }
98
+ }
99
+ generateBuilder(analyzeResult, pageSourceFile, tempFilePath) {
100
+ let importPath = this.config
101
+ .getRelativeBuilderPath(pageSourceFile)
102
+ .replaceAll(CommonConstants_1.default.FILE_SEPARATOR, CommonConstants_1.default.DELIMITER)
103
+ .replaceAll(CommonConstants_1.default.VIEW_NAME_SUFFIX, '');
104
+ let generatorViewName = CommonConstants_1.default.VIEW_NAME_PREFIX +
105
+ analyzeResult.name +
106
+ StringUtil_1.StringUtil.stringToHashCode(analyzeResult.pageUrl);
107
+ const templateModel = new PluginModel_1.TemplateModel(analyzeResult.pageUrl, importPath, analyzeResult.name, !!analyzeResult.dialog, generatorViewName);
108
+ const tpl = FileUtil_1.default.readFileSync(tempFilePath).toString();
109
+ const templateStr = ejs_1.default.render(tpl, templateModel);
110
+ const generatorFilePath = this.config.getGeneratedFilePath(templateModel.generatorViewName);
111
+ FileUtil_1.default.ensureFileSync(generatorFilePath);
112
+ FileUtil_1.default.writeFileSync(generatorFilePath, templateStr);
113
+ Logger_1.Logger.info(`Builder ${templateModel.generatorViewName}.ets has been generated in ${generatorFilePath}`);
114
+ return this.config
115
+ .getBuilderFilePath(templateModel.generatorViewName)
116
+ .replaceAll(CommonConstants_1.default.FILE_SEPARATOR, CommonConstants_1.default.DELIMITER);
117
+ }
112
118
  deepScan(scanPath, filePath) {
113
- let resolvePath = hvigor_1.FileUtil.pathResolve(scanPath, filePath);
114
- if (hvigor_1.FileUtil.exist(resolvePath) && hvigor_1.FileUtil.isDictionary(resolvePath)) {
115
- const files = fs_1.default.readdirSync(resolvePath);
119
+ let resolvePath = FileUtil_1.default.pathResolve(scanPath, filePath);
120
+ if (!FileUtil_1.default.exist(resolvePath)) {
121
+ return;
122
+ }
123
+ if (FileUtil_1.default.isDictionary(resolvePath)) {
124
+ const files = FileUtil_1.default.readdirSync(resolvePath);
116
125
  files.forEach((file) => {
117
126
  this.deepScan(resolvePath, file);
118
127
  });
@@ -121,17 +130,5 @@ class HMRouterHvigorPlugin {
121
130
  this.scanFiles.push(resolvePath);
122
131
  }
123
132
  }
124
- stringToHashCode(str) {
125
- let hash = 0;
126
- if (str.length === 0) {
127
- return hash;
128
- }
129
- for (let i = 0; i < str.length; i++) {
130
- const char = str.charCodeAt(i);
131
- hash = (hash << 5) - hash + char;
132
- hash |= 0;
133
- }
134
- return hash;
135
- }
136
133
  }
137
134
  exports.HMRouterHvigorPlugin = HMRouterHvigorPlugin;
@@ -1,13 +1,16 @@
1
1
  export declare class HMRouterPluginConfig {
2
2
  moduleName: string;
3
3
  modulePath: string;
4
+ configDir: string;
4
5
  scanDir: string[];
5
6
  routerMapDir: string;
6
7
  builderDir: string;
7
8
  annotation: string[];
8
- builderTpl: string;
9
+ defaultPageTemplate: string;
10
+ customPageTemplate: CustomPageTemplateImpl[];
9
11
  saveGeneratedFile: boolean;
10
- constructor(moduleName: string, modulePath: string, param: HMRouterPluginConfigParam);
12
+ autoObfuscation: boolean;
13
+ constructor(moduleName: string, modulePath: string, configDir: string, param: HMRouterPluginConfigParam);
11
14
  getScanPath(dir: string): string;
12
15
  getRelativeSourcePath(filePath: string): string;
13
16
  getRelativeBuilderPath(filePath: string): string;
@@ -17,12 +20,19 @@ export declare class HMRouterPluginConfig {
17
20
  getRouterMapDir(): string;
18
21
  getModuleRouterMapFilePath(routerMapFileName: string): string;
19
22
  getRawFilePath(): string;
20
- getTplFilePath(): string;
23
+ getDefaultTplFilePath(): string;
24
+ getObfuscationFilePath(): string;
21
25
  }
22
26
  export interface HMRouterPluginConfigParam {
23
27
  scanDir?: string[];
24
28
  routerMapDir?: string;
25
29
  builderDir?: string;
30
+ autoObfuscation?: boolean;
26
31
  saveGeneratedFile?: boolean;
27
- builderTpl?: string;
32
+ defaultPageTemplate?: string;
33
+ customPageTemplate?: CustomPageTemplateImpl[];
34
+ }
35
+ export interface CustomPageTemplateImpl {
36
+ srcPath: string[];
37
+ templatePath: string;
28
38
  }
@@ -5,21 +5,32 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.HMRouterPluginConfig = void 0;
7
7
  const path_1 = __importDefault(require("path"));
8
- const hvigor_1 = require("@ohos/hvigor");
9
- const Constant_1 = require("./common/Constant");
8
+ const FileUtil_1 = __importDefault(require("./utils/FileUtil"));
9
+ const ConfigConstants_1 = __importDefault(require("./constants/ConfigConstants"));
10
+ const CommonConstants_1 = __importDefault(require("./constants/CommonConstants"));
10
11
  class HMRouterPluginConfig {
11
- constructor(moduleName, modulePath, param) {
12
+ constructor(moduleName, modulePath, configDir, param) {
12
13
  this.moduleName = moduleName;
13
14
  this.modulePath = modulePath;
14
- this.scanDir = param.scanDir ? [...new Set(param.scanDir)] : ['src/main/ets'];
15
- this.routerMapDir = param.routerMapDir ? param.routerMapDir : 'src/main/resources/base/profile';
16
- this.builderDir = param.builderDir ? param.builderDir : 'src/main/ets/generated';
17
- this.annotation = ['HMRouter', 'HMAnimator', 'HMInterceptor', 'HMLifecycle', 'HMService'];
18
- this.builderTpl = param.builderTpl ? param.builderTpl : 'viewBuilder.tpl';
15
+ this.configDir = configDir;
16
+ this.scanDir = param.scanDir ? [...new Set(param.scanDir)] : [ConfigConstants_1.default.DEFAULT_SCAN_DIR];
17
+ this.routerMapDir = param.routerMapDir ? param.routerMapDir : ConfigConstants_1.default.DEFAULT_ROUTER_MAP_DIR;
18
+ this.builderDir = param.builderDir ? param.builderDir : ConfigConstants_1.default.DEFAULT_BUILD_DIR;
19
+ this.annotation = [
20
+ ConfigConstants_1.default.ROUTER_ANNOTATION,
21
+ ConfigConstants_1.default.ANIMATOR_ANNOTATION,
22
+ ConfigConstants_1.default.INTERCEPTOR_ANNOTATION,
23
+ ConfigConstants_1.default.LIFECYCLE_ANNOTATION,
24
+ ConfigConstants_1.default.SERVICE_ANNOTATION
25
+ ];
26
+ this.defaultPageTemplate =
27
+ param.defaultPageTemplate ? param.defaultPageTemplate : ConfigConstants_1.default.DEFAULT_BUILD_TPL;
28
+ this.customPageTemplate = param.customPageTemplate ? param.customPageTemplate : [];
19
29
  this.saveGeneratedFile = !!param.saveGeneratedFile;
30
+ this.autoObfuscation = !!param.autoObfuscation;
20
31
  }
21
32
  getScanPath(dir) {
22
- return hvigor_1.FileUtil.pathResolve(this.modulePath, dir);
33
+ return FileUtil_1.default.pathResolve(this.modulePath, dir);
23
34
  }
24
35
  getRelativeSourcePath(filePath) {
25
36
  return path_1.default.relative(this.modulePath, filePath);
@@ -28,25 +39,32 @@ class HMRouterPluginConfig {
28
39
  return path_1.default.relative(this.builderDir, filePath);
29
40
  }
30
41
  getGeneratedFilePath(generatorViewName) {
31
- return hvigor_1.FileUtil.pathResolve(this.modulePath, this.builderDir, generatorViewName + Constant_1.HMRouterPluginConstant.VIEW_NAME_SUFFIX);
42
+ return FileUtil_1.default.pathResolve(this.modulePath, this.builderDir, generatorViewName + CommonConstants_1.default.VIEW_NAME_SUFFIX);
32
43
  }
33
44
  getBuilderDir() {
34
- return hvigor_1.FileUtil.pathResolve(this.modulePath, this.builderDir);
45
+ return FileUtil_1.default.pathResolve(this.modulePath, this.builderDir);
35
46
  }
36
47
  getBuilderFilePath(generatorViewName) {
37
- return path_1.default.join(this.builderDir, generatorViewName + Constant_1.HMRouterPluginConstant.VIEW_NAME_SUFFIX);
48
+ return path_1.default.join(this.builderDir, generatorViewName + CommonConstants_1.default.VIEW_NAME_SUFFIX);
38
49
  }
39
50
  getRouterMapDir() {
40
- return hvigor_1.FileUtil.pathResolve(this.modulePath, this.routerMapDir, Constant_1.HMRouterPluginConstant.ROUTER_MAP_NAME);
51
+ return FileUtil_1.default.pathResolve(this.modulePath, this.routerMapDir, CommonConstants_1.default.ROUTER_MAP_NAME);
41
52
  }
42
53
  getModuleRouterMapFilePath(routerMapFileName) {
43
- return hvigor_1.FileUtil.pathResolve(this.modulePath, this.routerMapDir, routerMapFileName + Constant_1.HMRouterPluginConstant.JSON_SUFFIX);
54
+ return FileUtil_1.default.pathResolve(this.modulePath, this.routerMapDir, routerMapFileName + CommonConstants_1.default.JSON_SUFFIX);
44
55
  }
45
56
  getRawFilePath() {
46
- return hvigor_1.FileUtil.pathResolve(this.modulePath, Constant_1.HMRouterPluginConstant.RAWFILE_DIR);
57
+ return FileUtil_1.default.pathResolve(this.modulePath, CommonConstants_1.default.RAWFILE_DIR);
47
58
  }
48
- getTplFilePath() {
49
- return hvigor_1.FileUtil.pathResolve(__dirname, Constant_1.HMRouterPluginConstant.PARENT_DELIMITER + this.builderTpl);
59
+ getDefaultTplFilePath() {
60
+ let templateFilePath = FileUtil_1.default.pathResolve(this.configDir, this.defaultPageTemplate);
61
+ if (FileUtil_1.default.exist(templateFilePath)) {
62
+ return templateFilePath;
63
+ }
64
+ return FileUtil_1.default.pathResolve(__dirname, CommonConstants_1.default.PARENT_DELIMITER + this.defaultPageTemplate);
65
+ }
66
+ getObfuscationFilePath() {
67
+ return FileUtil_1.default.pathResolve(this.modulePath, CommonConstants_1.default.OBFUSCATION_FILE_NAME);
50
68
  }
51
69
  }
52
70
  exports.HMRouterPluginConfig = HMRouterPluginConfig;
@@ -2,16 +2,21 @@ import { HvigorNode } from '@ohos/hvigor';
2
2
  import { OhosHapContext, OhosHarContext, OhosHspContext } from '@ohos/hvigor-ohos-plugin';
3
3
  import { HMRouterPluginConfig } from './HMRouterPluginConfig';
4
4
  export declare class HMRouterPluginHandle {
5
+ readonly config: HMRouterPluginConfig;
5
6
  private readonly node;
6
7
  private readonly moduleContext;
7
- readonly config: HMRouterPluginConfig;
8
+ private readonly appContext;
8
9
  private readonly plugin;
9
10
  constructor(node: HvigorNode, moduleContext: OhosHapContext | OhosHarContext | OhosHspContext);
10
11
  start(): void;
12
+ private generateObfuscationFileTask;
13
+ private isEnableObfuscation;
11
14
  private copyRouterMapToRawFileTask;
15
+ private writeHspModuleName;
12
16
  private taskExec;
13
17
  private updateModuleJsonOpt;
14
18
  private updateBuildProfileOpt;
15
19
  private pushRouterInfo;
16
20
  private readConfig;
21
+ private ensureNestedObject;
17
22
  }
@@ -1,16 +1,24 @@
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.HMRouterPluginHandle = void 0;
4
7
  const hvigor_1 = require("@ohos/hvigor");
8
+ const hvigor_ohos_plugin_1 = require("@ohos/hvigor-ohos-plugin");
5
9
  const HMRouterPluginConfig_1 = require("./HMRouterPluginConfig");
6
10
  const HMRouterHvigorPlugin_1 = require("./HMRouterHvigorPlugin");
7
11
  const Logger_1 = require("./common/Logger");
8
- const Constant_1 = require("./common/Constant");
12
+ const CommonConstants_1 = __importDefault(require("./constants/CommonConstants"));
13
+ const TaskConstants_1 = __importDefault(require("./constants/TaskConstants"));
14
+ const FileUtil_1 = __importDefault(require("./utils/FileUtil"));
15
+ const ObfuscationUtil_1 = __importDefault(require("./utils/ObfuscationUtil"));
9
16
  class HMRouterPluginHandle {
10
17
  constructor(node, moduleContext) {
11
18
  this.node = node;
12
19
  this.moduleContext = moduleContext;
13
20
  this.config = this.readConfig();
21
+ this.appContext = this.node.getParentNode()?.getContext(hvigor_ohos_plugin_1.OhosPluginId.OHOS_APP_PLUGIN);
14
22
  this.plugin = new HMRouterHvigorPlugin_1.HMRouterHvigorPlugin(this.config);
15
23
  }
16
24
  start() {
@@ -18,28 +26,104 @@ class HMRouterPluginHandle {
18
26
  this.moduleContext.targets((target) => {
19
27
  const targetName = target.getTargetName();
20
28
  this.node.registerTask({
21
- name: `${targetName}@HMRouterPluginTask`,
29
+ name: targetName + TaskConstants_1.default.PLUGIN_TASK,
22
30
  run: () => {
23
31
  this.taskExec();
24
32
  },
25
- dependencies: [`${targetName}@PreBuild`],
26
- postDependencies: [`${targetName}@MergeProfile`],
33
+ dependencies: [targetName + TaskConstants_1.default.PRE_BUILD],
34
+ postDependencies: [targetName + TaskConstants_1.default.MERGE_PROFILE]
27
35
  });
28
36
  this.node.registerTask({
29
- name: `${targetName}@CopyRouterMapToRawFileTask`,
37
+ name: targetName + TaskConstants_1.default.COPY_ROUTER_MAP_TASK,
30
38
  run: () => {
31
39
  this.copyRouterMapToRawFileTask(target.getBuildTargetOutputPath(), targetName);
40
+ this.writeHspModuleName();
32
41
  },
33
- dependencies: [`${targetName}@ProcessRouterMap`],
34
- postDependencies: [`${targetName}@ProcessResource`],
42
+ dependencies: [targetName + TaskConstants_1.default.PROCESS_ROUTER_MAP],
43
+ postDependencies: [targetName + TaskConstants_1.default.PROCESS_RESOURCE]
44
+ });
45
+ this.node.registerTask({
46
+ name: targetName + TaskConstants_1.default.GENERATE_OBFUSCATION_TASK,
47
+ run: () => {
48
+ this.generateObfuscationFileTask(this.moduleContext.getBuildProfileOpt());
49
+ },
50
+ dependencies: [targetName + TaskConstants_1.default.PLUGIN_TASK],
51
+ postDependencies: [targetName + TaskConstants_1.default.MERGE_PROFILE]
35
52
  });
36
53
  });
37
54
  }
55
+ generateObfuscationFileTask(buildProfileOpt) {
56
+ if (!this.isEnableObfuscation(buildProfileOpt)) {
57
+ Logger_1.Logger.info('This compilation does not turn on code obfuscation, skip hmrouter_obfuscation_rules.txt file generation');
58
+ return;
59
+ }
60
+ let obfuscationFilePath = FileUtil_1.default.pathResolve(this.config.modulePath, CommonConstants_1.default.OBFUSCATION_FILE_NAME);
61
+ FileUtil_1.default.ensureFileSync(obfuscationFilePath);
62
+ FileUtil_1.default.writeFileSync(obfuscationFilePath, ObfuscationUtil_1.default.buildObfuscatedStrings(this.plugin.routerMap));
63
+ Logger_1.Logger.info('Generate obfuscation rule file successfully, filePath:', obfuscationFilePath);
64
+ }
65
+ isEnableObfuscation(buildProfileOpt) {
66
+ let currentBuildMode = this.appContext.getBuildMode();
67
+ let buildOption = buildProfileOpt.buildOptionSet?.find((item) => {
68
+ return item.name == currentBuildMode;
69
+ });
70
+ if (buildOption) {
71
+ let ruleOptions = this.ensureNestedObject(buildOption, ['arkOptions', 'obfuscation', 'ruleOptions']);
72
+ if (this.config.autoObfuscation && ruleOptions.enable) {
73
+ let files = this.ensureNestedObject(buildOption, ['arkOptions', 'obfuscation', 'ruleOptions', 'files']);
74
+ let obfuscationFilePath = CommonConstants_1.default.CURRENT_DELIMITER +
75
+ CommonConstants_1.default.OBFUSCATION_FILE_NAME;
76
+ if (typeof files === 'string') {
77
+ ruleOptions.files = [files, obfuscationFilePath];
78
+ }
79
+ else {
80
+ files.push(obfuscationFilePath);
81
+ }
82
+ this.moduleContext.setBuildProfileOpt(buildProfileOpt);
83
+ }
84
+ return ruleOptions.enable;
85
+ }
86
+ else {
87
+ return false;
88
+ }
89
+ }
38
90
  copyRouterMapToRawFileTask(buildOutputPath, targetName) {
39
- let routerMapFilePath = hvigor_1.FileUtil.pathResolve(buildOutputPath, Constant_1.HMRouterPluginConstant.TEMP_ROUTER_MAP_PATH, targetName, Constant_1.HMRouterPluginConstant.ROUTER_MAP_NAME);
91
+ let routerMapFilePath = FileUtil_1.default.pathResolve(buildOutputPath, CommonConstants_1.default.TEMP_ROUTER_MAP_PATH, targetName, CommonConstants_1.default.ROUTER_MAP_NAME);
40
92
  let rawFilePath = this.config.getRawFilePath();
41
- hvigor_1.FileUtil.ensureFileSync(rawFilePath);
42
- hvigor_1.FileUtil.copyFileSync(routerMapFilePath, rawFilePath);
93
+ FileUtil_1.default.ensureFileSync(rawFilePath);
94
+ FileUtil_1.default.copyFileSync(routerMapFilePath, rawFilePath);
95
+ }
96
+ writeHspModuleName() {
97
+ if (this.node.getAllPluginIds().includes(hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAP_PLUGIN)) {
98
+ let rawFilePath = this.config.getRawFilePath();
99
+ let rawFileRouterMap = JSON.parse(FileUtil_1.default.readFileSync(rawFilePath).toString());
100
+ rawFileRouterMap.hspModuleNames = [];
101
+ hvigor_1.hvigor.getAllNodes().forEach((node) => {
102
+ let pluginIds = node.getAllPluginIds();
103
+ if (pluginIds.includes(hvigor_ohos_plugin_1.OhosPluginId.OHOS_HSP_PLUGIN)) {
104
+ let packageJson = FileUtil_1.default.readJson5(FileUtil_1.default.pathResolve(node.getNodePath(), 'oh-package.json5'));
105
+ rawFileRouterMap.hspModuleNames.push(packageJson.name);
106
+ }
107
+ try {
108
+ pluginIds.forEach((id) => {
109
+ let context = node.getContext(id);
110
+ let signedHspObj = context.getOhpmRemoteHspDependencyInfo(true);
111
+ let remoteHspObj = context.getOhpmRemoteHspDependencyInfo(false);
112
+ for (const key in signedHspObj) {
113
+ rawFileRouterMap.hspModuleNames.push(signedHspObj[key].name);
114
+ }
115
+ for (const key in remoteHspObj) {
116
+ rawFileRouterMap.hspModuleNames.push(remoteHspObj[key].name);
117
+ }
118
+ });
119
+ }
120
+ catch (error) {
121
+ Logger_1.Logger.warn('Your DevEco Studio version less than 5.0.3.800, may cause remote hsp dependencies get failed');
122
+ }
123
+ });
124
+ rawFileRouterMap.hspModuleNames = [...new Set(rawFileRouterMap.hspModuleNames)];
125
+ FileUtil_1.default.writeFileSync(rawFilePath, JSON.stringify(rawFileRouterMap));
126
+ }
43
127
  }
44
128
  taskExec() {
45
129
  let startTime = Date.now();
@@ -56,25 +140,17 @@ class HMRouterPluginHandle {
56
140
  if (moduleJsonOpt.module.routerMap) {
57
141
  let routerMapFileName = moduleJsonOpt.module.routerMap.split(':')[1];
58
142
  let routerMapFilePath = this.config.getModuleRouterMapFilePath(routerMapFileName);
59
- let routerMapObj = hvigor_1.FileUtil.readJson5(routerMapFilePath);
60
- this.plugin.routerMap.unshift(...routerMapObj['routerMap']);
143
+ let routerMapObj = FileUtil_1.default.readJson5(routerMapFilePath);
144
+ this.plugin.routerMap.unshift(...routerMapObj[CommonConstants_1.default.ROUTER_MAP_KEY]);
61
145
  }
62
146
  this.plugin.generateRouterMap();
63
- moduleJsonOpt.module.routerMap = Constant_1.HMRouterPluginConstant.MODULE_ROUTER_MAP_NAME;
147
+ moduleJsonOpt.module.routerMap = CommonConstants_1.default.MODULE_ROUTER_MAP_NAME;
64
148
  this.moduleContext.setModuleJsonOpt(moduleJsonOpt);
65
149
  }
66
150
  updateBuildProfileOpt() {
67
151
  const buildProfileOpt = this.moduleContext.getBuildProfileOpt();
68
- if (!buildProfileOpt.buildOption) {
69
- buildProfileOpt.buildOption = {};
70
- }
71
- if (!buildProfileOpt.buildOption.arkOptions) {
72
- buildProfileOpt.buildOption.arkOptions = {};
73
- }
74
- if (!buildProfileOpt.buildOption.arkOptions.runtimeOnly) {
75
- buildProfileOpt.buildOption.arkOptions.runtimeOnly = {};
76
- }
77
- if (!buildProfileOpt.buildOption.arkOptions.runtimeOnly.sources) {
152
+ let sources = this.ensureNestedObject(buildProfileOpt, ['buildOption', 'arkOptions', 'runtimeOnly', 'sources']);
153
+ if (!Array.isArray(sources)) {
78
154
  buildProfileOpt.buildOption.arkOptions.runtimeOnly.sources = [];
79
155
  }
80
156
  this.pushRouterInfo(buildProfileOpt, this.plugin.routerMap);
@@ -84,26 +160,37 @@ class HMRouterPluginHandle {
84
160
  const sources = buildProfileOpt.buildOption.arkOptions.runtimeOnly.sources;
85
161
  routerMap.forEach((item) => {
86
162
  const name = item.name;
87
- if (name.includes(Constant_1.HMRouterPluginConstant.LIFECYCLE_PREFIX) ||
88
- name.includes(Constant_1.HMRouterPluginConstant.INTERCEPTOR_PREFIX) ||
89
- name.includes(Constant_1.HMRouterPluginConstant.ANIMATOR_PREFIX) ||
90
- name.includes(Constant_1.HMRouterPluginConstant.SERVICE_PREFIX)) {
91
- sources.push('./' + item.pageSourceFile);
163
+ if (name.includes(CommonConstants_1.default.LIFECYCLE_PREFIX) ||
164
+ name.includes(CommonConstants_1.default.INTERCEPTOR_PREFIX) ||
165
+ name.includes(CommonConstants_1.default.ANIMATOR_PREFIX) ||
166
+ name.includes(CommonConstants_1.default.SERVICE_PREFIX)) {
167
+ sources.push(CommonConstants_1.default.CURRENT_DELIMITER + item.pageSourceFile);
92
168
  }
93
169
  });
94
170
  }
95
171
  readConfig() {
96
172
  let levels = 0;
97
173
  let configParam = {};
174
+ let configFilePath = '';
98
175
  while (levels < 4) {
99
- let configFilePath = hvigor_1.FileUtil.pathResolve(this.node.getNodePath(), Constant_1.HMRouterPluginConstant.PARENT_DELIMITER.repeat(levels) + Constant_1.HMRouterPluginConstant.CONFIG_FILE_NAME);
100
- if (hvigor_1.FileUtil.exist(configFilePath)) {
101
- configParam = hvigor_1.FileUtil.readJson5(configFilePath);
176
+ configFilePath = FileUtil_1.default.pathResolve(this.node.getNodePath(), CommonConstants_1.default.PARENT_DELIMITER.repeat(levels) + CommonConstants_1.default.CONFIG_FILE_NAME);
177
+ if (FileUtil_1.default.exist(configFilePath)) {
178
+ configParam = FileUtil_1.default.readJson5(configFilePath);
102
179
  break;
103
180
  }
104
181
  levels++;
105
182
  }
106
- return new HMRouterPluginConfig_1.HMRouterPluginConfig(this.node.getNodeName(), this.node.getNodePath(), configParam);
183
+ let configDir = configFilePath.split(CommonConstants_1.default.FILE_SEPARATOR).slice(0, -1)
184
+ .join(CommonConstants_1.default.FILE_SEPARATOR);
185
+ return new HMRouterPluginConfig_1.HMRouterPluginConfig(this.node.getNodeName(), this.node.getNodePath(), configDir, configParam);
186
+ }
187
+ ensureNestedObject(obj, path) {
188
+ return path.reduce((acc, key) => {
189
+ if (!acc[key]) {
190
+ acc[key] = {};
191
+ }
192
+ return acc[key];
193
+ }, obj);
107
194
  }
108
195
  }
109
196
  exports.HMRouterPluginHandle = HMRouterPluginHandle;