@hadss/turbo-trans-json-plugin 1.0.0-rc.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (195) hide show
  1. package/LICENSE +177 -0
  2. package/README.md +253 -0
  3. package/dist/core/Types.d.ts +196 -0
  4. package/dist/core/Types.js +47 -0
  5. package/dist/core/analyzers/ClassAnalyzer.d.ts +23 -0
  6. package/dist/core/analyzers/ClassAnalyzer.js +480 -0
  7. package/dist/core/analyzers/CustomTypeAnalyzer.d.ts +21 -0
  8. package/dist/core/analyzers/CustomTypeAnalyzer.js +243 -0
  9. package/dist/core/analyzers/SendableMergeChecker.d.ts +9 -0
  10. package/dist/core/analyzers/SendableMergeChecker.js +100 -0
  11. package/dist/core/analyzers/SendableMergeabilityRegistry.d.ts +7 -0
  12. package/dist/core/analyzers/SendableMergeabilityRegistry.js +23 -0
  13. package/dist/core/constants/DecoratorConstants.d.ts +7 -0
  14. package/dist/core/constants/DecoratorConstants.js +13 -0
  15. package/dist/core/constants/PathConstants.d.ts +6 -0
  16. package/dist/core/constants/PathConstants.js +10 -0
  17. package/dist/core/constants/StringConstants.d.ts +83 -0
  18. package/dist/core/constants/StringConstants.js +87 -0
  19. package/dist/core/constants/index.d.ts +3 -0
  20. package/dist/core/constants/index.js +19 -0
  21. package/dist/core/handlers/BaseTypeHandler.d.ts +23 -0
  22. package/dist/core/handlers/BaseTypeHandler.js +57 -0
  23. package/dist/core/handlers/CustomClassHandler.d.ts +21 -0
  24. package/dist/core/handlers/CustomClassHandler.js +191 -0
  25. package/dist/core/handlers/DateHandler.d.ts +2 -0
  26. package/dist/core/handlers/DateHandler.js +60 -0
  27. package/dist/core/handlers/DecimalHandler.d.ts +2 -0
  28. package/dist/core/handlers/DecimalHandler.js +60 -0
  29. package/dist/core/handlers/EnumHandler.d.ts +2 -0
  30. package/dist/core/handlers/EnumHandler.js +89 -0
  31. package/dist/core/handlers/GenericContainerHandler.d.ts +2 -0
  32. package/dist/core/handlers/GenericContainerHandler.js +440 -0
  33. package/dist/core/handlers/GenericHandler.d.ts +18 -0
  34. package/dist/core/handlers/GenericHandler.js +92 -0
  35. package/dist/core/handlers/HandlerBootstrap.d.ts +2 -0
  36. package/dist/core/handlers/HandlerBootstrap.js +28 -0
  37. package/dist/core/handlers/ITypeHandler.d.ts +23 -0
  38. package/dist/core/handlers/ITypeHandler.js +8 -0
  39. package/dist/core/handlers/PrimitiveHandler.d.ts +2 -0
  40. package/dist/core/handlers/PrimitiveHandler.js +127 -0
  41. package/dist/core/handlers/TupleHandler.d.ts +2 -0
  42. package/dist/core/handlers/TupleHandler.js +98 -0
  43. package/dist/core/handlers/TypeHandlerRegistry.d.ts +20 -0
  44. package/dist/core/handlers/TypeHandlerRegistry.js +113 -0
  45. package/dist/core/handlers/UnionTypeHandler.d.ts +2 -0
  46. package/dist/core/handlers/UnionTypeHandler.js +263 -0
  47. package/dist/core/handlers/index.d.ts +2 -0
  48. package/dist/core/handlers/index.js +5 -0
  49. package/dist/core/import-rewrite/services/BuildProfileUpdater.d.ts +8 -0
  50. package/dist/core/import-rewrite/services/BuildProfileUpdater.js +92 -0
  51. package/dist/core/import-rewrite/services/ImportRewriteService.d.ts +9 -0
  52. package/dist/core/import-rewrite/services/ImportRewriteService.js +61 -0
  53. package/dist/core/import-rewrite/services/ImportTransformService.d.ts +15 -0
  54. package/dist/core/import-rewrite/services/ImportTransformService.js +109 -0
  55. package/dist/core/import-rewrite/strategies/ImportScanStrategy.d.ts +17 -0
  56. package/dist/core/import-rewrite/strategies/ImportScanStrategy.js +137 -0
  57. package/dist/core/import-rewrite/types/ImportRewriteTypes.d.ts +17 -0
  58. package/dist/core/import-rewrite/types/ImportRewriteTypes.js +2 -0
  59. package/dist/core/index.d.ts +9 -0
  60. package/dist/core/index.js +27 -0
  61. package/dist/core/interfaces/ITask.d.ts +7 -0
  62. package/dist/core/interfaces/ITask.js +2 -0
  63. package/dist/core/interfaces/ITaskContext.d.ts +11 -0
  64. package/dist/core/interfaces/ITaskContext.js +2 -0
  65. package/dist/core/interfaces/index.d.ts +2 -0
  66. package/dist/core/interfaces/index.js +2 -0
  67. package/dist/core/logger/Logger.d.ts +13 -0
  68. package/dist/core/logger/Logger.js +78 -0
  69. package/dist/core/services/CodeAnalysisService.d.ts +7 -0
  70. package/dist/core/services/CodeAnalysisService.js +43 -0
  71. package/dist/core/services/CodeGenerationEngine.d.ts +12 -0
  72. package/dist/core/services/CodeGenerationEngine.js +102 -0
  73. package/dist/core/services/CodeGenerationService/CodeGenerationService.d.ts +13 -0
  74. package/dist/core/services/CodeGenerationService/CodeGenerationService.js +110 -0
  75. package/dist/core/services/CodeGenerationService/generators/MergedSendableClassGenerator.d.ts +13 -0
  76. package/dist/core/services/CodeGenerationService/generators/MergedSendableClassGenerator.js +119 -0
  77. package/dist/core/services/CodeGenerationService/generators/OriginalClassGenerator.d.ts +21 -0
  78. package/dist/core/services/CodeGenerationService/generators/OriginalClassGenerator.js +224 -0
  79. package/dist/core/services/CodeGenerationService/generators/SendableClassGenerator.d.ts +24 -0
  80. package/dist/core/services/CodeGenerationService/generators/SendableClassGenerator.js +307 -0
  81. package/dist/core/services/CodeGenerationService/generators/SerializerGenerator.d.ts +28 -0
  82. package/dist/core/services/CodeGenerationService/generators/SerializerGenerator.js +259 -0
  83. package/dist/core/services/CodeGenerationService/generators/TempSerializerGenerator.d.ts +19 -0
  84. package/dist/core/services/CodeGenerationService/generators/TempSerializerGenerator.js +178 -0
  85. package/dist/core/services/CodeGenerationService/index.d.ts +1 -0
  86. package/dist/core/services/CodeGenerationService/index.js +5 -0
  87. package/dist/core/services/CodeGenerationService/shared/ImportManager.d.ts +27 -0
  88. package/dist/core/services/CodeGenerationService/shared/ImportManager.js +127 -0
  89. package/dist/core/template/HandlebarsTemplateEngine.d.ts +26 -0
  90. package/dist/core/template/HandlebarsTemplateEngine.js +226 -0
  91. package/dist/core/utils/ConfigManager.d.ts +10 -0
  92. package/dist/core/utils/ConfigManager.js +32 -0
  93. package/dist/core/utils/CustomError.d.ts +30 -0
  94. package/dist/core/utils/CustomError.js +37 -0
  95. package/dist/core/utils/DeepCopyUtil.d.ts +15 -0
  96. package/dist/core/utils/DeepCopyUtil.js +138 -0
  97. package/dist/core/utils/GenericTypeSubstitutionUtil.d.ts +9 -0
  98. package/dist/core/utils/GenericTypeSubstitutionUtil.js +68 -0
  99. package/dist/core/utils/SerializationPathUtil.d.ts +18 -0
  100. package/dist/core/utils/SerializationPathUtil.js +107 -0
  101. package/dist/core/utils/TsMorphUtil.d.ts +8 -0
  102. package/dist/core/utils/TsMorphUtil.js +34 -0
  103. package/dist/index.d.ts +4 -0
  104. package/dist/index.js +57 -0
  105. package/dist/json-plugin/JSONExecuteController.d.ts +13 -0
  106. package/dist/json-plugin/JSONExecuteController.js +103 -0
  107. package/dist/json-plugin/constants/TaskConstants.d.ts +7 -0
  108. package/dist/json-plugin/constants/TaskConstants.js +11 -0
  109. package/dist/json-plugin/interfaces/IModuleContext.d.ts +10 -0
  110. package/dist/json-plugin/interfaces/IModuleContext.js +2 -0
  111. package/dist/json-plugin/interfaces/ITargetContext.d.ts +8 -0
  112. package/dist/json-plugin/interfaces/ITargetContext.js +2 -0
  113. package/dist/json-plugin/interfaces/impl/ModuleContext.d.ts +12 -0
  114. package/dist/json-plugin/interfaces/impl/ModuleContext.js +24 -0
  115. package/dist/json-plugin/interfaces/impl/TargetContext.d.ts +23 -0
  116. package/dist/json-plugin/interfaces/impl/TargetContext.js +113 -0
  117. package/dist/json-plugin/tasks/BaseTask.d.ts +14 -0
  118. package/dist/json-plugin/tasks/BaseTask.js +53 -0
  119. package/dist/json-plugin/tasks/CleanTask.d.ts +8 -0
  120. package/dist/json-plugin/tasks/CleanTask.js +25 -0
  121. package/dist/json-plugin/tasks/CodeProcessingTask.d.ts +8 -0
  122. package/dist/json-plugin/tasks/CodeProcessingTask.js +26 -0
  123. package/dist/json-plugin/tasks/SyncTask.d.ts +8 -0
  124. package/dist/json-plugin/tasks/SyncTask.js +21 -0
  125. package/dist/json-plugin/tasks/WatchTask.d.ts +11 -0
  126. package/dist/json-plugin/tasks/WatchTask.js +102 -0
  127. package/package.json +46 -0
  128. package/src/core/Types.ts +356 -0
  129. package/src/core/analyzers/ClassAnalyzer.ts +824 -0
  130. package/src/core/analyzers/CustomTypeAnalyzer.ts +337 -0
  131. package/src/core/analyzers/SendableMergeChecker.ts +195 -0
  132. package/src/core/analyzers/SendableMergeabilityRegistry.ts +72 -0
  133. package/src/core/constants/DecoratorConstants.ts +27 -0
  134. package/src/core/constants/PathConstants.ts +31 -0
  135. package/src/core/constants/StringConstants.ts +152 -0
  136. package/src/core/constants/index.ts +21 -0
  137. package/src/core/handlers/BaseTypeHandler.ts +121 -0
  138. package/src/core/handlers/CustomClassHandler.ts +278 -0
  139. package/src/core/handlers/DateHandler.ts +75 -0
  140. package/src/core/handlers/DecimalHandler.ts +75 -0
  141. package/src/core/handlers/EnumHandler.ts +142 -0
  142. package/src/core/handlers/GenericContainerHandler.ts +621 -0
  143. package/src/core/handlers/GenericHandler.ts +130 -0
  144. package/src/core/handlers/HandlerBootstrap.ts +64 -0
  145. package/src/core/handlers/ITypeHandler.ts +133 -0
  146. package/src/core/handlers/PrimitiveHandler.ts +159 -0
  147. package/src/core/handlers/TupleHandler.ts +145 -0
  148. package/src/core/handlers/TypeHandlerRegistry.ts +236 -0
  149. package/src/core/handlers/UnionTypeHandler.ts +400 -0
  150. package/src/core/handlers/index.ts +18 -0
  151. package/src/core/import-rewrite/services/BuildProfileUpdater.ts +145 -0
  152. package/src/core/import-rewrite/services/ImportRewriteService.ts +129 -0
  153. package/src/core/import-rewrite/services/ImportTransformService.ts +200 -0
  154. package/src/core/import-rewrite/strategies/ImportScanStrategy.ts +303 -0
  155. package/src/core/import-rewrite/types/ImportRewriteTypes.ts +100 -0
  156. package/src/core/index.ts +31 -0
  157. package/src/core/interfaces/ITask.ts +23 -0
  158. package/src/core/interfaces/ITaskContext.ts +94 -0
  159. package/src/core/interfaces/index.ts +17 -0
  160. package/src/core/logger/Logger.ts +149 -0
  161. package/src/core/services/CodeAnalysisService.ts +67 -0
  162. package/src/core/services/CodeGenerationEngine.ts +181 -0
  163. package/src/core/services/CodeGenerationService/CodeGenerationService.ts +159 -0
  164. package/src/core/services/CodeGenerationService/generators/MergedSendableClassGenerator.ts +189 -0
  165. package/src/core/services/CodeGenerationService/generators/OriginalClassGenerator.ts +314 -0
  166. package/src/core/services/CodeGenerationService/generators/SendableClassGenerator.ts +421 -0
  167. package/src/core/services/CodeGenerationService/generators/SerializerGenerator.ts +392 -0
  168. package/src/core/services/CodeGenerationService/generators/TempSerializerGenerator.ts +277 -0
  169. package/src/core/services/CodeGenerationService/index.ts +16 -0
  170. package/src/core/services/CodeGenerationService/shared/ImportManager.ts +191 -0
  171. package/src/core/template/HandlebarsTemplateEngine.ts +282 -0
  172. package/src/core/utils/ConfigManager.ts +49 -0
  173. package/src/core/utils/CustomError.ts +51 -0
  174. package/src/core/utils/DeepCopyUtil.ts +185 -0
  175. package/src/core/utils/GenericTypeSubstitutionUtil.ts +136 -0
  176. package/src/core/utils/SerializationPathUtil.ts +142 -0
  177. package/src/core/utils/TsMorphUtil.ts +50 -0
  178. package/src/index.ts +81 -0
  179. package/src/json-plugin/JSONExecuteController.ts +134 -0
  180. package/src/json-plugin/constants/TaskConstants.ts +22 -0
  181. package/src/json-plugin/interfaces/IModuleContext.ts +27 -0
  182. package/src/json-plugin/interfaces/ITargetContext.ts +25 -0
  183. package/src/json-plugin/interfaces/impl/ModuleContext.ts +45 -0
  184. package/src/json-plugin/interfaces/impl/TargetContext.ts +196 -0
  185. package/src/json-plugin/tasks/BaseTask.ts +94 -0
  186. package/src/json-plugin/tasks/CleanTask.ts +36 -0
  187. package/src/json-plugin/tasks/CodeProcessingTask.ts +41 -0
  188. package/src/json-plugin/tasks/SyncTask.ts +33 -0
  189. package/src/json-plugin/tasks/WatchTask.ts +99 -0
  190. package/template/SerializerPerformanceTemplate.hbs +35 -0
  191. package/template/SerializerRegisterTemplate.hbs +10 -0
  192. package/template/SerializerStrictTemplate.hbs +89 -0
  193. package/template/SerializerTemplate.hbs +176 -0
  194. package/tsconfig.json +17 -0
  195. package/tslint.json +3 -0
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,12 @@
1
+ import { HvigorNode } from '@ohos/hvigor';
2
+ import { IModuleContext } from '../IModuleContext';
3
+ import { OhosHapContext } from '@ohos/hvigor-ohos-plugin';
4
+ import { ModuleBuildProfile } from '@ohos/hvigor-ohos-plugin/src/options/build/module-build-profile';
5
+ export declare class ModuleContextImpl implements IModuleContext {
6
+ readonly node: HvigorNode;
7
+ readonly moduleContext: OhosHapContext;
8
+ readonly scanFiles: string[];
9
+ constructor(node: HvigorNode, moduleContext: OhosHapContext);
10
+ get buildProfileOpt(): ModuleBuildProfile.ModuleBuildOpt;
11
+ initScanFiles(scanDir: string[]): void;
12
+ }
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ModuleContextImpl = void 0;
7
+ const SerializationPathUtil_1 = __importDefault(require("../../../core/utils/SerializationPathUtil"));
8
+ class ModuleContextImpl {
9
+ constructor(node, moduleContext) {
10
+ this.scanFiles = [];
11
+ this.node = node;
12
+ this.moduleContext = moduleContext;
13
+ }
14
+ get buildProfileOpt() {
15
+ return this.moduleContext.getBuildProfileOpt();
16
+ }
17
+ initScanFiles(scanDir) {
18
+ scanDir.forEach(dir => {
19
+ const scanPath = SerializationPathUtil_1.default.pathResolve(this.moduleContext.getModulePath(), dir);
20
+ this.scanFiles.push(...SerializationPathUtil_1.default.scanEtsFiles(scanPath));
21
+ });
22
+ }
23
+ }
24
+ exports.ModuleContextImpl = ModuleContextImpl;
@@ -0,0 +1,23 @@
1
+ import { Target } from '@ohos/hvigor-ohos-plugin';
2
+ import { IModuleContext } from '../IModuleContext';
3
+ import { ITargetContext } from '../ITargetContext';
4
+ export declare class TargetContextImpl implements ITargetContext {
5
+ readonly target: Target;
6
+ readonly context: IModuleContext;
7
+ private readonly DEFAULT_SOURCE_ROOT;
8
+ private readonly GENERATED_ROOT;
9
+ constructor(target: Target, context: IModuleContext);
10
+ private get targetOpt();
11
+ getCurrentTargetScanFiles(): string[];
12
+ getModuleName(): string;
13
+ getModulePath(): string;
14
+ getPackageName(): string;
15
+ mapToOutputPath(absSourceFile: string): string;
16
+ getOutputRoot(): string;
17
+ getRelativePath(absolutePath: string): string;
18
+ isValidOutputPath(outputPath: string): boolean;
19
+ ensureOutputDirectory(outputPath: string): void;
20
+ calculateSourceRootToModuleRoot(absolutePath: string): string;
21
+ private get targetSourceRoots();
22
+ private getAbsoluteSourceRoots;
23
+ }
@@ -0,0 +1,113 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.TargetContextImpl = void 0;
7
+ const core_1 = require("../../../core");
8
+ const SerializationPathUtil_1 = __importDefault(require("../../../core/utils/SerializationPathUtil"));
9
+ const CustomError_1 = require("../../../core/utils/CustomError");
10
+ class TargetContextImpl {
11
+ constructor(target, context) {
12
+ this.DEFAULT_SOURCE_ROOT = core_1.PathConstants.DEFAULT_SOURCE_ROOT;
13
+ this.GENERATED_ROOT = core_1.PathConstants.GENERATED_ROOT;
14
+ this.target = target;
15
+ this.context = context;
16
+ }
17
+ get targetOpt() {
18
+ return this.context.buildProfileOpt.targets?.find((item) => item.name === this.target.getTargetName());
19
+ }
20
+ getCurrentTargetScanFiles() {
21
+ const absoluteSourceRoots = this.getAbsoluteSourceRoots();
22
+ return this.context.scanFiles.filter(item => {
23
+ const normalizedFilePath = SerializationPathUtil_1.default.normalize(item);
24
+ return absoluteSourceRoots.some(sourceRoot => normalizedFilePath.startsWith(sourceRoot));
25
+ });
26
+ }
27
+ getModuleName() {
28
+ return this.context.moduleContext.getModuleName();
29
+ }
30
+ getModulePath() {
31
+ return this.context.moduleContext.getModulePath();
32
+ }
33
+ getPackageName() {
34
+ const packagePath = SerializationPathUtil_1.default.pathResolve(this.getModulePath(), 'oh-package.json5');
35
+ const ohPackageObj = SerializationPathUtil_1.default.readJson5(packagePath);
36
+ return ohPackageObj.name;
37
+ }
38
+ mapToOutputPath(absSourceFile) {
39
+ const normalizedFilePath = SerializationPathUtil_1.default.normalize(absSourceFile);
40
+ const moduleRoot = this.context.moduleContext.getModulePath();
41
+ const srcMainAbs = SerializationPathUtil_1.default.normalize(SerializationPathUtil_1.default.pathResolve(moduleRoot, this.DEFAULT_SOURCE_ROOT));
42
+ const generatedRootAbs = this.getOutputRoot();
43
+ if (normalizedFilePath.startsWith(srcMainAbs + SerializationPathUtil_1.default.sep)) {
44
+ const relativeToSrcMain = SerializationPathUtil_1.default.relative(srcMainAbs, normalizedFilePath);
45
+ return SerializationPathUtil_1.default.join(generatedRootAbs, relativeToSrcMain);
46
+ }
47
+ const absoluteSourceRoots = this.getAbsoluteSourceRoots();
48
+ const matchedRoot = absoluteSourceRoots.find(root => normalizedFilePath.startsWith(root));
49
+ if (matchedRoot) {
50
+ const rel = SerializationPathUtil_1.default.relative(matchedRoot, normalizedFilePath);
51
+ return SerializationPathUtil_1.default.join(generatedRootAbs, rel);
52
+ }
53
+ return SerializationPathUtil_1.default.join(generatedRootAbs, SerializationPathUtil_1.default.basename(normalizedFilePath));
54
+ }
55
+ getOutputRoot() {
56
+ const moduleRoot = this.context.moduleContext.getModulePath();
57
+ const absolutePath = SerializationPathUtil_1.default.pathResolve(moduleRoot, this.GENERATED_ROOT);
58
+ return SerializationPathUtil_1.default.normalize(absolutePath);
59
+ }
60
+ getRelativePath(absolutePath) {
61
+ const outputRoot = this.getOutputRoot();
62
+ const moduleRoot = SerializationPathUtil_1.default.dirname(SerializationPathUtil_1.default.dirname(outputRoot));
63
+ try {
64
+ return SerializationPathUtil_1.default.relative(moduleRoot, absolutePath).replaceAll(SerializationPathUtil_1.default.sep, '/');
65
+ }
66
+ catch (error) {
67
+ return absolutePath;
68
+ }
69
+ }
70
+ isValidOutputPath(outputPath) {
71
+ try {
72
+ const outputRoot = this.getOutputRoot();
73
+ const resolvedOutputPath = SerializationPathUtil_1.default.pathResolve(outputPath);
74
+ const resolvedOutputRoot = SerializationPathUtil_1.default.pathResolve(outputRoot);
75
+ return resolvedOutputPath.startsWith(resolvedOutputRoot);
76
+ }
77
+ catch (error) {
78
+ return false;
79
+ }
80
+ }
81
+ ensureOutputDirectory(outputPath) {
82
+ SerializationPathUtil_1.default.ensureDirSync(SerializationPathUtil_1.default.dirname(outputPath));
83
+ }
84
+ calculateSourceRootToModuleRoot(absolutePath) {
85
+ const normalizedAbsolutePath = absolutePath.replaceAll(SerializationPathUtil_1.default.sep, '/');
86
+ const sourceRoots = this.getAbsoluteSourceRoots().map(root => root.replaceAll(SerializationPathUtil_1.default.sep, '/').replace(/\/$/, '') + '/');
87
+ const sourceRoot = sourceRoots.find(root => normalizedAbsolutePath.startsWith(root));
88
+ if (!sourceRoot) {
89
+ throw new CustomError_1.CustomError(`无法找到匹配的源根路径: ${normalizedAbsolutePath}。可用的源根路径: ${sourceRoots.join(', ')}`, CustomError_1.ErrorCodes.ROOT_DIRECTROY_NOT_FOUND);
90
+ }
91
+ const sourceRootForRelative = sourceRoot.replace(/\/$/, '');
92
+ const absolutePathForRelative = normalizedAbsolutePath.replace(/\/$/, '');
93
+ return SerializationPathUtil_1.default.relative(sourceRootForRelative, absolutePathForRelative).replaceAll(SerializationPathUtil_1.default.sep, '/');
94
+ }
95
+ get targetSourceRoots() {
96
+ if (!this.targetOpt.source?.sourceRoots) {
97
+ return [this.DEFAULT_SOURCE_ROOT];
98
+ }
99
+ if (!this.targetOpt.source.sourceRoots.includes(this.DEFAULT_SOURCE_ROOT)) {
100
+ return [this.DEFAULT_SOURCE_ROOT, ...this.targetOpt.source.sourceRoots];
101
+ }
102
+ return this.targetOpt.source.sourceRoots;
103
+ }
104
+ getAbsoluteSourceRoots() {
105
+ const moduleRoot = this.context.moduleContext.getModulePath();
106
+ return this.targetSourceRoots.map((sourceRoot) => {
107
+ const absolutePath = SerializationPathUtil_1.default.pathResolve(moduleRoot, sourceRoot);
108
+ const normalizedPath = SerializationPathUtil_1.default.normalize(absolutePath);
109
+ return normalizedPath.endsWith(SerializationPathUtil_1.default.sep) ? normalizedPath : normalizedPath + SerializationPathUtil_1.default.sep;
110
+ });
111
+ }
112
+ }
113
+ exports.TargetContextImpl = TargetContextImpl;
@@ -0,0 +1,14 @@
1
+ import { ITask, CodeGenerationEngine } from '../../core';
2
+ import { BuildProfileUpdaterImpl } from '../../core/import-rewrite/services/BuildProfileUpdater';
3
+ import { ITargetContext } from '../interfaces/ITargetContext';
4
+ export declare abstract class BaseTask implements ITask {
5
+ abstract readonly taskName: string;
6
+ abstract readonly dependencies: string[];
7
+ abstract readonly postDependencies: string[];
8
+ protected readonly engine: CodeGenerationEngine;
9
+ protected readonly buildProfileUpdater: BuildProfileUpdaterImpl;
10
+ protected getScanFiles(context: ITargetContext): string[];
11
+ protected processWithLogging(scanFiles: string[], context: ITargetContext, taskName: string): import("../../core").IGenerationResult;
12
+ protected executeImportRewrite(context: ITargetContext, taskName: string): void;
13
+ abstract run(context: ITargetContext): void | Promise<void>;
14
+ }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.BaseTask = void 0;
7
+ const core_1 = require("../../core");
8
+ const BuildProfileUpdater_1 = require("../../core/import-rewrite/services/BuildProfileUpdater");
9
+ const TaskConstants_1 = require("../constants/TaskConstants");
10
+ const SerializationPathUtil_1 = __importDefault(require("../../core/utils/SerializationPathUtil"));
11
+ class BaseTask {
12
+ constructor() {
13
+ this.engine = new core_1.CodeGenerationEngine();
14
+ this.buildProfileUpdater = new BuildProfileUpdater_1.BuildProfileUpdaterImpl();
15
+ }
16
+ getScanFiles(context) {
17
+ const scanFiles = context.getCurrentTargetScanFiles();
18
+ if (!scanFiles || scanFiles.length === 0) {
19
+ core_1.Logger.debug('There are no source files to be processed');
20
+ return [];
21
+ }
22
+ return scanFiles;
23
+ }
24
+ processWithLogging(scanFiles, context, taskName) {
25
+ core_1.Logger.debug(`Start executing ${taskName} and process ${scanFiles.length} files`);
26
+ this.buildProfileUpdater.updateSourceRoots(context);
27
+ const processingResult = this.engine.processFilesUnified(scanFiles, context);
28
+ const totalGenerated = processingResult.generatedFiles.length;
29
+ const totalSkipped = processingResult.skippedFiles.length;
30
+ core_1.Logger.info(`${taskName} completed: generate ${totalGenerated} files and skip ${totalSkipped} files`);
31
+ if (totalGenerated > 0 && taskName === TaskConstants_1.TaskConstants.SYNC_TASK) {
32
+ const config = context.context.buildProfileOpt;
33
+ const buildProfilePath = SerializationPathUtil_1.default.join(context.getModulePath(), 'build-profile.json5');
34
+ SerializationPathUtil_1.default.writeFileSync(buildProfilePath, JSON.stringify(config, null, 2));
35
+ }
36
+ this.executeImportRewrite(context, taskName);
37
+ return processingResult;
38
+ }
39
+ executeImportRewrite(context, taskName) {
40
+ const configManager = core_1.ConfigManager.getInstance();
41
+ core_1.Logger.debug(`Start to execute the import rewrite - ${taskName}`);
42
+ const generatedFileInfos = this.engine.collectGeneratedFileInfos(context);
43
+ if (generatedFileInfos.length === 0) {
44
+ core_1.Logger.debug('No file was generated. Skip the import and rewrite');
45
+ return;
46
+ }
47
+ const importRewriteConfig = configManager.getImportRewriteConfig();
48
+ const importRewriteService = new core_1.ImportRewriteServiceImpl();
49
+ importRewriteService.rewriteImports(context, generatedFileInfos, importRewriteConfig);
50
+ core_1.Logger.info(`Import and rewrite completed - ${taskName}`);
51
+ }
52
+ }
53
+ exports.BaseTask = BaseTask;
@@ -0,0 +1,8 @@
1
+ import { ITask } from '../../core';
2
+ import { ITargetContext } from '../interfaces/ITargetContext';
3
+ export declare class CleanTask implements ITask {
4
+ readonly taskName: string;
5
+ readonly dependencies: string[];
6
+ readonly postDependencies: string[];
7
+ run(context: ITargetContext): void;
8
+ }
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.CleanTask = void 0;
7
+ const core_1 = require("../../core");
8
+ const TaskConstants_1 = require("../constants/TaskConstants");
9
+ const SerializationPathUtil_1 = __importDefault(require("../../core/utils/SerializationPathUtil"));
10
+ class CleanTask {
11
+ constructor() {
12
+ this.taskName = TaskConstants_1.TaskConstants.CLEAN_TASK;
13
+ this.dependencies = [];
14
+ this.postDependencies = [];
15
+ }
16
+ run(context) {
17
+ core_1.Logger.info('Start the code cleaning task');
18
+ const generatedRootDir = context.getOutputRoot();
19
+ if (SerializationPathUtil_1.default.exist(generatedRootDir)) {
20
+ core_1.Logger.info(`Clean up the generated directory: ${generatedRootDir}`);
21
+ SerializationPathUtil_1.default.rmSync(generatedRootDir, { recursive: true, force: true });
22
+ }
23
+ }
24
+ }
25
+ exports.CleanTask = CleanTask;
@@ -0,0 +1,8 @@
1
+ import { ITargetContext } from '../interfaces/ITargetContext';
2
+ import { BaseTask } from './BaseTask';
3
+ export declare class CodeProcessingTask extends BaseTask {
4
+ readonly taskName: string;
5
+ readonly dependencies: string[];
6
+ readonly postDependencies: string[];
7
+ run(context: ITargetContext): void;
8
+ }
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CodeProcessingTask = void 0;
4
+ const core_1 = require("../../core");
5
+ const TaskConstants_1 = require("../constants/TaskConstants");
6
+ const BaseTask_1 = require("./BaseTask");
7
+ class CodeProcessingTask extends BaseTask_1.BaseTask {
8
+ constructor() {
9
+ super(...arguments);
10
+ this.taskName = TaskConstants_1.TaskConstants.CODE_PROCESSING_TASK;
11
+ this.dependencies = [];
12
+ this.postDependencies = [TaskConstants_1.TaskConstants.PRE_BUILD_TASK];
13
+ }
14
+ run(context) {
15
+ const scanFiles = this.getScanFiles(context);
16
+ if (scanFiles.length === 0) {
17
+ return;
18
+ }
19
+ const processingResult = this.processWithLogging(scanFiles, context, TaskConstants_1.TaskConstants.CODE_PROCESSING_TASK);
20
+ const serializableClassCount = processingResult.generatedFiles.reduce((sum, file) => {
21
+ return sum + (file.serializerCount || 0);
22
+ }, 0);
23
+ core_1.Logger.info(`Discovered @Serializable classes: ${serializableClassCount}`);
24
+ }
25
+ }
26
+ exports.CodeProcessingTask = CodeProcessingTask;
@@ -0,0 +1,8 @@
1
+ import { ITargetContext } from '../interfaces/ITargetContext';
2
+ import { BaseTask } from './BaseTask';
3
+ export declare class SyncTask extends BaseTask {
4
+ readonly taskName: string;
5
+ readonly dependencies: string[];
6
+ readonly postDependencies: string[];
7
+ run(context: ITargetContext): void;
8
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SyncTask = void 0;
4
+ const TaskConstants_1 = require("../constants/TaskConstants");
5
+ const BaseTask_1 = require("./BaseTask");
6
+ class SyncTask extends BaseTask_1.BaseTask {
7
+ constructor() {
8
+ super(...arguments);
9
+ this.taskName = TaskConstants_1.TaskConstants.SYNC_TASK;
10
+ this.dependencies = [];
11
+ this.postDependencies = [];
12
+ }
13
+ run(context) {
14
+ const scanFiles = this.getScanFiles(context);
15
+ if (scanFiles.length === 0) {
16
+ return;
17
+ }
18
+ this.processWithLogging(scanFiles, context, TaskConstants_1.TaskConstants.SYNC_TASK);
19
+ }
20
+ }
21
+ exports.SyncTask = SyncTask;
@@ -0,0 +1,11 @@
1
+ import { ITargetContext } from '../interfaces/ITargetContext';
2
+ import { BaseTask } from './BaseTask';
3
+ export declare class WatchTask extends BaseTask {
4
+ readonly taskName: string;
5
+ readonly dependencies: string[];
6
+ readonly postDependencies: string[];
7
+ private watcher?;
8
+ run(context: ITargetContext): Promise<void>;
9
+ private setupWatchEvents;
10
+ private generateCode;
11
+ }
@@ -0,0 +1,102 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.WatchTask = void 0;
37
+ const chokidar = __importStar(require("chokidar"));
38
+ const core_1 = require("../../core");
39
+ const TaskConstants_1 = require("../constants/TaskConstants");
40
+ const BaseTask_1 = require("./BaseTask");
41
+ class WatchTask extends BaseTask_1.BaseTask {
42
+ constructor() {
43
+ super(...arguments);
44
+ this.taskName = TaskConstants_1.TaskConstants.WATCH_TASK;
45
+ this.dependencies = [];
46
+ this.postDependencies = [];
47
+ }
48
+ async run(context) {
49
+ core_1.Logger.info('Start the file listening task');
50
+ const watchPaths = this.getScanFiles(context);
51
+ if (watchPaths.length === 0) {
52
+ core_1.Logger.info('There are no files to listen to');
53
+ return;
54
+ }
55
+ core_1.Logger.info(`Start listening for file changes: ${watchPaths.join(', ')}`);
56
+ this.watcher = chokidar.watch(watchPaths, {
57
+ persistent: true,
58
+ ignoreInitial: false
59
+ });
60
+ this.setupWatchEvents(context);
61
+ core_1.Logger.info('File monitoring has been started. Press Ctrl+C to stop the monitoring');
62
+ return new Promise(_resolve => {
63
+ });
64
+ }
65
+ setupWatchEvents(context) {
66
+ if (!this.watcher)
67
+ return;
68
+ let debounceTimer = null;
69
+ const debouncedGenerate = (filePath) => {
70
+ if (debounceTimer) {
71
+ clearTimeout(debounceTimer);
72
+ }
73
+ debounceTimer = setTimeout(() => {
74
+ this.generateCode(filePath, context);
75
+ }, 500);
76
+ };
77
+ this.watcher
78
+ .on('add', (filePath) => {
79
+ core_1.Logger.info(`New file detected: ${filePath}`);
80
+ debouncedGenerate(filePath);
81
+ })
82
+ .on('change', (filePath) => {
83
+ core_1.Logger.info(`File change detected: ${filePath}`);
84
+ debouncedGenerate(filePath);
85
+ })
86
+ .on('unlink', (filePath) => {
87
+ core_1.Logger.info(`File deletion detected: ${filePath}`);
88
+ debouncedGenerate(filePath);
89
+ })
90
+ .on('error', (error) => {
91
+ core_1.Logger.error(`File monitoring error: ${error}`);
92
+ })
93
+ .on('ready', () => {
94
+ core_1.Logger.info('The initial file scan is complete. Start listening for file changes...');
95
+ });
96
+ }
97
+ generateCode(scanFile, context) {
98
+ const processingResult = this.engine.processFileUnified(scanFile, context);
99
+ core_1.Logger.info(`The code generation is complete, and the output path is ${processingResult?.outputPath}`);
100
+ }
101
+ }
102
+ exports.WatchTask = WatchTask;
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@hadss/turbo-trans-json-plugin",
3
+ "version": "1.0.0-rc.0",
4
+ "description": "TurboTransJSON Compiler Plugin for automatic serialization code generation",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "build": "tsc",
8
+ "dev": "tsc --watch",
9
+ "clean": "rm -rf dist",
10
+ "package": "rm -rf ./dist && npm i && npx tsc && npm pack",
11
+ "release": "rm -rf ./dist && npm i && npx tsc && npm publish --access public"
12
+ },
13
+ "keywords": [
14
+ "turbo-trans",
15
+ "serialization",
16
+ "json",
17
+ "harmonyos",
18
+ "openharmony"
19
+ ],
20
+ "author": "DTSE",
21
+ "license": "Apache-2.0",
22
+ "dependencies": {
23
+ "chokidar": "^4.0.3",
24
+ "ts-morph": "^26.0.0",
25
+ "handlebars": "^4.7.8"
26
+ },
27
+ "devDependencies": {
28
+ "@ohos/hvigor": "^5.17.6",
29
+ "@ohos/hvigor-ohos-plugin": "^5.17.6",
30
+ "@types/chokidar": "^2.1.7",
31
+ "@types/node": "^24.2.0",
32
+ "@types/handlebars": "^4.1.0",
33
+ "ts-node": "^10.9.2",
34
+ "typescript": "^5.9.2"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public",
38
+ "registry": "https://registry.npmjs.org"
39
+ },
40
+ "homepage": "https://gitcode.com/openharmony-sig/turbo_trans",
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "https://gitcode.com/openharmony-sig/turbo_trans.git",
44
+ "directory": "TurboTransJSONPlugin"
45
+ }
46
+ }