@react-native-windows/codegen 0.0.0-canary.4 → 0.0.0-canary.41

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 (52) hide show
  1. package/CHANGELOG.md +359 -4
  2. package/bin.js +0 -0
  3. package/lib-commonjs/Cli.d.ts +7 -0
  4. package/lib-commonjs/Cli.js +60 -0
  5. package/lib-commonjs/Cli.js.map +1 -0
  6. package/lib-commonjs/generators/AliasGen.d.ts +11 -0
  7. package/lib-commonjs/generators/AliasGen.js +72 -0
  8. package/lib-commonjs/generators/AliasGen.js.map +1 -0
  9. package/lib-commonjs/generators/AliasManaging.d.ts +15 -0
  10. package/lib-commonjs/generators/AliasManaging.js +49 -0
  11. package/lib-commonjs/generators/AliasManaging.js.map +1 -0
  12. package/lib-commonjs/generators/GenerateNM2.d.ts +12 -0
  13. package/lib-commonjs/generators/GenerateNM2.js +94 -0
  14. package/lib-commonjs/generators/GenerateNM2.js.map +1 -0
  15. package/lib-commonjs/generators/GenerateTypeScript.d.ts +11 -0
  16. package/lib-commonjs/generators/GenerateTypeScript.js +166 -0
  17. package/lib-commonjs/generators/GenerateTypeScript.js.map +1 -0
  18. package/lib-commonjs/generators/ObjectTypes.d.ts +8 -0
  19. package/lib-commonjs/generators/ObjectTypes.js +53 -0
  20. package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
  21. package/lib-commonjs/generators/ParamTypes.d.ts +11 -0
  22. package/lib-commonjs/generators/ParamTypes.js +114 -0
  23. package/lib-commonjs/generators/ParamTypes.js.map +1 -0
  24. package/lib-commonjs/generators/ReturnTypes.d.ts +9 -0
  25. package/lib-commonjs/generators/ReturnTypes.js +63 -0
  26. package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
  27. package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
  28. package/lib-commonjs/generators/ValidateConstants.js +38 -0
  29. package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
  30. package/lib-commonjs/generators/ValidateMethods.d.ts +8 -0
  31. package/lib-commonjs/generators/ValidateMethods.js +70 -0
  32. package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
  33. package/lib-commonjs/index.d.ts +35 -0
  34. package/lib-commonjs/index.js +190 -0
  35. package/lib-commonjs/index.js.map +1 -0
  36. package/package.json +32 -18
  37. package/src/Cli.ts +26 -152
  38. package/src/generators/AliasGen.ts +105 -0
  39. package/src/generators/AliasManaging.ts +75 -0
  40. package/src/generators/GenerateNM2.ts +69 -297
  41. package/src/generators/GenerateTypeScript.ts +247 -0
  42. package/src/generators/ObjectTypes.ts +73 -0
  43. package/src/generators/ParamTypes.ts +220 -0
  44. package/src/generators/ReturnTypes.ts +92 -0
  45. package/src/generators/ValidateConstants.ts +50 -0
  46. package/src/generators/ValidateMethods.ts +135 -0
  47. package/src/index.ts +321 -0
  48. package/.eslintrc.js +0 -4
  49. package/.vscode/launch.json +0 -23
  50. package/CHANGELOG.json +0 -462
  51. package/jest.config.js +0 -1
  52. package/tsconfig.json +0 -5
package/src/Cli.ts CHANGED
@@ -5,15 +5,8 @@
5
5
  * @format
6
6
  */
7
7
 
8
- import * as yargs from 'yargs';
9
- import * as path from 'path';
10
- import * as fs from 'fs';
11
- import * as 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 * as schemaValidator from 'react-native-tscodegen/lib/rncodegen/src/schemaValidator';
8
+ import yargs from 'yargs';
9
+ import {runCodeGen} from './index';
17
10
 
18
11
  const argv = yargs.options({
19
12
  file: {
@@ -21,164 +14,45 @@ 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
+ ts: {
22
+ type: 'boolean',
23
+ describe: 'generate turbo module definition files in TypeScript',
24
+ default: false,
25
+ },
26
+ methodonly: {
27
+ type: 'boolean',
28
+ describe: 'generate only method metadata in C++ turbo module spec',
29
+ default: false,
30
+ },
31
+ outdir: {
32
+ type: 'string',
33
+ describe: 'output directory',
34
+ default: 'codegen',
35
+ },
27
36
  test: {
28
37
  type: 'boolean',
29
38
  describe: 'Verify that the generated output is unchanged',
39
+ default: false,
30
40
  },
31
41
  namespace: {
32
42
  type: 'string',
33
43
  describe: 'C++/C# Namespace to put generated native modules in',
34
44
  default: 'MyNamespace',
35
45
  },
46
+ libraryName: {
47
+ type: 'string',
48
+ required: true,
49
+ describe: 'Used for part of the path generated within the codegen dir',
50
+ },
36
51
  }).argv;
37
52
 
38
- import {SchemaType} from 'react-native-tscodegen';
39
-
40
- interface Options {
41
- libraryName: string;
42
- schema: SchemaType;
43
- outputDirectory: string;
44
- moduleSpecName: string;
45
- }
46
-
47
- interface Config {
48
- generators: any[] /*Generators[]*/;
49
- test?: boolean;
50
- }
51
-
52
- /*
53
- const GENERATORS = {
54
- descriptors: [generateComponentDescriptorH.generate],
55
- events: [
56
- generateEventEmitterCpp.generate,
57
- generateEventEmitterH.generate,
58
- generateModuleHObjCpp.generate,
59
- generateModuleMm.generate,
60
- ],
61
- props: [
62
- generateComponentHObjCpp.generate,
63
- generatePropsCpp.generate,
64
- generatePropsH.generate,
65
- generatePropsJavaInterface.generate,
66
- generatePropsJavaDelegate.generate,
67
- ],
68
- modules: [generateModuleCpp.generate, generateModuleH.generate],
69
- tests: [generateTests.generate],
70
- 'shadow-nodes': [
71
- generateShadowNodeCpp.generate,
72
- generateShadowNodeH.generate,
73
- ],
74
- };
75
- */
76
-
77
- function checkFilesForChanges(
78
- map: Map<string, string>,
79
- outputDir: string,
80
- ): boolean {
81
- let hasChanges = false;
82
-
83
- for (const [contents, fileName] of map) {
84
- const location = path.join(outputDir, fileName);
85
- if (!fs.existsSync(location)) {
86
- hasChanges = true;
87
- continue;
88
- }
89
-
90
- const currentContents = fs.readFileSync(location, 'utf8');
91
- if (currentContents !== contents) {
92
- console.error(`- ${fileName} has changed`);
93
- hasChanges = true;
94
- continue;
95
- }
96
- }
97
-
98
- return hasChanges;
99
- }
100
-
101
- function writeMapToFiles(map: Map<string, string>, outputDir: string) {
102
- let success = true;
103
- map.forEach((contents: string, fileName: string) => {
104
- try {
105
- const location = path.join(outputDir, fileName);
106
- fs.mkdirSync(path.dirname(location), {recursive: true});
107
- fs.writeFileSync(location, contents);
108
- } catch (error) {
109
- success = false;
110
- console.error(`Failed to write ${fileName} to ${outputDir}`, error);
111
- }
112
- });
113
-
114
- return success;
115
- }
116
-
117
- function combineSchemas(files: string[]): SchemaType {
118
- return files.reduce(
119
- (merged, filename) => {
120
- const contents = fs.readFileSync(filename, 'utf8');
121
- if (
122
- contents &&
123
- (/export\s+default\s+\(?codegenNativeComponent</.test(contents) ||
124
- contents.includes('extends TurboModule'))
125
- ) {
126
- const schema = parseFile(filename);
127
-
128
- if (schema && schema.modules) {
129
- merged.modules = {...merged.modules, ...schema.modules};
130
- }
131
- }
132
- return merged;
133
- },
134
- {modules: {}},
135
- );
136
- }
137
-
138
- function generate(
139
- {libraryName, schema, outputDirectory, moduleSpecName}: Options,
140
- {/*generators,*/ test}: Config,
141
- ): boolean {
142
- schemaValidator.validate(schema);
143
-
144
- const generatedFiles = [];
145
- /*
146
- for (const name of generators) {
147
- for (const generator of GENERATORS[name]) {
148
- generatedFiles.push(...generator(libraryName, schema, moduleSpecName));
149
- }
150
- }
151
- */
152
-
153
- const generateNM2 = createNM2Generator({namespace: argv.namespace});
154
-
155
- generatedFiles.push(...generateNM2(libraryName, schema, moduleSpecName));
156
-
157
- const filesToUpdate = new Map<string, string>([...generatedFiles]);
158
-
159
- if (test === true) {
160
- return checkFilesForChanges(filesToUpdate, outputDirectory);
161
- }
162
-
163
- return writeMapToFiles(filesToUpdate, outputDirectory);
164
- }
165
-
166
53
  if ((argv.file && argv.files) || (!argv.file && !argv.files)) {
167
54
  console.error('You must specify either --file or --files.');
168
55
  process.exit(1);
169
56
  }
170
57
 
171
- let schema: SchemaType;
172
- if (argv.file) {
173
- schema = parseFile(argv.file);
174
- } else {
175
- schema = combineSchemas(globby.sync(argv.files as string[]));
176
- }
177
-
178
- const libraryName = 'libraryName';
179
- const moduleSpecName = 'moduleSpecName';
180
- const outputDirectory = 'codegen';
181
- generate(
182
- {libraryName, schema, outputDirectory, moduleSpecName},
183
- {generators: [], test: false},
184
- );
58
+ runCodeGen(argv);
@@ -0,0 +1,105 @@
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-tscodegen';
15
+ import {AliasMap, getAliasCppName} from './AliasManaging';
16
+ import {translateField} from './ObjectTypes';
17
+
18
+ function translateObjectBody(
19
+ type: NativeModuleObjectTypeAnnotation,
20
+ aliases: AliasMap,
21
+ baseAliasName: string,
22
+ prefix: string,
23
+ ) {
24
+ return type.properties
25
+ .map((prop: NamedShape<Nullable<NativeModuleBaseTypeAnnotation>>) => {
26
+ let propType = prop.typeAnnotation;
27
+ if (prop.optional && propType.type !== 'NullableTypeAnnotation') {
28
+ propType = {type: 'NullableTypeAnnotation', typeAnnotation: propType};
29
+ }
30
+ const first = `${prefix}REACT_FIELD(${prop.name})`;
31
+ const second = `${prefix}${translateField(
32
+ propType,
33
+ aliases,
34
+ `${baseAliasName}_${prop.name}`,
35
+ )} ${prop.name};`;
36
+ return `${first}\n${second}`;
37
+ })
38
+ .join('\n');
39
+ }
40
+
41
+ export function createAliasMap(nativeModuleAliases: {
42
+ [name: string]: NativeModuleObjectTypeAnnotation;
43
+ }): AliasMap {
44
+ const aliases: AliasMap = {types: {}, jobs: Object.keys(nativeModuleAliases)};
45
+ for (const aliasName of aliases.jobs) {
46
+ aliases.types[aliasName] = nativeModuleAliases[aliasName];
47
+ }
48
+ return aliases;
49
+ }
50
+
51
+ interface AliasCodeMap {
52
+ [name: string]: string;
53
+ }
54
+
55
+ function generateSingleAlias(
56
+ aliases: AliasMap,
57
+ aliasName: string,
58
+ aliasCode: AliasCodeMap,
59
+ ): void {
60
+ const aliasType = <NativeModuleObjectTypeAnnotation>aliases.types[aliasName];
61
+ aliasCode[aliasName] = `
62
+ REACT_STRUCT(${getAliasCppName(aliasName)})
63
+ struct ${getAliasCppName(aliasName)} {
64
+ ${translateObjectBody(aliasType, aliases, aliasName, ' ')}
65
+ };
66
+ `;
67
+ }
68
+
69
+ function generateNestedAliasesInCorrectOrder(
70
+ aliases: AliasMap,
71
+ aliasCode: AliasCodeMap,
72
+ aliasOrder: string[],
73
+ ): void {
74
+ // retrieve and clean all ungenerated aliases
75
+ const jobs = aliases.jobs;
76
+ aliases.jobs = [];
77
+
78
+ // generate each one in its found order
79
+ for (const aliasName of jobs) {
80
+ // generate a new struct and all fields will be examined
81
+ // new anonymous objects could be found
82
+ // they will be stored in aliases.jobs
83
+ generateSingleAlias(aliases, aliasName, aliasCode);
84
+ // nested C++ structs must be put before the current C++ struct
85
+ // as they will be used in the current C++ struct
86
+ // the order will be perfectly and easily ensured by doing this recursively
87
+ generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder);
88
+ // all referenced C++ structs are generated
89
+ // put the current one following them
90
+ aliasOrder.push(aliasName);
91
+ }
92
+ }
93
+
94
+ export function generateAliases(aliases: AliasMap): string {
95
+ const aliasCode: AliasCodeMap = {};
96
+ const aliasOrder: string[] = [];
97
+ generateNestedAliasesInCorrectOrder(aliases, aliasCode, aliasOrder);
98
+
99
+ // aliasOrder now has the correct order of C++ struct code
100
+ let traversedAliasedStructs = '';
101
+ for (const aliasName of aliasOrder) {
102
+ traversedAliasedStructs = `${traversedAliasedStructs}${aliasCode[aliasName]}`;
103
+ }
104
+ return traversedAliasedStructs;
105
+ }
@@ -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-tscodegen';
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 recordAnonymouseAlias(
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
+ recordAnonymouseAlias(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
+ recordAnonymouseAlias(aliases, `${baseAliasName}${index}`, extended),
74
+ );
75
+ }