Skip to content

Commit 7b9b9e7

Browse files
authored
fix(Windows): Support autolinking (#222)
1 parent 30ff688 commit 7b9b9e7

File tree

7 files changed

+101
-24
lines changed

7 files changed

+101
-24
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// AutolinkedNativeModules.g.cpp contents generated by "react-native autolink-windows"
2+
// clang-format off
3+
#include "pch.h"
4+
#include "AutolinkedNativeModules.g.h"
5+
6+
namespace winrt::Microsoft::ReactNative
7+
{
8+
9+
void RegisterAutolinkedNativeModulePackages(winrt::Windows::Foundation::Collections::IVector<winrt::Microsoft::ReactNative::IReactPackageProvider> const& packageProviders)
10+
{
11+
UNREFERENCED_PARAMETER(packageProviders);
12+
}
13+
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Copyright (c) Microsoft Corporation
3+
//
4+
// This source code is licensed under the MIT license found in the
5+
// LICENSE file in the root directory of this source tree.
6+
//
7+
8+
#pragma once
9+
10+
#include <winrt/Microsoft.ReactNative.h>
11+
#include <winrt/Windows.Foundation.Collections.h>
12+
13+
namespace winrt::Microsoft::ReactNative
14+
{
15+
void RegisterAutolinkedNativeModulePackages(
16+
Windows::Foundation::Collections::IVector<IReactPackageProvider> const &);
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<!-- AutolinkedNativeModules.g.targets contents generated by "react-native autolink-windows" -->
4+
<ItemGroup>
5+
</ItemGroup>
6+
</Project>

windows/ReactTestApp/ReactInstance.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <winrt/Windows.Web.Http.Headers.h>
1515

16+
#include "AutolinkedNativeModules.g.h"
1617
#include "ReactPackageProvider.h"
1718

1819
using ReactTestApp::ReactInstance;
@@ -24,6 +25,8 @@ using winrt::Windows::Web::Http::HttpClient;
2425
ReactInstance::ReactInstance()
2526
{
2627
reactNativeHost_.PackageProviders().Append(winrt::make<ReactPackageProvider>());
28+
winrt::Microsoft::ReactNative::RegisterAutolinkedNativeModulePackages(
29+
reactNativeHost_.PackageProviders());
2730
}
2831

2932
void ReactInstance::LoadJSBundleFrom(JSBundleSource source)

windows/ReactTestApp/ReactTestApp.vcxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<ClInclude Include="$(ReactTestAppDir)\App.h">
142142
<DependentUpon>$(ReactTestAppDir)\App.xaml</DependentUpon>
143143
</ClInclude>
144+
<ClInclude Include="$(ReactTestAppDir)\AutolinkedNativeModules.g.h" />
144145
<ClInclude Include="$(ReactTestAppDir)\MainPage.h">
145146
<DependentUpon>$(ReactTestAppDir)\MainPage.xaml</DependentUpon>
146147
</ClInclude>
@@ -194,11 +195,12 @@
194195
<ClCompile Include="$(ReactTestAppDir)\App.cpp">
195196
<DependentUpon>$(ReactTestAppDir)\App.xaml</DependentUpon>
196197
</ClCompile>
198+
<ClCompile Include="$(ProjectDir)\AutolinkedNativeModules.g.cpp" />
197199
<ClCompile Include="$(ReactTestAppDir)\MainPage.cpp">
198200
<DependentUpon>$(ReactTestAppDir)\MainPage.xaml</DependentUpon>
199201
</ClCompile>
200-
<ClCompile Include="$(GeneratedFilesDir)module.g.cpp" />
201202
<ClCompile Include="$(ReactTestAppDir)\Manifest.cpp" />
203+
<ClCompile Include="$(GeneratedFilesDir)module.g.cpp" />
202204
<ClCompile Include="$(ReactTestAppDir)\ReactInstance.cpp" />
203205
<ClCompile Include="$(ReactTestAppDir)\ReactPackageProvider.cpp" />
204206
</ItemGroup>

windows/ReactTestApp/ReactTestApp.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,21 @@
1313
<ItemGroup>
1414
<ClCompile Include="$(ReactTestAppDir)\pch.cpp" />
1515
<ClCompile Include="$(ReactTestAppDir)\App.cpp" />
16+
<ClCompile Include="$(ProjectDir)\AutolinkedNativeModules.g.cpp" />
1617
<ClCompile Include="$(ReactTestAppDir)\MainPage.cpp" />
1718
<ClCompile Include="$(ReactTestAppDir)\Manifest.cpp" />
1819
<ClCompile Include="$(ReactTestAppDir)\ReactInstance.cpp" />
20+
<ClCompile Include="$(ReactTestAppDir)\ReactPackageProvider.cpp" />
1921
<ClCompile Include="$(GeneratedFilesDir)module.g.cpp" />
2022
</ItemGroup>
2123
<ItemGroup>
2224
<ClInclude Include="$(ReactTestAppDir)\pch.h" />
25+
<ClInclude Include="$(ReactTestAppDir)\App.h" />
26+
<ClInclude Include="$(ReactTestAppDir)\AutolinkedNativeModules.g.h" />
27+
<ClInclude Include="$(ReactTestAppDir)\MainPage.h" />
2328
<ClInclude Include="$(ReactTestAppDir)\Manifest.h" />
2429
<ClInclude Include="$(ReactTestAppDir)\ReactInstance.h" />
30+
<ClInclude Include="$(ReactTestAppDir)\ReactPackageProvider.h" />
2531
</ItemGroup>
2632
<ItemGroup>
2733
<Text Include="$(ProjectRootDir)\app.json">

windows/test-app.js

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ function copyAndReplace(srcPath, destPath, replacements = {}) {
163163

164164
if (binaryExtensions.includes(path.extname(srcPath))) {
165165
// Binary file
166-
fs.copyFile(srcPath, destPath, throwOnError);
166+
return fs.copyFile(srcPath, destPath, throwOnError);
167167
} else {
168168
// Text file
169-
fs.writeFile(
169+
return fs.writeFile(
170170
destPath,
171171
replaceContent(
172172
fs.readFileSync(srcPath, { encoding: "utf8" }),
@@ -214,9 +214,10 @@ function getBundleResources(manifestFilePath, projectFilesDestPath) {
214214
/**
215215
* Generates Visual Studio solution.
216216
* @param {string} destPath Destination path.
217+
* @param {boolean} [noAutolink] Skip autolinking.
217218
* @returns {string | undefined} An error message; `undefined` otherwise.
218219
*/
219-
function generateSolution(destPath) {
220+
function generateSolution(destPath, noAutolink) {
220221
if (!destPath) {
221222
throw "Missing or invalid destination path";
222223
}
@@ -264,14 +265,16 @@ function generateSolution(destPath) {
264265
"\\$\\(BundleFileContentPaths\\)": bundleFileContent,
265266
};
266267

267-
[
268+
const copyTasks = [
269+
"AutolinkedNativeModules.g.cpp",
270+
"AutolinkedNativeModules.g.targets",
268271
"Package.appxmanifest",
269272
"packages.config",
270273
"PropertySheet.props",
271274
"ReactTestApp_TemporaryKey.pfx",
272275
"ReactTestApp.vcxproj.filters",
273276
"ReactTestApp.vcxproj",
274-
].forEach((file) =>
277+
].map((file) =>
275278
copyAndReplace(
276279
path.join(__dirname, projDir, file),
277280
path.join(projectFilesDestPath, file),
@@ -314,7 +317,11 @@ function generateSolution(destPath) {
314317
);
315318
} else {
316319
const mustache = require("mustache");
317-
fs.writeFile(
320+
const reactTestAppProjectPath = path.join(
321+
projectFilesDestPath,
322+
"ReactTestApp.vcxproj"
323+
);
324+
const solutionTask = fs.writeFile(
318325
path.join(destPath, `${appName}.sln`),
319326
mustache
320327
.render(
@@ -323,10 +330,7 @@ function generateSolution(destPath) {
323330
)
324331
.replace(
325332
"ReactTestApp\\ReactTestApp.vcxproj",
326-
path.relative(
327-
destPath,
328-
path.join(projectFilesDestPath, "ReactTestApp.vcxproj")
329-
)
333+
path.relative(destPath, reactTestAppProjectPath)
330334
)
331335
.replace(
332336
/EndProject\r?\nGlobal/,
@@ -340,25 +344,50 @@ function generateSolution(destPath) {
340344
if (e) throw e;
341345
}
342346
);
347+
if (!noAutolink) {
348+
Promise.all([...copyTasks, solutionTask]).then(() => {
349+
const { spawn } = require("child_process");
350+
spawn(path.join(path.dirname(process.argv0), "npx.cmd"), [
351+
"react-native",
352+
"autolink-windows",
353+
"--proj",
354+
reactTestAppProjectPath,
355+
]);
356+
});
357+
}
343358
}
344359

345360
return undefined;
346361
}
347362

348363
if (require.main === module) {
349-
const { argv } = require("yargs")
350-
.usage("Usage: $0 --projectDirectory=directory")
351-
.option("projectDirectory", {
352-
alias: "p",
353-
type: "string",
354-
description: "Directory where solution will be created",
355-
default: "windows",
356-
});
357-
const error = generateSolution(path.resolve(argv.projectDirectory));
358-
if (error) {
359-
console.error(error);
360-
process.exit(1);
361-
}
364+
require("yargs").usage(
365+
"$0 [options]",
366+
"Generate a Visual Studio solution for React Test App",
367+
{
368+
projectDirectory: {
369+
alias: "p",
370+
type: "string",
371+
description: "Directory where solution will be created",
372+
default: "windows",
373+
},
374+
noAutolink: {
375+
type: "boolean",
376+
description: "Skip autolinking",
377+
default: false,
378+
},
379+
},
380+
({ projectDirectory, noAutolink }) => {
381+
const error = generateSolution(
382+
path.resolve(projectDirectory),
383+
noAutolink
384+
);
385+
if (error) {
386+
console.error(error);
387+
process.exit(1);
388+
}
389+
}
390+
).argv;
362391
} else {
363392
exports["copyAndReplace"] = copyAndReplace;
364393
exports["findNearest"] = findNearest;

0 commit comments

Comments
 (0)