From c9c8647c9afcfab00c65c6242d75202b01e1189a Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Tue, 8 Aug 2023 02:46:55 -0700 Subject: [PATCH 01/13] Add `allInOne` to `createNM2Generator` --- .../codegen/src/generators/GenerateNM2.ts | 84 +++++++++++++++---- 1 file changed, 69 insertions(+), 15 deletions(-) diff --git a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts index a46fa1c4208..66930541eb3 100644 --- a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts +++ b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts @@ -17,22 +17,16 @@ export type {CppStringTypes} from './ObjectTypes'; type FilesOutput = Map; -const moduleTemplate = ` -/* +const headerTemplate = `/* * This file is auto-generated from a NativeModule spec file in js. * * This is a C++ Spec class that should be used with MakeTurboModuleProvider to register native modules * in a way that also verifies at compile time that the native module matches the interface required * by the TurboModule JS spec. */ -#pragma once +#pragma once`; -#include -#include - -namespace ::_NAMESPACE_:: { -::_MODULE_CUSTPM_TYPES_:: -::_MODULE_CUSTPM_TYPES_REFLECTION_:: +const specTemplate = `::_MODULE_CUSTPM_TYPES_REFLECTION_:: struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec { ::_MODULE_MEMBERS_TUPLES_:: @@ -41,7 +35,46 @@ struct ::_MODULE_NAME_::Spec : winrt::Microsoft::ReactNative::TurboModuleSpec { ::_MODULE_MEMBERS_CHECKS_:: ::_MODULE_MEMBERS_ERRORS_:: - } + }`; + +const typeOnlyTemplate = ` +${headerTemplate} + +#include +#include +#include +#include +#include + +namespace ::_NAMESPACE_:: { +::_MODULE_CUSTPM_TYPES_:: +}; + +} // namespace ::_NAMESPACE_:: +`; + +const moduleOnlyTemplate = ` +${headerTemplate} + +#include <::_TYPE_DEFINITION_FILE_NAME_::> +#include + +namespace ::_NAMESPACE_:: { +${specTemplate} +}; + +} // namespace ::_NAMESPACE_:: +`; + +const allInOneTemplate = ` +${headerTemplate} + +#include +#include + +namespace ::_NAMESPACE_:: { +::_MODULE_CUSTPM_TYPES_:: +${specTemplate} }; } // namespace ::_NAMESPACE_:: @@ -51,10 +84,12 @@ export function createNM2Generator({ methodOnly, namespace, cppStringType, + allInOne, }: { methodOnly: boolean; namespace: string; cppStringType: CppStringTypes; + allInOne: boolean; }) { return ( _libraryName: string, @@ -110,17 +145,36 @@ ${errors}`; cppStringType, }); - files.set( - `Native${preferredModuleName}Spec.g.h`, - moduleTemplate + const replaceContent = function (template: string): string { + return template .replace(/::_MODULE_CUSTPM_TYPES_::/g, customTypes) .replace(/::_MODULE_CUSTPM_TYPES_REFLECTION_::/g, customReflection) .replace(/::_MODULE_MEMBERS_TUPLES_::/g, tuples.substring(1)) .replace(/::_MODULE_MEMBERS_CHECKS_::/g, checks.substring(1)) .replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors) .replace(/::_MODULE_NAME_::/g, preferredModuleName) - .replace(/::_NAMESPACE_::/g, namespace), - ); + .replace( + /::_TYPE_DEFINITION_FILE_NAME_::/g, + `Native${preferredModuleName}Types.g.h`, + ) + .replace(/::_NAMESPACE_::/g, namespace); + }; + + if (allInOne) { + files.set( + `Native${preferredModuleName}Spec.g.h`, + replaceContent(allInOneTemplate), + ); + } else { + files.set( + `Native${preferredModuleName}Types.g.h`, + replaceContent(typeOnlyTemplate), + ); + files.set( + `Native${preferredModuleName}Spec.g.h`, + replaceContent(moduleOnlyTemplate), + ); + } } } From 32200287ad493f77eec8f3bb672cc29333c70bdb Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Tue, 8 Aug 2023 02:54:42 -0700 Subject: [PATCH 02/13] Add `allInOne` to @rnw/cli and @rnw/codegen --- .../@react-native-windows/cli/src/codegen.ts | 20 ++++++++++++++++++- .../@react-native-windows/codegen/src/Cli.ts | 5 +++++ .../codegen/src/index.ts | 5 +++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/packages/@react-native-windows/cli/src/codegen.ts b/packages/@react-native-windows/cli/src/codegen.ts index 98477c41d6b..404d9d79408 100644 --- a/packages/@react-native-windows/cli/src/codegen.ts +++ b/packages/@react-native-windows/cli/src/codegen.ts @@ -106,7 +106,24 @@ export class CodeGenWindows { 'InvalidCodegenConfig', `Value of ${chalk.bold( 'codegenConfig.windows.cppStringType', - )} package.json should be either 'std::string' or 'std::wstring'`, + )} in package.json should be either 'std::string' or 'std::wstring'`, + ); + } + } + + let allInOne = true; + if (pkgJson.codegenConfig.windows.allInOne) { + switch (pkgJson.codegenConfig.windows.allInOne) { + case 'true': + case 'false': + allInOne = pkgJson.codegenConfig.windows.allInOne; + break; + default: + throw new CodedError( + 'InvalidCodegenConfig', + `Value of ${chalk.bold( + 'codegenConfig.windows.allInOne', + )} in package.json should be either true or false`, ); } } @@ -138,6 +155,7 @@ export class CodeGenWindows { }**/*Native*.[jt]s`, ], cppStringType, + allInOne, libraryName: projectName, methodOnly: false, modulesCxx: generators.indexOf('modulesCxx') !== -1, diff --git a/packages/@react-native-windows/codegen/src/Cli.ts b/packages/@react-native-windows/codegen/src/Cli.ts index 1b192a6016f..1d84eb3bede 100644 --- a/packages/@react-native-windows/codegen/src/Cli.ts +++ b/packages/@react-native-windows/codegen/src/Cli.ts @@ -64,6 +64,11 @@ const argv = yargs.options({ 'C++ string type in generated code, should be "std::string" or "std::wstring"', default: 'std::string', }, + allInOne: { + type: 'boolean', + describe: 'generate custom types and turbo module spec in one file', + default: true, + }, }).argv; if ((argv.file && argv.files) || (!argv.file && !argv.files)) { diff --git a/packages/@react-native-windows/codegen/src/index.ts b/packages/@react-native-windows/codegen/src/index.ts index 782161ba1a9..e39496b17a2 100644 --- a/packages/@react-native-windows/codegen/src/index.ts +++ b/packages/@react-native-windows/codegen/src/index.ts @@ -52,6 +52,7 @@ export interface SharedOptions { namespace: string; outputDirectory: string; cppStringType: CppStringTypes; + allInOne: boolean; } interface Options extends SharedOptions { @@ -216,6 +217,7 @@ export function generate( namespace, outputDirectory, cppStringType, + allInOne, moduleSpecName, schema, }: Options, @@ -240,6 +242,7 @@ export function generate( methodOnly, namespace, cppStringType, + allInOne, }); const generateJsiModuleH = require(path.resolve( @@ -369,6 +372,7 @@ export function runCodeGen(options: CodeGenOptions): boolean { namespace, outputDirectory, cppStringType, + allInOne, } = options; return generate( { @@ -380,6 +384,7 @@ export function runCodeGen(options: CodeGenOptions): boolean { namespace, outputDirectory, cppStringType, + allInOne, moduleSpecName, schema, }, From 510473951e9551bdd91b59e9cdaea6fc962b1d68 Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Tue, 8 Aug 2023 03:58:20 -0700 Subject: [PATCH 03/13] ... --- .../@react-native-windows/cli/src/codegen.ts | 4 ++-- .../codegen/src/generators/GenerateNM2.ts | 22 ++++++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/packages/@react-native-windows/cli/src/codegen.ts b/packages/@react-native-windows/cli/src/codegen.ts index 404d9d79408..e491a08fecf 100644 --- a/packages/@react-native-windows/cli/src/codegen.ts +++ b/packages/@react-native-windows/cli/src/codegen.ts @@ -114,8 +114,8 @@ export class CodeGenWindows { let allInOne = true; if (pkgJson.codegenConfig.windows.allInOne) { switch (pkgJson.codegenConfig.windows.allInOne) { - case 'true': - case 'false': + case true: + case false: allInOne = pkgJson.codegenConfig.windows.allInOne; break; default: diff --git a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts index 66930541eb3..3c130c39850 100644 --- a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts +++ b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts @@ -44,7 +44,6 @@ ${headerTemplate} #include #include #include -#include namespace ::_NAMESPACE_:: { ::_MODULE_CUSTPM_TYPES_:: @@ -56,8 +55,9 @@ namespace ::_NAMESPACE_:: { const moduleOnlyTemplate = ` ${headerTemplate} -#include <::_TYPE_DEFINITION_FILE_NAME_::> +::_TYPE_DEFINITION_INCLUDE_:: #include +#include namespace ::_NAMESPACE_:: { ${specTemplate} @@ -145,6 +145,8 @@ ${errors}`; cppStringType, }); + const customTypesExist = customTypes !== ''; + const replaceContent = function (template: string): string { return template .replace(/::_MODULE_CUSTPM_TYPES_::/g, customTypes) @@ -154,8 +156,10 @@ ${errors}`; .replace(/::_MODULE_MEMBERS_ERRORS_::/g, errors) .replace(/::_MODULE_NAME_::/g, preferredModuleName) .replace( - /::_TYPE_DEFINITION_FILE_NAME_::/g, - `Native${preferredModuleName}Types.g.h`, + /::_TYPE_DEFINITION_INCLUDE_::/g, + customTypesExist + ? `#include ` + : '', ) .replace(/::_NAMESPACE_::/g, namespace); }; @@ -166,10 +170,12 @@ ${errors}`; replaceContent(allInOneTemplate), ); } else { - files.set( - `Native${preferredModuleName}Types.g.h`, - replaceContent(typeOnlyTemplate), - ); + if (customTypesExist) { + files.set( + `Native${preferredModuleName}Types.g.h`, + replaceContent(typeOnlyTemplate), + ); + } files.set( `Native${preferredModuleName}Spec.g.h`, replaceContent(moduleOnlyTemplate), From 06a150c38f5a21be1378b24f1d509e6afa13fbc4 Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Tue, 8 Aug 2023 04:13:38 -0700 Subject: [PATCH 04/13] ... --- packages/@react-native-windows/cli/src/codegen.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/@react-native-windows/cli/src/codegen.ts b/packages/@react-native-windows/cli/src/codegen.ts index e491a08fecf..8ae70b0305f 100644 --- a/packages/@react-native-windows/cli/src/codegen.ts +++ b/packages/@react-native-windows/cli/src/codegen.ts @@ -112,7 +112,7 @@ export class CodeGenWindows { } let allInOne = true; - if (pkgJson.codegenConfig.windows.allInOne) { + if (pkgJson.codegenConfig.windows.allInOne !== undefined) { switch (pkgJson.codegenConfig.windows.allInOne) { case true: case false: From 46723a8c66dda2e6fe26265f62712720b21516b6 Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Tue, 8 Aug 2023 04:25:49 -0700 Subject: [PATCH 05/13] ... --- .../codegen/src/generators/GenerateNM2.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts index 3c130c39850..01501387204 100644 --- a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts +++ b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts @@ -42,13 +42,11 @@ ${headerTemplate} #include #include -#include +#include #include namespace ::_NAMESPACE_:: { ::_MODULE_CUSTPM_TYPES_:: -}; - } // namespace ::_NAMESPACE_:: `; @@ -158,7 +156,7 @@ ${errors}`; .replace( /::_TYPE_DEFINITION_INCLUDE_::/g, customTypesExist - ? `#include ` + ? `#include "Native${preferredModuleName}Types.g.h"` : '', ) .replace(/::_NAMESPACE_::/g, namespace); From 117945de9bdc209dd293796ffa36d94d462a349d Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Tue, 8 Aug 2023 04:28:03 -0700 Subject: [PATCH 06/13] Update sample-apps --- .../codegen/NativeMyModuleSpec.g.h | 8 +------ .../codegen/NativeMyModuleTypes.g.h | 24 +++++++++++++++++++ packages/sample-apps/package.json | 4 +++- 3 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 packages/sample-apps/codegen/NativeMyModuleTypes.g.h diff --git a/packages/sample-apps/codegen/NativeMyModuleSpec.g.h b/packages/sample-apps/codegen/NativeMyModuleSpec.g.h index 4dfd5f7fdcd..60f2cb2076a 100644 --- a/packages/sample-apps/codegen/NativeMyModuleSpec.g.h +++ b/packages/sample-apps/codegen/NativeMyModuleSpec.g.h @@ -8,18 +8,12 @@ */ #pragma once +#include "NativeMyModuleTypes.g.h" #include #include namespace SampleLibraryCodegen { -struct MyModuleSpec_Constants { - bool const1; - double const2; - std::string const3; -}; - - inline winrt::Microsoft::ReactNative::FieldMap GetStructInfo(MyModuleSpec_Constants*) noexcept { winrt::Microsoft::ReactNative::FieldMap fieldMap { {L"const1", &MyModuleSpec_Constants::const1}, diff --git a/packages/sample-apps/codegen/NativeMyModuleTypes.g.h b/packages/sample-apps/codegen/NativeMyModuleTypes.g.h new file mode 100644 index 00000000000..b44cd2cfb38 --- /dev/null +++ b/packages/sample-apps/codegen/NativeMyModuleTypes.g.h @@ -0,0 +1,24 @@ + +/* + * This file is auto-generated from a NativeModule spec file in js. + * + * This is a C++ Spec class that should be used with MakeTurboModuleProvider to register native modules + * in a way that also verifies at compile time that the native module matches the interface required + * by the TurboModule JS spec. + */ +#pragma once + +#include +#include +#include +#include + +namespace SampleLibraryCodegen { + +struct MyModuleSpec_Constants { + bool const1; + double const2; + std::string const3; +}; + +} // namespace SampleLibraryCodegen diff --git a/packages/sample-apps/package.json b/packages/sample-apps/package.json index 512e77f2d6c..f149a644e51 100644 --- a/packages/sample-apps/package.json +++ b/packages/sample-apps/package.json @@ -23,6 +23,7 @@ "@babel/eslint-parser": "^7.20.0", "@babel/runtime": "^7.8.4", "@react-native-windows/codegen": "0.0.0-canary.67", + "@react-native-windows/cli": "0.0.0-canary.183", "@rnw-scripts/babel-react-native-config": "0.0.0", "@rnw-scripts/eslint-config": "1.2.2", "@rnw-scripts/just-task": "2.3.15", @@ -39,7 +40,8 @@ "type": "modules", "jsSrcsDir": "src", "windows": { - "namespace": "SampleLibraryCodegen" + "namespace": "SampleLibraryCodegen", + "allInOne": false } }, "engines": { From 15471710df05288f1a9c891397ed7d2979f8366b Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Tue, 8 Aug 2023 04:28:31 -0700 Subject: [PATCH 07/13] Change files --- ...e-windows-cli-f3143569-d5a6-4a78-bfcc-84628b54933e.json | 7 +++++++ ...ndows-codegen-be2112bc-3d94-4028-88d1-e2bfaae94185.json | 7 +++++++ ...ative-windows-14398591-3062-462e-a9a5-25ca35480231.json | 7 +++++++ 3 files changed, 21 insertions(+) create mode 100644 change/@react-native-windows-cli-f3143569-d5a6-4a78-bfcc-84628b54933e.json create mode 100644 change/@react-native-windows-codegen-be2112bc-3d94-4028-88d1-e2bfaae94185.json create mode 100644 change/react-native-windows-14398591-3062-462e-a9a5-25ca35480231.json diff --git a/change/@react-native-windows-cli-f3143569-d5a6-4a78-bfcc-84628b54933e.json b/change/@react-native-windows-cli-f3143569-d5a6-4a78-bfcc-84628b54933e.json new file mode 100644 index 00000000000..2fd9e7f576a --- /dev/null +++ b/change/@react-native-windows-cli-f3143569-d5a6-4a78-bfcc-84628b54933e.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Add `allInOne` to @rnw/cli and @rnw/codegen", + "packageName": "@react-native-windows/cli", + "email": "53799235+ZihanChen-MSFT@users.noreply.github.com", + "dependentChangeType": "patch" +} diff --git a/change/@react-native-windows-codegen-be2112bc-3d94-4028-88d1-e2bfaae94185.json b/change/@react-native-windows-codegen-be2112bc-3d94-4028-88d1-e2bfaae94185.json new file mode 100644 index 00000000000..a90e0b7750a --- /dev/null +++ b/change/@react-native-windows-codegen-be2112bc-3d94-4028-88d1-e2bfaae94185.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Add `allInOne` to @rnw/cli and @rnw/codegen", + "packageName": "@react-native-windows/codegen", + "email": "53799235+ZihanChen-MSFT@users.noreply.github.com", + "dependentChangeType": "patch" +} diff --git a/change/react-native-windows-14398591-3062-462e-a9a5-25ca35480231.json b/change/react-native-windows-14398591-3062-462e-a9a5-25ca35480231.json new file mode 100644 index 00000000000..f1fe8b88747 --- /dev/null +++ b/change/react-native-windows-14398591-3062-462e-a9a5-25ca35480231.json @@ -0,0 +1,7 @@ +{ + "type": "prerelease", + "comment": "Add `allInOne` to @rnw/cli and @rnw/codegen", + "packageName": "react-native-windows", + "email": "53799235+ZihanChen-MSFT@users.noreply.github.com", + "dependentChangeType": "patch" +} From 3281083cc90d8327c8cf24a74e52f10c00b53f0e Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Tue, 8 Aug 2023 14:50:31 -0700 Subject: [PATCH 08/13] Fix code review comment --- .../@react-native-windows/codegen/src/generators/GenerateNM2.ts | 2 +- packages/sample-apps/codegen/NativeMyModuleSpec.g.h | 2 +- packages/sample-apps/windows/SampleLibraryCPP/MyModule.h | 1 + 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts index 01501387204..6b1b3b81ecf 100644 --- a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts +++ b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts @@ -156,7 +156,7 @@ ${errors}`; .replace( /::_TYPE_DEFINITION_INCLUDE_::/g, customTypesExist - ? `#include "Native${preferredModuleName}Types.g.h"` + ? `// #include "Native${preferredModuleName}Types.g.h" before this file to use the generated type definition` : '', ) .replace(/::_NAMESPACE_::/g, namespace); diff --git a/packages/sample-apps/codegen/NativeMyModuleSpec.g.h b/packages/sample-apps/codegen/NativeMyModuleSpec.g.h index 60f2cb2076a..3613c9b2a15 100644 --- a/packages/sample-apps/codegen/NativeMyModuleSpec.g.h +++ b/packages/sample-apps/codegen/NativeMyModuleSpec.g.h @@ -8,7 +8,7 @@ */ #pragma once -#include "NativeMyModuleTypes.g.h" +// #include "NativeMyModuleTypes.g.h" before this file to use the generated type definition #include #include diff --git a/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h b/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h index 6c68752ebdc..cfbe92f4dc3 100644 --- a/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h +++ b/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h @@ -3,6 +3,7 @@ #pragma once +#include "..\..\codegen\NativeMyModuleTypes.g.h" #include "..\..\codegen\NativeMyModuleSpec.g.h" #include "DebugHelpers.h" From d16a86ccbc39e1cb3b7e6764e3ebd0e6d7a1b39e Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Mon, 14 Aug 2023 13:52:34 -0700 Subject: [PATCH 09/13] Change allInOne:true to separateDataTypes:false --- .../@react-native-windows/cli/src/codegen.ts | 12 ++++++------ .../@react-native-windows/codegen/src/Cli.ts | 6 +++--- .../codegen/src/generators/GenerateNM2.ts | 16 ++++++++-------- .../@react-native-windows/codegen/src/index.ts | 10 +++++----- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/packages/@react-native-windows/cli/src/codegen.ts b/packages/@react-native-windows/cli/src/codegen.ts index 8ae70b0305f..1fc17db94f4 100644 --- a/packages/@react-native-windows/cli/src/codegen.ts +++ b/packages/@react-native-windows/cli/src/codegen.ts @@ -111,18 +111,18 @@ export class CodeGenWindows { } } - let allInOne = true; - if (pkgJson.codegenConfig.windows.allInOne !== undefined) { - switch (pkgJson.codegenConfig.windows.allInOne) { + let separateDataTypes = true; + if (pkgJson.codegenConfig.windows.separateDataTypes !== undefined) { + switch (pkgJson.codegenConfig.windows.separateDataTypes) { case true: case false: - allInOne = pkgJson.codegenConfig.windows.allInOne; + separateDataTypes = pkgJson.codegenConfig.windows.separateDataTypes; break; default: throw new CodedError( 'InvalidCodegenConfig', `Value of ${chalk.bold( - 'codegenConfig.windows.allInOne', + 'codegenConfig.windows.separateDataTypes', )} in package.json should be either true or false`, ); } @@ -155,7 +155,7 @@ export class CodeGenWindows { }**/*Native*.[jt]s`, ], cppStringType, - allInOne, + separateDataTypes, libraryName: projectName, methodOnly: false, modulesCxx: generators.indexOf('modulesCxx') !== -1, diff --git a/packages/@react-native-windows/codegen/src/Cli.ts b/packages/@react-native-windows/codegen/src/Cli.ts index 1d84eb3bede..14e2f4473d9 100644 --- a/packages/@react-native-windows/codegen/src/Cli.ts +++ b/packages/@react-native-windows/codegen/src/Cli.ts @@ -64,10 +64,10 @@ const argv = yargs.options({ 'C++ string type in generated code, should be "std::string" or "std::wstring"', default: 'std::string', }, - allInOne: { + separateDataTypes: { type: 'boolean', - describe: 'generate custom types and turbo module spec in one file', - default: true, + describe: 'generate data types in a separate file', + default: false, }, }).argv; diff --git a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts index 6b1b3b81ecf..f1397813868 100644 --- a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts +++ b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts @@ -82,12 +82,12 @@ export function createNM2Generator({ methodOnly, namespace, cppStringType, - allInOne, + separateDataTypes, }: { methodOnly: boolean; namespace: string; cppStringType: CppStringTypes; - allInOne: boolean; + separateDataTypes: boolean; }) { return ( _libraryName: string, @@ -162,12 +162,7 @@ ${errors}`; .replace(/::_NAMESPACE_::/g, namespace); }; - if (allInOne) { - files.set( - `Native${preferredModuleName}Spec.g.h`, - replaceContent(allInOneTemplate), - ); - } else { + if (separateDataTypes) { if (customTypesExist) { files.set( `Native${preferredModuleName}Types.g.h`, @@ -178,6 +173,11 @@ ${errors}`; `Native${preferredModuleName}Spec.g.h`, replaceContent(moduleOnlyTemplate), ); + } else { + files.set( + `Native${preferredModuleName}Spec.g.h`, + replaceContent(allInOneTemplate), + ); } } } diff --git a/packages/@react-native-windows/codegen/src/index.ts b/packages/@react-native-windows/codegen/src/index.ts index e39496b17a2..75e7712410d 100644 --- a/packages/@react-native-windows/codegen/src/index.ts +++ b/packages/@react-native-windows/codegen/src/index.ts @@ -52,7 +52,7 @@ export interface SharedOptions { namespace: string; outputDirectory: string; cppStringType: CppStringTypes; - allInOne: boolean; + separateDataTypes: boolean; } interface Options extends SharedOptions { @@ -217,7 +217,7 @@ export function generate( namespace, outputDirectory, cppStringType, - allInOne, + separateDataTypes, moduleSpecName, schema, }: Options, @@ -242,7 +242,7 @@ export function generate( methodOnly, namespace, cppStringType, - allInOne, + separateDataTypes, }); const generateJsiModuleH = require(path.resolve( @@ -372,7 +372,7 @@ export function runCodeGen(options: CodeGenOptions): boolean { namespace, outputDirectory, cppStringType, - allInOne, + separateDataTypes, } = options; return generate( { @@ -384,7 +384,7 @@ export function runCodeGen(options: CodeGenOptions): boolean { namespace, outputDirectory, cppStringType, - allInOne, + separateDataTypes, moduleSpecName, schema, }, From c96ab14bb3142c3bf05cd21f7664ee9d85e85d7a Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Mon, 14 Aug 2023 13:53:19 -0700 Subject: [PATCH 10/13] ... --- ...native-windows-cli-f3143569-d5a6-4a78-bfcc-84628b54933e.json | 2 +- ...ve-windows-codegen-be2112bc-3d94-4028-88d1-e2bfaae94185.json | 2 +- ...act-native-windows-14398591-3062-462e-a9a5-25ca35480231.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/change/@react-native-windows-cli-f3143569-d5a6-4a78-bfcc-84628b54933e.json b/change/@react-native-windows-cli-f3143569-d5a6-4a78-bfcc-84628b54933e.json index 2fd9e7f576a..9f2ae718711 100644 --- a/change/@react-native-windows-cli-f3143569-d5a6-4a78-bfcc-84628b54933e.json +++ b/change/@react-native-windows-cli-f3143569-d5a6-4a78-bfcc-84628b54933e.json @@ -1,6 +1,6 @@ { "type": "prerelease", - "comment": "Add `allInOne` to @rnw/cli and @rnw/codegen", + "comment": "Add `separateDataFiles` to @rnw/cli and @rnw/codegen", "packageName": "@react-native-windows/cli", "email": "53799235+ZihanChen-MSFT@users.noreply.github.com", "dependentChangeType": "patch" diff --git a/change/@react-native-windows-codegen-be2112bc-3d94-4028-88d1-e2bfaae94185.json b/change/@react-native-windows-codegen-be2112bc-3d94-4028-88d1-e2bfaae94185.json index a90e0b7750a..641ae8bc657 100644 --- a/change/@react-native-windows-codegen-be2112bc-3d94-4028-88d1-e2bfaae94185.json +++ b/change/@react-native-windows-codegen-be2112bc-3d94-4028-88d1-e2bfaae94185.json @@ -1,6 +1,6 @@ { "type": "prerelease", - "comment": "Add `allInOne` to @rnw/cli and @rnw/codegen", + "comment": "Add `separateDataFiles` to @rnw/cli and @rnw/codegen", "packageName": "@react-native-windows/codegen", "email": "53799235+ZihanChen-MSFT@users.noreply.github.com", "dependentChangeType": "patch" diff --git a/change/react-native-windows-14398591-3062-462e-a9a5-25ca35480231.json b/change/react-native-windows-14398591-3062-462e-a9a5-25ca35480231.json index f1fe8b88747..0511d9cf3ca 100644 --- a/change/react-native-windows-14398591-3062-462e-a9a5-25ca35480231.json +++ b/change/react-native-windows-14398591-3062-462e-a9a5-25ca35480231.json @@ -1,6 +1,6 @@ { "type": "prerelease", - "comment": "Add `allInOne` to @rnw/cli and @rnw/codegen", + "comment": "Add `separateDataFiles` to @rnw/cli and @rnw/codegen", "packageName": "react-native-windows", "email": "53799235+ZihanChen-MSFT@users.noreply.github.com", "dependentChangeType": "patch" From 19dc3a0cdf05a924b2d4b4583ce61efb913a0004 Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Mon, 14 Aug 2023 13:57:33 -0700 Subject: [PATCH 11/13] Change Types.g.h to DataTypes.g.h --- .../codegen/src/generators/GenerateNM2.ts | 4 ++-- .../{NativeMyModuleTypes.g.h => NativeMyModuleDataTypes.g.h} | 0 packages/sample-apps/codegen/NativeMyModuleSpec.g.h | 2 +- packages/sample-apps/package.json | 2 +- packages/sample-apps/windows/SampleLibraryCPP/MyModule.h | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) rename packages/sample-apps/codegen/{NativeMyModuleTypes.g.h => NativeMyModuleDataTypes.g.h} (100%) diff --git a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts index f1397813868..fa1e12f4f94 100644 --- a/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts +++ b/packages/@react-native-windows/codegen/src/generators/GenerateNM2.ts @@ -156,7 +156,7 @@ ${errors}`; .replace( /::_TYPE_DEFINITION_INCLUDE_::/g, customTypesExist - ? `// #include "Native${preferredModuleName}Types.g.h" before this file to use the generated type definition` + ? `// #include "Native${preferredModuleName}DataTypes.g.h" before this file to use the generated type definition` : '', ) .replace(/::_NAMESPACE_::/g, namespace); @@ -165,7 +165,7 @@ ${errors}`; if (separateDataTypes) { if (customTypesExist) { files.set( - `Native${preferredModuleName}Types.g.h`, + `Native${preferredModuleName}DataTypes.g.h`, replaceContent(typeOnlyTemplate), ); } diff --git a/packages/sample-apps/codegen/NativeMyModuleTypes.g.h b/packages/sample-apps/codegen/NativeMyModuleDataTypes.g.h similarity index 100% rename from packages/sample-apps/codegen/NativeMyModuleTypes.g.h rename to packages/sample-apps/codegen/NativeMyModuleDataTypes.g.h diff --git a/packages/sample-apps/codegen/NativeMyModuleSpec.g.h b/packages/sample-apps/codegen/NativeMyModuleSpec.g.h index 3613c9b2a15..5b5861629ab 100644 --- a/packages/sample-apps/codegen/NativeMyModuleSpec.g.h +++ b/packages/sample-apps/codegen/NativeMyModuleSpec.g.h @@ -8,7 +8,7 @@ */ #pragma once -// #include "NativeMyModuleTypes.g.h" before this file to use the generated type definition +// #include "NativeMyModuleDataTypes.g.h" before this file to use the generated type definition #include #include diff --git a/packages/sample-apps/package.json b/packages/sample-apps/package.json index f149a644e51..d4cbc9ee379 100644 --- a/packages/sample-apps/package.json +++ b/packages/sample-apps/package.json @@ -41,7 +41,7 @@ "jsSrcsDir": "src", "windows": { "namespace": "SampleLibraryCodegen", - "allInOne": false + "separateDataFiles": true } }, "engines": { diff --git a/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h b/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h index cfbe92f4dc3..c29423cfb56 100644 --- a/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h +++ b/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h @@ -3,7 +3,7 @@ #pragma once -#include "..\..\codegen\NativeMyModuleTypes.g.h" +#include "..\..\codegen\NativeMyModuleDataTypes.g.h" #include "..\..\codegen\NativeMyModuleSpec.g.h" #include "DebugHelpers.h" From 1491b3785f837954bd86424eaa5701e0d233d6b1 Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Mon, 14 Aug 2023 14:00:39 -0700 Subject: [PATCH 12/13] ... --- packages/sample-apps/windows/SampleLibraryCPP/MyModule.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h b/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h index c29423cfb56..f7a9276b775 100644 --- a/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h +++ b/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h @@ -3,6 +3,9 @@ #pragma once +// a demo of codegenConfig.windows.separateDataTypes to true in package.json +// it generates data types in a separate files, but one doesn't include another +// including them in a expected order is required #include "..\..\codegen\NativeMyModuleDataTypes.g.h" #include "..\..\codegen\NativeMyModuleSpec.g.h" #include "DebugHelpers.h" From c2908bf0e3487f951199710b17487fcf0e28f6eb Mon Sep 17 00:00:00 2001 From: "Zihan Chen (MSFT)" <53799235+ZihanChen-MSFT@users.noreply.github.com> Date: Mon, 14 Aug 2023 14:05:42 -0700 Subject: [PATCH 13/13] Update MyModule.h --- packages/sample-apps/windows/SampleLibraryCPP/MyModule.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h b/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h index f7a9276b775..a2f99412c75 100644 --- a/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h +++ b/packages/sample-apps/windows/SampleLibraryCPP/MyModule.h @@ -3,8 +3,8 @@ #pragma once -// a demo of codegenConfig.windows.separateDataTypes to true in package.json -// it generates data types in a separate files, but one doesn't include another +// a demo of setting codegenConfig.windows.separateDataTypes to true in package.json +// it generates data types in a separate file, but one doesn't include another // including them in a expected order is required #include "..\..\codegen\NativeMyModuleDataTypes.g.h" #include "..\..\codegen\NativeMyModuleSpec.g.h"