@react-native-windows/codegen 0.0.0-canary.9 → 0.0.1-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 (53) hide show
  1. package/CHANGELOG.md +726 -15
  2. package/README.md +1 -1
  3. package/bin.js +0 -0
  4. package/lib-commonjs/Cli.d.ts +7 -0
  5. package/lib-commonjs/Cli.js +92 -0
  6. package/lib-commonjs/Cli.js.map +1 -0
  7. package/lib-commonjs/generators/AliasGen.d.ts +12 -0
  8. package/lib-commonjs/generators/AliasGen.js +88 -0
  9. package/lib-commonjs/generators/AliasGen.js.map +1 -0
  10. package/lib-commonjs/generators/AliasManaging.d.ts +15 -0
  11. package/lib-commonjs/generators/AliasManaging.js +49 -0
  12. package/lib-commonjs/generators/AliasManaging.js.map +1 -0
  13. package/lib-commonjs/generators/GenerateNM2.d.ts +15 -0
  14. package/lib-commonjs/generators/GenerateNM2.js +142 -0
  15. package/lib-commonjs/generators/GenerateNM2.js.map +1 -0
  16. package/lib-commonjs/generators/GenerateTypeScript.d.ts +11 -0
  17. package/lib-commonjs/generators/GenerateTypeScript.js +166 -0
  18. package/lib-commonjs/generators/GenerateTypeScript.js.map +1 -0
  19. package/lib-commonjs/generators/ObjectTypes.d.ts +13 -0
  20. package/lib-commonjs/generators/ObjectTypes.js +75 -0
  21. package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
  22. package/lib-commonjs/generators/ParamTypes.d.ts +12 -0
  23. package/lib-commonjs/generators/ParamTypes.js +137 -0
  24. package/lib-commonjs/generators/ParamTypes.js.map +1 -0
  25. package/lib-commonjs/generators/ReturnTypes.d.ts +10 -0
  26. package/lib-commonjs/generators/ReturnTypes.js +29 -0
  27. package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
  28. package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
  29. package/lib-commonjs/generators/ValidateConstants.js +38 -0
  30. package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
  31. package/lib-commonjs/generators/ValidateMethods.d.ts +9 -0
  32. package/lib-commonjs/generators/ValidateMethods.js +75 -0
  33. package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
  34. package/lib-commonjs/index.d.ts +37 -0
  35. package/lib-commonjs/index.js +224 -0
  36. package/lib-commonjs/index.js.map +1 -0
  37. package/package.json +37 -20
  38. package/src/Cli.ts +57 -190
  39. package/src/generators/AliasGen.ts +149 -0
  40. package/src/generators/AliasManaging.ts +75 -0
  41. package/src/generators/GenerateNM2.ts +138 -295
  42. package/src/generators/GenerateTypeScript.ts +247 -0
  43. package/src/generators/ObjectTypes.ts +130 -0
  44. package/src/generators/ParamTypes.ts +298 -0
  45. package/src/generators/ReturnTypes.ts +70 -0
  46. package/src/generators/ValidateConstants.ts +50 -0
  47. package/src/generators/ValidateMethods.ts +177 -0
  48. package/src/index.ts +393 -0
  49. package/.eslintrc.js +0 -4
  50. package/.vscode/launch.json +0 -23
  51. package/CHANGELOG.json +0 -583
  52. package/jest.config.js +0 -1
  53. package/tsconfig.json +0 -5
package/src/Cli.ts CHANGED
@@ -6,14 +6,7 @@
6
6
  */
7
7
 
8
8
  import yargs from 'yargs';
9
- import path from 'path';
10
- import fs from 'fs';
11
- import globby from 'globby';
12
- import {createNM2Generator} from './generators/GenerateNM2';
13
- // @ts-ignore
14
- import {parseFile} from 'react-native-tscodegen/lib/rncodegen/src/parsers/flow';
15
- // @ts-ignore
16
- import schemaValidator from 'react-native-tscodegen/lib/rncodegen/src/schemaValidator';
9
+ import {CodeGenOptions, runCodeGen} from './index';
17
10
 
18
11
  const argv = yargs.options({
19
12
  file: {
@@ -21,12 +14,39 @@ const argv = yargs.options({
21
14
  describe: 'file which contains spec',
22
15
  },
23
16
  files: {
24
- type: 'array',
17
+ type: 'string',
18
+ array: true,
25
19
  describe: 'glob patterns for files which contains specs',
26
20
  },
21
+ modulesTypeScriptTypes: {
22
+ type: 'boolean',
23
+ describe: 'generate turbo module definition files in TypeScript',
24
+ default: false,
25
+ },
26
+ modulesCxx: {
27
+ type: 'boolean',
28
+ describe: 'generate C++ JSI turbo module spec files',
29
+ default: false,
30
+ },
31
+ modulesWindows: {
32
+ type: 'boolean',
33
+ describe: 'generate turbo module spec files for REACT_MODULE',
34
+ default: false,
35
+ },
36
+ methodOnly: {
37
+ type: 'boolean',
38
+ describe: 'generate only method metadata in C++ turbo module spec',
39
+ default: false,
40
+ },
41
+ outputDirectory: {
42
+ type: 'string',
43
+ describe: 'output directory',
44
+ default: 'codegen',
45
+ },
27
46
  test: {
28
47
  type: 'boolean',
29
48
  describe: 'Verify that the generated output is unchanged',
49
+ default: false,
30
50
  },
31
51
  namespace: {
32
52
  type: 'string',
@@ -38,193 +58,40 @@ const argv = yargs.options({
38
58
  required: true,
39
59
  describe: 'Used for part of the path generated within the codegen dir',
40
60
  },
61
+ cppStringType: {
62
+ choices: ['std::string', 'std::wstring'],
63
+ describe:
64
+ 'C++ string type in generated code, should be "std::string" or "std::wstring"',
65
+ default: 'std::string',
66
+ },
67
+ separateDataTypes: {
68
+ type: 'boolean',
69
+ describe: 'generate data types in a separate file',
70
+ default: false,
71
+ },
41
72
  }).argv;
42
73
 
43
- import {SchemaType} from 'react-native-tscodegen';
44
-
45
- interface Options {
46
- libraryName: string;
47
- schema: SchemaType;
48
- outputDirectory: string;
49
- moduleSpecName: string;
50
- }
51
-
52
- interface Config {
53
- generators: any[] /*Generators[]*/;
54
- test?: boolean;
55
- }
56
-
57
- /*
58
- const GENERATORS = {
59
- descriptors: [generateComponentDescriptorH.generate],
60
- events: [
61
- generateEventEmitterCpp.generate,
62
- generateEventEmitterH.generate,
63
- generateModuleHObjCpp.generate,
64
- generateModuleMm.generate,
65
- ],
66
- props: [
67
- generateComponentHObjCpp.generate,
68
- generatePropsCpp.generate,
69
- generatePropsH.generate,
70
- generatePropsJavaInterface.generate,
71
- generatePropsJavaDelegate.generate,
72
- ],
73
- modules: [generateModuleCpp.generate, generateModuleH.generate],
74
- tests: [generateTests.generate],
75
- 'shadow-nodes': [
76
- generateShadowNodeCpp.generate,
77
- generateShadowNodeH.generate,
78
- ],
79
- };
80
- */
81
-
82
- function checkFilesForChanges(
83
- map: Map<string, string>,
84
- outputDir: string,
85
- ): boolean {
86
- let hasChanges = false;
87
-
88
- for (const [contents, fileName] of map) {
89
- const location = path.join(outputDir, fileName);
90
- if (!fs.existsSync(location)) {
91
- hasChanges = true;
92
- continue;
93
- }
94
-
95
- const currentContents = fs.readFileSync(location, 'utf8');
96
- if (currentContents !== contents) {
97
- console.error(`- ${fileName} has changed`);
98
- hasChanges = true;
99
- continue;
100
- }
101
- }
102
-
103
- return hasChanges;
104
- }
105
-
106
- function writeMapToFiles(map: Map<string, string>, outputDir: string) {
107
- let success = true;
108
- map.forEach((contents: string, fileName: string) => {
109
- try {
110
- const location = path.join(outputDir, fileName);
111
- fs.mkdirSync(path.dirname(location), {recursive: true});
112
- fs.writeFileSync(location, contents);
113
- } catch (error) {
114
- success = false;
115
- console.error(`Failed to write ${fileName} to ${outputDir}`, error);
116
- }
117
- });
118
-
119
- return success;
120
- }
121
-
122
- function combineSchemas(files: string[]): SchemaType {
123
- return files.reduce(
124
- (merged, filename) => {
125
- const contents = fs.readFileSync(filename, 'utf8');
126
- if (
127
- contents &&
128
- (/export\s+default\s+\(?codegenNativeComponent</.test(contents) ||
129
- contents.includes('extends TurboModule'))
130
- ) {
131
- const schema = parseFile(filename);
132
-
133
- if (schema && schema.modules) {
134
- merged.modules = {...merged.modules, ...schema.modules};
135
- }
136
- }
137
- return merged;
138
- },
139
- {modules: {}},
140
- );
141
- }
142
-
143
- function generate(
144
- {libraryName, schema, outputDirectory, moduleSpecName}: Options,
145
- {/*generators,*/ test}: Config,
146
- ): boolean {
147
- schemaValidator.validate(schema);
148
-
149
- const componentOutputdir = path.join(
150
- outputDirectory,
151
- 'react/components',
152
- libraryName,
153
- );
154
-
155
- const generatedModuleFiles = [];
156
- const generatedComponentFiles = [];
157
- /*
158
- for (const name of generators) {
159
- for (const generator of GENERATORS[name]) {
160
- generatedFiles.push(...generator(libraryName, schema, moduleSpecName));
161
- }
162
- }
163
- */
164
-
165
- const generateNM2 = createNM2Generator({namespace: argv.namespace});
166
- const generatorPropsH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsH')
167
- .generate;
168
- const generatorPropsCPP = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsCPP')
169
- .generate;
170
- const generatorShadowNodeH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeH')
171
- .generate;
172
- const generatorShadowNodeCPP = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeCPP')
173
- .generate;
174
- const generatorComponentDescriptorH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateComponentDescriptorH')
175
- .generate;
176
- const generatorEventEmitterH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH')
177
- .generate;
178
-
179
- generatedModuleFiles.push(
180
- ...generateNM2(libraryName, schema, moduleSpecName),
181
- );
182
-
183
- generatedComponentFiles.push(
184
- ...generatorPropsH(libraryName, schema, moduleSpecName),
185
- ...generatorPropsCPP(libraryName, schema, moduleSpecName),
186
- ...generatorShadowNodeH(libraryName, schema, moduleSpecName),
187
- ...generatorShadowNodeCPP(libraryName, schema, moduleSpecName),
188
- ...generatorComponentDescriptorH(libraryName, schema, moduleSpecName),
189
- ...generatorEventEmitterH(libraryName, schema, moduleSpecName),
190
- );
191
-
192
- const moduleFilesToUpdate = new Map<string, string>([
193
- ...generatedModuleFiles,
194
- ]);
195
- const componentFilesToUpdate = new Map<string, string>([
196
- ...generatedComponentFiles,
197
- ]);
198
-
199
- if (test === true) {
200
- return (
201
- checkFilesForChanges(moduleFilesToUpdate, outputDirectory) &&
202
- checkFilesForChanges(componentFilesToUpdate, componentOutputdir)
203
- );
204
- }
205
-
206
- return (
207
- writeMapToFiles(moduleFilesToUpdate, outputDirectory) &&
208
- writeMapToFiles(componentFilesToUpdate, componentOutputdir)
209
- );
210
- }
211
-
212
74
  if ((argv.file && argv.files) || (!argv.file && !argv.files)) {
213
75
  console.error('You must specify either --file or --files.');
214
76
  process.exit(1);
215
77
  }
216
78
 
217
- let schema: SchemaType;
218
- if (argv.file) {
219
- schema = parseFile(argv.file);
220
- } else {
221
- schema = combineSchemas(globby.sync(argv.files as string[]));
79
+ if (
80
+ argv.cppStringType !== 'std::string' &&
81
+ argv.cppStringType !== 'std::wstring'
82
+ ) {
83
+ console.error('cppStringType should be "std::string" or "std::wstring".');
84
+ process.exit(1);
222
85
  }
223
86
 
224
- const libraryName = argv.libraryName;
225
- const moduleSpecName = 'moduleSpecName';
226
- const outputDirectory = 'codegen';
227
- generate(
228
- {libraryName, schema, outputDirectory, moduleSpecName},
229
- {generators: [], test: false},
230
- );
87
+ // type casting is necessary here because
88
+ // cppStringType does not become union of string literals
89
+ // until yargs.options get improved in the future
90
+ const changesNecessary = runCodeGen(<CodeGenOptions>argv);
91
+
92
+ if (argv.test && changesNecessary) {
93
+ console.error(
94
+ 'There is a change in the output of codegen. Rerun "react-native codegen-windows" to regenerate.',
95
+ );
96
+ process.exit(2);
97
+ }
@@ -0,0 +1,149 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ import type {
10
+ NativeModuleBaseTypeAnnotation,
11
+ NativeModuleObjectTypeAnnotation,
12
+ NamedShape,
13
+ Nullable,
14
+ } from '@react-native/codegen/lib/CodegenSchema';
15
+ import {AliasMap, getAliasCppName} from './AliasManaging';
16
+ import {CppCodegenOptions, translateField} from './ObjectTypes';
17
+
18
+ function translateObjectMembersDefinition(
19
+ type: NativeModuleObjectTypeAnnotation,
20
+ aliases: AliasMap,
21
+ baseAliasName: string,
22
+ prefix: string,
23
+ options: CppCodegenOptions,
24
+ ) {
25
+ return type.properties
26
+ .map((prop: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>) => {
27
+ let propType = prop.typeAnnotation;
28
+ if (prop.optional && propType.type !== 'NullableTypeAnnotation') {
29
+ propType = {type: 'NullableTypeAnnotation', typeAnnotation: propType};
30
+ }
31
+ return `${prefix}${translateField(
32
+ propType,
33
+ aliases,
34
+ `${baseAliasName}_${prop.name}`,
35
+ options,
36
+ )} ${prop.name};`;
37
+ })
38
+ .join('\n');
39
+ }
40
+
41
+ function translateObjectMembersReflection(
42
+ type: NativeModuleObjectTypeAnnotation,
43
+ aliasCppName: string,
44
+ prefix: string,
45
+ ) {
46
+ return type.properties
47
+ .map((prop: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>) => {
48
+ return `${prefix}{L"${prop.name}", &${aliasCppName}::${prop.name}},`;
49
+ })
50
+ .join('\n');
51
+ }
52
+
53
+ export function createAliasMap(nativeModuleAliases: {
54
+ [name: string]: NativeModuleObjectTypeAnnotation;
55
+ }): AliasMap {
56
+ const aliases: AliasMap = {types: {}, jobs: Object.keys(nativeModuleAliases)};
57
+ for (const aliasName of aliases.jobs) {
58
+ aliases.types[aliasName] = nativeModuleAliases[aliasName];
59
+ }
60
+ return aliases;
61
+ }
62
+
63
+ interface AliasCode {
64
+ definition: string;
65
+ reflection: string;
66
+ }
67
+
68
+ interface AliasCodeMap {
69
+ [name: string]: AliasCode;
70
+ }
71
+
72
+ function generateSingleAlias(
73
+ aliases: AliasMap,
74
+ aliasName: string,
75
+ aliasCode: AliasCodeMap,
76
+ options: CppCodegenOptions,
77
+ ): void {
78
+ const aliasCppName = getAliasCppName(aliasName);
79
+ const aliasType = <NativeModuleObjectTypeAnnotation>aliases.types[aliasName];
80
+ const definition = `
81
+ struct ${aliasCppName} {
82
+ ${translateObjectMembersDefinition(
83
+ aliasType,
84
+ aliases,
85
+ aliasName,
86
+ ' ',
87
+ options,
88
+ )}
89
+ };
90
+ `;
91
+ const reflection = `
92
+ inline winrt::Microsoft::ReactNative::FieldMap GetStructInfo(${aliasCppName}*) noexcept {
93
+ winrt::Microsoft::ReactNative::FieldMap fieldMap {
94
+ ${translateObjectMembersReflection(aliasType, aliasCppName, ' ')}
95
+ };
96
+ return fieldMap;
97
+ }
98
+ `;
99
+ aliasCode[aliasName] = {definition, reflection};
100
+ }
101
+
102
+ function generateNestedAliasesInCorrectOrder(
103
+ aliases: AliasMap,
104
+ aliasCode: AliasCodeMap,
105
+ aliasOrder: string[],
106
+ options: CppCodegenOptions,
107
+ ): void {
108
+ // retrieve and clean all ungenerated aliases
109
+ const jobs = aliases.jobs;
110
+ aliases.jobs = [];
111
+
112
+ // generate each one in its found order
113
+ for (const aliasName of jobs) {
114
+ // generate a new struct and all fields will be examined
115
+ // new anonymous objects could be found
116
+ // they will be stored in aliases.jobs
117
+ generateSingleAlias(aliases, aliasName, aliasCode, options);
118
+ // nested C++ structs must be put before the current C++ struct
119
+ // as they will be used in the current C++ struct
120
+ // the order will be perfectly and easily ensured by doing this recursively
121
+ generateNestedAliasesInCorrectOrder(
122
+ aliases,
123
+ aliasCode,
124
+ aliasOrder,
125
+ options,
126
+ );
127
+ // all referenced C++ structs are generated
128
+ // put the current one following them
129
+ aliasOrder.push(aliasName);
130
+ }
131
+ }
132
+
133
+ export function generateAliases(
134
+ aliases: AliasMap,
135
+ options: CppCodegenOptions,
136
+ ): [string, string] {
137
+ const aliasCode: AliasCodeMap = {};
138
+ const aliasOrder: string[] = [];
139
+ generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder, options);
140
+
141
+ // aliasOrder now has the correct order of C++ struct code
142
+ let customTypes = '';
143
+ let customReflection = '';
144
+ for (const aliasName of aliasOrder) {
145
+ customTypes = `${customTypes}${aliasCode[aliasName].definition}`;
146
+ customReflection = `${customReflection}${aliasCode[aliasName].reflection}`;
147
+ }
148
+ return [customTypes, customReflection];
149
+ }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * Copyright (c) Microsoft Corporation.
3
+ * Licensed under the MIT License.
4
+ * @format
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ import type {NativeModuleObjectTypeAnnotation} from '@react-native/codegen/lib/CodegenSchema';
10
+
11
+ let preferredModuleName: string = '';
12
+
13
+ export function setPreferredModuleName(moduleName: string): void {
14
+ preferredModuleName = moduleName;
15
+ }
16
+
17
+ export function getAliasCppName(typeName: string): string {
18
+ return `${preferredModuleName}Spec_${typeName}`;
19
+ }
20
+
21
+ export interface AliasMap {
22
+ types: {[name: string]: NativeModuleObjectTypeAnnotation | undefined};
23
+ jobs: string[];
24
+ }
25
+
26
+ const ExtendedObjectKey = '$RNW-TURBOMODULE-ALIAS';
27
+ interface ExtendedObject extends NativeModuleObjectTypeAnnotation {
28
+ '$RNW-TURBOMODULE-ALIAS'?: string;
29
+ }
30
+
31
+ function recordAnonymousAlias(
32
+ aliases: AliasMap,
33
+ baseAliasName: string,
34
+ extended: ExtendedObject,
35
+ ): string {
36
+ extended[ExtendedObjectKey] = baseAliasName;
37
+ aliases.types[baseAliasName] = extended;
38
+ aliases.jobs.push(baseAliasName);
39
+ return baseAliasName;
40
+ }
41
+
42
+ export function getAnonymousAliasCppName(
43
+ aliases: AliasMap,
44
+ baseAliasName: string,
45
+ objectType: NativeModuleObjectTypeAnnotation,
46
+ ): string {
47
+ // someone found an anonymous object literal type
48
+ // if the ExtendedObjectKey flag has been set
49
+ // then it is a known one
50
+ // this happens because method signatures are generate twice in spec and error messages
51
+ const extended = <ExtendedObject>objectType;
52
+ const key = extended[ExtendedObjectKey];
53
+ if (key !== undefined) {
54
+ return getAliasCppName(key);
55
+ }
56
+
57
+ // if the ExtendedObjectKey flag has not been set
58
+ // it means it is a unknown one
59
+ // associate the name with this object literal type and return
60
+ if (aliases.types[baseAliasName] === undefined) {
61
+ return getAliasCppName(
62
+ recordAnonymousAlias(aliases, baseAliasName, extended),
63
+ );
64
+ }
65
+
66
+ // sometimes names could be anonymous
67
+ let index = 2;
68
+ while (aliases.types[`${baseAliasName}${index}`] !== undefined) {
69
+ index++;
70
+ }
71
+
72
+ return getAliasCppName(
73
+ recordAnonymousAlias(aliases, `${baseAliasName}${index}`, extended),
74
+ );
75
+ }