Skip to content
Draft
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
16 changes: 15 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,14 @@
#if __has_include("codegen/Native{{ pascalName }}DataTypes.g.h")
#include "codegen/Native{{ pascalName }}DataTypes.g.h"
#endif
#include "codegen/Native{{ pascalName }}Spec.g.h"
{{#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 +25,14 @@ namespace winrt::{{ namespaceCpp }}
REACT_MODULE({{ pascalName }})
struct {{ pascalName }}
{
{{#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