@react-native-windows/codegen 0.0.0-canary.43 → 0.0.0-canary.45
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.
- package/CHANGELOG.md +25 -5
- package/lib-commonjs/Cli.js +13 -3
- package/lib-commonjs/Cli.js.map +1 -1
- package/lib-commonjs/generators/GenerateNM2.d.ts +2 -2
- package/lib-commonjs/generators/GenerateNM2.js +2 -2
- package/lib-commonjs/generators/GenerateNM2.js.map +1 -1
- package/lib-commonjs/index.d.ts +12 -8
- package/lib-commonjs/index.js +37 -18
- package/lib-commonjs/index.js.map +1 -1
- package/package.json +5 -5
- package/src/Cli.ts +13 -3
- package/src/generators/GenerateNM2.ts +3 -3
- package/src/index.ts +79 -37
package/CHANGELOG.md
CHANGED
|
@@ -1,18 +1,38 @@
|
|
|
1
1
|
# Change Log - @react-native-windows/codegen
|
|
2
2
|
|
|
3
|
-
This log was last generated on
|
|
3
|
+
This log was last generated on Fri, 02 Dec 2022 06:17:25 GMT and should not be manually modified.
|
|
4
4
|
|
|
5
5
|
<!-- Start content -->
|
|
6
6
|
|
|
7
|
-
## 0.0.0-canary.
|
|
7
|
+
## 0.0.0-canary.45
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
Fri, 02 Dec 2022 06:17:25 GMT
|
|
10
10
|
|
|
11
11
|
### Changes
|
|
12
12
|
|
|
13
|
-
-
|
|
14
|
-
- Bump
|
|
13
|
+
- Integrate 10/10 (34109996+chiaramooney@users.noreply.github.com)
|
|
14
|
+
- Bump codegen version (30809111+acoates-ms@users.noreply.github.com)
|
|
15
|
+
- Bump @react-native-windows/fs to v0.0.0-canary.11
|
|
16
|
+
- Bump @rnw-scripts/jest-unittest-config to v1.4.2
|
|
17
|
+
- Bump @rnw-scripts/just-task to v2.3.3
|
|
15
18
|
|
|
19
|
+
## 0.0.0-canary.44
|
|
20
|
+
|
|
21
|
+
Mon, 21 Nov 2022 06:15:36 GMT
|
|
22
|
+
|
|
23
|
+
### Changes
|
|
24
|
+
|
|
25
|
+
- Enable ability to codegen JSI C++ TurboModule specs (30809111+acoates-ms@users.noreply.github.com)
|
|
26
|
+
|
|
27
|
+
## 0.0.0-canary.43
|
|
28
|
+
|
|
29
|
+
Wed, 16 Nov 2022 06:16:01 GMT
|
|
30
|
+
|
|
31
|
+
### Changes
|
|
32
|
+
|
|
33
|
+
- Bump @react-native-windows/fs to v0.0.0-canary.10
|
|
34
|
+
- Bump @rnw-scripts/just-task to v2.3.2
|
|
35
|
+
|
|
16
36
|
## 0.0.0-canary.42
|
|
17
37
|
|
|
18
38
|
Sat, 12 Nov 2022 06:17:21 GMT
|
package/lib-commonjs/Cli.js
CHANGED
|
@@ -21,17 +21,27 @@ const argv = yargs_1.default.options({
|
|
|
21
21
|
array: true,
|
|
22
22
|
describe: 'glob patterns for files which contains specs',
|
|
23
23
|
},
|
|
24
|
-
|
|
24
|
+
modulesTypeScriptTypes: {
|
|
25
25
|
type: 'boolean',
|
|
26
26
|
describe: 'generate turbo module definition files in TypeScript',
|
|
27
27
|
default: false,
|
|
28
28
|
},
|
|
29
|
-
|
|
29
|
+
modulesCxx: {
|
|
30
|
+
type: 'boolean',
|
|
31
|
+
describe: 'generate C++ JSI turbo module spec files',
|
|
32
|
+
default: false,
|
|
33
|
+
},
|
|
34
|
+
modulesWindows: {
|
|
35
|
+
type: 'boolean',
|
|
36
|
+
describe: 'generate turbo module spec files for REACT_MODULE',
|
|
37
|
+
default: false,
|
|
38
|
+
},
|
|
39
|
+
methodOnly: {
|
|
30
40
|
type: 'boolean',
|
|
31
41
|
describe: 'generate only method metadata in C++ turbo module spec',
|
|
32
42
|
default: false,
|
|
33
43
|
},
|
|
34
|
-
|
|
44
|
+
outputDirectory: {
|
|
35
45
|
type: 'string',
|
|
36
46
|
describe: 'output directory',
|
|
37
47
|
default: 'codegen',
|
package/lib-commonjs/Cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Cli.js","sourceRoot":"","sources":["../src/Cli.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;AAEH,kDAA0B;AAC1B,mCAAmC;AAEnC,MAAM,IAAI,GAAG,eAAK,CAAC,OAAO,CAAC;IACzB,IAAI,EAAE;QACJ,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,0BAA0B;KACrC;IACD,KAAK,EAAE;QACL,IAAI,EAAE,QAAQ;QACd,KAAK,EAAE,IAAI;QACX,QAAQ,EAAE,8CAA8C;KACzD;IACD,
|
|
1
|
+
{"version":3,"file":"Cli.js","sourceRoot":"","sources":["../src/Cli.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;AAEH,kDAA0B;AAC1B,mCAAmC;AAEnC,MAAM,IAAI,GAAG,eAAK,CAAC,OAAO,CAAC;IACzB,IAAI,EAAE;QACJ,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,0BAA0B;KACrC;IACD,KAAK,EAAE;QACL,IAAI,EAAE,QAAQ;QACd,KAAK,EAAE,IAAI;QACX,QAAQ,EAAE,8CAA8C;KACzD;IACD,sBAAsB,EAAE;QACtB,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,sDAAsD;QAChE,OAAO,EAAE,KAAK;KACf;IACD,UAAU,EAAE;QACV,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,0CAA0C;QACpD,OAAO,EAAE,KAAK;KACf;IACD,cAAc,EAAE;QACd,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,mDAAmD;QAC7D,OAAO,EAAE,KAAK;KACf;IACD,UAAU,EAAE;QACV,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,wDAAwD;QAClE,OAAO,EAAE,KAAK;KACf;IACD,eAAe,EAAE;QACf,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,kBAAkB;QAC5B,OAAO,EAAE,SAAS;KACnB;IACD,IAAI,EAAE;QACJ,IAAI,EAAE,SAAS;QACf,QAAQ,EAAE,+CAA+C;QACzD,OAAO,EAAE,KAAK;KACf;IACD,SAAS,EAAE;QACT,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,qDAAqD;QAC/D,OAAO,EAAE,aAAa;KACvB;IACD,WAAW,EAAE;QACX,IAAI,EAAE,QAAQ;QACd,QAAQ,EAAE,IAAI;QACd,QAAQ,EAAE,4DAA4D;KACvE;CACF,CAAC,CAAC,IAAI,CAAC;AAER,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE;IAC5D,OAAO,CAAC,KAAK,CAAC,4CAA4C,CAAC,CAAC;IAC5D,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;CACjB;AAED,IAAA,kBAAU,EAAC,IAAI,CAAC,CAAC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n *\n * @format\n */\n\nimport yargs from 'yargs';\nimport {runCodeGen} from './index';\n\nconst argv = yargs.options({\n file: {\n type: 'string',\n describe: 'file which contains spec',\n },\n files: {\n type: 'string',\n array: true,\n describe: 'glob patterns for files which contains specs',\n },\n modulesTypeScriptTypes: {\n type: 'boolean',\n describe: 'generate turbo module definition files in TypeScript',\n default: false,\n },\n modulesCxx: {\n type: 'boolean',\n describe: 'generate C++ JSI turbo module spec files',\n default: false,\n },\n modulesWindows: {\n type: 'boolean',\n describe: 'generate turbo module spec files for REACT_MODULE',\n default: false,\n },\n methodOnly: {\n type: 'boolean',\n describe: 'generate only method metadata in C++ turbo module spec',\n default: false,\n },\n outputDirectory: {\n type: 'string',\n describe: 'output directory',\n default: 'codegen',\n },\n test: {\n type: 'boolean',\n describe: 'Verify that the generated output is unchanged',\n default: false,\n },\n namespace: {\n type: 'string',\n describe: 'C++/C# Namespace to put generated native modules in',\n default: 'MyNamespace',\n },\n libraryName: {\n type: 'string',\n required: true,\n describe: 'Used for part of the path generated within the codegen dir',\n },\n}).argv;\n\nif ((argv.file && argv.files) || (!argv.file && !argv.files)) {\n console.error('You must specify either --file or --files.');\n process.exit(1);\n}\n\nrunCodeGen(argv);\n"]}
|
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import type { SchemaType } from 'react-native-tscodegen';
|
|
7
7
|
declare type FilesOutput = Map<string, string>;
|
|
8
|
-
export declare function createNM2Generator({
|
|
8
|
+
export declare function createNM2Generator({ methodOnly, namespace, }: {
|
|
9
|
+
methodOnly: boolean;
|
|
9
10
|
namespace: string;
|
|
10
|
-
methodonly: boolean;
|
|
11
11
|
}): (_libraryName: string, schema: SchemaType, _moduleSpecName: string) => FilesOutput;
|
|
12
12
|
export {};
|
|
@@ -38,7 +38,7 @@ struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {
|
|
|
38
38
|
|
|
39
39
|
} // namespace ::_NAMESPACE_::
|
|
40
40
|
`;
|
|
41
|
-
function createNM2Generator({
|
|
41
|
+
function createNM2Generator({ methodOnly, namespace, }) {
|
|
42
42
|
return (_libraryName, schema, _moduleSpecName) => {
|
|
43
43
|
const files = new Map();
|
|
44
44
|
for (const moduleName of Object.keys(schema.modules)) {
|
|
@@ -65,7 +65,7 @@ ${methods[0]}
|
|
|
65
65
|
let errors = methods[1];
|
|
66
66
|
// prepare constants
|
|
67
67
|
const constants = (0, ValidateConstants_1.generateValidateConstants)(nativeModule, aliases);
|
|
68
|
-
if (constants !== undefined && !
|
|
68
|
+
if (constants !== undefined && !methodOnly) {
|
|
69
69
|
tuples = `
|
|
70
70
|
static constexpr auto constants = std::tuple{
|
|
71
71
|
${constants[0]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GenerateNM2.js","sourceRoot":"","sources":["../../src/generators/GenerateNM2.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,YAAY,CAAC;;;AAGb,mDAAiE;AACjE,yCAA2D;AAC3D,2DAA8D;AAC9D,uDA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IAAI,MAAM,GAAG;;EAEnB,OAAO,CAAC,CAAC,CAAC;KACP,CAAC;gBACE,IAAI,MAAM,GAAG;wFACmE,CAAC;gBACjF,IAAI,MAAM,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;gBAExB,oBAAoB;gBACpB,MAAM,SAAS,GAAG,IAAA,6CAAyB,EAAC,YAAY,EAAE,OAAO,CAAC,CAAC;gBACnE,IAAI,SAAS,KAAK,SAAS,IAAI,CAAC,UAAU,EAAE;oBAC1C,MAAM,GAAG;;EAEjB,SAAS,CAAC,CAAC,CAAC;MACR,MAAM,EAAE,CAAC;oBACL,MAAM,GAAG;6FAC0E,MAAM,EAAE,CAAC;oBAC5F,MAAM,GAAG,GAAG,SAAS,CAAC,CAAC,CAAC;;EAEhC,MAAM,EAAE,CAAC;iBACF;gBAED,4BAA4B;gBAC5B,MAAM,uBAAuB,GAAG,IAAA,0BAAe,EAAC,OAAO,CAAC,CAAC;gBAEzD,KAAK,CAAC,GAAG,CACP,SAAS,mBAAmB,UAAU,EACtC,cAAc;qBACX,OAAO,CAAC,+BAA+B,EAAE,uBAAuB,CAAC;qBACjE,OAAO,CAAC,8BAA8B,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;qBAC5D,OAAO,CAAC,8BAA8B,EAAE,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;qBAC5D,OAAO,CAAC,8BAA8B,EAAE,MAAM,CAAC;qBAC/C,OAAO,CAAC,oBAAoB,EAAE,mBAAmB,CAAC;qBAClD,OAAO,CAAC,kBAAkB,EAAE,SAAS,CAAC,CAC1C,CAAC;aACH;SACF;QAED,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;AACJ,CAAC;AAxED,gDAwEC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n * @format\n */\n\n'use strict';\n\nimport type {SchemaType} from 'react-native-tscodegen';\nimport {AliasMap, setPreferredModuleName} from './AliasManaging';\nimport {createAliasMap, generateAliases} from './AliasGen';\nimport {generateValidateConstants} from './ValidateConstants';\nimport {generateValidateMethods} from './ValidateMethods';\n\ntype FilesOutput = Map<string, string>;\n\nconst moduleTemplate = `\n/*\n * This file is auto-generated from a NativeModule spec file in js.\n *\n * This is a C++ Spec class that should be used with MakeTurboModuleProvider to register native modules\n * in a way that also verifies at compile time that the native module matches the interface required\n * by the TurboModule JS spec.\n */\n#pragma once\n\n#include \"NativeModules.h\"\n#include <tuple>\n\nnamespace ::_NAMESPACE_:: {\n::_MODULE_ALIASED_STRUCTS_::\nstruct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {\n::_MODULE_MEMBERS_TUPLES_::\n\n template <class TModule>\n static constexpr void ValidateModule() noexcept {\n::_MODULE_MEMBERS_CHECKS_::\n\n::_MODULE_MEMBERS_ERRORS_::\n }\n};\n\n} // namespace ::_NAMESPACE_::\n`;\n\nexport function createNM2Generator({\n methodOnly,\n namespace,\n}: {\n methodOnly: boolean;\n namespace: string;\n}) {\n return (\n _libraryName: string,\n schema: SchemaType,\n _moduleSpecName: string,\n ): FilesOutput => {\n const files = new Map<string, string>();\n\n for (const moduleName of Object.keys(schema.modules)) {\n const nativeModule = schema.modules[moduleName];\n // from 0.65 facebook's react-native-codegen\n // the module name has the Native prefix comparing to 0.63\n // when reading files we provided\n const preferredModuleName = moduleName.startsWith('Native')\n ? moduleName.substr(6)\n : moduleName;\n setPreferredModuleName(preferredModuleName);\n\n if (nativeModule.type === 'NativeModule') {\n console.log(`Generating Native${preferredModuleName}Spec.g.h`);\n\n // copy all explicit to a map\n const aliases: AliasMap = createAliasMap(nativeModule.aliases);\n\n // prepare methods\n const methods = generateValidateMethods(nativeModule, aliases);\n let tuples = `\n static constexpr auto methods = std::tuple{\n${methods[0]}\n };`;\n let checks = `\n constexpr auto methodCheckResults = CheckMethods<TModule, ::_MODULE_NAME_::Spec>();`;\n let errors = methods[1];\n\n // prepare constants\n const constants = generateValidateConstants(nativeModule, aliases);\n if (constants !== undefined && !methodOnly) {\n tuples = `\n static constexpr auto constants = std::tuple{\n${constants[0]}\n };${tuples}`;\n checks = `\n constexpr auto constantCheckResults = CheckConstants<TModule, ::_MODULE_NAME_::Spec>();${checks}`;\n errors = `${constants[1]}\n\n${errors}`;\n }\n\n // generate code for structs\n const traversedAliasedStructs = generateAliases(aliases);\n\n files.set(\n `Native${preferredModuleName}Spec.g.h`,\n moduleTemplate\n .replace(/::_MODULE_ALIASED_STRUCTS_::/g, traversedAliasedStructs)\n .replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substring(1))\n .replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substring(1))\n .replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors)\n .replace(/::_MODULE_NAME_::/g, preferredModuleName)\n .replace(/::_NAMESPACE_::/g, namespace),\n );\n }\n }\n\n return files;\n };\n}\n"]}
|
package/lib-commonjs/index.d.ts
CHANGED
|
@@ -7,12 +7,14 @@
|
|
|
7
7
|
import type { SchemaType } from 'react-native-tscodegen';
|
|
8
8
|
interface Options {
|
|
9
9
|
libraryName: string;
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
methodOnly: boolean;
|
|
11
|
+
modulesCxx: boolean;
|
|
12
12
|
moduleSpecName: string;
|
|
13
|
+
modulesTypeScriptTypes: boolean;
|
|
14
|
+
modulesWindows: boolean;
|
|
13
15
|
namespace: string;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
+
outputDirectory: string;
|
|
17
|
+
schema: SchemaType;
|
|
16
18
|
}
|
|
17
19
|
interface Config {
|
|
18
20
|
generators: any[];
|
|
@@ -20,15 +22,17 @@ interface Config {
|
|
|
20
22
|
}
|
|
21
23
|
export declare function parseFile(filename: string): SchemaType;
|
|
22
24
|
export declare function combineSchemas(files: string[]): SchemaType;
|
|
23
|
-
export declare function generate({ libraryName,
|
|
25
|
+
export declare function generate({ libraryName, methodOnly, modulesCxx, moduleSpecName, modulesTypeScriptTypes, modulesWindows, namespace, outputDirectory, schema, }: Options, { /*generators,*/ test }: Config): boolean;
|
|
24
26
|
export declare type CodeGenOptions = {
|
|
25
27
|
file?: string;
|
|
26
28
|
files?: string[];
|
|
27
29
|
libraryName: string;
|
|
28
|
-
|
|
30
|
+
methodOnly: boolean;
|
|
31
|
+
modulesCxx: boolean;
|
|
32
|
+
modulesTypeScriptTypes: boolean;
|
|
33
|
+
modulesWindows: boolean;
|
|
29
34
|
namespace: string;
|
|
30
|
-
|
|
31
|
-
ts: boolean;
|
|
35
|
+
outputDirectory: string;
|
|
32
36
|
test: boolean;
|
|
33
37
|
};
|
|
34
38
|
export declare function runCodeGen(options: CodeGenOptions): boolean;
|
package/lib-commonjs/index.js
CHANGED
|
@@ -82,9 +82,10 @@ function writeMapToFiles(map, outputDir) {
|
|
|
82
82
|
function parseFile(filename) {
|
|
83
83
|
try {
|
|
84
84
|
const isTypeScript = path_1.default.extname(filename) === '.ts' || path_1.default.extname(filename) === '.tsx';
|
|
85
|
+
const contents = fs_1.default.readFileSync(filename, 'utf8');
|
|
85
86
|
const schema = isTypeScript
|
|
86
|
-
? TypeScriptParser.
|
|
87
|
-
: FlowParser.
|
|
87
|
+
? TypeScriptParser.parseString(contents, filename)
|
|
88
|
+
: FlowParser.parseString(contents, filename);
|
|
88
89
|
// there will be at most one turbo module per file
|
|
89
90
|
const moduleName = Object.keys(schema.modules)[0];
|
|
90
91
|
if (moduleName) {
|
|
@@ -125,15 +126,17 @@ function combineSchemas(files) {
|
|
|
125
126
|
}, { modules: {} });
|
|
126
127
|
}
|
|
127
128
|
exports.combineSchemas = combineSchemas;
|
|
128
|
-
function generate({ libraryName,
|
|
129
|
+
function generate({ libraryName, methodOnly, modulesCxx, moduleSpecName, modulesTypeScriptTypes, modulesWindows, namespace, outputDirectory, schema, }, { /*generators,*/ test }) {
|
|
129
130
|
schemaValidator.validate(schema);
|
|
130
131
|
const componentOutputdir = path_1.default.join(outputDirectory, 'react/components', libraryName);
|
|
131
132
|
const generatedFiles = new Map();
|
|
132
133
|
generatedFiles.set(path_1.default.join(outputDirectory, '.clang-format'), 'DisableFormat: true\nSortIncludes: false');
|
|
133
134
|
const generateNM2 = (0, GenerateNM2_1.createNM2Generator)({
|
|
135
|
+
methodOnly,
|
|
134
136
|
namespace,
|
|
135
|
-
methodonly,
|
|
136
137
|
});
|
|
138
|
+
const generateJsiModuleH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/modules/GenerateModuleH')).generate;
|
|
139
|
+
const generateJsiModuleCpp = require(path_1.default.resolve(rncodegenPath, 'lib/generators/modules/GenerateModuleCpp')).generate;
|
|
137
140
|
const generatorPropsH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GeneratePropsH')).generate;
|
|
138
141
|
const generatorPropsCPP = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GeneratePropsCPP')).generate;
|
|
139
142
|
const generatorShadowNodeH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateShadowNodeH')).generate;
|
|
@@ -141,19 +144,34 @@ function generate({ libraryName, schema, outputDirectory, moduleSpecName, namesp
|
|
|
141
144
|
const generatorComponentDescriptorH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateComponentDescriptorH')).generate;
|
|
142
145
|
const generatorEventEmitterH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateEventEmitterH')).generate;
|
|
143
146
|
const generatorEventEmitterCPP = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateEventEmitterCpp')).generate;
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
+
const generatorStateCPP = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateStateCpp')).generate;
|
|
148
|
+
const generatorStateH = require(path_1.default.resolve(rncodegenPath, 'lib/generators/components/GenerateStateH')).generate;
|
|
149
|
+
const moduleGenerators = [];
|
|
150
|
+
if (modulesWindows) {
|
|
151
|
+
moduleGenerators.push(generateNM2);
|
|
152
|
+
}
|
|
153
|
+
if (modulesCxx) {
|
|
154
|
+
moduleGenerators.push(generateJsiModuleH);
|
|
155
|
+
moduleGenerators.push(generateJsiModuleCpp);
|
|
156
|
+
}
|
|
157
|
+
if (modulesTypeScriptTypes) {
|
|
158
|
+
moduleGenerators.push(GenerateTypeScript_1.generateTypeScript);
|
|
147
159
|
}
|
|
160
|
+
moduleGenerators.forEach(generator => {
|
|
161
|
+
const generated = generator(libraryName, schema, moduleSpecName);
|
|
162
|
+
normalizeFileMap(generated, outputDirectory, generatedFiles);
|
|
163
|
+
});
|
|
148
164
|
if (Object.keys(schema.modules).some(moduleName => schema.modules[moduleName].type === 'Component')) {
|
|
149
165
|
const componentGenerators = [
|
|
150
|
-
generatorPropsH,
|
|
151
|
-
generatorPropsCPP,
|
|
152
|
-
generatorShadowNodeH,
|
|
153
|
-
generatorShadowNodeCPP,
|
|
154
166
|
generatorComponentDescriptorH,
|
|
155
|
-
generatorEventEmitterH,
|
|
156
167
|
generatorEventEmitterCPP,
|
|
168
|
+
generatorEventEmitterH,
|
|
169
|
+
generatorPropsCPP,
|
|
170
|
+
generatorPropsH,
|
|
171
|
+
generatorShadowNodeCPP,
|
|
172
|
+
generatorShadowNodeH,
|
|
173
|
+
generatorStateCPP,
|
|
174
|
+
generatorStateH,
|
|
157
175
|
];
|
|
158
176
|
componentGenerators.forEach(generator => {
|
|
159
177
|
const generated = generator(libraryName, schema, moduleSpecName);
|
|
@@ -174,16 +192,17 @@ function runCodeGen(options) {
|
|
|
174
192
|
: combineSchemas(globby_1.default.sync(options.files));
|
|
175
193
|
const libraryName = options.libraryName;
|
|
176
194
|
const moduleSpecName = 'moduleSpecName';
|
|
177
|
-
const outputDirectory = options
|
|
178
|
-
const { namespace, methodonly, ts } = options;
|
|
195
|
+
const { methodOnly, modulesCxx, modulesTypeScriptTypes, modulesWindows, namespace, outputDirectory, } = options;
|
|
179
196
|
return generate({
|
|
180
197
|
libraryName,
|
|
181
|
-
|
|
182
|
-
|
|
198
|
+
methodOnly,
|
|
199
|
+
modulesCxx,
|
|
183
200
|
moduleSpecName,
|
|
201
|
+
modulesTypeScriptTypes,
|
|
202
|
+
modulesWindows,
|
|
184
203
|
namespace,
|
|
185
|
-
|
|
186
|
-
|
|
204
|
+
outputDirectory,
|
|
205
|
+
schema,
|
|
187
206
|
}, { generators: [], test: options.test });
|
|
188
207
|
}
|
|
189
208
|
exports.runCodeGen = runCodeGen;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;AAEH,gDAAwB;AACxB,kEAA0C;AAC1C,oDAA4B;AAC5B,0DAA4D;AAC5D,wEAGyC;AAGzC,8CAA8C;AAC9C,MAAM,MAAM,GAAG,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,2BAA2B,CAAC,CAAC,CAAC;AAC1E,MAAM,aAAa,GAAG,cAAI,CAAC,OAAO,CAChC,OAAO,CAAC,OAAO,CAAC,mCAAmC,EAAE,EAAC,KAAK,EAAE,CAAC,MAAM,CAAC,EAAC,CAAC,CACxE,CAAC;AACF,MAAM,UAAU,GAAG,OAAO,CAAC,cAAI,CAAC,OAAO,CAAC,aAAa,EAAE,kBAAkB,CAAC,CAAC,CAAC;AAC5E,MAAM,gBAAgB,GAAG,OAAO,CAAC,cAAI,CAAC,OAAO,CAC3C,aAAa,EACb,wBAAwB,CACzB,CAAC,CAAC;AAEH,MAAM,eAAe,GAAG,OAAO,CAAC,cAAI,CAAC,OAAO,CAC1C,aAAa,EACb,qBAAqB,CACtB,CAAC,CAAC;AAiBH,SAAS,gBAAgB,CACvB,GAAwB,EACxB,SAAiB,EACjB,MAA2B;IAE3B,KAAK,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,IAAI,GAAG,EAAE;QACtC,MAAM,QAAQ,GAAG,cAAI,CAAC,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;QAChD,MAAM,CAAC,G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E;QACjB,OAAO,oBAAoB,CAAC,cAAc,EAAE,eAAe,CAAC,CAAC;KAC9D;IAED,OAAO,eAAe,CAAC,cAAc,EAAE,eAAe,CAAC,CAAC;AAC1D,CAAC;AAzGD,4BAyGC;AAaD,SAAgB,UAAU,CAAC,OAAuB;IAChD,IAAI,CAAC,OAAO,CAAC,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK;QACjC,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IAEvD,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI;QACzB,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC;QACzB,CAAC,CAAC,cAAc,CAAC,gBAAM,CAAC,IAAI,CAAC,OAAO,CAAC,KAAM,CAAC,CAAC,CAAC;IAEhD,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IACxC,MAAM,cAAc,GAAG,gBAAgB,CAAC;IACxC,MAAM,eAAe,GAAG,OAAO,CAAC,MAAM,CAAC;IACvC,MAAM,EAAC,SAAS,EAAE,UAAU,EAAE,EAAE,EAAC,GAAG,OAAO,CAAC;IAC5C,OAAO,QAAQ,CACb;QACE,WAAW;QACX,MAAM;QACN,eAAe;QACf,cAAc;QACd,SAAS;QACT,UAAU;QACV,EAAE;KACH,EACD,EAAC,UAAU,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAC,CACrC,CAAC;AACJ,CAAC;AAxBD,gCAwBC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n *\n * @format\n */\n\nimport path from 'path';\nimport fs from '@react-native-windows/fs';\nimport globby from 'globby';\nimport {createNM2Generator} from './generators/GenerateNM2';\nimport {\n generateTypeScript,\n setOptionalTurboModule,\n} from './generators/GenerateTypeScript';\nimport type {SchemaType} from 'react-native-tscodegen';\n\n// Load react-native-codegen from react-native\nconst rnPath = path.dirname(require.resolve('react-native/package.json'));\nconst rncodegenPath = path.dirname(\n require.resolve('react-native-codegen/package.json', {paths: [rnPath]}),\n);\nconst FlowParser = require(path.resolve(rncodegenPath, 'lib/parsers/flow'));\nconst TypeScriptParser = require(path.resolve(\n rncodegenPath,\n 'lib/parsers/typescript',\n));\n\nconst schemaValidator = require(path.resolve(\n rncodegenPath,\n 'lib/schemaValidator',\n));\n\ninterface Options {\n libraryName: string;\n schema: SchemaType;\n outputDirectory: string;\n moduleSpecName: string;\n namespace: string;\n methodonly: boolean;\n ts: boolean;\n}\n\ninterface Config {\n generators: any[] /*Generators[]*/;\n test?: boolean;\n}\n\nfunction normalizeFileMap(\n map: Map<string, string>,\n outputDir: string,\n outMap: Map<string, string>,\n): void {\n for (const [fileName, contents] of map) {\n const location = path.join(outputDir, fileName);\n outMap.set(path.normalize(location), contents);\n }\n}\n\nfunction checkFilesForChanges(\n map: Map<string, string>,\n outputDir: string,\n): boolean {\n let hasChanges = false;\n\n const allExistingFiles = globby\n .sync(`${outputDir}/**`)\n .map(_ => path.normalize(_))\n .sort();\n const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();\n\n if (\n allExistingFiles.length !== allGeneratedFiles.length ||\n !allExistingFiles.every((val, index) => val === allGeneratedFiles[index])\n )\n return true;\n\n for (const [fileName, contents] of map) {\n if (!fs.existsSync(fileName)) {\n hasChanges = true;\n continue;\n }\n\n const currentContents = fs.readFileSync(fileName, 'utf8');\n if (currentContents !== contents) {\n console.log(`- ${fileName} has changed`);\n hasChanges = true;\n continue;\n }\n }\n\n return hasChanges;\n}\n\nfunction writeMapToFiles(map: Map<string, string>, outputDir: string) {\n let success = true;\n\n // This ensures that we delete any generated files from modules that have been deleted\n const allExistingFiles = globby.sync(`${outputDir}/**`);\n allExistingFiles.forEach(existingFile => {\n if (!map.has(path.normalize(existingFile))) {\n fs.unlinkSync(existingFile);\n }\n });\n\n for (const [fileName, contents] of map) {\n try {\n fs.mkdirSync(path.dirname(fileName), {recursive: true});\n\n if (fs.existsSync(fileName)) {\n const currentContents = fs.readFileSync(fileName, 'utf8');\n // Don't update the files if there are no changes as this breaks incremental builds\n if (currentContents === contents) {\n continue;\n }\n }\n\n fs.writeFileSync(fileName, contents);\n } catch (error) {\n success = false;\n console.error(`Failed to write ${fileName} to ${fileName}`, error);\n }\n }\n\n return success;\n}\n\nexport function parseFile(filename: string): SchemaType {\n try {\n const isTypeScript =\n path.extname(filename) === '.ts' || path.extname(filename) === '.tsx';\n\n const schema = isTypeScript\n ? TypeScriptParser.parseFile(filename)\n : FlowParser.parseFile(filename);\n // there will be at most one turbo module per file\n const moduleName = Object.keys(schema.modules)[0];\n if (moduleName) {\n const spec = schema.modules[moduleName];\n if (spec.type === 'NativeModule') {\n const contents = fs.readFileSync(filename, 'utf8');\n if (contents) {\n // This is a temporary implementation until such information is added to the schema in facebook/react-native\n if (contents.includes('TurboModuleRegistry.get<')) {\n setOptionalTurboModule(spec, true);\n } else if (contents.includes('TurboModuleRegistry.getEnforcing<')) {\n setOptionalTurboModule(spec, false);\n }\n }\n }\n }\n return schema;\n } catch (e) {\n if (e instanceof Error) {\n e.message = `(${filename}): ${e.message}`;\n }\n throw e;\n }\n}\n\nexport function combineSchemas(files: string[]): SchemaType {\n return files.reduce(\n (merged, filename) => {\n const contents = fs.readFileSync(filename, 'utf8');\n if (\n contents &&\n (/export\\s+default\\s+\\(?codegenNativeComponent</.test(contents) ||\n contents.includes('extends TurboModule'))\n ) {\n const schema = parseFile(filename);\n merged.modules = {...merged.modules, ...schema.modules};\n }\n return merged;\n },\n {modules: {}},\n );\n}\n\nexport function generate(\n {\n libraryName,\n schema,\n outputDirectory,\n moduleSpecName,\n namespace,\n methodonly,\n ts,\n }: Options,\n {/*generators,*/ test}: Config,\n): boolean {\n schemaValidator.validate(schema);\n\n const componentOutputdir = path.join(\n outputDirectory,\n 'react/components',\n libraryName,\n );\n\n const generatedFiles = new Map<string, string>();\n\n generatedFiles.set(\n path.join(outputDirectory, '.clang-format'),\n 'DisableFormat: true\\nSortIncludes: false',\n );\n\n const generateNM2 = createNM2Generator({\n namespace,\n methodonly,\n });\n\n const generatorPropsH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GeneratePropsH',\n )).generate;\n const generatorPropsCPP = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GeneratePropsCPP',\n )).generate;\n const generatorShadowNodeH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateShadowNodeH',\n )).generate;\n const generatorShadowNodeCPP = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateShadowNodeCPP',\n )).generate;\n const generatorComponentDescriptorH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateComponentDescriptorH',\n )).generate;\n const generatorEventEmitterH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateEventEmitterH',\n )).generate;\n const generatorEventEmitterCPP = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateEventEmitterCpp',\n )).generate;\n\n normalizeFileMap(\n generateNM2(libraryName, schema, moduleSpecName),\n outputDirectory,\n generatedFiles,\n );\n\n if (ts) {\n normalizeFileMap(\n generateTypeScript(libraryName, schema, moduleSpecName),\n outputDirectory,\n generatedFiles,\n );\n }\n\n if (\n Object.keys(schema.modules).some(\n moduleName => schema.modules[moduleName].type === 'Component',\n )\n ) {\n const componentGenerators = [\n generatorPropsH,\n generatorPropsCPP,\n generatorShadowNodeH,\n generatorShadowNodeCPP,\n generatorComponentDescriptorH,\n generatorEventEmitterH,\n generatorEventEmitterCPP,\n ];\n\n componentGenerators.forEach(generator => {\n const generated: Map<string, string> = generator(\n libraryName,\n schema,\n moduleSpecName,\n );\n normalizeFileMap(generated, componentOutputdir, generatedFiles);\n });\n }\n\n if (test === true) {\n return checkFilesForChanges(generatedFiles, outputDirectory);\n }\n\n return writeMapToFiles(generatedFiles, outputDirectory);\n}\n\nexport type CodeGenOptions = {\n file?: string;\n files?: string[];\n libraryName: string;\n outdir: string;\n namespace: string;\n methodonly: boolean;\n ts: boolean;\n test: boolean;\n};\n\nexport function runCodeGen(options: CodeGenOptions): boolean {\n if (!options.file && !options.files)\n throw new Error('Must specify file or files option');\n\n const schema = options.file\n ? parseFile(options.file)\n : combineSchemas(globby.sync(options.files!));\n\n const libraryName = options.libraryName;\n const moduleSpecName = 'moduleSpecName';\n const outputDirectory = options.outdir;\n const {namespace, methodonly, ts} = options;\n return generate(\n {\n libraryName,\n schema,\n outputDirectory,\n moduleSpecName,\n namespace,\n methodonly,\n ts,\n },\n {generators: [], test: options.test},\n );\n}\n"]}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;AAEH,gDAAwB;AACxB,kEAA0C;AAC1C,oDAA4B;AAC5B,0DAA4D;AAC5D,wEAGyC;AAGzC,8CAA8C;AAC9C,MAAM,MAAM,GAAG,cAAI,CAAC,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,2BAA2B,CAAC,CAAC,CAAC;AAC1E,MAAM,aAAa,GAAG,cAAI,CAAC,OAAO,CAChC,OAAO,CAAC,OAAO,CAAC,mCAAmC,EAAE,EAAC,KAAK,EAAE,CAAC,MAAM,CAAC,EAAC,CAAC,CACxE,CAAC;AACF,MAAM,UAAU,GAAG,OAAO,CAAC,cAAI,CAAC,OAAO,CAAC,aAAa,EAAE,kBAAkB,CAAC,CAAC,CAAC;AAC5E,MAAM,gBAAgB,GAAG,OAAO,CAAC,cAAI,CAAC,OAAO,CAC3C,aAAa,EACb,wBAAwB,CACzB,CAAC,CAAC;AAEH,MAAM,eAAe,GAAG,OAAO,CAAC,cAAI,CAAC,OAAO,CAC1C,aAAa,EACb,qBAAqB,CACtB,CAAC,CAAC;AAmBH,SAAS,gBAAgB,CACvB,GAAwB,EACxB,SAAiB,EACjB,MAA2B;IAE3B,KAAK,MAAM,CAAC,QAAQ,EAAE,QAAQ,CAAC,IAAI,GAAG,EAAE;QACtC,MAAM,QAAQ,GAAG,cAAI,CAAC,IAAI,CAAC,SAAS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C,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IAEvD,MAAM,MAAM,GAAG,OAAO,CAAC,IAAI;QACzB,CAAC,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC;QACzB,CAAC,CAAC,cAAc,CAAC,gBAAM,CAAC,IAAI,CAAC,OAAO,CAAC,KAAM,CAAC,CAAC,CAAC;IAEhD,MAAM,WAAW,GAAG,OAAO,CAAC,WAAW,CAAC;IACxC,MAAM,cAAc,GAAG,gBAAgB,CAAC;IACxC,MAAM,EACJ,UAAU,EACV,UAAU,EACV,sBAAsB,EACtB,cAAc,EACd,SAAS,EACT,eAAe,GAChB,GAAG,OAAO,CAAC;IACZ,OAAO,QAAQ,CACb;QACE,WAAW;QACX,UAAU;QACV,UAAU;QACV,cAAc;QACd,sBAAsB;QACtB,cAAc;QACd,SAAS;QACT,eAAe;QACf,MAAM;KACP,EACD,EAAC,UAAU,EAAE,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAC,CACrC,CAAC;AACJ,CAAC;AAhCD,gCAgCC","sourcesContent":["/**\n * Copyright (c) Microsoft Corporation.\n * Licensed under the MIT License.\n *\n * @format\n */\n\nimport path from 'path';\nimport fs from '@react-native-windows/fs';\nimport globby from 'globby';\nimport {createNM2Generator} from './generators/GenerateNM2';\nimport {\n generateTypeScript,\n setOptionalTurboModule,\n} from './generators/GenerateTypeScript';\nimport type {SchemaType} from 'react-native-tscodegen';\n\n// Load react-native-codegen from react-native\nconst rnPath = path.dirname(require.resolve('react-native/package.json'));\nconst rncodegenPath = path.dirname(\n require.resolve('react-native-codegen/package.json', {paths: [rnPath]}),\n);\nconst FlowParser = require(path.resolve(rncodegenPath, 'lib/parsers/flow'));\nconst TypeScriptParser = require(path.resolve(\n rncodegenPath,\n 'lib/parsers/typescript',\n));\n\nconst schemaValidator = require(path.resolve(\n rncodegenPath,\n 'lib/schemaValidator',\n));\n\ninterface Options {\n libraryName: string;\n methodOnly: boolean;\n modulesCxx: boolean;\n moduleSpecName: string;\n modulesTypeScriptTypes: boolean;\n modulesWindows: boolean;\n namespace: string;\n outputDirectory: string;\n schema: SchemaType;\n}\n\ninterface Config {\n generators: any[] /*Generators[]*/;\n test?: boolean;\n}\n\nfunction normalizeFileMap(\n map: Map<string, string>,\n outputDir: string,\n outMap: Map<string, string>,\n): void {\n for (const [fileName, contents] of map) {\n const location = path.join(outputDir, fileName);\n outMap.set(path.normalize(location), contents);\n }\n}\n\nfunction checkFilesForChanges(\n map: Map<string, string>,\n outputDir: string,\n): boolean {\n let hasChanges = false;\n\n const allExistingFiles = globby\n .sync(`${outputDir}/**`)\n .map(_ => path.normalize(_))\n .sort();\n const allGeneratedFiles = [...map.keys()].map(_ => path.normalize(_)).sort();\n\n if (\n allExistingFiles.length !== allGeneratedFiles.length ||\n !allExistingFiles.every((val, index) => val === allGeneratedFiles[index])\n )\n return true;\n\n for (const [fileName, contents] of map) {\n if (!fs.existsSync(fileName)) {\n hasChanges = true;\n continue;\n }\n\n const currentContents = fs.readFileSync(fileName, 'utf8');\n if (currentContents !== contents) {\n console.log(`- ${fileName} has changed`);\n hasChanges = true;\n continue;\n }\n }\n\n return hasChanges;\n}\n\nfunction writeMapToFiles(map: Map<string, string>, outputDir: string) {\n let success = true;\n\n // This ensures that we delete any generated files from modules that have been deleted\n const allExistingFiles = globby.sync(`${outputDir}/**`);\n allExistingFiles.forEach(existingFile => {\n if (!map.has(path.normalize(existingFile))) {\n fs.unlinkSync(existingFile);\n }\n });\n\n for (const [fileName, contents] of map) {\n try {\n fs.mkdirSync(path.dirname(fileName), {recursive: true});\n\n if (fs.existsSync(fileName)) {\n const currentContents = fs.readFileSync(fileName, 'utf8');\n // Don't update the files if there are no changes as this breaks incremental builds\n if (currentContents === contents) {\n continue;\n }\n }\n\n fs.writeFileSync(fileName, contents);\n } catch (error) {\n success = false;\n console.error(`Failed to write ${fileName} to ${fileName}`, error);\n }\n }\n\n return success;\n}\n\nexport function parseFile(filename: string): SchemaType {\n try {\n const isTypeScript =\n path.extname(filename) === '.ts' || path.extname(filename) === '.tsx';\n const contents = fs.readFileSync(filename, 'utf8');\n const schema = isTypeScript\n ? TypeScriptParser.parseString(contents, filename)\n : FlowParser.parseString(contents, filename);\n // there will be at most one turbo module per file\n const moduleName = Object.keys(schema.modules)[0];\n if (moduleName) {\n const spec = schema.modules[moduleName];\n if (spec.type === 'NativeModule') {\n const contents = fs.readFileSync(filename, 'utf8');\n if (contents) {\n // This is a temporary implementation until such information is added to the schema in facebook/react-native\n if (contents.includes('TurboModuleRegistry.get<')) {\n setOptionalTurboModule(spec, true);\n } else if (contents.includes('TurboModuleRegistry.getEnforcing<')) {\n setOptionalTurboModule(spec, false);\n }\n }\n }\n }\n return schema;\n } catch (e) {\n if (e instanceof Error) {\n e.message = `(${filename}): ${e.message}`;\n }\n throw e;\n }\n}\n\nexport function combineSchemas(files: string[]): SchemaType {\n return files.reduce(\n (merged, filename) => {\n const contents = fs.readFileSync(filename, 'utf8');\n if (\n contents &&\n (/export\\s+default\\s+\\(?codegenNativeComponent</.test(contents) ||\n contents.includes('extends TurboModule'))\n ) {\n const schema = parseFile(filename);\n merged.modules = {...merged.modules, ...schema.modules};\n }\n return merged;\n },\n {modules: {}},\n );\n}\n\nexport function generate(\n {\n libraryName,\n methodOnly,\n modulesCxx,\n moduleSpecName,\n modulesTypeScriptTypes,\n modulesWindows,\n namespace,\n outputDirectory,\n schema,\n }: Options,\n {/*generators,*/ test}: Config,\n): boolean {\n schemaValidator.validate(schema);\n\n const componentOutputdir = path.join(\n outputDirectory,\n 'react/components',\n libraryName,\n );\n\n const generatedFiles = new Map<string, string>();\n\n generatedFiles.set(\n path.join(outputDirectory, '.clang-format'),\n 'DisableFormat: true\\nSortIncludes: false',\n );\n\n const generateNM2 = createNM2Generator({\n methodOnly,\n namespace,\n });\n\n const generateJsiModuleH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/modules/GenerateModuleH',\n )).generate;\n const generateJsiModuleCpp = require(path.resolve(\n rncodegenPath,\n 'lib/generators/modules/GenerateModuleCpp',\n )).generate;\n const generatorPropsH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GeneratePropsH',\n )).generate;\n const generatorPropsCPP = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GeneratePropsCPP',\n )).generate;\n const generatorShadowNodeH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateShadowNodeH',\n )).generate;\n const generatorShadowNodeCPP = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateShadowNodeCPP',\n )).generate;\n const generatorComponentDescriptorH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateComponentDescriptorH',\n )).generate;\n const generatorEventEmitterH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateEventEmitterH',\n )).generate;\n const generatorEventEmitterCPP = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateEventEmitterCpp',\n )).generate;\n const generatorStateCPP = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateStateCpp',\n )).generate;\n const generatorStateH = require(path.resolve(\n rncodegenPath,\n 'lib/generators/components/GenerateStateH',\n )).generate;\n\n const moduleGenerators = [];\n\n if (modulesWindows) {\n moduleGenerators.push(generateNM2);\n }\n\n if (modulesCxx) {\n moduleGenerators.push(generateJsiModuleH);\n moduleGenerators.push(generateJsiModuleCpp);\n }\n\n if (modulesTypeScriptTypes) {\n moduleGenerators.push(generateTypeScript);\n }\n\n moduleGenerators.forEach(generator => {\n const generated: Map<string, string> = generator(\n libraryName,\n schema,\n moduleSpecName,\n );\n normalizeFileMap(generated, outputDirectory, generatedFiles);\n });\n\n if (\n Object.keys(schema.modules).some(\n moduleName => schema.modules[moduleName].type === 'Component',\n )\n ) {\n const componentGenerators = [\n generatorComponentDescriptorH,\n generatorEventEmitterCPP,\n generatorEventEmitterH,\n generatorPropsCPP,\n generatorPropsH,\n generatorShadowNodeCPP,\n generatorShadowNodeH,\n generatorStateCPP,\n generatorStateH,\n ];\n\n componentGenerators.forEach(generator => {\n const generated: Map<string, string> = generator(\n libraryName,\n schema,\n moduleSpecName,\n );\n normalizeFileMap(generated, componentOutputdir, generatedFiles);\n });\n }\n\n if (test === true) {\n return checkFilesForChanges(generatedFiles, outputDirectory);\n }\n\n return writeMapToFiles(generatedFiles, outputDirectory);\n}\n\nexport type CodeGenOptions = {\n file?: string;\n files?: string[];\n libraryName: string;\n methodOnly: boolean;\n modulesCxx: boolean;\n modulesTypeScriptTypes: boolean;\n modulesWindows: boolean;\n namespace: string;\n outputDirectory: string;\n test: boolean;\n};\n\nexport function runCodeGen(options: CodeGenOptions): boolean {\n if (!options.file && !options.files)\n throw new Error('Must specify file or files option');\n\n const schema = options.file\n ? parseFile(options.file)\n : combineSchemas(globby.sync(options.files!));\n\n const libraryName = options.libraryName;\n const moduleSpecName = 'moduleSpecName';\n const {\n methodOnly,\n modulesCxx,\n modulesTypeScriptTypes,\n modulesWindows,\n namespace,\n outputDirectory,\n } = options;\n return generate(\n {\n libraryName,\n methodOnly,\n modulesCxx,\n moduleSpecName,\n modulesTypeScriptTypes,\n modulesWindows,\n namespace,\n outputDirectory,\n schema,\n },\n {generators: [], test: options.test},\n );\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@react-native-windows/codegen",
|
|
3
|
-
"version": "0.0.0-canary.
|
|
3
|
+
"version": "0.0.0-canary.45",
|
|
4
4
|
"description": "Generators for react-native-codegen targeting react-native-windows",
|
|
5
5
|
"main": "lib-commonjs/index.js",
|
|
6
6
|
"repository": "https://github.com/microsoft/react-native-windows",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"react-native-windows-codegen": "./bin.js"
|
|
19
19
|
},
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@react-native-windows/fs": "^0.0.0-canary.
|
|
21
|
+
"@react-native-windows/fs": "^0.0.0-canary.11",
|
|
22
22
|
"@typescript-eslint/eslint-plugin": "^5.30.5",
|
|
23
23
|
"@typescript-eslint/parser": "^5.30.5",
|
|
24
24
|
"chalk": "^4.1.0",
|
|
@@ -29,8 +29,8 @@
|
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
31
|
"@rnw-scripts/eslint-config": "1.1.14",
|
|
32
|
-
"@rnw-scripts/jest-unittest-config": "1.4.
|
|
33
|
-
"@rnw-scripts/just-task": "2.3.
|
|
32
|
+
"@rnw-scripts/jest-unittest-config": "1.4.2",
|
|
33
|
+
"@rnw-scripts/just-task": "2.3.3",
|
|
34
34
|
"@rnw-scripts/ts-config": "2.0.2",
|
|
35
35
|
"@types/chalk": "^2.2.0",
|
|
36
36
|
"@types/jest": "^29.2.2",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"@types/yargs": "16.0.0",
|
|
39
39
|
"babel-jest": "^29.3.0",
|
|
40
40
|
"eslint": "^8.19.0",
|
|
41
|
-
"jest": "^29.
|
|
41
|
+
"jest": "^29.2.1",
|
|
42
42
|
"prettier": "^2.4.1",
|
|
43
43
|
"react-native-tscodegen": "0.68.4",
|
|
44
44
|
"typescript": "^4.4.4"
|
package/src/Cli.ts
CHANGED
|
@@ -18,17 +18,27 @@ const argv = yargs.options({
|
|
|
18
18
|
array: true,
|
|
19
19
|
describe: 'glob patterns for files which contains specs',
|
|
20
20
|
},
|
|
21
|
-
|
|
21
|
+
modulesTypeScriptTypes: {
|
|
22
22
|
type: 'boolean',
|
|
23
23
|
describe: 'generate turbo module definition files in TypeScript',
|
|
24
24
|
default: false,
|
|
25
25
|
},
|
|
26
|
-
|
|
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: {
|
|
27
37
|
type: 'boolean',
|
|
28
38
|
describe: 'generate only method metadata in C++ turbo module spec',
|
|
29
39
|
default: false,
|
|
30
40
|
},
|
|
31
|
-
|
|
41
|
+
outputDirectory: {
|
|
32
42
|
type: 'string',
|
|
33
43
|
describe: 'output directory',
|
|
34
44
|
default: 'codegen',
|
|
@@ -44,11 +44,11 @@ struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec {
|
|
|
44
44
|
`;
|
|
45
45
|
|
|
46
46
|
export function createNM2Generator({
|
|
47
|
+
methodOnly,
|
|
47
48
|
namespace,
|
|
48
|
-
methodonly,
|
|
49
49
|
}: {
|
|
50
|
+
methodOnly: boolean;
|
|
50
51
|
namespace: string;
|
|
51
|
-
methodonly: boolean;
|
|
52
52
|
}) {
|
|
53
53
|
return (
|
|
54
54
|
_libraryName: string,
|
|
@@ -85,7 +85,7 @@ ${methods[0]}
|
|
|
85
85
|
|
|
86
86
|
// prepare constants
|
|
87
87
|
const constants = generateValidateConstants(nativeModule, aliases);
|
|
88
|
-
if (constants !== undefined && !
|
|
88
|
+
if (constants !== undefined && !methodOnly) {
|
|
89
89
|
tuples = `
|
|
90
90
|
static constexpr auto constants = std::tuple{
|
|
91
91
|
${constants[0]}
|
package/src/index.ts
CHANGED
|
@@ -33,12 +33,14 @@ const schemaValidator = require(path.resolve(
|
|
|
33
33
|
|
|
34
34
|
interface Options {
|
|
35
35
|
libraryName: string;
|
|
36
|
-
|
|
37
|
-
|
|
36
|
+
methodOnly: boolean;
|
|
37
|
+
modulesCxx: boolean;
|
|
38
38
|
moduleSpecName: string;
|
|
39
|
+
modulesTypeScriptTypes: boolean;
|
|
40
|
+
modulesWindows: boolean;
|
|
39
41
|
namespace: string;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
+
outputDirectory: string;
|
|
43
|
+
schema: SchemaType;
|
|
42
44
|
}
|
|
43
45
|
|
|
44
46
|
interface Config {
|
|
@@ -129,10 +131,10 @@ export function parseFile(filename: string): SchemaType {
|
|
|
129
131
|
try {
|
|
130
132
|
const isTypeScript =
|
|
131
133
|
path.extname(filename) === '.ts' || path.extname(filename) === '.tsx';
|
|
132
|
-
|
|
134
|
+
const contents = fs.readFileSync(filename, 'utf8');
|
|
133
135
|
const schema = isTypeScript
|
|
134
|
-
? TypeScriptParser.
|
|
135
|
-
: FlowParser.
|
|
136
|
+
? TypeScriptParser.parseString(contents, filename)
|
|
137
|
+
: FlowParser.parseString(contents, filename);
|
|
136
138
|
// there will be at most one turbo module per file
|
|
137
139
|
const moduleName = Object.keys(schema.modules)[0];
|
|
138
140
|
if (moduleName) {
|
|
@@ -179,12 +181,14 @@ export function combineSchemas(files: string[]): SchemaType {
|
|
|
179
181
|
export function generate(
|
|
180
182
|
{
|
|
181
183
|
libraryName,
|
|
182
|
-
|
|
183
|
-
|
|
184
|
+
methodOnly,
|
|
185
|
+
modulesCxx,
|
|
184
186
|
moduleSpecName,
|
|
187
|
+
modulesTypeScriptTypes,
|
|
188
|
+
modulesWindows,
|
|
185
189
|
namespace,
|
|
186
|
-
|
|
187
|
-
|
|
190
|
+
outputDirectory,
|
|
191
|
+
schema,
|
|
188
192
|
}: Options,
|
|
189
193
|
{/*generators,*/ test}: Config,
|
|
190
194
|
): boolean {
|
|
@@ -204,10 +208,18 @@ export function generate(
|
|
|
204
208
|
);
|
|
205
209
|
|
|
206
210
|
const generateNM2 = createNM2Generator({
|
|
211
|
+
methodOnly,
|
|
207
212
|
namespace,
|
|
208
|
-
methodonly,
|
|
209
213
|
});
|
|
210
214
|
|
|
215
|
+
const generateJsiModuleH = require(path.resolve(
|
|
216
|
+
rncodegenPath,
|
|
217
|
+
'lib/generators/modules/GenerateModuleH',
|
|
218
|
+
)).generate;
|
|
219
|
+
const generateJsiModuleCpp = require(path.resolve(
|
|
220
|
+
rncodegenPath,
|
|
221
|
+
'lib/generators/modules/GenerateModuleCpp',
|
|
222
|
+
)).generate;
|
|
211
223
|
const generatorPropsH = require(path.resolve(
|
|
212
224
|
rncodegenPath,
|
|
213
225
|
'lib/generators/components/GeneratePropsH',
|
|
@@ -236,34 +248,54 @@ export function generate(
|
|
|
236
248
|
rncodegenPath,
|
|
237
249
|
'lib/generators/components/GenerateEventEmitterCpp',
|
|
238
250
|
)).generate;
|
|
251
|
+
const generatorStateCPP = require(path.resolve(
|
|
252
|
+
rncodegenPath,
|
|
253
|
+
'lib/generators/components/GenerateStateCpp',
|
|
254
|
+
)).generate;
|
|
255
|
+
const generatorStateH = require(path.resolve(
|
|
256
|
+
rncodegenPath,
|
|
257
|
+
'lib/generators/components/GenerateStateH',
|
|
258
|
+
)).generate;
|
|
239
259
|
|
|
240
|
-
|
|
241
|
-
generateNM2(libraryName, schema, moduleSpecName),
|
|
242
|
-
outputDirectory,
|
|
243
|
-
generatedFiles,
|
|
244
|
-
);
|
|
260
|
+
const moduleGenerators = [];
|
|
245
261
|
|
|
246
|
-
if (
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
);
|
|
262
|
+
if (modulesWindows) {
|
|
263
|
+
moduleGenerators.push(generateNM2);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if (modulesCxx) {
|
|
267
|
+
moduleGenerators.push(generateJsiModuleH);
|
|
268
|
+
moduleGenerators.push(generateJsiModuleCpp);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
if (modulesTypeScriptTypes) {
|
|
272
|
+
moduleGenerators.push(generateTypeScript);
|
|
252
273
|
}
|
|
253
274
|
|
|
275
|
+
moduleGenerators.forEach(generator => {
|
|
276
|
+
const generated: Map<string, string> = generator(
|
|
277
|
+
libraryName,
|
|
278
|
+
schema,
|
|
279
|
+
moduleSpecName,
|
|
280
|
+
);
|
|
281
|
+
normalizeFileMap(generated, outputDirectory, generatedFiles);
|
|
282
|
+
});
|
|
283
|
+
|
|
254
284
|
if (
|
|
255
285
|
Object.keys(schema.modules).some(
|
|
256
286
|
moduleName => schema.modules[moduleName].type === 'Component',
|
|
257
287
|
)
|
|
258
288
|
) {
|
|
259
289
|
const componentGenerators = [
|
|
260
|
-
generatorPropsH,
|
|
261
|
-
generatorPropsCPP,
|
|
262
|
-
generatorShadowNodeH,
|
|
263
|
-
generatorShadowNodeCPP,
|
|
264
290
|
generatorComponentDescriptorH,
|
|
265
|
-
generatorEventEmitterH,
|
|
266
291
|
generatorEventEmitterCPP,
|
|
292
|
+
generatorEventEmitterH,
|
|
293
|
+
generatorPropsCPP,
|
|
294
|
+
generatorPropsH,
|
|
295
|
+
generatorShadowNodeCPP,
|
|
296
|
+
generatorShadowNodeH,
|
|
297
|
+
generatorStateCPP,
|
|
298
|
+
generatorStateH,
|
|
267
299
|
];
|
|
268
300
|
|
|
269
301
|
componentGenerators.forEach(generator => {
|
|
@@ -287,10 +319,12 @@ export type CodeGenOptions = {
|
|
|
287
319
|
file?: string;
|
|
288
320
|
files?: string[];
|
|
289
321
|
libraryName: string;
|
|
290
|
-
|
|
322
|
+
methodOnly: boolean;
|
|
323
|
+
modulesCxx: boolean;
|
|
324
|
+
modulesTypeScriptTypes: boolean;
|
|
325
|
+
modulesWindows: boolean;
|
|
291
326
|
namespace: string;
|
|
292
|
-
|
|
293
|
-
ts: boolean;
|
|
327
|
+
outputDirectory: string;
|
|
294
328
|
test: boolean;
|
|
295
329
|
};
|
|
296
330
|
|
|
@@ -304,17 +338,25 @@ export function runCodeGen(options: CodeGenOptions): boolean {
|
|
|
304
338
|
|
|
305
339
|
const libraryName = options.libraryName;
|
|
306
340
|
const moduleSpecName = 'moduleSpecName';
|
|
307
|
-
const
|
|
308
|
-
|
|
341
|
+
const {
|
|
342
|
+
methodOnly,
|
|
343
|
+
modulesCxx,
|
|
344
|
+
modulesTypeScriptTypes,
|
|
345
|
+
modulesWindows,
|
|
346
|
+
namespace,
|
|
347
|
+
outputDirectory,
|
|
348
|
+
} = options;
|
|
309
349
|
return generate(
|
|
310
350
|
{
|
|
311
351
|
libraryName,
|
|
312
|
-
|
|
313
|
-
|
|
352
|
+
methodOnly,
|
|
353
|
+
modulesCxx,
|
|
314
354
|
moduleSpecName,
|
|
355
|
+
modulesTypeScriptTypes,
|
|
356
|
+
modulesWindows,
|
|
315
357
|
namespace,
|
|
316
|
-
|
|
317
|
-
|
|
358
|
+
outputDirectory,
|
|
359
|
+
schema,
|
|
318
360
|
},
|
|
319
361
|
{generators: [], test: options.test},
|
|
320
362
|
);
|