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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Use codegenConfig name for C++ library template codegen includes",
"packageName": "react-native-windows",
"email": "vivekjm77@gmail.com",
"dependentChangeType": "patch"
}
47 changes: 25 additions & 22 deletions vnext/templates/cpp-lib/template.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

const crypto = require('crypto');
const existsSync = require('fs').existsSync;
const readFileSync = require('fs').readFileSync;
const path = require('path');
const username = require('username');
const util = require('util');
Expand All @@ -16,6 +17,23 @@ const glob = util.promisify(require('glob'));

const templateUtils = require('../templateUtils');

function getCodegenProjectName(projectRoot, fallbackName) {
try {
const packageJson = JSON.parse(
readFileSync(path.join(projectRoot, 'package.json'), 'utf8'),
);
const codegenName = packageJson?.codegenConfig?.name;
if (typeof codegenName === 'string' && codegenName.length > 0) {
return codegenName.replace(/[^a-zA-Z]/g, '');
}
} catch (e) {
// Fall back to the existing project-name behavior if package.json is missing
// or malformed. Other template paths still handle package.json validation.
}

return fallbackName;
}

function resolveArgs(config = {}, options = {}) {
const projectRoot = config?.root ?? process.cwd();

Expand Down Expand Up @@ -85,6 +103,10 @@ async function getFileMappings(config = {}, options = {}) {
libConfig?.project?.windows?.projects[0]?.projectName ??
libOptions?.name ??
'MyLib';
const codegenProjectName = getCodegenProjectName(
projectRoot,
`${templateUtils.pascalCase(projectName)}Spec`,
);
const namespace = libOptions?.namespace ?? projectName;
const namespaceCpp = namespace.replace(/\./g, '::');
const projectGuid =
Expand All @@ -93,24 +115,6 @@ 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 @@ -119,14 +123,13 @@ async function getFileMappings(config = {}, options = {}) {

name: projectName,
pascalName: templateUtils.pascalCase(projectName),
codegenProjectName,
namespace: namespace,
namespaceCpp: namespaceCpp,

// Codegen spec files information
existingSpecFiles: existingSpecFiles,
hasExistingSpecFiles: existingSpecFiles.length > 0,
firstSpecFile: existingSpecFiles.length > 0 ? existingSpecFiles[0] : null,
firstSpecName: firstSpecName,
codegenSpecFile: `Native${codegenProjectName}.g.h`,
codegenDataTypesFile: `Native${codegenProjectName}DataTypes.g.h`,

rnwVersion: rnwVersion,
rnwPathFromProjectRoot: path
Expand Down
30 changes: 8 additions & 22 deletions vnext/templates/cpp-lib/windows/MyLib/MyLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,13 @@
#include "pch.h"
#include "resource.h"

#if __has_include("codegen/Native{{ pascalName }}DataTypes.g.h")
#include "codegen/Native{{ pascalName }}DataTypes.g.h"
#if __has_include("codegen/{{ codegenDataTypesFile }}")
#include "codegen/{{ codegenDataTypesFile }}"
#endif
// Note: The following lines use Mustache template syntax 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"

#if __has_include("codegen/{{ codegenSpecFile }}")
#include "codegen/{{ codegenSpecFile }}"
#endif
{{/hasExistingSpecFiles}}

#include "NativeModules.h"

Expand All @@ -28,16 +21,9 @@ namespace winrt::{{ namespaceCpp }}
REACT_MODULE({{ pascalName }})
struct {{ pascalName }}
{
// Note: Mustache template syntax below 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;
#if __has_include("codegen/{{ codegenSpecFile }}")
using ModuleSpec = {{ namespaceCpp }}Codegen::{{ codegenProjectName }};
#endif
{{/hasExistingSpecFiles}}

REACT_INIT(Initialize)
void Initialize(React::ReactContext const &reactContext) noexcept;
Expand All @@ -49,4 +35,4 @@ struct {{ pascalName }}
React::ReactContext m_context;
};

} // namespace winrt::{{ namespaceCpp }}
} // namespace winrt::{{ namespaceCpp }}
Loading