@react-native-oh/react-native-harmony-cli 0.0.26 → 0.0.27

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 (86) hide show
  1. package/dist/assetResolver.js +19 -0
  2. package/dist/codegen/core/CodeGenerator.js +2 -0
  3. package/dist/codegen/core/CodegenError.js +7 -0
  4. package/dist/codegen/core/EventUtils.js +19 -0
  5. package/dist/codegen/core/TypeAnnotationToTS.js +132 -0
  6. package/dist/codegen/core/UberSchema.js +103 -0
  7. package/dist/codegen/core/index.js +21 -0
  8. package/dist/codegen/generators/AppBuildTimeGlueCodeGenerator.js +61 -0
  9. package/dist/codegen/generators/ArkTSComponentCodeGeneratorArkTS.js +76 -0
  10. package/dist/codegen/generators/ArkTSComponentCodeGeneratorCAPI.js +74 -0
  11. package/dist/codegen/generators/CppComponentCodeGenerator.js +56 -0
  12. package/dist/codegen/generators/LibGlueCodeGenerator.js +50 -0
  13. package/dist/codegen/generators/NativeModuleCodeGenerator.js +73 -0
  14. package/dist/codegen/generators/SharedComponentCodeGenerator.js +34 -0
  15. package/dist/codegen/generators/UberGeneratorV1.js +50 -0
  16. package/dist/codegen/generators/UberGeneratorV2.js +71 -0
  17. package/dist/codegen/generators/index.js +24 -0
  18. package/dist/codegen/index.js +18 -0
  19. package/dist/codegen/templates/BaseComponentInstanceHTemplate.js +105 -0
  20. package/dist/codegen/templates/BasePackageHTemplate.js +130 -0
  21. package/dist/codegen/templates/ComponentDescriptorHTemplate.js +62 -0
  22. package/dist/codegen/templates/ComponentJSIBinderHTemplate.js +85 -0
  23. package/dist/codegen/templates/ComponentUtilsTSTemplate.js +192 -0
  24. package/dist/codegen/templates/IndexTSTemplate.js +52 -0
  25. package/dist/codegen/templates/RNOHGeneratedPackageHTemplate.js +124 -0
  26. package/dist/codegen/templates/TurboModuleCppTemplate.js +53 -0
  27. package/dist/codegen/templates/TurboModuleHTemplate.js +42 -0
  28. package/dist/codegen/templates/TurboModuleSpecTSTemplate.js +75 -0
  29. package/dist/codegen/templates/index.js +25 -0
  30. package/dist/commands/bundle-harmony.js +179 -0
  31. package/dist/commands/codegen-harmony.js +201 -0
  32. package/dist/commands/codegen-lib-harmony.js +253 -0
  33. package/dist/commands/create-metro-host-constants.js +61 -0
  34. package/dist/commands/index.js +23 -0
  35. package/dist/commands/pack-harmony.js +100 -0
  36. package/dist/commands/unpack-harmony.js +184 -0
  37. package/dist/commands/verify-package-harmony.js +122 -0
  38. package/dist/core/AbsolutePath.js +47 -0
  39. package/dist/core/CodegenConfig.js +57 -0
  40. package/dist/core/DescriptiveError.js +49 -0
  41. package/dist/core/OHPackageJSON5.js +23 -0
  42. package/dist/core/PackageJSON.js +58 -0
  43. package/dist/core/ProjectDependenciesManager.js +70 -0
  44. package/dist/core/ValidationError.js +6 -0
  45. package/dist/core/ValueObject.js +2 -0
  46. package/dist/core/fs.js +36 -0
  47. package/dist/core/index.js +25 -0
  48. package/dist/index.js +15 -0
  49. package/dist/io/Logger.js +71 -0
  50. package/dist/io/TarGz.js +28 -0
  51. package/dist/io/fsUtils.js +23 -0
  52. package/dist/io/index.js +19 -0
  53. package/dist/package-verifier/checks/HarmonyDirContainsOnlyHarsCheck.js +38 -0
  54. package/dist/package-verifier/checks/HarmonyDirExistsCheck.js +32 -0
  55. package/dist/package-verifier/checks/NpmAnAndOhPackageVersionsAreEqual.js +26 -0
  56. package/dist/package-verifier/checks/OhPackageNameFollowsNamingConventionCheck.js +51 -0
  57. package/dist/package-verifier/checks/index.js +20 -0
  58. package/dist/package-verifier/index.js +18 -0
  59. package/dist/package-verifier/types.js +2 -0
  60. package/dist/package-verifier/verify-package.js +101 -0
  61. package/package.json +1 -1
  62. package/src/codegen/core/UberSchema.ts +4 -0
  63. package/src/codegen/generators/{GlueCodeGenerator.ts → AppBuildTimeGlueCodeGenerator.ts} +13 -9
  64. package/src/codegen/generators/{ComponentCodeGeneratorArkTS.ts → ArkTSComponentCodeGeneratorArkTS.ts} +11 -5
  65. package/src/codegen/generators/ArkTSComponentCodeGeneratorCAPI.ts +112 -0
  66. package/src/codegen/generators/{ComponentCodeGeneratorCAPI.ts → CppComponentCodeGenerator.ts} +14 -43
  67. package/src/codegen/generators/LibGlueCodeGenerator.ts +91 -0
  68. package/src/codegen/generators/NativeModuleCodeGenerator.ts +7 -5
  69. package/src/codegen/generators/SharedComponentCodeGenerator.ts +50 -0
  70. package/src/codegen/generators/UberGeneratorV1.ts +12 -8
  71. package/src/codegen/generators/UberGeneratorV2.ts +32 -16
  72. package/src/codegen/generators/index.ts +5 -3
  73. package/src/codegen/templates/BaseComponentInstanceHTemplate.ts +12 -7
  74. package/src/codegen/templates/{GeneratedPackageHTemplate.ts → BasePackageHTemplate.ts} +45 -12
  75. package/src/codegen/templates/ComponentDescriptorHTemplate.ts +2 -1
  76. package/src/codegen/templates/ComponentJSIBinderHTemplate.ts +8 -10
  77. package/src/codegen/templates/ComponentUtilsTSTemplate.ts +13 -8
  78. package/src/codegen/templates/IndexTSTemplate.ts +11 -9
  79. package/src/codegen/templates/RNOHGeneratedPackageHTemplate.ts +1 -0
  80. package/src/codegen/templates/TurboModuleCppTemplate.ts +9 -9
  81. package/src/codegen/templates/TurboModuleHTemplate.ts +9 -9
  82. package/src/codegen/templates/TurboModuleSpecTSTemplate.ts +13 -9
  83. package/src/commands/codegen-harmony.ts +24 -7
  84. package/src/commands/codegen-lib-harmony.ts +387 -0
  85. package/src/commands/index.ts +1 -0
  86. package/src/index.ts +2 -0
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
3
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
4
+ var m = o[Symbol.asyncIterator], i;
5
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
6
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
7
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
8
+ };
9
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
10
+ var __asyncDelegator = (this && this.__asyncDelegator) || function (o) {
11
+ var i, p;
12
+ return i = {}, verb("next"), verb("throw", function (e) { throw e; }), verb("return"), i[Symbol.iterator] = function () { return this; }, i;
13
+ function verb(n, f) { i[n] = o[n] ? function (v) { return (p = !p) ? { value: __await(o[n](v)), done: false } : f ? f(v) : v; } : f; }
14
+ };
15
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
16
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
17
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
18
+ return i = {}, verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
19
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
20
+ function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
21
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
22
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
23
+ function fulfill(value) { resume("next", value); }
24
+ function reject(value) { resume("throw", value); }
25
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
26
+ };
27
+ var __importDefault = (this && this.__importDefault) || function (mod) {
28
+ return (mod && mod.__esModule) ? mod : { "default": mod };
29
+ };
30
+ Object.defineProperty(exports, "__esModule", { value: true });
31
+ exports.verifyRNOHPackage = void 0;
32
+ const node_fs_1 = __importDefault(require("node:fs"));
33
+ const tmp_1 = __importDefault(require("tmp"));
34
+ const core_1 = require("../core");
35
+ const checks_1 = require("./checks");
36
+ const io_1 = require("../io");
37
+ function verifyRNOHPackage(_a) {
38
+ return __asyncGenerator(this, arguments, function* verifyRNOHPackage_1({ npmPackagePath, hasHarmonyCode, checkNamesToSkip, }) {
39
+ const npmPackageJson = core_1.PackageJSON.fromProjectRootPath(npmPackagePath, npmPackagePath);
40
+ const initialChecks = [
41
+ (0, checks_1.createHarmonyDirExistsCheck)(npmPackagePath, hasHarmonyCode),
42
+ (0, checks_1.createHarmonyDirContainsOnlyHarsCheck)(npmPackagePath, hasHarmonyCode),
43
+ ];
44
+ yield __await(yield* __asyncDelegator(__asyncValues(runChecks(initialChecks, checkNamesToSkip))));
45
+ if (hasHarmonyCode) {
46
+ const harPath = findHarPath(npmPackagePath);
47
+ if (!harPath) {
48
+ throw new core_1.DescriptiveError({
49
+ whatHappened: 'Attempted to verify har but no har was found',
50
+ whatCanUserDo: {
51
+ default: [
52
+ 'Run this command without --no-harmony-code flag.',
53
+ 'Create har files by running DevEco Studio > Build > Make Module (MODULE_NAME). The har file can be later found in MODULE_NAME/build/default/outputs/default/MODULE_NAME.har. Copy that file to the PACKAGE_ROOT/harmony directory.',
54
+ ],
55
+ },
56
+ });
57
+ }
58
+ const tmpDirPath = new core_1.AbsolutePath(tmp_1.default.dirSync().name);
59
+ yield yield __await({
60
+ type: 'START_PROCESS',
61
+ name: `Unpacking the har file to the tmpdir`,
62
+ });
63
+ yield __await((0, io_1.unpackTarGz)(harPath, tmpDirPath));
64
+ const ohPackageJson5 = core_1.OHPackageJSON5.fromPath(tmpDirPath.copyWithNewSegment('package', 'oh-package.json5'));
65
+ const harChecks = [
66
+ (0, checks_1.createOhPackageNameFollowsNamingConventionCheck)(npmPackageJson.name, ohPackageJson5.name),
67
+ (0, checks_1.createNpmAndOhPackageVersionsAreEqualCheck)(npmPackageJson.version, ohPackageJson5.version),
68
+ ];
69
+ yield __await(yield* __asyncDelegator(__asyncValues(runChecks(harChecks, checkNamesToSkip))));
70
+ }
71
+ });
72
+ }
73
+ exports.verifyRNOHPackage = verifyRNOHPackage;
74
+ function runChecks(checks, checkNamesToSkip) {
75
+ return __asyncGenerator(this, arguments, function* runChecks_1() {
76
+ for (const { checkIf, run } of checks) {
77
+ if (checkNamesToSkip.includes(checkIf)) {
78
+ yield yield __await({
79
+ type: 'FINISHED_CHECKING',
80
+ name: checkIf,
81
+ status: 'skip',
82
+ message: '',
83
+ });
84
+ continue;
85
+ }
86
+ const result = yield __await(run());
87
+ const status = typeof result === 'string' ? result : result.status;
88
+ const message = typeof result === 'string' ? '' : result.message;
89
+ yield yield __await({ type: 'FINISHED_CHECKING', name: checkIf, status, message });
90
+ }
91
+ });
92
+ }
93
+ function findHarPath(packagePath) {
94
+ const packageHarmonyPath = packagePath.copyWithNewSegment('harmony');
95
+ const fileOrDirNames = node_fs_1.default.readdirSync(packageHarmonyPath.getValue());
96
+ const harFilenames = fileOrDirNames.filter((fileOrDirName) => fileOrDirName.endsWith('.har'));
97
+ if (harFilenames.length != 1) {
98
+ return null;
99
+ }
100
+ return packageHarmonyPath.copyWithNewSegment(harFilenames[0]);
101
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-native-oh/react-native-harmony-cli",
3
- "version": "0.0.26",
3
+ "version": "0.0.27",
4
4
  "description": "",
5
5
  "publishConfig": {
6
6
  "registry": "https://registry.npmjs.org/",
@@ -33,6 +33,10 @@ type FindSpecSchemaByType<
33
33
  * Contains component and turbo module (NativeModule) schemas. The "Uber" word is used here to highlight that SpecSchemas don't extend this class.
34
34
  */
35
35
  export class UberSchema implements ValueObject {
36
+ static fromSpecFilePaths(specPaths: AbsolutePath[]): UberSchema {
37
+ return new UberSchema(createRawUberSchemaFromSpecFilePaths(specPaths));
38
+ }
39
+
36
40
  static fromCodegenConfig(codegenConfig: CodegenConfig): UberSchema {
37
41
  try {
38
42
  return new UberSchema(
@@ -2,11 +2,11 @@ import Case from 'case';
2
2
  import { AbsolutePath } from '../../core';
3
3
  import { CodeGenerator } from '../core';
4
4
  import { IndexTSTemplate, RNOHGeneratedPackageHTemplate } from '../templates';
5
- import { BasePackageHTemplate } from '../templates/GeneratedPackageHTemplate';
5
+ import { BasePackageHTemplate } from '../templates/BasePackageHTemplate';
6
6
 
7
7
  export type GlueCodeComponentDataV1 = { name: string; eventNames: string[] };
8
8
 
9
- export type GlueCodeComponentDataV2 = { name: string; libraryName: string };
9
+ export type GlueCodeComponentDataV2 = { name: string; libraryCppName: string };
10
10
 
11
11
  export type GlueCodeTurboModuleData = { name: string };
12
12
 
@@ -33,18 +33,21 @@ export type GlueCodeData = {
33
33
  * Generates code based on UberGenerators results. If UberGenerators produce "leaves" (concrete turbo module or component classes),
34
34
  * this class generates "branches" (barrel files like index.ts) and "trunks" (package file or files).
35
35
  */
36
- export class GlueCodeGenerator implements CodeGenerator<GlueCodeData> {
36
+ export class AppBuildTimeGlueCodeGenerator
37
+ implements CodeGenerator<GlueCodeData>
38
+ {
37
39
  constructor(
38
40
  private cppOutputPath: AbsolutePath,
39
- private etsOutputPath: AbsolutePath
41
+ private etsOutputPath: AbsolutePath,
42
+ private codegenNoticeLines: string[]
40
43
  ) {}
41
44
 
42
45
  generate({ v1, v2 }: GlueCodeData): Map<AbsolutePath, string> {
43
46
  const fileContentByPath = new Map<AbsolutePath, string>();
44
47
  const generatedPackageH_v1 = new RNOHGeneratedPackageHTemplate();
45
- const components_indexTS = new IndexTSTemplate();
46
- const turboModules_indexTS = new IndexTSTemplate();
47
- const generated_indexETS = new IndexTSTemplate();
48
+ const components_indexTS = new IndexTSTemplate(this.codegenNoticeLines);
49
+ const turboModules_indexTS = new IndexTSTemplate(this.codegenNoticeLines);
50
+ const generated_indexETS = new IndexTSTemplate(this.codegenNoticeLines);
48
51
 
49
52
  generated_indexETS.addReexport({ from: './ts' });
50
53
  v1.components.forEach(({ name, eventNames }) => {
@@ -62,7 +65,8 @@ export class GlueCodeGenerator implements CodeGenerator<GlueCodeData> {
62
65
  v2.forEach(({ components, turboModules }, libraryName) => {
63
66
  const basePackageClassName = `Base${Case.pascal(libraryName)}Package`;
64
67
  const generatedPackageH_v2 = new BasePackageHTemplate(
65
- basePackageClassName
68
+ basePackageClassName,
69
+ this.codegenNoticeLines
66
70
  );
67
71
  components.forEach((component) => {
68
72
  generatedPackageH_v2.addComponent(component);
@@ -87,7 +91,7 @@ export class GlueCodeGenerator implements CodeGenerator<GlueCodeData> {
87
91
  );
88
92
  fileContentByPath.set(
89
93
  this.etsOutputPath.copyWithNewSegment('ts.ts'),
90
- new IndexTSTemplate()
94
+ new IndexTSTemplate(this.codegenNoticeLines)
91
95
  .addReexport({ from: './components/ts', as: 'RNC' })
92
96
  .addReexport({ from: './turboModules/ts', as: 'TM' })
93
97
  .build()
@@ -12,14 +12,16 @@ import {
12
12
  ComponentJSIBinderHTemplate,
13
13
  ComponentUtilsTSTemplate,
14
14
  } from '../templates';
15
- import { GlueCodeComponentDataV1 } from './GlueCodeGenerator';
15
+ import { GlueCodeComponentDataV1 } from './AppBuildTimeGlueCodeGenerator';
16
16
 
17
- export class ComponentCodeGeneratorArkTS implements SpecCodeGenerator {
17
+ export class ArkTSComponentCodeGeneratorArkTS implements SpecCodeGenerator {
18
18
  private glueCodeDataItems: GlueCodeComponentDataV1[] = [];
19
19
 
20
20
  constructor(
21
21
  private cppOutputPath: AbsolutePath,
22
- private tsOutputPath: AbsolutePath
22
+ private tsOutputPath: AbsolutePath,
23
+ private codegenNoticeLines: string[],
24
+ private rnohImport: string
23
25
  ) {}
24
26
 
25
27
  getGlueCodeData(): GlueCodeComponentDataV1[] {
@@ -41,9 +43,13 @@ export class ComponentCodeGeneratorArkTS implements SpecCodeGenerator {
41
43
  );
42
44
  const componentJSIBinderHTemplate = new ComponentJSIBinderHTemplate(
43
45
  name,
44
- 1
46
+ this.codegenNoticeLines
47
+ );
48
+ const componentUtilsTSTemplate = new ComponentUtilsTSTemplate(
49
+ name,
50
+ this.codegenNoticeLines,
51
+ this.rnohImport
45
52
  );
46
- const componentUtilsTSTemplate = new ComponentUtilsTSTemplate(name);
47
53
 
48
54
  shape.props.forEach((prop) => {
49
55
  componentJSIBinderHTemplate.addProp({
@@ -0,0 +1,112 @@
1
+ import { AbsolutePath } from '../../core';
2
+ import {
3
+ TypeAnnotationToTS,
4
+ createEventNameFromEventHandlerName,
5
+ getStringifiedTSDefaultValueFromPropTypeAnnotation,
6
+ } from '../core';
7
+ import type { CodeGenerator, UberSchema } from '../core';
8
+ import {
9
+ ComponentJSIBinderHTemplate,
10
+ ComponentUtilsTSTemplate,
11
+ } from '../templates';
12
+ import { ArkTSComponentLibGlueCodeData } from './LibGlueCodeGenerator';
13
+
14
+ /**
15
+ * Generates component code for ArkTS components used in C-API architecture.
16
+ */
17
+ export class ArkTSComponentCodeGeneratorCAPI
18
+ implements CodeGenerator<UberSchema>
19
+ {
20
+ private glueCodeDataItems: ArkTSComponentLibGlueCodeData[] = [];
21
+
22
+ constructor(
23
+ private cppOutputPath: AbsolutePath,
24
+ private tsOutputPath: AbsolutePath,
25
+ private codegenNoticeLines: string[],
26
+ private rnohImport: string
27
+ ) {}
28
+
29
+ getLibGlueCodeData(): ArkTSComponentLibGlueCodeData[] {
30
+ return this.glueCodeDataItems;
31
+ }
32
+
33
+ generate(uberSchema: UberSchema): Map<AbsolutePath, string> {
34
+ const typeAnnotationToTS = new TypeAnnotationToTS();
35
+ const result = new Map<AbsolutePath, string>();
36
+ const componentSpecSchemas =
37
+ uberSchema.findAllSpecSchemasByType('Component');
38
+
39
+ for (const componentSpecSchema of componentSpecSchemas) {
40
+ Object.entries(componentSpecSchema.components).map(([name, shape]) => {
41
+ const componentJSIBinderHTemplate = new ComponentJSIBinderHTemplate(
42
+ name,
43
+ this.codegenNoticeLines
44
+ );
45
+ const componentUtilsTSTemplate = new ComponentUtilsTSTemplate(
46
+ name,
47
+ this.codegenNoticeLines,
48
+ this.rnohImport
49
+ );
50
+ shape.props.forEach((prop) => {
51
+ componentJSIBinderHTemplate.addProp({
52
+ name: prop.name,
53
+ isObject: prop.typeAnnotation.type === 'ObjectTypeAnnotation',
54
+ });
55
+
56
+ componentUtilsTSTemplate.addProp({
57
+ name: prop.name,
58
+ tsType: typeAnnotationToTS.convert(prop.typeAnnotation),
59
+ defaultValue: getStringifiedTSDefaultValueFromPropTypeAnnotation(
60
+ prop.typeAnnotation
61
+ ),
62
+ isOptional: prop.optional,
63
+ });
64
+ });
65
+ shape.commands.forEach((command) => {
66
+ componentUtilsTSTemplate.addCommand({
67
+ name: command.name,
68
+ argsTSType:
69
+ '[' +
70
+ command.typeAnnotation.params
71
+ .map(
72
+ (param) =>
73
+ typeAnnotationToTS.convert(param.typeAnnotation) +
74
+ (param.optional ? '?' : '')
75
+ )
76
+ .join(', ') +
77
+ ']',
78
+ });
79
+ });
80
+
81
+ shape.events.forEach((event) => {
82
+ componentUtilsTSTemplate.addEvent({
83
+ name: createEventNameFromEventHandlerName(event.name),
84
+ payloadTSType: event.typeAnnotation.argument
85
+ ? typeAnnotationToTS.convert(event.typeAnnotation.argument)
86
+ : 'undefined',
87
+ });
88
+ componentJSIBinderHTemplate.addEvent({
89
+ type: event.bubblingType,
90
+ eventHandlerName: event.name,
91
+ });
92
+ });
93
+ result.set(
94
+ this.cppOutputPath.copyWithNewSegment(`${name}JSIBinder.h`),
95
+ componentJSIBinderHTemplate.build()
96
+ );
97
+ result.set(
98
+ this.tsOutputPath.copyWithNewSegment(`${name}.ts`),
99
+ componentUtilsTSTemplate.build()
100
+ );
101
+ this.glueCodeDataItems.push({
102
+ componentName: name,
103
+ eventNames: shape.events.map((event) =>
104
+ createEventNameFromEventHandlerName(event.name)
105
+ ),
106
+ });
107
+ });
108
+ }
109
+
110
+ return result;
111
+ }
112
+ }
@@ -1,30 +1,22 @@
1
- import RNCodegen from '@react-native/codegen/lib/generators/RNCodegen';
2
1
  import { AbsolutePath } from '../../core';
3
2
  import { CodeGenerator, UberSchema } from '../core';
4
3
  import {
5
4
  BaseComponentInstanceHTemplate,
6
5
  ComponentJSIBinderHTemplate,
7
6
  } from '../templates';
8
- import { GlueCodeComponentDataV2 } from './GlueCodeGenerator';
7
+ import { GlueCodeComponentDataV2 } from './AppBuildTimeGlueCodeGenerator';
9
8
 
10
- /**
11
- * NPM package that extends RNOH capabilities.
12
- */
13
9
  export type LibraryData = {
14
- /**
15
- * C++ directory name which is used in #include statements
16
- */
17
- name: string;
10
+ libraryCppName: string;
18
11
  uberSchema: UberSchema;
19
12
  };
20
13
 
21
- export class ComponentCodeGeneratorCAPI implements CodeGenerator<LibraryData> {
14
+ export class CppComponentCodeGenerator implements CodeGenerator<LibraryData> {
22
15
  private glueCodeComponentsData: GlueCodeComponentDataV2[] = [];
23
16
 
24
17
  constructor(
25
- /// "shared" — platform independent code that can be shared between platforms
26
- private onGetSharedOutputPath: (filename: string) => AbsolutePath,
27
- private onGetRNOHOutputPath: (filename: string) => AbsolutePath
18
+ private onGetRNOHOutputPath: (filename: string) => AbsolutePath,
19
+ private codegenNoticeLines: string[]
28
20
  ) {}
29
21
 
30
22
  getGlueCodeData() {
@@ -32,46 +24,25 @@ export class ComponentCodeGeneratorCAPI implements CodeGenerator<LibraryData> {
32
24
  }
33
25
 
34
26
  generate({
35
- name: libraryName,
27
+ libraryCppName,
36
28
  uberSchema,
37
29
  }: LibraryData): Map<AbsolutePath, string> {
38
30
  const fileContentByPath = new Map<AbsolutePath, string>();
39
- const generatorFns = [
40
- RNCodegen.allGenerators.generatePropsH,
41
- RNCodegen.allGenerators.generatePropsCpp,
42
- RNCodegen.allGenerators.generateEventEmitterH,
43
- RNCodegen.allGenerators.generateEventEmitterCpp,
44
- RNCodegen.allGenerators.generateComponentDescriptorH,
45
- RNCodegen.allGenerators.generateStateH,
46
- RNCodegen.allGenerators.generateStateCpp,
47
- RNCodegen.allGenerators.generateShadowNodeH,
48
- RNCodegen.allGenerators.generateShadowNodeCpp,
49
- ];
50
- generatorFns.forEach((generate) => {
51
- const filesOutput = generate(
52
- libraryName,
53
- uberSchema.getValue(),
54
- 'UNUSED_packageName',
55
- true
56
- );
57
- filesOutput.forEach((fileContent, filename) => {
58
- fileContentByPath.set(
59
- this.onGetSharedOutputPath(filename),
60
- fileContent
61
- );
62
- });
63
- });
64
- for (const [filename, schema] of uberSchema
31
+ for (const [_filename, schema] of uberSchema
65
32
  .getSpecSchemaByFilenameMap()
66
33
  .entries()) {
67
34
  if (schema.type === 'Component') {
68
35
  Object.entries(schema.components).map(([componentName, shape]) => {
69
36
  const componentJSIBinderHTemplate = new ComponentJSIBinderHTemplate(
70
37
  componentName,
71
- 2
38
+ this.codegenNoticeLines
72
39
  );
73
40
  const componentInstanceSpecHTemplate =
74
- new BaseComponentInstanceHTemplate(componentName, libraryName);
41
+ new BaseComponentInstanceHTemplate(
42
+ componentName,
43
+ libraryCppName,
44
+ this.codegenNoticeLines
45
+ );
75
46
  shape.props.forEach((prop) => {
76
47
  componentJSIBinderHTemplate.addProp({
77
48
  name: prop.name,
@@ -103,7 +74,7 @@ export class ComponentCodeGeneratorCAPI implements CodeGenerator<LibraryData> {
103
74
  );
104
75
  this.glueCodeComponentsData.push({
105
76
  name: componentName,
106
- libraryName,
77
+ libraryCppName,
107
78
  });
108
79
  });
109
80
  }
@@ -0,0 +1,91 @@
1
+ import Case from 'case';
2
+ import { AbsolutePath } from '../../core';
3
+ import { CodeGenerator } from '../core';
4
+ import { BasePackageHTemplate } from '../templates/BasePackageHTemplate';
5
+ import { IndexTSTemplate } from '../templates';
6
+ import { GlueCodeTurboModuleData } from './AppBuildTimeGlueCodeGenerator';
7
+
8
+ export type ArkTSComponentLibGlueCodeData = {
9
+ componentName: string;
10
+ eventNames: string[];
11
+ };
12
+ export type CppComponentLibGlueCodeData = { componentName: string };
13
+
14
+ type LibGlueCodeData = {
15
+ libraryCppName: string;
16
+ turboModules: GlueCodeTurboModuleData[];
17
+ arkTSComponents: ArkTSComponentLibGlueCodeData[];
18
+ cppComponents: CppComponentLibGlueCodeData[];
19
+ };
20
+
21
+ /**
22
+ * NOTE: This class is coupled with the generated code structure defined in the codegen-harmony-lib.
23
+ */
24
+ export class LibGlueCodeGenerator implements CodeGenerator<LibGlueCodeData> {
25
+ constructor(
26
+ private cppOutputPath: AbsolutePath,
27
+ private etsOutputPath: AbsolutePath,
28
+ private codegenNoticeLines: string[]
29
+ ) {}
30
+
31
+ generate({
32
+ libraryCppName,
33
+ turboModules,
34
+ arkTSComponents,
35
+ cppComponents,
36
+ }: LibGlueCodeData): Map<AbsolutePath, string> {
37
+ const fileContentByPath = new Map<AbsolutePath, string>();
38
+ const basePackageClassName = `Base${Case.pascal(libraryCppName)}Package`;
39
+ const turboModules_indexTS = new IndexTSTemplate(this.codegenNoticeLines);
40
+ const components_indexTS = new IndexTSTemplate(this.codegenNoticeLines);
41
+ const generated_indexETS = new IndexTSTemplate(this.codegenNoticeLines);
42
+
43
+ const basePackageTemplate = new BasePackageHTemplate(
44
+ basePackageClassName,
45
+ this.codegenNoticeLines
46
+ );
47
+ turboModules.forEach((turboModule) => {
48
+ basePackageTemplate.addTurboModule(turboModule);
49
+ turboModules_indexTS.addReexport({ from: `./${turboModule.name}` });
50
+ });
51
+ arkTSComponents.forEach(({ componentName, eventNames }) => {
52
+ components_indexTS.addReexport({ from: `./${componentName}` });
53
+ basePackageTemplate.addComponent({ name: componentName, libraryCppName });
54
+ eventNames.forEach((eventName) =>
55
+ basePackageTemplate.addArkTSEvent({ name: eventName })
56
+ );
57
+ });
58
+ cppComponents.forEach(({ componentName }) => {
59
+ basePackageTemplate.addComponent({ name: componentName, libraryCppName });
60
+ });
61
+ fileContentByPath.set(
62
+ this.cppOutputPath.copyWithNewSegment(
63
+ 'RNOH',
64
+ 'generated',
65
+ `${basePackageClassName}.h`
66
+ ),
67
+ basePackageTemplate.build()
68
+ );
69
+ fileContentByPath.set(
70
+ this.etsOutputPath.copyWithNewSegment('components', 'ts.ts'),
71
+ components_indexTS.build()
72
+ );
73
+ fileContentByPath.set(
74
+ this.etsOutputPath.copyWithNewSegment('turboModules', 'ts.ts'),
75
+ turboModules_indexTS.build()
76
+ );
77
+ fileContentByPath.set(
78
+ this.etsOutputPath.copyWithNewSegment('ts.ts'),
79
+ new IndexTSTemplate(this.codegenNoticeLines)
80
+ .addReexport({ from: './components/ts', as: 'RNC' })
81
+ .addReexport({ from: './turboModules/ts', as: 'TM' })
82
+ .build()
83
+ );
84
+ generated_indexETS.addReexport({ from: './ts' });
85
+ fileContentByPath.set(
86
+ this.etsOutputPath.copyWithNewSegment('index.ets'),
87
+ generated_indexETS.build()
88
+ );
89
+ return fileContentByPath;
90
+ }
91
+ }
@@ -11,7 +11,7 @@ import {
11
11
  SpecSchema,
12
12
  CodegenError,
13
13
  } from '../core';
14
- import { GlueCodeTurboModuleData } from './GlueCodeGenerator';
14
+ import { GlueCodeTurboModuleData } from './AppBuildTimeGlueCodeGenerator';
15
15
 
16
16
  /**
17
17
  * Generates boilerplate code for Turbo Modules.
@@ -21,7 +21,8 @@ export class NativeModuleCodeGenerator implements SpecCodeGenerator {
21
21
  constructor(
22
22
  private cppOutputPath: AbsolutePath,
23
23
  private etsOutputPath: AbsolutePath,
24
- private generatorVersion: number
24
+ private codegenNoticeLines: string[],
25
+ private rnohImport: string
25
26
  ) {}
26
27
 
27
28
  getGlueCodeData() {
@@ -39,11 +40,12 @@ export class NativeModuleCodeGenerator implements SpecCodeGenerator {
39
40
  const typeAnnotationToTS = new TypeAnnotationToTS();
40
41
  const turboModuleInterfaceTS = new TurboModuleInterfaceTS(
41
42
  schema.moduleName,
42
- this.generatorVersion
43
+ this.codegenNoticeLines,
44
+ this.rnohImport
43
45
  );
44
46
  const turboModuleH = new TurboModuleHTemplate(
45
47
  schema.moduleName,
46
- this.generatorVersion
48
+ this.codegenNoticeLines
47
49
  ).build();
48
50
  result.set(
49
51
  this.cppOutputPath.copyWithNewSegment(`${schema.moduleName}.h`),
@@ -52,7 +54,7 @@ export class NativeModuleCodeGenerator implements SpecCodeGenerator {
52
54
 
53
55
  const turboModuleCpp = new TurboModuleCppTemplate(
54
56
  schema.moduleName,
55
- this.generatorVersion
57
+ this.codegenNoticeLines
56
58
  );
57
59
 
58
60
  Object.entries(schema.aliasMap).map(([name, typeAnnotation]) => {
@@ -0,0 +1,50 @@
1
+ import RNCodegen from '@react-native/codegen/lib/generators/RNCodegen';
2
+ import { AbsolutePath } from '../../core';
3
+ import { CodeGenerator, UberSchema } from '../core';
4
+
5
+ type LibraryData = {
6
+ libraryCppName: string;
7
+ uberSchema: UberSchema;
8
+ };
9
+
10
+ export class SharedComponentCodeGenerator
11
+ implements CodeGenerator<LibraryData>
12
+ {
13
+ constructor(
14
+ private onGetSharedOutputPath: (filename: string) => AbsolutePath
15
+ ) {}
16
+
17
+ generate({
18
+ libraryCppName,
19
+ uberSchema,
20
+ }: LibraryData): Map<AbsolutePath, string> {
21
+ const fileContentByPath = new Map<AbsolutePath, string>();
22
+
23
+ const generatorFns = [
24
+ RNCodegen.allGenerators.generatePropsH,
25
+ RNCodegen.allGenerators.generatePropsCpp,
26
+ RNCodegen.allGenerators.generateEventEmitterH,
27
+ RNCodegen.allGenerators.generateEventEmitterCpp,
28
+ RNCodegen.allGenerators.generateComponentDescriptorH,
29
+ RNCodegen.allGenerators.generateStateH,
30
+ RNCodegen.allGenerators.generateStateCpp,
31
+ RNCodegen.allGenerators.generateShadowNodeH,
32
+ RNCodegen.allGenerators.generateShadowNodeCpp,
33
+ ];
34
+ generatorFns.forEach((generate) => {
35
+ const filesOutput = generate(
36
+ libraryCppName,
37
+ uberSchema.getValue(),
38
+ 'UNUSED_packageName',
39
+ true
40
+ );
41
+ filesOutput.forEach((fileContent, filename) => {
42
+ fileContentByPath.set(
43
+ this.onGetSharedOutputPath(filename),
44
+ fileContent
45
+ );
46
+ });
47
+ });
48
+ return fileContentByPath;
49
+ }
50
+ }
@@ -6,9 +6,9 @@ import {
6
6
  SpecSchemaType,
7
7
  UberSchema,
8
8
  } from '../core';
9
- import { GlueCodeDataV1 } from './GlueCodeGenerator';
9
+ import { GlueCodeDataV1 } from './AppBuildTimeGlueCodeGenerator';
10
10
  import { NativeModuleCodeGenerator } from './NativeModuleCodeGenerator';
11
- import { ComponentCodeGeneratorArkTS } from './ComponentCodeGeneratorArkTS';
11
+ import { ArkTSComponentCodeGeneratorArkTS } from './ArkTSComponentCodeGeneratorArkTS';
12
12
 
13
13
  /**
14
14
  * Generates code for libraries built on top of RNOH's ArkTS architecture.
@@ -18,7 +18,8 @@ export class UberGeneratorV1 implements CodeGenerator<UberSchema> {
18
18
 
19
19
  constructor(
20
20
  private cppOutputPath: AbsolutePath,
21
- private etsOutputPath: AbsolutePath
21
+ private etsOutputPath: AbsolutePath,
22
+ private codegenNoticeLines: string[]
22
23
  ) {}
23
24
 
24
25
  getGlueCodeData(): GlueCodeDataV1 {
@@ -27,17 +28,20 @@ export class UberGeneratorV1 implements CodeGenerator<UberSchema> {
27
28
 
28
29
  generate(uberSchema: UberSchema) {
29
30
  const fileContentByPath = new Map<AbsolutePath, string>();
30
- const componentCodeGeneratorArkTS = new ComponentCodeGeneratorArkTS(
31
+ const componentCodeGenerator = new ArkTSComponentCodeGeneratorArkTS(
31
32
  this.cppOutputPath,
32
- this.etsOutputPath.copyWithNewSegment('components')
33
+ this.etsOutputPath.copyWithNewSegment('components'),
34
+ this.codegenNoticeLines,
35
+ '../../ts'
33
36
  );
34
37
  const nativeModuleCodeGenerator = new NativeModuleCodeGenerator(
35
38
  this.cppOutputPath,
36
39
  this.etsOutputPath.copyWithNewSegment('turboModules'),
37
- 1
40
+ this.codegenNoticeLines,
41
+ '../../ts'
38
42
  );
39
43
  const generatorBySpecType = {
40
- Component: componentCodeGeneratorArkTS,
44
+ Component: componentCodeGenerator,
41
45
  NativeModule: nativeModuleCodeGenerator,
42
46
  } satisfies Record<SpecSchemaType, CodeGenerator<SpecSchema>>;
43
47
  for (const [_fileName, specSchema] of uberSchema
@@ -56,7 +60,7 @@ export class UberGeneratorV1 implements CodeGenerator<UberSchema> {
56
60
  });
57
61
  }
58
62
  this.glueCodeData = {
59
- components: componentCodeGeneratorArkTS.getGlueCodeData(),
63
+ components: componentCodeGenerator.getGlueCodeData(),
60
64
  turboModules: nativeModuleCodeGenerator.getGlueCodeData(),
61
65
  };
62
66
  return fileContentByPath;