diff --git a/vnext/templates/cpp-lib/template.config.js b/vnext/templates/cpp-lib/template.config.js index 592005498f9..46e37c4d197 100644 --- a/vnext/templates/cpp-lib/template.config.js +++ b/vnext/templates/cpp-lib/template.config.js @@ -93,6 +93,24 @@ async function getFileMappings(config = {}, options = {}) { .replace('}', '') ?? crypto.randomUUID(); const currentUser = username.sync(); // Gets the current username depending on the platform. + // Check for existing codegen spec files + const codegenPath = path.join(projectRoot, 'windows', projectName, 'codegen'); + let existingSpecFiles = []; + let firstSpecName = null; + if (existsSync(codegenPath)) { + try { + const specFiles = await glob('*Spec.g.h', { cwd: codegenPath }); + existingSpecFiles = specFiles; + if (specFiles.length > 0) { + // Extract the spec name from filename (e.g., "NativeMyModuleSpec.g.h" -> "MyModuleSpec") + const firstFile = specFiles[0]; + firstSpecName = firstFile.replace(/^Native/, '').replace(/\.g\.h$/, ''); + } + } catch (e) { + // If we can't read the codegen directory, continue with empty array + } + } + const cppNugetPackages = []; const replacements = { @@ -104,6 +122,12 @@ async function getFileMappings(config = {}, options = {}) { namespace: namespace, namespaceCpp: namespaceCpp, + // Codegen spec files information + existingSpecFiles: existingSpecFiles, + hasExistingSpecFiles: existingSpecFiles.length > 0, + firstSpecFile: existingSpecFiles.length > 0 ? existingSpecFiles[0] : null, + firstSpecName: firstSpecName, + rnwVersion: rnwVersion, rnwPathFromProjectRoot: path .relative(projectRoot, rnwPath) diff --git a/vnext/templates/cpp-lib/windows/MyLib/MyLib.h b/vnext/templates/cpp-lib/windows/MyLib/MyLib.h index 01c66a13e79..6ebc8576f1a 100644 --- a/vnext/templates/cpp-lib/windows/MyLib/MyLib.h +++ b/vnext/templates/cpp-lib/windows/MyLib/MyLib.h @@ -6,7 +6,17 @@ #if __has_include("codegen/Native{{ pascalName }}DataTypes.g.h") #include "codegen/Native{{ pascalName }}DataTypes.g.h" #endif -#include "codegen/Native{{ pascalName }}Spec.g.h" +// Note: The following lines use Mustache template syntax ({{#variable}}, {{^variable}}, {{/variable}}) +// which will be processed during project generation to produce standard C++ code. +// If existing codegen spec files are found, use the actual filename; otherwise use conditional includes. +{{#hasExistingSpecFiles}} +#include "codegen/{{ firstSpecFile }}" +{{/hasExistingSpecFiles}} +{{^hasExistingSpecFiles}} +#if __has_include("codegen/Native{{ pascalName }}Spec.g.h") + #include "codegen/Native{{ pascalName }}Spec.g.h" +#endif +{{/hasExistingSpecFiles}} #include "NativeModules.h" @@ -18,7 +28,16 @@ namespace winrt::{{ namespaceCpp }} REACT_MODULE({{ pascalName }}) struct {{ pascalName }} { + // Note: Mustache template syntax below ({{#variable}}, {{^variable}}, {{/variable}}) will be + // processed during project generation to produce standard C++ code based on detected codegen files. +{{#hasExistingSpecFiles}} + using ModuleSpec = {{ namespaceCpp }}Codegen::{{ firstSpecName }}; +{{/hasExistingSpecFiles}} +{{^hasExistingSpecFiles}} +#if __has_include("codegen/Native{{ pascalName }}Spec.g.h") using ModuleSpec = {{ namespaceCpp }}Codegen::{{ pascalName }}Spec; +#endif +{{/hasExistingSpecFiles}} REACT_INIT(Initialize) void Initialize(React::ReactContext const &reactContext) noexcept;