@react-native-oh/react-native-harmony-cli 0.0.25 → 0.0.26-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 (96) hide show
  1. package/.prettierrc +6 -6
  2. package/dist/assetResolver.js +1 -23
  3. package/dist/codegen/core/CodeGenerator.js +2 -0
  4. package/dist/codegen/core/UberSchema.js +35 -9
  5. package/dist/codegen/core/index.js +1 -1
  6. package/dist/codegen/generators/{ComponentCodeGenerator.js → ComponentCodeGeneratorArkTS.js} +12 -4
  7. package/dist/codegen/generators/ComponentCodeGeneratorCAPI.js +79 -0
  8. package/dist/codegen/generators/GlueCodeGenerator.js +60 -0
  9. package/dist/codegen/generators/NativeModuleCodeGenerator.js +14 -6
  10. package/dist/codegen/generators/UberGeneratorV1.js +49 -0
  11. package/dist/codegen/generators/UberGeneratorV2.js +60 -0
  12. package/dist/codegen/generators/index.js +5 -2
  13. package/dist/codegen/templates/BaseComponentInstanceHTemplate.js +103 -0
  14. package/dist/codegen/templates/ComponentDescriptorHTemplate.js +42 -36
  15. package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +45 -36
  16. package/dist/codegen/templates/ComponentUtilsTSTemplate.js +135 -127
  17. package/dist/codegen/templates/GeneratedPackageHTemplate.js +101 -0
  18. package/dist/codegen/templates/IndexTSTemplate.js +23 -5
  19. package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +88 -82
  20. package/dist/codegen/templates/TurboModuleCppTemplate.js +26 -18
  21. package/dist/codegen/templates/TurboModuleHTemplate.js +27 -17
  22. package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +37 -27
  23. package/dist/codegen/templates/index.js +1 -0
  24. package/dist/commands/codegen-harmony.js +99 -14
  25. package/dist/commands/verify-package-harmony.js +7 -12
  26. package/dist/core/AbsolutePath.js +6 -0
  27. package/dist/core/CodegenConfig.js +8 -4
  28. package/dist/core/PackageJSON.js +8 -3
  29. package/dist/core/ProjectDependenciesManager.js +2 -2
  30. package/dist/package-verifier/verify-package.js +5 -4
  31. package/jest.config.js +5 -5
  32. package/package.json +57 -56
  33. package/react-native.config.js +10 -10
  34. package/src/assetResolver.ts +21 -44
  35. package/src/codegen/core/CodeGenerator.ts +9 -0
  36. package/src/codegen/core/CodegenError.ts +3 -3
  37. package/src/codegen/core/EventUtils.ts +18 -18
  38. package/src/codegen/core/TypeAnnotationToTS.test.ts +75 -75
  39. package/src/codegen/core/TypeAnnotationToTS.ts +188 -188
  40. package/src/codegen/core/UberSchema.ts +142 -99
  41. package/src/codegen/core/index.ts +5 -5
  42. package/src/codegen/generators/{ComponentCodeGenerator.ts → ComponentCodeGeneratorArkTS.ts} +112 -98
  43. package/src/codegen/generators/ComponentCodeGeneratorCAPI.ts +114 -0
  44. package/src/codegen/generators/GlueCodeGenerator.ts +109 -0
  45. package/src/codegen/generators/NativeModuleCodeGenerator.ts +115 -95
  46. package/src/codegen/generators/UberGeneratorV1.ts +64 -0
  47. package/src/codegen/generators/UberGeneratorV2.ts +91 -0
  48. package/src/codegen/generators/index.ts +6 -3
  49. package/src/codegen/index.ts +2 -2
  50. package/src/codegen/templates/BaseComponentInstanceHTemplate.ts +116 -0
  51. package/src/codegen/templates/ComponentDescriptorHTemplate.ts +55 -49
  52. package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +93 -85
  53. package/src/codegen/templates/ComponentUtilsTSTemplate.ts +210 -202
  54. package/src/codegen/templates/GeneratedPackageHTemplate.ts +107 -0
  55. package/src/codegen/templates/IndexTSTemplate.ts +53 -33
  56. package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +130 -124
  57. package/src/codegen/templates/TurboModuleCppTemplate.ts +54 -47
  58. package/src/codegen/templates/TurboModuleHTemplate.ts +35 -26
  59. package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +90 -81
  60. package/src/codegen/templates/index.ts +9 -8
  61. package/src/commands/bundle-harmony.ts +203 -203
  62. package/src/commands/codegen-harmony.ts +272 -143
  63. package/src/commands/create-metro-host-constants.ts +48 -48
  64. package/src/commands/index.ts +6 -6
  65. package/src/commands/pack-harmony.ts +121 -121
  66. package/src/commands/unpack-harmony.ts +231 -231
  67. package/src/commands/verify-package-harmony.ts +106 -106
  68. package/src/core/AbsolutePath.ts +50 -43
  69. package/src/core/CodegenConfig.ts +62 -54
  70. package/src/core/DescriptiveError.ts +61 -61
  71. package/src/core/OHPackageJSON5.ts +23 -23
  72. package/src/core/PackageJSON.ts +75 -63
  73. package/src/core/ProjectDependenciesManager.ts +77 -77
  74. package/src/core/ValidationError.ts +1 -1
  75. package/src/core/ValueObject.ts +2 -2
  76. package/src/core/fs.ts +31 -31
  77. package/src/core/index.ts +9 -9
  78. package/src/index.ts +20 -20
  79. package/src/io/Logger.ts +68 -68
  80. package/src/io/TarGz.ts +26 -26
  81. package/src/io/fsUtils.ts +24 -24
  82. package/src/io/index.ts +3 -3
  83. package/src/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.ts +28 -28
  84. package/src/package-verifier/checks/HarmonyDirExistsCheck.ts +24 -24
  85. package/src/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.ts +18 -18
  86. package/src/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.ts +47 -47
  87. package/src/package-verifier/checks/index.ts +4 -4
  88. package/src/package-verifier/index.ts +2 -2
  89. package/src/package-verifier/types.ts +12 -12
  90. package/src/package-verifier/verify-package.ts +117 -117
  91. package/tsconfig.build.json +6 -6
  92. package/tsconfig.json +2 -2
  93. package/dist/codegen/core/Codegen.js +0 -35
  94. package/dist/codegen/generators/PackageCodeGenerator.js +0 -52
  95. package/src/codegen/core/Codegen.ts +0 -44
  96. package/src/codegen/generators/PackageCodeGenerator.ts +0 -73
package/.prettierrc CHANGED
@@ -1,6 +1,6 @@
1
- {
2
- "trailingComma": "es5",
3
- "tabWidth": 2,
4
- "semi": true,
5
- "singleQuote": true
6
- }
1
+ {
2
+ "trailingComma": "es5",
3
+ "tabWidth": 2,
4
+ "semi": true,
5
+ "singleQuote": true
6
+ }
@@ -1,26 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.getAssetDestRelativePath = void 0;
4
- /*
5
- cyrb53 (c) 2018 bryc (github.com/bryc)
6
- License: Public domain. Attribution appreciated.
7
- A fast and simple 53-bit string hash function with decent collision resistance.
8
- Largely inspired by MurmurHash2/3, but with a focus on speed/simplicity.
9
- */
10
- function cyrb53(str, seed = 0) {
11
- let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
12
- for (let i = 0, ch; i < str.length; i++) {
13
- ch = str.charCodeAt(i);
14
- h1 = Math.imul(h1 ^ ch, 2654435761);
15
- h2 = Math.imul(h2 ^ ch, 1597334677);
16
- }
17
- h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
18
- h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
19
- h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
20
- h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
21
- return 4294967296 * (2097151 & h2) + (h1 >>> 0);
22
- }
23
- ;
24
4
  function getAssetDestRelativePath(asset) {
25
5
  const fileName = getResourceIdentifier(asset);
26
6
  return `${fileName}.${asset.type}`;
@@ -28,9 +8,7 @@ function getAssetDestRelativePath(asset) {
28
8
  exports.getAssetDestRelativePath = getAssetDestRelativePath;
29
9
  function getResourceIdentifier(asset) {
30
10
  const folderPath = getBasePath(asset);
31
- const fullAssetPath = `${folderPath}/${asset.name}.${asset.type}`.toLowerCase();
32
- const assetPathHash = cyrb53(fullAssetPath);
33
- return assetPathHash.toString(16);
11
+ return `${folderPath}/${asset.name}`.slice('assets/'.length);
34
12
  }
35
13
  function getBasePath(asset) {
36
14
  let basePath = asset.httpServerLocation;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -20,23 +20,49 @@ const CodegenError_1 = require("./CodegenError");
20
20
  function createRawUberSchemaFromSpecFilePaths(projectSourceFilePaths) {
21
21
  return (0, combine_js_to_schema_js_1.default)(projectSourceFilePaths.map((p) => p.getValue()));
22
22
  }
23
+ /**
24
+ * Contains component and turbo module (NativeModule) schemas. The "Uber" word is used here to highlight that SpecSchemas don't extend this class.
25
+ */
23
26
  class UberSchema {
24
- static fromProjectRootPath(projectRootPath) {
27
+ static fromCodegenConfig(codegenConfig) {
28
+ try {
29
+ return new UberSchema(createRawUberSchemaFromSpecFilePaths(codegenConfig.getSpecFilePaths()));
30
+ }
31
+ catch (err) {
32
+ if (err instanceof Error) {
33
+ throw new CodegenError_1.CodegenError({
34
+ whatHappened: "Couldn't create the schema",
35
+ whatCanUserDo: {
36
+ default: [
37
+ `There's probably at least one spec file defined in your project or in a third-party package that breaks some code generation restrictions. Please check the message below. If it's ambiguous, debug the problem with divide and conquer strategy.\n\n${err.message}`,
38
+ ],
39
+ },
40
+ });
41
+ }
42
+ throw err;
43
+ }
44
+ }
45
+ static fromProject(projectRootPath, onShouldAcceptCodegenConfig) {
25
46
  return __awaiter(this, void 0, void 0, function* () {
47
+ const onShouldAcceptCodegenConfig_ = onShouldAcceptCodegenConfig !== null && onShouldAcceptCodegenConfig !== void 0 ? onShouldAcceptCodegenConfig : ((x) => true);
26
48
  const packageJSON = core_1.PackageJSON.fromProjectRootPath(projectRootPath, projectRootPath);
27
- const codegenConfigs = [];
28
- const appCodegenConfig = packageJSON.maybeCreateCodegenConfig();
29
- if (appCodegenConfig) {
30
- codegenConfigs.push(appCodegenConfig);
49
+ const acceptedCodegenConfigs = [];
50
+ const appCodegenConfigs = packageJSON.getCodegenConfigs();
51
+ for (const codegenConfig of appCodegenConfigs) {
52
+ if (onShouldAcceptCodegenConfig_(codegenConfig.getVersion(), packageJSON.name)) {
53
+ acceptedCodegenConfigs.push(codegenConfig);
54
+ }
31
55
  }
32
56
  yield new core_1.ProjectDependenciesManager(projectRootPath).forEachAsync((dependency) => {
33
- const codegenConfig = dependency.maybeCreateCodegenConfig();
34
- if (codegenConfig) {
35
- codegenConfigs.push(codegenConfig);
57
+ const codegenConfigs = dependency.getCodegenConfigs();
58
+ for (const codegenConfig of codegenConfigs) {
59
+ if (onShouldAcceptCodegenConfig_(codegenConfig.getVersion(), dependency.readPackageJSON().name)) {
60
+ acceptedCodegenConfigs.push(codegenConfig);
61
+ }
36
62
  }
37
63
  });
38
64
  try {
39
- return new UberSchema(createRawUberSchemaFromSpecFilePaths(codegenConfigs.flatMap((codegenConfig) => codegenConfig.getSpecFilePaths())));
65
+ return new UberSchema(createRawUberSchemaFromSpecFilePaths(acceptedCodegenConfigs.flatMap((codegenConfig) => codegenConfig.getSpecFilePaths())));
40
66
  }
41
67
  catch (err) {
42
68
  if (err instanceof Error) {
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./Codegen"), exports);
17
+ __exportStar(require("./CodeGenerator"), exports);
18
18
  __exportStar(require("./UberSchema"), exports);
19
19
  __exportStar(require("./CodegenError"), exports);
20
20
  __exportStar(require("./EventUtils"), exports);
@@ -1,12 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ComponentCodeGenerator = void 0;
3
+ exports.ComponentCodeGeneratorArkTS = void 0;
4
4
  const core_1 = require("../core");
5
5
  const templates_1 = require("../templates");
6
- class ComponentCodeGenerator {
6
+ class ComponentCodeGeneratorArkTS {
7
7
  constructor(cppOutputPath, tsOutputPath) {
8
8
  this.cppOutputPath = cppOutputPath;
9
9
  this.tsOutputPath = tsOutputPath;
10
+ this.glueCodeDataItems = [];
11
+ }
12
+ getGlueCodeData() {
13
+ return this.glueCodeDataItems;
10
14
  }
11
15
  generate(schema) {
12
16
  if (schema.type !== 'Component') {
@@ -19,7 +23,7 @@ class ComponentCodeGenerator {
19
23
  const result = new Map();
20
24
  Object.entries(schema.components).map(([name, shape]) => {
21
25
  const componentDescriptorHTemplate = new templates_1.ComponentDescriptorHTemplate(name);
22
- const componentJSIBinderHTemplate = new templates_1.ComponentJSIBinderHTemplate(name);
26
+ const componentJSIBinderHTemplate = new templates_1.ComponentJSIBinderHTemplate(name, 1);
23
27
  const componentUtilsTSTemplate = new templates_1.ComponentUtilsTSTemplate(name);
24
28
  shape.props.forEach((prop) => {
25
29
  componentJSIBinderHTemplate.addProp({
@@ -59,8 +63,12 @@ class ComponentCodeGenerator {
59
63
  result.set(this.cppOutputPath.copyWithNewSegment(`${name}ComponentDescriptor.h`), componentDescriptorHTemplate.build());
60
64
  result.set(this.cppOutputPath.copyWithNewSegment(`${name}JSIBinder.h`), componentJSIBinderHTemplate.build());
61
65
  result.set(this.tsOutputPath.copyWithNewSegment(`${name}.ts`), componentUtilsTSTemplate.build());
66
+ this.glueCodeDataItems.push({
67
+ name,
68
+ eventNames: shape.events.map((e) => (0, core_1.createEventNameFromEventHandlerName)(e.name)),
69
+ });
62
70
  });
63
71
  return result;
64
72
  }
65
73
  }
66
- exports.ComponentCodeGenerator = ComponentCodeGenerator;
74
+ exports.ComponentCodeGeneratorArkTS = ComponentCodeGeneratorArkTS;
@@ -0,0 +1,79 @@
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.ComponentCodeGeneratorCAPI = void 0;
7
+ const RNCodegen_1 = __importDefault(require("@react-native/codegen/lib/generators/RNCodegen"));
8
+ const templates_1 = require("../templates");
9
+ class ComponentCodeGeneratorCAPI {
10
+ constructor(
11
+ /// "shared" — platform independent code that can be shared between platforms
12
+ onGetSharedOutputPath, onGetRNOHOutputPath) {
13
+ this.onGetSharedOutputPath = onGetSharedOutputPath;
14
+ this.onGetRNOHOutputPath = onGetRNOHOutputPath;
15
+ this.glueCodeComponentsData = [];
16
+ }
17
+ getGlueCodeData() {
18
+ return this.glueCodeComponentsData;
19
+ }
20
+ generate({ name: libraryName, uberSchema, }) {
21
+ const fileContentByPath = new Map();
22
+ const generatorFns = [
23
+ RNCodegen_1.default.allGenerators.generatePropsH,
24
+ RNCodegen_1.default.allGenerators.generatePropsCpp,
25
+ RNCodegen_1.default.allGenerators.generateEventEmitterH,
26
+ RNCodegen_1.default.allGenerators.generateEventEmitterCpp,
27
+ RNCodegen_1.default.allGenerators.generateComponentDescriptorH,
28
+ RNCodegen_1.default.allGenerators.generateStateH,
29
+ RNCodegen_1.default.allGenerators.generateStateCpp,
30
+ RNCodegen_1.default.allGenerators.generateShadowNodeH,
31
+ RNCodegen_1.default.allGenerators.generateShadowNodeCpp,
32
+ ];
33
+ generatorFns.forEach((generate) => {
34
+ const filesOutput = generate(libraryName, uberSchema.getValue(), 'UNUSED_packageName', true);
35
+ filesOutput.forEach((fileContent, filename) => {
36
+ fileContentByPath.set(this.onGetSharedOutputPath(filename), fileContent);
37
+ });
38
+ });
39
+ for (const [filename, schema] of uberSchema
40
+ .getSpecSchemaByFilenameMap()
41
+ .entries()) {
42
+ if (schema.type === 'Component') {
43
+ Object.entries(schema.components).map(([componentName, shape]) => {
44
+ const componentJSIBinderHTemplate = new templates_1.ComponentJSIBinderHTemplate(componentName, 2);
45
+ const componentInstanceSpecHTemplate = new templates_1.BaseComponentInstanceHTemplate(componentName, libraryName);
46
+ shape.props.forEach((prop) => {
47
+ componentJSIBinderHTemplate.addProp({
48
+ name: prop.name,
49
+ isObject: prop.typeAnnotation.type === 'ObjectTypeAnnotation',
50
+ });
51
+ });
52
+ shape.events.forEach((event) => {
53
+ componentJSIBinderHTemplate.addEvent({
54
+ type: event.bubblingType,
55
+ eventHandlerName: event.name,
56
+ });
57
+ });
58
+ shape.commands.forEach((command) => {
59
+ componentInstanceSpecHTemplate.addCommand({
60
+ name: command.name,
61
+ args: command.typeAnnotation.params.map((param) => ({
62
+ name: param.name,
63
+ typeAnnotation: param.typeAnnotation,
64
+ })),
65
+ });
66
+ });
67
+ fileContentByPath.set(this.onGetRNOHOutputPath(`${componentName}JSIBinder.h`), componentJSIBinderHTemplate.build());
68
+ fileContentByPath.set(this.onGetRNOHOutputPath(`Base${componentName}ComponentInstance.h`), componentInstanceSpecHTemplate.build());
69
+ this.glueCodeComponentsData.push({
70
+ name: componentName,
71
+ libraryName,
72
+ });
73
+ });
74
+ }
75
+ }
76
+ return fileContentByPath;
77
+ }
78
+ }
79
+ exports.ComponentCodeGeneratorCAPI = ComponentCodeGeneratorCAPI;
@@ -0,0 +1,60 @@
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.GlueCodeGenerator = void 0;
7
+ const case_1 = __importDefault(require("case"));
8
+ const templates_1 = require("../templates");
9
+ const GeneratedPackageHTemplate_1 = require("../templates/GeneratedPackageHTemplate");
10
+ /**
11
+ * Generates code based on UberGenerators results. If UberGenerators produce "leaves" (concrete turbo module or component classes),
12
+ * this class generates "branches" (barrel files like index.ts) and "trunks" (package file or files).
13
+ */
14
+ class GlueCodeGenerator {
15
+ constructor(cppOutputPath, etsOutputPath) {
16
+ this.cppOutputPath = cppOutputPath;
17
+ this.etsOutputPath = etsOutputPath;
18
+ }
19
+ generate({ v1, v2 }) {
20
+ const fileContentByPath = new Map();
21
+ const generatedPackageH_v1 = new templates_1.RNOHGeneratedPackageHTemplate();
22
+ const components_indexTS = new templates_1.IndexTSTemplate();
23
+ const turboModules_indexTS = new templates_1.IndexTSTemplate();
24
+ const generated_indexETS = new templates_1.IndexTSTemplate();
25
+ generated_indexETS.addReexport({ from: './ts' });
26
+ v1.components.forEach(({ name, eventNames }) => {
27
+ components_indexTS.addReexport({ from: `./${name}` });
28
+ generatedPackageH_v1.addComponent({
29
+ name,
30
+ supportedEventNames: eventNames,
31
+ });
32
+ });
33
+ v1.turboModules.forEach((turboModule) => {
34
+ turboModules_indexTS.addReexport({ from: `./${turboModule.name}` });
35
+ generatedPackageH_v1.addTurboModule(turboModule);
36
+ });
37
+ v2.forEach(({ components, turboModules }, libraryName) => {
38
+ const basePackageClassName = `Base${case_1.default.pascal(libraryName)}Package`;
39
+ const generatedPackageH_v2 = new GeneratedPackageHTemplate_1.BasePackageHTemplate(basePackageClassName);
40
+ components.forEach((component) => {
41
+ generatedPackageH_v2.addComponent(component);
42
+ });
43
+ turboModules.forEach((turboModule) => {
44
+ turboModules_indexTS.addReexport({ from: `./${turboModule.name}` });
45
+ generatedPackageH_v2.addTurboModule(turboModule);
46
+ });
47
+ fileContentByPath.set(this.cppOutputPath.copyWithNewSegment(libraryName, 'RNOH', 'generated', `${basePackageClassName}.h`), generatedPackageH_v2.build());
48
+ });
49
+ fileContentByPath.set(this.cppOutputPath.copyWithNewSegment('RNOHGeneratedPackage.h'), generatedPackageH_v1.build());
50
+ fileContentByPath.set(this.etsOutputPath.copyWithNewSegment('ts.ts'), new templates_1.IndexTSTemplate()
51
+ .addReexport({ from: './components/ts', as: 'RNC' })
52
+ .addReexport({ from: './turboModules/ts', as: 'TM' })
53
+ .build());
54
+ fileContentByPath.set(this.etsOutputPath.copyWithNewSegment('components', 'ts.ts'), components_indexTS.build());
55
+ fileContentByPath.set(this.etsOutputPath.copyWithNewSegment('turboModules', 'ts.ts'), turboModules_indexTS.build());
56
+ fileContentByPath.set(this.etsOutputPath.copyWithNewSegment('index.ets'), generated_indexETS.build());
57
+ return fileContentByPath;
58
+ }
59
+ }
60
+ exports.GlueCodeGenerator = GlueCodeGenerator;
@@ -1,27 +1,34 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NativeModuleCodeGenerator = void 0;
4
- const CodegenError_1 = require("../core/CodegenError");
5
4
  const templates_1 = require("../templates");
6
5
  const core_1 = require("../core");
6
+ /**
7
+ * Generates boilerplate code for Turbo Modules.
8
+ */
7
9
  class NativeModuleCodeGenerator {
8
- constructor(cppOutputPath, etsOutputPath) {
10
+ constructor(cppOutputPath, etsOutputPath, generatorVersion) {
9
11
  this.cppOutputPath = cppOutputPath;
10
12
  this.etsOutputPath = etsOutputPath;
13
+ this.generatorVersion = generatorVersion;
14
+ this.glueCodeTurboModulesData = [];
15
+ }
16
+ getGlueCodeData() {
17
+ return this.glueCodeTurboModulesData;
11
18
  }
12
19
  generate(schema) {
13
20
  const result = new Map();
14
21
  if (schema.type !== 'NativeModule') {
15
- throw new CodegenError_1.CodegenError({
22
+ throw new core_1.CodegenError({
16
23
  whatHappened: `NativeModuleCodeGenerator can't generate code for module type: ${schema.type}`,
17
24
  unexpected: true,
18
25
  });
19
26
  }
20
27
  const typeAnnotationToTS = new core_1.TypeAnnotationToTS();
21
- const turboModuleInterfaceTS = new templates_1.TurboModuleInterfaceTS(schema.moduleName);
22
- const turboModuleH = new templates_1.TurboModuleHTemplate(schema.moduleName).build();
28
+ const turboModuleInterfaceTS = new templates_1.TurboModuleInterfaceTS(schema.moduleName, this.generatorVersion);
29
+ const turboModuleH = new templates_1.TurboModuleHTemplate(schema.moduleName, this.generatorVersion).build();
23
30
  result.set(this.cppOutputPath.copyWithNewSegment(`${schema.moduleName}.h`), turboModuleH);
24
- const turboModuleCpp = new templates_1.TurboModuleCppTemplate(schema.moduleName);
31
+ const turboModuleCpp = new templates_1.TurboModuleCppTemplate(schema.moduleName, this.generatorVersion);
25
32
  Object.entries(schema.aliasMap).map(([name, typeAnnotation]) => {
26
33
  turboModuleInterfaceTS.addAlias({
27
34
  name: name,
@@ -58,6 +65,7 @@ class NativeModuleCodeGenerator {
58
65
  });
59
66
  result.set(this.cppOutputPath.copyWithNewSegment(`${schema.moduleName}.cpp`), turboModuleCpp.build());
60
67
  result.set(this.etsOutputPath.copyWithNewSegment(`${schema.moduleName}.ts`), turboModuleInterfaceTS.build());
68
+ this.glueCodeTurboModulesData.push({ name: schema.moduleName });
61
69
  return result;
62
70
  }
63
71
  }
@@ -0,0 +1,49 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UberGeneratorV1 = void 0;
4
+ const core_1 = require("../core");
5
+ const NativeModuleCodeGenerator_1 = require("./NativeModuleCodeGenerator");
6
+ const ComponentCodeGeneratorArkTS_1 = require("./ComponentCodeGeneratorArkTS");
7
+ /**
8
+ * Generates code for libraries built on top of RNOH's ArkTS architecture.
9
+ */
10
+ class UberGeneratorV1 {
11
+ constructor(cppOutputPath, etsOutputPath) {
12
+ this.cppOutputPath = cppOutputPath;
13
+ this.etsOutputPath = etsOutputPath;
14
+ this.glueCodeData = { components: [], turboModules: [] };
15
+ }
16
+ getGlueCodeData() {
17
+ return this.glueCodeData;
18
+ }
19
+ generate(uberSchema) {
20
+ const fileContentByPath = new Map();
21
+ const componentCodeGeneratorArkTS = new ComponentCodeGeneratorArkTS_1.ComponentCodeGeneratorArkTS(this.cppOutputPath, this.etsOutputPath.copyWithNewSegment('components'));
22
+ const nativeModuleCodeGenerator = new NativeModuleCodeGenerator_1.NativeModuleCodeGenerator(this.cppOutputPath, this.etsOutputPath.copyWithNewSegment('turboModules'), 1);
23
+ const generatorBySpecType = {
24
+ Component: componentCodeGeneratorArkTS,
25
+ NativeModule: nativeModuleCodeGenerator,
26
+ };
27
+ for (const [_fileName, specSchema] of uberSchema
28
+ .getSpecSchemaByFilenameMap()
29
+ .entries()) {
30
+ if (!(specSchema.type in generatorBySpecType)) {
31
+ throw new core_1.CodegenError({
32
+ whatHappened: `Could not find a code generator for a specification of type '${specSchema.type}'`,
33
+ unexpected: true,
34
+ });
35
+ }
36
+ generatorBySpecType[specSchema.type]
37
+ .generate(specSchema)
38
+ .forEach((fileContent, filePath) => {
39
+ fileContentByPath.set(filePath, fileContent);
40
+ });
41
+ }
42
+ this.glueCodeData = {
43
+ components: componentCodeGeneratorArkTS.getGlueCodeData(),
44
+ turboModules: nativeModuleCodeGenerator.getGlueCodeData(),
45
+ };
46
+ return fileContentByPath;
47
+ }
48
+ }
49
+ exports.UberGeneratorV1 = UberGeneratorV1;
@@ -0,0 +1,60 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.UberGeneratorV2 = void 0;
4
+ const ComponentCodeGeneratorCAPI_1 = require("./ComponentCodeGeneratorCAPI");
5
+ const NativeModuleCodeGenerator_1 = require("./NativeModuleCodeGenerator");
6
+ /**
7
+ * Generates code for libraries built on top of RNOH's C-API architecture.
8
+ */
9
+ class UberGeneratorV2 {
10
+ constructor(cppOutputPath, etsOutputPath) {
11
+ this.cppOutputPath = cppOutputPath;
12
+ this.etsOutputPath = etsOutputPath;
13
+ this.glueCodeData = new Map();
14
+ }
15
+ getGlueCodeData() {
16
+ return this.glueCodeData;
17
+ }
18
+ generate(npmPackagesInfo) {
19
+ const fileContentByPath = new Map();
20
+ npmPackagesInfo.forEach(({ name, uberSchema }) => {
21
+ this.glueCodeData.set(name, { components: [], turboModules: [] });
22
+ const libraryGlueCodeData = this.glueCodeData.get(name);
23
+ const libraryCppOutput = this.cppOutputPath.copyWithNewSegment(name);
24
+ const componentCodeGeneratorCAPI = new ComponentCodeGeneratorCAPI_1.ComponentCodeGeneratorCAPI((filename) => {
25
+ return libraryCppOutput.copyWithNewSegment('react', 'renderer', 'components', name, filename);
26
+ }, (filename) => {
27
+ return libraryCppOutput.copyWithNewSegment('RNOH', 'generated', 'components', filename);
28
+ });
29
+ componentCodeGeneratorCAPI
30
+ .generate({ name, uberSchema })
31
+ .forEach((fileContent, path) => {
32
+ fileContentByPath.set(path, fileContent);
33
+ });
34
+ componentCodeGeneratorCAPI
35
+ .getGlueCodeData()
36
+ .forEach((componentGlueCodeData) => {
37
+ libraryGlueCodeData.components.push(componentGlueCodeData);
38
+ });
39
+ for (const [_fileName, specSchema] of uberSchema
40
+ .getSpecSchemaByFilenameMap()
41
+ .entries()) {
42
+ if (specSchema.type === 'NativeModule') {
43
+ const nativeModuleCodeGenerator = new NativeModuleCodeGenerator_1.NativeModuleCodeGenerator(libraryCppOutput.copyWithNewSegment('RNOH', 'generated', 'turbo_modules'), this.etsOutputPath.copyWithNewSegment('turboModules'), 2);
44
+ nativeModuleCodeGenerator
45
+ .generate(specSchema)
46
+ .forEach((fileContent, filePath) => {
47
+ fileContentByPath.set(filePath, fileContent);
48
+ });
49
+ nativeModuleCodeGenerator
50
+ .getGlueCodeData()
51
+ .forEach((turboModuleGlueCodeData) => {
52
+ libraryGlueCodeData.turboModules.push(turboModuleGlueCodeData);
53
+ });
54
+ }
55
+ }
56
+ });
57
+ return fileContentByPath;
58
+ }
59
+ }
60
+ exports.UberGeneratorV2 = UberGeneratorV2;
@@ -14,6 +14,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./ComponentCodeGenerator"), exports);
18
17
  __exportStar(require("./NativeModuleCodeGenerator"), exports);
19
- __exportStar(require("./PackageCodeGenerator"), exports);
18
+ __exportStar(require("./GlueCodeGenerator"), exports);
19
+ __exportStar(require("./ComponentCodeGeneratorArkTS"), exports);
20
+ __exportStar(require("./ComponentCodeGeneratorCAPI"), exports);
21
+ __exportStar(require("./UberGeneratorV1"), exports);
22
+ __exportStar(require("./UberGeneratorV2"), exports);
@@ -0,0 +1,103 @@
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.BaseComponentInstanceHTemplate = void 0;
7
+ const mustache_1 = __importDefault(require("mustache"));
8
+ const case_1 = __importDefault(require("case"));
9
+ const TEMPLATE = `
10
+ /**
11
+ * This code was generated by "react-native codegen-harmony"
12
+ *
13
+ * Do not edit this file as changes may cause incorrect behavior and will be
14
+ * lost once the code is regenerated.
15
+ *
16
+ * @generatorVersion: 2
17
+ */
18
+ #pragma once
19
+
20
+ #include "RNOH/CppComponentInstance.h"
21
+ #include <react/renderer/components/{{libraryName}}/ShadowNodes.h>
22
+
23
+ namespace rnoh {
24
+ class Base{{name}}ComponentInstance
25
+ : public CppComponentInstance<facebook::react::{{name}}ShadowNode> {
26
+ public:
27
+ using CppComponentInstance::CppComponentInstance;
28
+
29
+ void handleCommand(std::string const &commandName, folly::dynamic const &args) override {
30
+ {{#commands}}
31
+ if (commandName == "{{commandName}}") {
32
+ this->on{{CommandName}}Command({{argsFromDynamic}});
33
+ }
34
+ {{/commands}}
35
+ }
36
+
37
+ {{#commands}}
38
+ virtual void on{{CommandName}}Command({{argsDeclaration}}) = 0;
39
+ {{/commands}}
40
+ };
41
+ } // namespace rnoh
42
+ `;
43
+ class BaseComponentInstanceHTemplate {
44
+ constructor(componentName, libraryName) {
45
+ this.componentName = componentName;
46
+ this.libraryName = libraryName;
47
+ this.commands = [];
48
+ }
49
+ addCommand(command) {
50
+ this.commands.push(command);
51
+ }
52
+ build() {
53
+ return mustache_1.default.render(TEMPLATE, {
54
+ name: this.componentName,
55
+ libraryName: this.libraryName,
56
+ commands: this.commands.map(({ name, args }) => {
57
+ return {
58
+ commandName: name,
59
+ CommandName: case_1.default.pascal(name),
60
+ argsDeclaration: args
61
+ .map(({ name, typeAnnotation }) => `${cppTypeFromTypeAnnotation(typeAnnotation)} ${name}`)
62
+ .join(', '),
63
+ argsFromDynamic: args
64
+ .map(({ typeAnnotation }, index) => `args[${index}]${dynamicExtractorFromTypeAnnotation(typeAnnotation)}`)
65
+ .join(', '),
66
+ };
67
+ }),
68
+ });
69
+ }
70
+ }
71
+ exports.BaseComponentInstanceHTemplate = BaseComponentInstanceHTemplate;
72
+ function cppTypeFromTypeAnnotation(typeAnnotation) {
73
+ switch (typeAnnotation.type) {
74
+ case 'BooleanTypeAnnotation':
75
+ return 'bool';
76
+ case 'Int32TypeAnnotation':
77
+ return 'int';
78
+ case 'FloatTypeAnnotation':
79
+ return 'float';
80
+ case 'DoubleTypeAnnotation':
81
+ return 'double';
82
+ case 'StringTypeAnnotation':
83
+ return 'std::string';
84
+ default:
85
+ return 'folly::dynamic';
86
+ }
87
+ }
88
+ function dynamicExtractorFromTypeAnnotation(typeAnnotation) {
89
+ switch (typeAnnotation.type) {
90
+ case 'BooleanTypeAnnotation':
91
+ return '.asBool()';
92
+ case 'Int32TypeAnnotation':
93
+ return '.asInt()';
94
+ case 'FloatTypeAnnotation':
95
+ return '.asDouble()';
96
+ case 'DoubleTypeAnnotation':
97
+ return '.asDouble()';
98
+ case 'StringTypeAnnotation':
99
+ return '.asString()';
100
+ default:
101
+ return '';
102
+ }
103
+ }