Skip to content
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"comment": "Fix init-windows template to automatically detect existing codegen spec files",
"type": "prerelease",
"packageName": "react-native-windows",
"email": "[email protected]",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ function MultiColumnExample(): React.Node {
return {length, offset: length * index, index};
};

// eslint-disable-next-line react/no-unstable-nested-components
const _renderItemComponent = ({
item,
}: ListRenderItemInfo<any | Item>): $FlowFixMe => {
Expand Down
24 changes: 24 additions & 0 deletions vnext/templates/cpp-lib/template.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion vnext/templates/cpp-lib/windows/MyLib/MyLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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;
Expand Down