Skip to content

Commit 427ba35

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Stamp React Native Version Into C++ Code (#28036)
Summary: The PlatformConstants native module exposes the ability to query the React Native version used to build native code. This is managed on iOS and Android by a version bumping script, which replaces module code based on a template. It is currently difficult to accurately determine this version for out-of-tree C++ platforms (I.e. React Native Windows). The version of upstream react-native we resolve to is ultimately dependent on the version of react-native chosen a peer dependency, which is not neccesarily constant given a build of react-native-windows. We could try to hack around this, and make our native build try to reason about the resolved pacakge for react-native using a lockfile, but a much cleaner solution is to embed version into C++ code, similar to what is done for Android and iOS. This change does that, adding a header with React Native version and updating the build stamping script to write to it. Usage sample: ```c++ constants["reactNativeVersion"] = folly::dynamic::object(); constants["reactNativeVersion"]["major"] = ReactNativeVersion.Major; constants["reactNativeVersion"]["minor"] = ReactNativeVersion.Minor; constants["reactNativeVersion"]["patch"] = ReactNativeVersion.Patch; ``` ## Changelog [General] [Added] - Stamp React Native Version Into C++ Code Pull Request resolved: #28036 Test Plan: Validated that the bumping script will accurately update the header, can compile under both MSVC and Clang. Differential Revision: D19865992 Pulled By: hramos fbshipit-source-id: 9e0b8e9519015bb62c60b9935a234cd367a1926a
1 parent d33ead3 commit 427ba35

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
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+
* @generated by scripts/bump-oss-version.js
8+
*/
9+
10+
#pragma once
11+
12+
#include <cstdint>
13+
#include <string_view>
14+
15+
namespace facebook::react {
16+
17+
constexpr struct {
18+
int32_t Major = 0;
19+
int32_t Minor = 0;
20+
int32_t Patch = 0;
21+
std::string_view Prerelease = "";
22+
} ReactNativeVersion;
23+
24+
} // namespace facebook::react

scripts/bump-oss-version.js

+13
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,19 @@ fs.writeFileSync(
8484
'utf-8',
8585
);
8686

87+
fs.writeFileSync(
88+
'ReactCommon/cxxreact/ReactNativeVersion.h',
89+
cat('scripts/versiontemplates/ReactNativeVersion.h.template')
90+
.replace('${major}', major)
91+
.replace('${minor}', minor)
92+
.replace('${patch}', patch)
93+
.replace(
94+
'${prerelease}',
95+
prerelease !== undefined ? `"${prerelease}"` : '""',
96+
),
97+
'utf-8',
98+
);
99+
87100
fs.writeFileSync(
88101
'Libraries/Core/ReactNativeVersion.js',
89102
cat('scripts/versiontemplates/ReactNativeVersion.js.template')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
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+
* @generated by scripts/bump-oss-version.js
8+
*/
9+
10+
#pragma once
11+
12+
#include <cstdint>
13+
#include <string_view>
14+
15+
namespace facebook::react {
16+
17+
constexpr struct {
18+
int32_t Major = ${major};
19+
int32_t Minor = ${minor};
20+
int32_t Patch = ${patch};
21+
std::string_view Prerelease = ${prerelease};
22+
} ReactNativeVersion;
23+
24+
} // namespace facebook::react

0 commit comments

Comments
 (0)