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

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,17 @@ 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
7
  const handlebars_1 = __importDefault(require("handlebars"));
9
- const hvigor_1 = require("@ohos/hvigor");
10
8
  const PluginModel_1 = require("./common/PluginModel");
11
- const HMRouterAnalyzer_1 = require("./HMRouterAnalyzer");
12
9
  const Logger_1 = require("./common/Logger");
13
- const Constant_1 = require("./common/Constant");
10
+ const HMRouterAnalyzer_1 = require("./HMRouterAnalyzer");
11
+ const CommonConstants_1 = __importDefault(require("./constants/CommonConstants"));
12
+ const FileUtil_1 = __importDefault(require("./utils/FileUtil"));
14
13
  class HMRouterHvigorPlugin {
15
14
  constructor(config) {
16
15
  this.routerMap = [];
17
16
  this.scanFiles = [];
18
- this.HMRouterNum = 0;
17
+ this.analyzerController = new HMRouterAnalyzer_1.AnalyzerController();
19
18
  this.config = config;
20
19
  }
21
20
  analyzeAnnotation() {
@@ -25,44 +24,63 @@ class HMRouterHvigorPlugin {
25
24
  });
26
25
  Logger_1.Logger.info(`Scanned ${this.scanFiles.length} files`, this.scanFiles);
27
26
  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;
27
+ if (filePath.endsWith(CommonConstants_1.default.VIEW_NAME_SUFFIX)) {
28
+ this.analyzerController.analyzeFile(filePath, this.config);
37
29
  }
38
30
  });
31
+ this.analyzerController.getAnalyzeResultSet().forEach((analyzerResult) => {
32
+ this.pushRouterInfo(analyzerResult);
33
+ });
39
34
  }
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注解`);
35
+ generateRouterMap() {
36
+ let set = new Set();
37
+ this.routerMap.forEach((item) => {
38
+ if (set.has(item.name)) {
39
+ Logger_1.Logger.error(Logger_1.PluginError.ERR_DUPLICATE_NAME, item.name);
40
+ throw new Error(`路由${item.name}重复`);
41
+ }
42
+ else {
43
+ set.add(item.name);
44
+ }
45
+ });
46
+ let routerMap = {
47
+ routerMap: this.routerMap.map((item) => {
48
+ if (item.customData && item.customData.annotation) {
49
+ delete item.customData.annotation;
50
+ delete item.customData.pageSourceFile;
47
51
  }
52
+ return item;
53
+ })
54
+ };
55
+ const routerMapJsonStr = JSON.stringify(routerMap, null, 2);
56
+ const routerMapFilePath = this.config.getRouterMapDir();
57
+ FileUtil_1.default.ensureFileSync(routerMapFilePath);
58
+ FileUtil_1.default.writeFileSync(routerMapFilePath, routerMapJsonStr);
59
+ Logger_1.Logger.info(`hm_router_map.json has been generated in ${routerMapFilePath}`);
60
+ }
61
+ pushRouterInfo(analyzeResult) {
62
+ let pageSourceFile = this.config.getRelativeSourcePath(analyzeResult.pageSourceFile)
63
+ .replaceAll(CommonConstants_1.default.FILE_SEPARATOR, CommonConstants_1.default.DELIMITER);
64
+ switch (analyzeResult.annotation) {
65
+ case CommonConstants_1.default.ROUTER_ANNOTATION:
48
66
  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));
67
+ generatorFilePath = generatorFilePath.replaceAll(CommonConstants_1.default.FILE_SEPARATOR, CommonConstants_1.default.DELIMITER);
68
+ this.routerMap.push(new PluginModel_1.RouterInfo(analyzeResult.pageUrl, generatorFilePath, componentName + 'Builder', analyzeResult));
51
69
  break;
52
- case Constant_1.HMRouterPluginConstant.ANIMATOR_ANNOTATION:
53
- let animatorName = Constant_1.HMRouterPluginConstant.ANIMATOR_PREFIX + analyzeResult.animatorName;
70
+ case CommonConstants_1.default.ANIMATOR_ANNOTATION:
71
+ let animatorName = CommonConstants_1.default.ANIMATOR_PREFIX + analyzeResult.animatorName;
54
72
  this.routerMap.push(new PluginModel_1.RouterInfo(animatorName, pageSourceFile, '', analyzeResult));
55
73
  break;
56
- case Constant_1.HMRouterPluginConstant.INTERCEPTOR_ANNOTATION:
57
- let interceptorName = Constant_1.HMRouterPluginConstant.INTERCEPTOR_PREFIX + analyzeResult.interceptorName;
74
+ case CommonConstants_1.default.INTERCEPTOR_ANNOTATION:
75
+ let interceptorName = CommonConstants_1.default.INTERCEPTOR_PREFIX + analyzeResult.interceptorName;
58
76
  this.routerMap.push(new PluginModel_1.RouterInfo(interceptorName, pageSourceFile, '', analyzeResult));
59
77
  break;
60
- case Constant_1.HMRouterPluginConstant.LIFECYCLE_ANNOTATION:
61
- let lifecycleName = Constant_1.HMRouterPluginConstant.LIFECYCLE_PREFIX + analyzeResult.lifecycleName;
78
+ case CommonConstants_1.default.LIFECYCLE_ANNOTATION:
79
+ let lifecycleName = CommonConstants_1.default.LIFECYCLE_PREFIX + analyzeResult.lifecycleName;
62
80
  this.routerMap.push(new PluginModel_1.RouterInfo(lifecycleName, pageSourceFile, '', analyzeResult));
63
81
  break;
64
- case Constant_1.HMRouterPluginConstant.SERVICE_ANNOTATION:
65
- let serviceName = Constant_1.HMRouterPluginConstant.SERVICE_PREFIX + analyzeResult.serviceName;
82
+ case CommonConstants_1.default.SERVICE_ANNOTATION:
83
+ let serviceName = CommonConstants_1.default.SERVICE_PREFIX + analyzeResult.serviceName;
66
84
  this.routerMap.push(new PluginModel_1.RouterInfo(serviceName, pageSourceFile, '', analyzeResult));
67
85
  break;
68
86
  }
@@ -70,49 +88,24 @@ class HMRouterHvigorPlugin {
70
88
  generateBuilder(analyzeResult, pageSourceFile) {
71
89
  let importPath = this.config
72
90
  .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);
91
+ .replaceAll(CommonConstants_1.default.FILE_SEPARATOR, CommonConstants_1.default.DELIMITER)
92
+ .replaceAll(CommonConstants_1.default.VIEW_NAME_SUFFIX, '');
93
+ let generatorViewName = CommonConstants_1.default.VIEW_NAME_PREFIX + analyzeResult.name + this.stringToHashCode(analyzeResult.pageUrl);
76
94
  const templateModel = new PluginModel_1.TemplateModel(analyzeResult.pageUrl, importPath, analyzeResult.name, !!analyzeResult.dialog, generatorViewName);
77
95
  const templateFilePath = this.config.getTplFilePath();
78
- const tpl = hvigor_1.FileUtil.readFileSync(templateFilePath).toString();
96
+ const tpl = FileUtil_1.default.readFileSync(templateFilePath).toString();
79
97
  const template = handlebars_1.default.compile(tpl);
80
98
  const output = template(templateModel);
81
99
  const generatorFilePath = this.config.getGeneratedFilePath(templateModel.generatorViewName);
82
- hvigor_1.FileUtil.ensureFileSync(generatorFilePath);
83
- hvigor_1.FileUtil.writeFileSync(generatorFilePath, output);
100
+ FileUtil_1.default.ensureFileSync(generatorFilePath);
101
+ FileUtil_1.default.writeFileSync(generatorFilePath, output);
84
102
  Logger_1.Logger.info(`Builder ${templateModel.generatorViewName}.ets has been generated in ${generatorFilePath}`);
85
103
  return [this.config.getBuilderFilePath(templateModel.generatorViewName), templateModel.componentName];
86
104
  }
87
- generateRouterMap() {
88
- let set = new Set();
89
- this.routerMap.forEach((item) => {
90
- if (set.has(item.name)) {
91
- Logger_1.Logger.error(Logger_1.PluginError.ERR_DUPLICATE_NAME, item.name);
92
- throw new Error(`路由${item.name}重复`);
93
- }
94
- else {
95
- set.add(item.name);
96
- }
97
- });
98
- let routerMap = {
99
- routerMap: this.routerMap.map((item) => {
100
- if (item.customData && item.customData.annotation) {
101
- delete item.customData.annotation;
102
- }
103
- return item;
104
- }),
105
- };
106
- const routerMapJsonStr = JSON.stringify(routerMap, null, 2);
107
- const routerMapFilePath = this.config.getRouterMapDir();
108
- hvigor_1.FileUtil.ensureFileSync(routerMapFilePath);
109
- hvigor_1.FileUtil.writeFileSync(routerMapFilePath, routerMapJsonStr);
110
- Logger_1.Logger.info(`hm_router_map.json has been generated in ${routerMapFilePath}`);
111
- }
112
105
  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);
106
+ let resolvePath = FileUtil_1.default.pathResolve(scanPath, filePath);
107
+ if (FileUtil_1.default.exist(resolvePath) && FileUtil_1.default.isDictionary(resolvePath)) {
108
+ const files = FileUtil_1.default.readdirSync(resolvePath);
116
109
  files.forEach((file) => {
117
110
  this.deepScan(resolvePath, file);
118
111
  });
@@ -7,6 +7,7 @@ export declare class HMRouterPluginConfig {
7
7
  annotation: string[];
8
8
  builderTpl: string;
9
9
  saveGeneratedFile: boolean;
10
+ autoObfuscation: boolean;
10
11
  constructor(moduleName: string, modulePath: string, param: HMRouterPluginConfigParam);
11
12
  getScanPath(dir: string): string;
12
13
  getRelativeSourcePath(filePath: string): string;
@@ -18,11 +19,13 @@ export declare class HMRouterPluginConfig {
18
19
  getModuleRouterMapFilePath(routerMapFileName: string): string;
19
20
  getRawFilePath(): string;
20
21
  getTplFilePath(): string;
22
+ getConfusionFilePath(): string;
21
23
  }
22
24
  export interface HMRouterPluginConfigParam {
23
25
  scanDir?: string[];
24
26
  routerMapDir?: string;
25
27
  builderDir?: string;
28
+ autoObfuscation?: boolean;
26
29
  saveGeneratedFile?: boolean;
27
30
  builderTpl?: string;
28
31
  }
@@ -5,21 +5,29 @@ 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 CommonConstants_1 = __importDefault(require("./constants/CommonConstants"));
10
+ const ConfigConstants_1 = __importDefault(require("./constants/ConfigConstants"));
10
11
  class HMRouterPluginConfig {
11
12
  constructor(moduleName, modulePath, 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.scanDir = param.scanDir ? [...new Set(param.scanDir)] : [ConfigConstants_1.default.DEFAULT_SCAN_DIR];
16
+ this.routerMapDir = param.routerMapDir ? param.routerMapDir : ConfigConstants_1.default.DEFAULT_ROUTER_MAP_DIR;
17
+ this.builderDir = param.builderDir ? param.builderDir : ConfigConstants_1.default.DEFAULT_BUILD_DIR;
18
+ this.annotation = [
19
+ ConfigConstants_1.default.ROUTER_ANNOTATION,
20
+ ConfigConstants_1.default.ANIMATOR_ANNOTATION,
21
+ ConfigConstants_1.default.INTERCEPTOR_ANNOTATION,
22
+ ConfigConstants_1.default.LIFECYCLE_ANNOTATION,
23
+ ConfigConstants_1.default.SERVICE_ANNOTATION
24
+ ];
25
+ this.builderTpl = param.builderTpl ? param.builderTpl : ConfigConstants_1.default.DEFAULT_BUILD_TPL;
19
26
  this.saveGeneratedFile = !!param.saveGeneratedFile;
27
+ this.autoObfuscation = !!param.autoObfuscation;
20
28
  }
21
29
  getScanPath(dir) {
22
- return hvigor_1.FileUtil.pathResolve(this.modulePath, dir);
30
+ return FileUtil_1.default.pathResolve(this.modulePath, dir);
23
31
  }
24
32
  getRelativeSourcePath(filePath) {
25
33
  return path_1.default.relative(this.modulePath, filePath);
@@ -28,25 +36,28 @@ class HMRouterPluginConfig {
28
36
  return path_1.default.relative(this.builderDir, filePath);
29
37
  }
30
38
  getGeneratedFilePath(generatorViewName) {
31
- return hvigor_1.FileUtil.pathResolve(this.modulePath, this.builderDir, generatorViewName + Constant_1.HMRouterPluginConstant.VIEW_NAME_SUFFIX);
39
+ return FileUtil_1.default.pathResolve(this.modulePath, this.builderDir, generatorViewName + CommonConstants_1.default.VIEW_NAME_SUFFIX);
32
40
  }
33
41
  getBuilderDir() {
34
- return hvigor_1.FileUtil.pathResolve(this.modulePath, this.builderDir);
42
+ return FileUtil_1.default.pathResolve(this.modulePath, this.builderDir);
35
43
  }
36
44
  getBuilderFilePath(generatorViewName) {
37
- return path_1.default.join(this.builderDir, generatorViewName + Constant_1.HMRouterPluginConstant.VIEW_NAME_SUFFIX);
45
+ return path_1.default.join(this.builderDir, generatorViewName + CommonConstants_1.default.VIEW_NAME_SUFFIX);
38
46
  }
39
47
  getRouterMapDir() {
40
- return hvigor_1.FileUtil.pathResolve(this.modulePath, this.routerMapDir, Constant_1.HMRouterPluginConstant.ROUTER_MAP_NAME);
48
+ return FileUtil_1.default.pathResolve(this.modulePath, this.routerMapDir, CommonConstants_1.default.ROUTER_MAP_NAME);
41
49
  }
42
50
  getModuleRouterMapFilePath(routerMapFileName) {
43
- return hvigor_1.FileUtil.pathResolve(this.modulePath, this.routerMapDir, routerMapFileName + Constant_1.HMRouterPluginConstant.JSON_SUFFIX);
51
+ return FileUtil_1.default.pathResolve(this.modulePath, this.routerMapDir, routerMapFileName + CommonConstants_1.default.JSON_SUFFIX);
44
52
  }
45
53
  getRawFilePath() {
46
- return hvigor_1.FileUtil.pathResolve(this.modulePath, Constant_1.HMRouterPluginConstant.RAWFILE_DIR);
54
+ return FileUtil_1.default.pathResolve(this.modulePath, CommonConstants_1.default.RAWFILE_DIR);
47
55
  }
48
56
  getTplFilePath() {
49
- return hvigor_1.FileUtil.pathResolve(__dirname, Constant_1.HMRouterPluginConstant.PARENT_DELIMITER + this.builderTpl);
57
+ return FileUtil_1.default.pathResolve(__dirname, CommonConstants_1.default.PARENT_DELIMITER + this.builderTpl);
58
+ }
59
+ getConfusionFilePath() {
60
+ return FileUtil_1.default.pathResolve(this.modulePath, CommonConstants_1.default.OBFUSCATION_FILE_NAME);
50
61
  }
51
62
  }
52
63
  exports.HMRouterPluginConfig = HMRouterPluginConfig;
@@ -4,14 +4,19 @@ import { HMRouterPluginConfig } from './HMRouterPluginConfig';
4
4
  export declare class HMRouterPluginHandle {
5
5
  private readonly node;
6
6
  private readonly moduleContext;
7
+ private readonly appContext;
7
8
  readonly config: HMRouterPluginConfig;
8
9
  private readonly plugin;
9
10
  constructor(node: HvigorNode, moduleContext: OhosHapContext | OhosHarContext | OhosHspContext);
10
11
  start(): void;
12
+ private generateConfusionFileTask;
13
+ private isEnableConfusion;
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 FileUtil_1 = __importDefault(require("./utils/FileUtil"));
14
+ const TaskConstants_1 = __importDefault(require("./constants/TaskConstants"));
15
+ const ConfusionUtil_1 = __importDefault(require("./utils/ConfusionUtil"));
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,99 @@ 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]
35
44
  });
45
+ this.node.registerTask({
46
+ name: targetName + TaskConstants_1.default.GENERATE_CONFUSION_TASK,
47
+ run: () => {
48
+ this.generateConfusionFileTask(this.moduleContext.getBuildProfileOpt());
49
+ },
50
+ dependencies: [targetName + TaskConstants_1.default.PLUGIN_TASK],
51
+ postDependencies: [targetName + TaskConstants_1.default.MERGE_PROFILE]
52
+ });
53
+ });
54
+ }
55
+ generateConfusionFileTask(buildProfileOpt) {
56
+ if (!this.isEnableConfusion(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, ConfusionUtil_1.default.buildObfuscatedStrings(this.plugin.routerMap));
63
+ Logger_1.Logger.info('generate confusion rule file successfully, filePath:', obfuscationFilePath);
64
+ }
65
+ isEnableConfusion(buildProfileOpt) {
66
+ let currentBuildMode = this.appContext.getBuildMode();
67
+ let buildOption = buildProfileOpt.buildOptionSet?.find((item) => {
68
+ return item.name == currentBuildMode;
36
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
+ }
37
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
+ pluginIds.forEach((id) => {
108
+ let context = node.getContext(id);
109
+ let signedHspObj = context.getOhpmRemoteHspDependencyInfo(true);
110
+ let remoteHspObj = context.getOhpmRemoteHspDependencyInfo(false);
111
+ for (const key in signedHspObj) {
112
+ rawFileRouterMap.hspModuleNames.push(signedHspObj[key].name);
113
+ }
114
+ for (const key in remoteHspObj) {
115
+ rawFileRouterMap.hspModuleNames.push(remoteHspObj[key].name);
116
+ }
117
+ });
118
+ });
119
+ rawFileRouterMap.hspModuleNames = [...new Set(rawFileRouterMap.hspModuleNames)];
120
+ FileUtil_1.default.writeFileSync(rawFilePath, JSON.stringify(rawFileRouterMap));
121
+ }
43
122
  }
44
123
  taskExec() {
45
124
  let startTime = Date.now();
@@ -56,11 +135,11 @@ class HMRouterPluginHandle {
56
135
  if (moduleJsonOpt.module.routerMap) {
57
136
  let routerMapFileName = moduleJsonOpt.module.routerMap.split(':')[1];
58
137
  let routerMapFilePath = this.config.getModuleRouterMapFilePath(routerMapFileName);
59
- let routerMapObj = hvigor_1.FileUtil.readJson5(routerMapFilePath);
60
- this.plugin.routerMap.unshift(...routerMapObj['routerMap']);
138
+ let routerMapObj = FileUtil_1.default.readJson5(routerMapFilePath);
139
+ this.plugin.routerMap.unshift(...routerMapObj[CommonConstants_1.default.ROUTER_MAP_KEY]);
61
140
  }
62
141
  this.plugin.generateRouterMap();
63
- moduleJsonOpt.module.routerMap = Constant_1.HMRouterPluginConstant.MODULE_ROUTER_MAP_NAME;
142
+ moduleJsonOpt.module.routerMap = CommonConstants_1.default.MODULE_ROUTER_MAP_NAME;
64
143
  this.moduleContext.setModuleJsonOpt(moduleJsonOpt);
65
144
  }
66
145
  updateBuildProfileOpt() {
@@ -84,11 +163,11 @@ class HMRouterPluginHandle {
84
163
  const sources = buildProfileOpt.buildOption.arkOptions.runtimeOnly.sources;
85
164
  routerMap.forEach((item) => {
86
165
  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);
166
+ if (name.includes(CommonConstants_1.default.LIFECYCLE_PREFIX) ||
167
+ name.includes(CommonConstants_1.default.INTERCEPTOR_PREFIX) ||
168
+ name.includes(CommonConstants_1.default.ANIMATOR_PREFIX) ||
169
+ name.includes(CommonConstants_1.default.SERVICE_PREFIX)) {
170
+ sources.push(CommonConstants_1.default.CURRENT_DELIMITER + item.pageSourceFile);
92
171
  }
93
172
  });
94
173
  }
@@ -96,14 +175,22 @@ class HMRouterPluginHandle {
96
175
  let levels = 0;
97
176
  let configParam = {};
98
177
  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);
178
+ let configFilePath = FileUtil_1.default.pathResolve(this.node.getNodePath(), CommonConstants_1.default.PARENT_DELIMITER.repeat(levels) + CommonConstants_1.default.CONFIG_FILE_NAME);
179
+ if (FileUtil_1.default.exist(configFilePath)) {
180
+ configParam = FileUtil_1.default.readJson5(configFilePath);
102
181
  break;
103
182
  }
104
183
  levels++;
105
184
  }
106
185
  return new HMRouterPluginConfig_1.HMRouterPluginConfig(this.node.getNodeName(), this.node.getNodePath(), configParam);
107
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);
194
+ }
108
195
  }
109
196
  exports.HMRouterPluginHandle = HMRouterPluginHandle;
package/dist/Index.js CHANGED
@@ -6,12 +6,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.hapPlugin = hapPlugin;
7
7
  exports.hspPlugin = hspPlugin;
8
8
  exports.harPlugin = harPlugin;
9
- const fs_1 = __importDefault(require("fs"));
10
9
  const hvigor_1 = require("@ohos/hvigor");
11
10
  const hvigor_ohos_plugin_1 = require("@ohos/hvigor-ohos-plugin");
12
11
  const HMRouterPluginHandle_1 = require("./HMRouterPluginHandle");
13
- const Constant_1 = require("./common/Constant");
14
12
  const Logger_1 = require("./common/Logger");
13
+ const CommonConstants_1 = __importDefault(require("./constants/CommonConstants"));
14
+ const FileUtil_1 = __importDefault(require("./utils/FileUtil"));
15
+ const TsAstUtil_1 = require("./utils/TsAstUtil");
15
16
  class HMRouterPluginMgr {
16
17
  constructor() {
17
18
  this.hmRouterPluginSet = new Set();
@@ -21,8 +22,21 @@ class HMRouterPluginMgr {
21
22
  });
22
23
  });
23
24
  hvigor_1.hvigor.buildFinished(() => {
24
- this.deleteGeneratorFile();
25
+ Logger_1.Logger.info('buildFinished deleteFile exec...');
26
+ this.hmRouterPluginSet.forEach((pluginHandle) => {
27
+ if (pluginHandle.config.saveGeneratedFile) {
28
+ Logger_1.Logger.info(pluginHandle.config.moduleName + ' saveGeneratedFile is true, skip deleting');
29
+ return;
30
+ }
31
+ this.deleteRouterMapFile(pluginHandle);
32
+ this.deleteRawFile(pluginHandle);
33
+ this.deleteGeneratorFile(pluginHandle);
34
+ if (pluginHandle.config.autoObfuscation) {
35
+ this.deleteConfusionFile(pluginHandle);
36
+ }
37
+ });
25
38
  HMRouterPluginMgrInstance = null;
39
+ TsAstUtil_1.TsAstUtil.clearProject();
26
40
  });
27
41
  }
28
42
  registerHMRouterPlugin(node, pluginId) {
@@ -33,69 +47,73 @@ class HMRouterPluginMgr {
33
47
  }
34
48
  let pluginHandle = new HMRouterPluginHandle_1.HMRouterPluginHandle(node, moduleContext);
35
49
  if (pluginId === hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAP_PLUGIN) {
36
- let packageJson = hvigor_1.FileUtil.readJson5(hvigor_1.FileUtil.pathResolve(node.getNodePath(), 'oh-package.json5'));
50
+ let packageJson = FileUtil_1.default.readJson5(FileUtil_1.default.pathResolve(node.getNodePath(), 'oh-package.json5'));
37
51
  pluginHandle.config.moduleName = packageJson.name;
38
52
  }
39
53
  this.hmRouterPluginSet.add(pluginHandle);
40
54
  }
41
- deleteGeneratorFile() {
42
- Logger_1.Logger.info('deleteGeneratorFile exec...');
43
- for (let hmRouterPlugin of this.hmRouterPluginSet) {
44
- if (hmRouterPlugin.config.saveGeneratedFile) {
45
- Logger_1.Logger.info(hmRouterPlugin.config.moduleName + ' saveGeneratedFile is true, skip deleting');
46
- continue;
47
- }
48
- let routerMapDirPath = hmRouterPlugin.config.getRouterMapDir();
49
- if (hvigor_1.FileUtil.exist(routerMapDirPath)) {
50
- fs_1.default.unlinkSync(routerMapDirPath);
51
- Logger_1.Logger.info(routerMapDirPath + ' delete hm_router_map.json');
52
- }
53
- let builderDirPath = hmRouterPlugin.config.getBuilderDir();
54
- if (hvigor_1.FileUtil.exist(builderDirPath)) {
55
- fs_1.default.rmSync(builderDirPath, {
56
- recursive: true,
57
- });
58
- Logger_1.Logger.info(hmRouterPlugin.config.modulePath + ' delete builder dir');
59
- }
60
- let rawFilePath = hmRouterPlugin.config.getRawFilePath();
61
- if (hvigor_1.FileUtil.exist(rawFilePath)) {
62
- fs_1.default.unlinkSync(rawFilePath);
63
- Logger_1.Logger.info(hmRouterPlugin.config.modulePath + ' delete rawfile hm_router_map.json');
64
- }
55
+ deleteGeneratorFile(pluginHandle) {
56
+ let builderDirPath = pluginHandle.config.getBuilderDir();
57
+ if (FileUtil_1.default.exist(builderDirPath)) {
58
+ FileUtil_1.default.rmSync(builderDirPath, {
59
+ recursive: true
60
+ });
61
+ Logger_1.Logger.info(pluginHandle.config.modulePath + ' delete builder dir');
62
+ }
63
+ }
64
+ deleteRouterMapFile(pluginHandle) {
65
+ let routerMapDirPath = pluginHandle.config.getRouterMapDir();
66
+ if (FileUtil_1.default.exist(routerMapDirPath)) {
67
+ FileUtil_1.default.unlinkSync(routerMapDirPath);
68
+ Logger_1.Logger.info(routerMapDirPath + ' delete hm_router_map.json');
69
+ }
70
+ }
71
+ deleteRawFile(pluginHandle) {
72
+ let rawFilePath = pluginHandle.config.getRawFilePath();
73
+ if (FileUtil_1.default.exist(rawFilePath)) {
74
+ FileUtil_1.default.unlinkSync(rawFilePath);
75
+ Logger_1.Logger.info(pluginHandle.config.modulePath + ' delete rawfile hm_router_map.json');
76
+ }
77
+ }
78
+ deleteConfusionFile(pluginHandle) {
79
+ let confusionFilePath = pluginHandle.config.getConfusionFilePath();
80
+ if (FileUtil_1.default.exist(confusionFilePath)) {
81
+ FileUtil_1.default.unlinkSync(confusionFilePath);
82
+ Logger_1.Logger.info(pluginHandle.config.modulePath + ' delete confusion hmrouter_obfuscation_rules.txt');
65
83
  }
66
84
  }
67
85
  }
68
86
  let HMRouterPluginMgrInstance = null;
69
87
  function hapPlugin() {
70
88
  return {
71
- pluginId: Constant_1.HMRouterPluginConstant.HAP_PLUGIN_ID,
89
+ pluginId: CommonConstants_1.default.HAP_PLUGIN_ID,
72
90
  apply(node) {
73
91
  if (!HMRouterPluginMgrInstance) {
74
92
  HMRouterPluginMgrInstance = new HMRouterPluginMgr();
75
93
  }
76
94
  HMRouterPluginMgrInstance.registerHMRouterPlugin(node, hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAP_PLUGIN);
77
- },
95
+ }
78
96
  };
79
97
  }
80
98
  function hspPlugin() {
81
99
  return {
82
- pluginId: Constant_1.HMRouterPluginConstant.HSP_PLUGIN_ID,
100
+ pluginId: CommonConstants_1.default.HSP_PLUGIN_ID,
83
101
  apply(node) {
84
102
  if (!HMRouterPluginMgrInstance) {
85
103
  HMRouterPluginMgrInstance = new HMRouterPluginMgr();
86
104
  }
87
105
  HMRouterPluginMgrInstance.registerHMRouterPlugin(node, hvigor_ohos_plugin_1.OhosPluginId.OHOS_HSP_PLUGIN);
88
- },
106
+ }
89
107
  };
90
108
  }
91
109
  function harPlugin() {
92
110
  return {
93
- pluginId: Constant_1.HMRouterPluginConstant.HAR_PLUGIN_ID,
111
+ pluginId: CommonConstants_1.default.HAR_PLUGIN_ID,
94
112
  apply(node) {
95
113
  if (!HMRouterPluginMgrInstance) {
96
114
  HMRouterPluginMgrInstance = new HMRouterPluginMgr();
97
115
  }
98
116
  HMRouterPluginMgrInstance.registerHMRouterPlugin(node, hvigor_ohos_plugin_1.OhosPluginId.OHOS_HAR_PLUGIN);
99
- },
117
+ }
100
118
  };
101
119
  }
@@ -3,6 +3,7 @@ export interface BaseAnalyzeResult {
3
3
  name?: string;
4
4
  module?: string;
5
5
  annotation?: string;
6
+ pageSourceFile?: string;
6
7
  }
7
8
  export interface HMRouterResult extends BaseAnalyzeResult {
8
9
  pageUrl?: any;