@react-native-windows/codegen 0.0.0-canary.2 → 0.0.0-canary.20

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 (44) hide show
  1. package/CHANGELOG.md +175 -8
  2. package/bin.js +0 -0
  3. package/lib-commonjs/Cli.d.ts +7 -0
  4. package/lib-commonjs/Cli.js +203 -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 +11 -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/ObjectTypes.d.ts +8 -0
  16. package/lib-commonjs/generators/ObjectTypes.js +53 -0
  17. package/lib-commonjs/generators/ObjectTypes.js.map +1 -0
  18. package/lib-commonjs/generators/ParamTypes.d.ts +11 -0
  19. package/lib-commonjs/generators/ParamTypes.js +114 -0
  20. package/lib-commonjs/generators/ParamTypes.js.map +1 -0
  21. package/lib-commonjs/generators/ReturnTypes.d.ts +9 -0
  22. package/lib-commonjs/generators/ReturnTypes.js +63 -0
  23. package/lib-commonjs/generators/ReturnTypes.js.map +1 -0
  24. package/lib-commonjs/generators/ValidateConstants.d.ts +8 -0
  25. package/lib-commonjs/generators/ValidateConstants.js +38 -0
  26. package/lib-commonjs/generators/ValidateConstants.js.map +1 -0
  27. package/lib-commonjs/generators/ValidateMethods.d.ts +8 -0
  28. package/lib-commonjs/generators/ValidateMethods.js +70 -0
  29. package/lib-commonjs/generators/ValidateMethods.js.map +1 -0
  30. package/package.json +22 -12
  31. package/src/Cli.ts +128 -34
  32. package/src/generators/AliasGen.ts +105 -0
  33. package/src/generators/AliasManaging.ts +75 -0
  34. package/src/generators/GenerateNM2.ts +62 -296
  35. package/src/generators/ObjectTypes.ts +70 -0
  36. package/src/generators/ParamTypes.ts +220 -0
  37. package/src/generators/ReturnTypes.ts +92 -0
  38. package/src/generators/ValidateConstants.ts +50 -0
  39. package/src/generators/ValidateMethods.ts +135 -0
  40. package/.eslintrc.js +0 -4
  41. package/.vscode/launch.json +0 -23
  42. package/CHANGELOG.json +0 -426
  43. package/jest.config.js +0 -1
  44. package/tsconfig.json +0 -5
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@react-native-windows/codegen",
3
- "version": "0.0.0-canary.2",
3
+ "version": "0.0.0-canary.20",
4
4
  "description": "Generators for react-native-codegen targeting react-native-windows",
5
5
  "main": "index.js",
6
6
  "repository": "https://github.com/microsoft/react-native-windows",
@@ -15,33 +15,40 @@
15
15
  "watch": "rnw-scripts watch"
16
16
  },
17
17
  "bin": {
18
- "codegen": "./bin.js"
18
+ "react-native-windows-codegen": "./bin.js"
19
19
  },
20
20
  "dependencies": {
21
21
  "chalk": "^4.1.0",
22
22
  "globby": "^9.2.0",
23
23
  "mustache": "^4.0.1",
24
- "react-native-tscodegen": "0.66.1",
24
+ "react-native-tscodegen": "0.68.4",
25
25
  "source-map-support": "^0.5.19",
26
- "yargs": "^15.4.1"
26
+ "yargs": "^16.2.0"
27
27
  },
28
28
  "devDependencies": {
29
- "@rnw-scripts/eslint-config": "1.1.6",
30
- "@rnw-scripts/jest-unittest-config": "1.1.1",
31
- "@rnw-scripts/just-task": "2.0.2",
32
- "@rnw-scripts/ts-config": "1.1.0",
29
+ "@rnw-scripts/eslint-config": "1.1.8",
30
+ "@rnw-scripts/jest-unittest-config": "1.2.4",
31
+ "@rnw-scripts/just-task": "2.2.1",
32
+ "@rnw-scripts/ts-config": "2.0.1",
33
33
  "@types/chalk": "^2.2.0",
34
34
  "@types/globby": "^9.1.0",
35
35
  "@types/jest": "^26.0.20",
36
36
  "@types/node": "^14.14.22",
37
- "@types/yargs": "15.0.5",
37
+ "@types/yargs": "16.0.0",
38
38
  "babel-jest": "^26.3.0",
39
39
  "eslint": "7.12.0",
40
- "jest": "^26.5.2",
40
+ "jest": "^26.6.3",
41
41
  "just-scripts": "^1.3.3",
42
42
  "prettier": "1.19.1",
43
- "typescript": "^3.8.3"
43
+ "typescript": "^4.4.4"
44
44
  },
45
+ "files": [
46
+ "bin.js",
47
+ "CHANGELOG.md",
48
+ "README.md",
49
+ "lib-commonjs",
50
+ "src"
51
+ ],
45
52
  "beachball": {
46
53
  "defaultNpmTag": "canary",
47
54
  "disallowedChangeTypes": [
@@ -50,5 +57,8 @@
50
57
  "patch"
51
58
  ]
52
59
  },
53
- "promoteRelease": true
60
+ "promoteRelease": true,
61
+ "engines": {
62
+ "node": ">= 12.0.0"
63
+ }
54
64
  }
package/src/Cli.ts CHANGED
@@ -5,15 +5,15 @@
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';
8
+ import yargs from 'yargs';
9
+ import path from 'path';
10
+ import fs from 'fs';
11
+ import globby from 'globby';
12
12
  import {createNM2Generator} from './generators/GenerateNM2';
13
13
  // @ts-ignore
14
14
  import {parseFile} from 'react-native-tscodegen/lib/rncodegen/src/parsers/flow';
15
15
  // @ts-ignore
16
- import * as schemaValidator from 'react-native-tscodegen/lib/rncodegen/src/schemaValidator';
16
+ import schemaValidator from 'react-native-tscodegen/lib/rncodegen/src/schemaValidator';
17
17
 
18
18
  const argv = yargs.options({
19
19
  file: {
@@ -33,6 +33,11 @@ const argv = yargs.options({
33
33
  describe: 'C++/C# Namespace to put generated native modules in',
34
34
  default: 'MyNamespace',
35
35
  },
36
+ libraryName: {
37
+ type: 'string',
38
+ required: true,
39
+ describe: 'Used for part of the path generated within the codegen dir',
40
+ },
36
41
  }).argv;
37
42
 
38
43
  import {SchemaType} from 'react-native-tscodegen';
@@ -74,20 +79,42 @@ const GENERATORS = {
74
79
  };
75
80
  */
76
81
 
82
+ function normalizeFileMap(
83
+ map: Map<string, string>,
84
+ outputDir: string,
85
+ outMap: Map<string, string>,
86
+ ): void {
87
+ for (const [fileName, contents] of map) {
88
+ const location = path.join(outputDir, fileName);
89
+ outMap.set(path.normalize(location), contents);
90
+ }
91
+ }
92
+
77
93
  function checkFilesForChanges(
78
94
  map: Map<string, string>,
79
95
  outputDir: string,
80
96
  ): boolean {
81
97
  let hasChanges = false;
82
98
 
83
- for (const [contents, fileName] of map) {
84
- const location = path.join(outputDir, fileName);
85
- if (!fs.existsSync(location)) {
99
+ const allExistingFiles = globby
100
+ .sync(`${outputDir}/**`)
101
+ .map(_ => path.normalize(_))
102
+ .sort();
103
+ const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();
104
+
105
+ if (
106
+ allExistingFiles.length !== allGeneratedFiles.length ||
107
+ !allExistingFiles.every((val, index) => val === allGeneratedFiles[index])
108
+ )
109
+ return true;
110
+
111
+ for (const [fileName, contents] of map) {
112
+ if (!fs.existsSync(fileName)) {
86
113
  hasChanges = true;
87
114
  continue;
88
115
  }
89
116
 
90
- const currentContents = fs.readFileSync(location, 'utf8');
117
+ const currentContents = fs.readFileSync(fileName, 'utf8');
91
118
  if (currentContents !== contents) {
92
119
  console.error(`- ${fileName} has changed`);
93
120
  hasChanges = true;
@@ -100,20 +127,48 @@ function checkFilesForChanges(
100
127
 
101
128
  function writeMapToFiles(map: Map<string, string>, outputDir: string) {
102
129
  let success = true;
103
- map.forEach((contents: string, fileName: string) => {
130
+
131
+ // This ensures that we delete any generated files from modules that have been deleted
132
+ const allExistingFiles = globby.sync(`${outputDir}/**`);
133
+ allExistingFiles.forEach(existingFile => {
134
+ if (!map.has(path.normalize(existingFile))) {
135
+ fs.unlinkSync(existingFile);
136
+ }
137
+ });
138
+
139
+ for (const [fileName, contents] of map) {
104
140
  try {
105
- const location = path.join(outputDir, fileName);
106
- fs.mkdirSync(path.dirname(location), {recursive: true});
107
- fs.writeFileSync(location, contents);
141
+ fs.mkdirSync(path.dirname(fileName), {recursive: true});
142
+
143
+ if (fs.existsSync(fileName)) {
144
+ const currentContents = fs.readFileSync(fileName, 'utf8');
145
+ // Don't update the files if there are no changes as this breaks incremental builds
146
+ if (currentContents === contents) {
147
+ continue;
148
+ }
149
+ }
150
+
151
+ fs.writeFileSync(fileName, contents);
108
152
  } catch (error) {
109
153
  success = false;
110
- console.error(`Failed to write ${fileName} to ${outputDir}`, error);
154
+ console.error(`Failed to write ${fileName} to ${fileName}`, error);
111
155
  }
112
- });
156
+ }
113
157
 
114
158
  return success;
115
159
  }
116
160
 
161
+ function parseFlowFile(filename: string): SchemaType {
162
+ try {
163
+ return parseFile(filename);
164
+ } catch (e) {
165
+ if (e instanceof Error) {
166
+ e.message = `(${filename}): ${e.message}`;
167
+ }
168
+ throw e;
169
+ }
170
+ }
171
+
117
172
  function combineSchemas(files: string[]): SchemaType {
118
173
  return files.reduce(
119
174
  (merged, filename) => {
@@ -123,11 +178,8 @@ function combineSchemas(files: string[]): SchemaType {
123
178
  (/export\s+default\s+\(?codegenNativeComponent</.test(contents) ||
124
179
  contents.includes('extends TurboModule'))
125
180
  ) {
126
- const schema = parseFile(filename);
127
-
128
- if (schema && schema.modules) {
129
- merged.modules = {...merged.modules, ...schema.modules};
130
- }
181
+ const schema = parseFlowFile(filename);
182
+ merged.modules = {...merged.modules, ...schema.modules};
131
183
  }
132
184
  return merged;
133
185
  },
@@ -141,26 +193,68 @@ function generate(
141
193
  ): boolean {
142
194
  schemaValidator.validate(schema);
143
195
 
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
- */
196
+ const componentOutputdir = path.join(
197
+ outputDirectory,
198
+ 'react/components',
199
+ libraryName,
200
+ );
201
+
202
+ const generatedFiles = new Map<string, string>();
203
+
204
+ generatedFiles.set(
205
+ path.join(outputDirectory, '.clang-format'),
206
+ 'DisableFormat: true\nSortIncludes: false',
207
+ );
152
208
 
153
209
  const generateNM2 = createNM2Generator({namespace: argv.namespace});
210
+ const generatorPropsH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsH')
211
+ .generate;
212
+ const generatorPropsCPP = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GeneratePropsCPP')
213
+ .generate;
214
+ const generatorShadowNodeH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeH')
215
+ .generate;
216
+ const generatorShadowNodeCPP = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateShadowNodeCPP')
217
+ .generate;
218
+ const generatorComponentDescriptorH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateComponentDescriptorH')
219
+ .generate;
220
+ const generatorEventEmitterH = require('react-native-tscodegen/lib/rncodegen/src/generators/components/GenerateEventEmitterH')
221
+ .generate;
222
+
223
+ normalizeFileMap(
224
+ generateNM2(libraryName, schema, moduleSpecName),
225
+ outputDirectory,
226
+ generatedFiles,
227
+ );
154
228
 
155
- generatedFiles.push(...generateNM2(libraryName, schema, moduleSpecName));
229
+ if (
230
+ Object.keys(schema.modules).some(
231
+ moduleName => schema.modules[moduleName].type === 'Component',
232
+ )
233
+ ) {
234
+ const componentGenerators = [
235
+ generatorPropsH,
236
+ generatorPropsCPP,
237
+ generatorShadowNodeH,
238
+ generatorShadowNodeCPP,
239
+ generatorComponentDescriptorH,
240
+ generatorEventEmitterH,
241
+ ];
156
242
 
157
- const filesToUpdate = new Map<string, string>([...generatedFiles]);
243
+ componentGenerators.forEach(generator => {
244
+ const generated: Map<string, string> = generator(
245
+ libraryName,
246
+ schema,
247
+ moduleSpecName,
248
+ );
249
+ normalizeFileMap(generated, componentOutputdir, generatedFiles);
250
+ });
251
+ }
158
252
 
159
253
  if (test === true) {
160
- return checkFilesForChanges(filesToUpdate, outputDirectory);
254
+ return checkFilesForChanges(generatedFiles, outputDirectory);
161
255
  }
162
256
 
163
- return writeMapToFiles(filesToUpdate, outputDirectory);
257
+ return writeMapToFiles(generatedFiles, outputDirectory);
164
258
  }
165
259
 
166
260
  if ((argv.file && argv.files) || (!argv.file && !argv.files)) {
@@ -170,12 +264,12 @@ if ((argv.file && argv.files) || (!argv.file && !argv.files)) {
170
264
 
171
265
  let schema: SchemaType;
172
266
  if (argv.file) {
173
- schema = parseFile(argv.file);
267
+ schema = parseFlowFile(argv.file);
174
268
  } else {
175
269
  schema = combineSchemas(globby.sync(argv.files as string[]));
176
270
  }
177
271
 
178
- const libraryName = 'libraryName';
272
+ const libraryName = argv.libraryName;
179
273
  const moduleSpecName = 'moduleSpecName';
180
274
  const outputDirectory = 'codegen';
181
275
  generate(
@@ -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 {
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 {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
+ }