Skip to content

Commit 3340bda

Browse files
Copilotanupriya13sundaramramaswamy
authored andcommitted
Fix init-windows template to automatically detect existing codegen spec files (#15096)
* Initial plan * Fix init-windows template to use conditional codegen includes Co-authored-by: anupriya13 <[email protected]> * Automatically detect existing codegen spec files instead of using hardcoded names Co-authored-by: anupriya13 <[email protected]> * Add explanatory comments about Mustache template syntax for C++ developers Co-authored-by: sundaramramaswamy <[email protected]> * Change files * Run yarn change prerelease and yarn lint:fix Co-authored-by: anupriya13 <[email protected]> * Revert unrelated changes to FlatList-multiColumn.windows.js Co-authored-by: anupriya13 <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: anupriya13 <[email protected]> Co-authored-by: sundaramramaswamy <[email protected]> Co-authored-by: Sundaram Ramaswamy <[email protected]>
1 parent e23caae commit 3340bda

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"comment": "Fix init-windows template to automatically detect existing codegen spec files",
3+
"type": "prerelease",
4+
"packageName": "react-native-windows",
5+
"email": "[email protected]",
6+
"dependentChangeType": "patch"
7+
}

vnext/templates/cpp-lib/template.config.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,24 @@ async function getFileMappings(config = {}, options = {}) {
9393
.replace('}', '') ?? crypto.randomUUID();
9494
const currentUser = username.sync(); // Gets the current username depending on the platform.
9595

96+
// Check for existing codegen spec files
97+
const codegenPath = path.join(projectRoot, 'windows', projectName, 'codegen');
98+
let existingSpecFiles = [];
99+
let firstSpecName = null;
100+
if (existsSync(codegenPath)) {
101+
try {
102+
const specFiles = await glob('*Spec.g.h', {cwd: codegenPath});
103+
existingSpecFiles = specFiles;
104+
if (specFiles.length > 0) {
105+
// Extract the spec name from filename (e.g., "NativeMyModuleSpec.g.h" -> "MyModuleSpec")
106+
const firstFile = specFiles[0];
107+
firstSpecName = firstFile.replace(/^Native/, '').replace(/\.g\.h$/, '');
108+
}
109+
} catch (e) {
110+
// If we can't read the codegen directory, continue with empty array
111+
}
112+
}
113+
96114
const cppNugetPackages = [];
97115

98116
const replacements = {
@@ -104,6 +122,12 @@ async function getFileMappings(config = {}, options = {}) {
104122
namespace: namespace,
105123
namespaceCpp: namespaceCpp,
106124

125+
// Codegen spec files information
126+
existingSpecFiles: existingSpecFiles,
127+
hasExistingSpecFiles: existingSpecFiles.length > 0,
128+
firstSpecFile: existingSpecFiles.length > 0 ? existingSpecFiles[0] : null,
129+
firstSpecName: firstSpecName,
130+
107131
rnwVersion: rnwVersion,
108132
rnwPathFromProjectRoot: path
109133
.relative(projectRoot, rnwPath)

vnext/templates/cpp-lib/windows/MyLib/MyLib.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,17 @@
66
#if __has_include("codegen/Native{{ pascalName }}DataTypes.g.h")
77
#include "codegen/Native{{ pascalName }}DataTypes.g.h"
88
#endif
9-
#include "codegen/Native{{ pascalName }}Spec.g.h"
9+
// Note: The following lines use Mustache template syntax which will be processed during
10+
// project generation to produce standard C++ code. If existing codegen spec files are found,
11+
// use the actual filename; otherwise use conditional includes.
12+
{{#hasExistingSpecFiles}}
13+
#include "codegen/{{ firstSpecFile }}"
14+
{{/hasExistingSpecFiles}}
15+
{{^hasExistingSpecFiles}}
16+
#if __has_include("codegen/Native{{ pascalName }}Spec.g.h")
17+
#include "codegen/Native{{ pascalName }}Spec.g.h"
18+
#endif
19+
{{/hasExistingSpecFiles}}
1020

1121
#include "NativeModules.h"
1222

@@ -18,7 +28,16 @@ namespace winrt::{{ namespaceCpp }}
1828
REACT_MODULE({{ pascalName }})
1929
struct {{ pascalName }}
2030
{
31+
// Note: Mustache template syntax below will be processed during project generation
32+
// to produce standard C++ code based on detected codegen files.
33+
{{#hasExistingSpecFiles}}
34+
using ModuleSpec = {{ namespaceCpp }}Codegen::{{ firstSpecName }};
35+
{{/hasExistingSpecFiles}}
36+
{{^hasExistingSpecFiles}}
37+
#if __has_include("codegen/Native{{ pascalName }}Spec.g.h")
2138
using ModuleSpec = {{ namespaceCpp }}Codegen::{{ pascalName }}Spec;
39+
#endif
40+
{{/hasExistingSpecFiles}}
2241

2342
REACT_INIT(Initialize)
2443
void Initialize(React::ReactContext const &reactContext) noexcept;

0 commit comments

Comments
 (0)