Skip to content

Commit b02696d

Browse files
Copilotanupriya13
andcommitted
Automatically detect existing codegen spec files instead of using hardcoded names
Co-authored-by: anupriya13 <[email protected]>
1 parent 7c6247f commit b02696d

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

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: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
#if __has_include("codegen/Native{{ pascalName }}DataTypes.g.h")
77
#include "codegen/Native{{ pascalName }}DataTypes.g.h"
88
#endif
9-
// TODO: Update this include to match your actual TurboModule name
10-
// For example, if your module is named 'MyCustomModule', use:
11-
// #include "codegen/NativeMyCustomModuleSpec.g.h"
9+
{{#hasExistingSpecFiles}}
10+
#include "codegen/{{ firstSpecFile }}"
11+
{{/hasExistingSpecFiles}}
12+
{{^hasExistingSpecFiles}}
1213
#if __has_include("codegen/Native{{ pascalName }}Spec.g.h")
1314
#include "codegen/Native{{ pascalName }}Spec.g.h"
1415
#endif
16+
{{/hasExistingSpecFiles}}
1517

1618
#include "NativeModules.h"
1719

@@ -23,16 +25,14 @@ namespace winrt::{{ namespaceCpp }}
2325
REACT_MODULE({{ pascalName }})
2426
struct {{ pascalName }}
2527
{
26-
// TODO: Update this to match your actual TurboModule spec name
27-
// For example, if your module is named 'MyCustomModule', use:
28-
// using ModuleSpec = {{ namespaceCpp }}Codegen::MyCustomModuleSpec;
29-
#ifdef __has_include
28+
{{#hasExistingSpecFiles}}
29+
using ModuleSpec = {{ namespaceCpp }}Codegen::{{ firstSpecName }};
30+
{{/hasExistingSpecFiles}}
31+
{{^hasExistingSpecFiles}}
3032
#if __has_include("codegen/Native{{ pascalName }}Spec.g.h")
3133
using ModuleSpec = {{ namespaceCpp }}Codegen::{{ pascalName }}Spec;
3234
#endif
33-
#else
34-
using ModuleSpec = {{ namespaceCpp }}Codegen::{{ pascalName }}Spec;
35-
#endif
35+
{{/hasExistingSpecFiles}}
3636

3737
REACT_INIT(Initialize)
3838
void Initialize(React::ReactContext const &reactContext) noexcept;

0 commit comments

Comments
 (0)