@hadss/hmrouter-plugin 1.0.0-rc.0 → 1.0.0-rc.10

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.
Files changed (50) hide show
  1. package/LICENSE +77 -77
  2. package/README.md +260 -37
  3. package/dist/HMRouterAnalyzer.d.ts +31 -0
  4. package/dist/HMRouterAnalyzer.js +293 -0
  5. package/dist/HMRouterHvigorPlugin.d.ts +15 -0
  6. package/dist/HMRouterHvigorPlugin.js +139 -0
  7. package/dist/HMRouterPluginConfig.d.ts +39 -0
  8. package/dist/HMRouterPluginConfig.js +73 -0
  9. package/dist/HMRouterPluginHandle.d.ts +23 -0
  10. package/dist/HMRouterPluginHandle.js +231 -0
  11. package/dist/Index.d.ts +4 -0
  12. package/dist/Index.js +124 -0
  13. package/dist/common/Constant.d.ts +27 -0
  14. package/dist/common/Constant.js +35 -0
  15. package/dist/common/Logger.d.ts +13 -0
  16. package/dist/common/Logger.js +55 -0
  17. package/dist/common/PluginModel.d.ts +50 -0
  18. package/dist/common/PluginModel.js +23 -0
  19. package/dist/constants/CommonConstants.d.ts +39 -0
  20. package/dist/constants/CommonConstants.js +46 -0
  21. package/dist/constants/ConfigConstants.d.ts +11 -0
  22. package/dist/constants/ConfigConstants.js +15 -0
  23. package/dist/constants/TaskConstants.d.ts +9 -0
  24. package/dist/constants/TaskConstants.js +13 -0
  25. package/dist/store/PluginStore.d.ts +12 -0
  26. package/dist/store/PluginStore.js +19 -0
  27. package/dist/utils/ConfusionUtil.d.ts +4 -0
  28. package/dist/utils/ConfusionUtil.js +27 -0
  29. package/dist/utils/FileUtil.d.ts +11 -0
  30. package/dist/utils/FileUtil.js +20 -0
  31. package/dist/utils/ObfuscationUtil.d.ts +4 -0
  32. package/dist/utils/ObfuscationUtil.js +34 -0
  33. package/dist/utils/StringUtil.d.ts +3 -0
  34. package/dist/utils/StringUtil.js +18 -0
  35. package/dist/utils/TsAstUtil.d.ts +9 -0
  36. package/dist/utils/TsAstUtil.js +89 -0
  37. package/package.json +46 -27
  38. package/viewBuilder.ejs +103 -0
  39. package/lib/HMRouterAnalyzer.js +0 -223
  40. package/lib/HMRouterHvigorPlugin.js +0 -175
  41. package/lib/HMRouterPluginConfig.js +0 -31
  42. package/lib/Index.js +0 -201
  43. package/lib/PluginModel.js +0 -33
  44. package/src/HMRouterAnalyzer.ts +0 -253
  45. package/src/HMRouterHvigorPlugin.ts +0 -246
  46. package/src/HMRouterPluginConfig.ts +0 -44
  47. package/src/Index.ts +0 -218
  48. package/src/PluginModel.ts +0 -50
  49. package/tsconfig.json +0 -14
  50. package/viewBuilder.tpl +0 -97
package/src/Index.ts DELETED
@@ -1,218 +0,0 @@
1
- /*
2
- * Copyright (c) 2024 Huawei Device Co., Ltd.
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- import { hvigor, HvigorLogger, HvigorNode, HvigorPlugin, Json5Reader, levels } from '@ohos/hvigor'
17
- import {
18
- OhosHapContext,
19
- OhosHarContext,
20
- OhosHspContext,
21
- OhosPluginId,
22
- Target
23
- } from '@ohos/hvigor-ohos-plugin'
24
- import fs from 'fs'
25
- import path from 'path'
26
- import { HMRouterPluginConfig } from './HMRouterPluginConfig'
27
- import { OhosModuleContext } from '@ohos/hvigor-ohos-plugin/src/plugin/context/plugin-context'
28
- import { HMRouterHvigorPlugin, RouterInfo } from './HMRouterHvigorPlugin'
29
-
30
- type ContextLike = OhosHapContext | OhosHarContext | OhosHspContext
31
-
32
- const HAP_PLUGIN_ID = 'HAP_HMROUTER_PLUGIN'
33
- const HSP_PLUGIN_ID = 'HSP_HMROUTER_PLUGIN'
34
- const HAR_PLUGIN_ID = 'HAR_HMROUTER_PLUGIN'
35
-
36
- const Logger = HvigorLogger.getLogger()
37
-
38
- let modulePathArr: string[] = []
39
-
40
- function readConfig(node: HvigorNode): HMRouterPluginConfig {
41
- let configParam = {}
42
- let configFilePath = path.resolve(node.getNodePath(), './hmrouter_config.json')
43
- if (fs.existsSync(configFilePath)) {
44
- configParam = JSON.parse(fs.readFileSync(configFilePath, 'utf-8'))
45
- } else {
46
- configFilePath = path.resolve(node.getNodePath(), '../hmrouter_config.json')
47
- if (fs.existsSync(configFilePath)) {
48
- configParam = JSON.parse(fs.readFileSync(configFilePath, 'utf-8'))
49
- } else {
50
- configFilePath = path.resolve(node.getNodePath(), '../../hmrouter_config.json')
51
- if (fs.existsSync(configFilePath)) {
52
- configParam = JSON.parse(fs.readFileSync(configFilePath, 'utf-8'))
53
- } else {
54
- configParam = {}
55
- }
56
- }
57
- }
58
- return new HMRouterPluginConfig(node.getNodeName(), node.getNodePath(), configParam)
59
- }
60
-
61
- export function hapPlugin(): HvigorPlugin {
62
- return {
63
- pluginId: HAP_PLUGIN_ID,
64
- apply(node: HvigorNode) {
65
- HMRouterPlugin(node, OhosPluginId.OHOS_HAP_PLUGIN)
66
- }
67
- }
68
- }
69
-
70
- export function hspPlugin(): HvigorPlugin {
71
- return {
72
- pluginId: HSP_PLUGIN_ID,
73
- apply(node: HvigorNode) {
74
- HMRouterPlugin(node, OhosPluginId.OHOS_HSP_PLUGIN)
75
- }
76
- }
77
- }
78
-
79
- export function harPlugin(): HvigorPlugin {
80
- return {
81
- pluginId: HAR_PLUGIN_ID,
82
- apply(node: HvigorNode) {
83
- HMRouterPlugin(node, OhosPluginId.OHOS_HAR_PLUGIN)
84
- }
85
- }
86
- }
87
-
88
- function HMRouterPluginTask(
89
- context: OhosModuleContext,
90
- conf: HMRouterPluginConfig,
91
- node: HvigorNode
92
- ) {
93
- let startTime = Date.now()
94
- Logger.log(node.getNodeName() + ' :HMRouterPluginTask start---' + startTime)
95
- modulePathArr.push(node.getNodePath())
96
- const plugin = new HMRouterHvigorPlugin(conf)
97
- plugin.analyzeAnnotation()
98
- const buildProfileOpt = context.getBuildProfileOpt()
99
- const moduleJsonOpt = context.getModuleJsonOpt()
100
- if (moduleJsonOpt.module.routerMap) {
101
- let routerMapFileName = moduleJsonOpt.module.routerMap.split(':')[1]
102
- let jsonObj = fs.readFileSync(
103
- node.getNodePath().replace(/\\/g, '/') +
104
- `/src/main/resources/base/profile/${routerMapFileName}.json`,
105
- 'utf-8'
106
- )
107
- plugin.routerMap.unshift(...(JSON.parse(jsonObj)['routerMap'] as Array<RouterInfo>))
108
- }
109
- let routerMap = plugin.generateRouterMap()
110
- if (!buildProfileOpt.buildOption?.arkOptions) {
111
- buildProfileOpt.buildOption!.arkOptions = {
112
- runtimeOnly: {
113
- sources: []
114
- },
115
- buildProfileFields: {
116
- HMRouterHash: Date.now()
117
- }
118
- }
119
- pushRouterInfo(buildProfileOpt, routerMap)
120
- } else {
121
- if (!buildProfileOpt.buildOption.arkOptions.runtimeOnly) {
122
- buildProfileOpt.buildOption.arkOptions.runtimeOnly = {
123
- sources: []
124
- }
125
- pushRouterInfo(buildProfileOpt, routerMap)
126
- } else {
127
- if (!buildProfileOpt.buildOption.arkOptions.runtimeOnly.sources) {
128
- buildProfileOpt.buildOption.arkOptions.runtimeOnly.sources = []
129
- pushRouterInfo(buildProfileOpt, routerMap)
130
- } else {
131
- pushRouterInfo(buildProfileOpt, routerMap)
132
- }
133
- }
134
-
135
- if (!buildProfileOpt.buildOption.arkOptions.buildProfileFields) {
136
- buildProfileOpt.buildOption.arkOptions.buildProfileFields = {
137
- HMRouterHash: Date.now()
138
- }
139
- } else {
140
- // @ts-ignore
141
- buildProfileOpt.buildOption.arkOptions.buildProfileFields.HMRouterHash = Date.now()
142
- }
143
- }
144
-
145
- moduleJsonOpt.module.routerMap = `$profile:hm_router_map`
146
- context.setModuleJsonOpt(moduleJsonOpt)
147
- context.setBuildProfileOpt(buildProfileOpt)
148
- Logger.log(levels.DEBUG, JSON.stringify(context.getBuildProfileOpt()))
149
- let endTime = Date.now()
150
- Logger.log(node.getNodeName() + ' :HMRouterPluginTask end---' + endTime)
151
- Logger.log(node.getNodeName() + ' :HMRouterPluginTask cost---' + (endTime - startTime) + 'ms')
152
- }
153
-
154
- function pushRouterInfo(buildProfileOpt: any, routerMap: RouterInfo[]) {
155
- routerMap.forEach((item: RouterInfo) => {
156
- if (item.name.includes('_')) {
157
- buildProfileOpt.buildOption!.arkOptions!.runtimeOnly!.sources!.push(
158
- './' + item.pageSourceFile
159
- )
160
- }
161
- })
162
- }
163
-
164
- export function deleteGeneratorFile(conf: HMRouterPluginConfig) {
165
- if (conf.saveGeneratedFile) {
166
- Logger.info('saveGeneratedFile is true, do not delete file')
167
- return
168
- }
169
- Logger.info('deleteGeneratorFile exec...', modulePathArr)
170
- for (let modulePath of modulePathArr) {
171
- if (fs.existsSync(modulePath + '/' + conf.routerMapDir + '/hm_router_map.json')) {
172
- fs.unlinkSync(modulePath + '/' + conf.routerMapDir + '/hm_router_map.json')
173
- Logger.log(modulePath + 'delete hm_router_map.json')
174
- }
175
- if (fs.existsSync(modulePath + '/' + conf.builderDir)) {
176
- fs.rmSync(modulePath + '/' + conf.builderDir, {
177
- recursive: true
178
- })
179
- Logger.log(modulePath + 'delete builder dir')
180
- }
181
- }
182
- modulePathArr = []
183
- }
184
-
185
- function HMRouterPlugin(node: HvigorNode, pluginId: string) {
186
- Logger.info(`Exec ${pluginId}..., node:${node.getNodeName()}, nodePath:${node.getNodePath()}`)
187
- hvigor.nodesEvaluated(async () => {
188
- let conf = readConfig(node)
189
- if (pluginId === OhosPluginId.OHOS_HAP_PLUGIN) {
190
- let packageJson = await Json5Reader.readJson5File(
191
- path.resolve(node.getNodePath(), './oh-package.json5'),
192
- 'utf-8'
193
- )
194
- conf.moduleName = packageJson.name
195
- }
196
- const context = node.getContext(pluginId) as ContextLike
197
- if (!context) {
198
- Logger.error(`errorCode: 40000005, errorMsg: context is null 请检查插件的hvigorfile配置`)
199
- deleteGeneratorFile(conf)
200
- throw new Error('hspContext is null')
201
- }
202
- context.targets((target: Target) => {
203
- const targetName = target.getTargetName()
204
- node.registerTask({
205
- name: `${targetName}@HMRouterPLuginTask`,
206
- run: () => {
207
- HMRouterPluginTask(context, conf, node)
208
- let taskName = 'Package' + pluginId.split('.')[2].charAt(0).toUpperCase() + pluginId.split('.')[2].slice(1)
209
- node.getTaskByName(`${targetName}@${taskName}`)?.afterRun(() => {
210
- deleteGeneratorFile(conf)
211
- })
212
- },
213
- dependencies: [`${targetName}@PreBuild`],
214
- postDependencies: [`${targetName}@MergeProfile`]
215
- })
216
- })
217
- })
218
- }
@@ -1,50 +0,0 @@
1
- /*
2
- * Copyright (c) 2024 Huawei Device Co., Ltd.
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- export type AnalyzerResultLike =
17
- | HMRouterResult
18
- | HMAnimatorResult
19
- | HMInterceptorResult
20
- | HMLifecycleResult
21
- export class BaseAnalyzeResult {
22
- name?: string // 类名
23
- module?: string // 模块名
24
- annotation?: string // 注解
25
- }
26
-
27
- export class HMRouterResult extends BaseAnalyzeResult {
28
- pageUrl?: string // 跳转路径
29
- dialog?: boolean // 是否弹窗
30
- singleton?: boolean // 是否单例
31
- interceptors?: string[] // 拦截器
32
- animator?: string // 动画
33
- lifecycle?: string // 生命周期
34
- }
35
-
36
- export class HMAnimatorResult extends BaseAnalyzeResult {
37
- animatorName?: string // 动画名称
38
- }
39
-
40
- export class HMInterceptorResult extends BaseAnalyzeResult {
41
- interceptorName?: string // 拦截器名称
42
- priority?: number // 优先级
43
- global?: boolean // 是否全局
44
- }
45
-
46
- export class HMLifecycleResult extends BaseAnalyzeResult {
47
- lifecycleName?: string // 生命周期名称
48
- priority?: number // 优先级
49
- global?: boolean // 是否全局
50
- }
package/tsconfig.json DELETED
@@ -1,14 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ES2021",
4
- "module": "commonjs",
5
- "strict": true,
6
- "esModuleInterop": true,
7
- "skipLibCheck": true,
8
- "forceConsistentCasingInFileNames": true,
9
- "sourceMap": true,
10
- "outDir": "./lib"
11
- },
12
- "include": [".eslintrc.js","src/**/*"],
13
- "exclude": ["node_modules", "lib/**/*"]
14
- }
package/viewBuilder.tpl DELETED
@@ -1,97 +0,0 @@
1
- /*
2
- * Copyright (c) 2024 Huawei Device Co., Ltd.
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- // auto-generated {{componentName}}Builder.ets
17
- import { {{componentName}} } from '{{importPath}}'
18
- import { TemplateMgr, TranslateOption, ScaleOption, OpacityOption } from '@hadss/hmrouter'
19
-
20
- @Builder
21
- export function {{componentName}}Builder(name: string, param: Object) {
22
- {{componentName}}Generated()
23
- }
24
-
25
- @Component
26
- export struct {{componentName}}Generated {
27
- @State translateOption: TranslateOption = new TranslateOption()
28
- @State scaleOption: ScaleOption = new ScaleOption()
29
- @State opacityOption: OpacityOption = new OpacityOption()
30
- private pageUrl: string = '{{pageUrl}}'
31
- private ndId: string = ''
32
-
33
- aboutToAppear(): void {
34
- TemplateMgr.registerAnimatorHandle(this.pageUrl, this.translateOption, this.scaleOption, this.opacityOption)
35
- TemplateMgr.registerHMLifecycleHandle(this.pageUrl, this.getUIContext())
36
- }
37
-
38
- aboutToDisappear(): void {
39
- TemplateMgr.unRegisterAnimatorHandle(this.ndId)
40
- TemplateMgr.destroyHMLifecycle(this.pageUrl, this.ndId)
41
- }
42
-
43
- build() {
44
- NavDestination() {
45
- {{componentName}}()
46
- }
47
- .mode({{dialog}}?NavDestinationMode.DIALOG:NavDestinationMode.STANDARD)
48
- .hideTitleBar(true)
49
- .gesture(PanGesture()
50
- .onActionStart((event: GestureEvent) => {
51
- TemplateMgr.interactiveStart(this.ndId, event)
52
- })
53
- .onActionUpdate((event: GestureEvent) =>{
54
- TemplateMgr.interactiveProgress(this.ndId, event)
55
- })
56
- .onActionEnd((event: GestureEvent) =>{
57
- TemplateMgr.interactiveFinish(this.ndId, event)
58
- })
59
- )
60
- .translate(this.translateOption)
61
- .scale(this.scaleOption)
62
- .opacity(this.opacityOption.opacity)
63
- .onAppear(() => {
64
- TemplateMgr.onAppear(this.pageUrl, this.ndId)
65
- })
66
- .onDisAppear(() => {
67
- TemplateMgr.onDisAppear(this.pageUrl, this.ndId)
68
- })
69
- .onShown(() => {
70
- TemplateMgr.onShown(this.pageUrl, this.ndId)
71
- })
72
- .onHidden(() => {
73
- TemplateMgr.onHidden(this.pageUrl, this.ndId)
74
- })
75
- .onWillAppear(() => {
76
- TemplateMgr.onWillAppear(this.pageUrl)
77
- })
78
- .onWillDisappear(() => {
79
- TemplateMgr.onWillDisappear(this.pageUrl, this.ndId)
80
- })
81
- .onWillShow(() => {
82
- TemplateMgr.onWillShow(this.pageUrl, this.ndId)
83
- })
84
- .onWillHide(() => {
85
- TemplateMgr.onWillHide(this.pageUrl, this.ndId)
86
- })
87
- .onReady((navContext: NavDestinationContext) => {
88
- this.ndId = navContext.navDestinationId!
89
- TemplateMgr.updateAnimatorHandle(this.pageUrl, this.ndId)
90
- TemplateMgr.updateLifecycleNavContext(this.pageUrl, navContext)
91
- TemplateMgr.onReady(this.pageUrl, this.ndId)
92
- })
93
- .onBackPressed(() => {
94
- return TemplateMgr.onBackPressed(this.pageUrl, this.ndId)
95
- })
96
- }
97
- }