Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Please add a preceding comment about these Mustache template nuances as these are foreign to regular C++ programmers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added explanatory comments about the Mustache template syntax before both conditional include blocks to help C++ developers understand that these constructs will be processed during project generation. (commit a098073)

#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
Loading