Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update set-version to reuse set-rn-version #43109

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion scripts/releases/set-rn-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async function main() {
async function setReactNativeVersion(
version /*: string */,
dependencyVersions /*: ?Record<string, string> */,
buildType /*: BuildType */,
buildType /*: ?BuildType */,
) {
const versionInfo = parseVersion(version, buildType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ exports[`setVersion updates monorepo for nightly: packages/react-native/package.
\\"metro-config\\": \\"^0.80.3\\",
\\"metro-runtime\\": \\"^0.80.3\\"
}
}
"
}"
`;

exports[`setVersion updates monorepo for nightly: packages/react-native/template/package.json 1`] = `
Expand Down Expand Up @@ -375,8 +374,7 @@ exports[`setVersion updates monorepo for release-candidate: packages/react-nativ
\\"metro-config\\": \\"^0.80.3\\",
\\"metro-runtime\\": \\"^0.80.3\\"
}
}
"
}"
`;

exports[`setVersion updates monorepo for release-candidate: packages/react-native/template/package.json 1`] = `
Expand Down Expand Up @@ -574,8 +572,7 @@ exports[`setVersion updates monorepo for stable version: packages/react-native/p
\\"metro-config\\": \\"^0.80.3\\",
\\"metro-runtime\\": \\"^0.80.3\\"
}
}
"
}"
`;

exports[`setVersion updates monorepo for stable version: packages/react-native/template/package.json 1`] = `
Expand Down Expand Up @@ -640,6 +637,127 @@ exports[`setVersion updates monorepo on main after release cut: packages/monorep
"
`;

exports[`setVersion updates monorepo on main after release cut: packages/react-native/Libraries/Core/ReactNativeVersion.js 1`] = `
"/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict
* @generated by scripts/releases/set-rn-version.js
*/

const version: $ReadOnly<{
major: number,
minor: number,
patch: number,
prerelease: string | null,
}> = {
major: 1000,
minor: 0,
patch: 0,
prerelease: null,
};

module.exports = {version};
"
`;

exports[`setVersion updates monorepo on main after release cut: packages/react-native/React/Base/RCTVersion.m 1`] = `
"/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated by scripts/releases/set-rn-version.js
*/

#import \\"RCTVersion.h\\"

NSString* const RCTVersionMajor = @\\"major\\";
NSString* const RCTVersionMinor = @\\"minor\\";
NSString* const RCTVersionPatch = @\\"patch\\";
NSString* const RCTVersionPrerelease = @\\"prerelease\\";


NSDictionary* RCTGetReactNativeVersion(void)
{
static NSDictionary* __rnVersion;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^(void){
__rnVersion = @{
RCTVersionMajor: @(1000),
RCTVersionMinor: @(0),
RCTVersionPatch: @(0),
RCTVersionPrerelease: [NSNull null],
};
});
return __rnVersion;
}
"
`;

exports[`setVersion updates monorepo on main after release cut: packages/react-native/ReactAndroid/gradle.properties 1`] = `
"VERSION_NAME=1000.0.0
"
`;

exports[`setVersion updates monorepo on main after release cut: packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/ReactNativeVersion.java 1`] = `
"/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated by scripts/releases/set-rn-version.js
*/

package com.facebook.react.modules.systeminfo;

import com.facebook.react.common.MapBuilder;

import java.util.Map;

public class ReactNativeVersion {
public static final Map<String, Object> VERSION = MapBuilder.<String, Object>of(
\\"major\\", 1000,
\\"minor\\", 0,
\\"patch\\", 0,
\\"prerelease\\", null);
}
"
`;

exports[`setVersion updates monorepo on main after release cut: packages/react-native/ReactCommon/cxxreact/ReactNativeVersion.h 1`] = `
"/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @generated by scripts/releases/set-rn-version.js
*/

#pragma once

#include <cstdint>
#include <string_view>

namespace facebook::react {

constexpr struct {
int32_t Major = 1000;
int32_t Minor = 0;
int32_t Patch = 0;
std::string_view Prerelease = \\"\\";
} ReactNativeVersion;

} // namespace facebook::react
"
`;

exports[`setVersion updates monorepo on main after release cut: packages/react-native/package.json 1`] = `
"{
\\"name\\": \\"react-native\\",
Expand All @@ -652,8 +770,7 @@ exports[`setVersion updates monorepo on main after release cut: packages/react-n
\\"metro-config\\": \\"^0.80.3\\",
\\"metro-runtime\\": \\"^0.80.3\\"
}
}
"
}"
`;

exports[`setVersion updates monorepo on main after release cut: packages/react-native/template/package.json 1`] = `
Expand Down
15 changes: 1 addition & 14 deletions scripts/releases/set-version/__tests__/set-version-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ describe('setVersion', () => {

return {
...originalFs,
writeFileSync: writeFileMock,
promises: {
...originalFs.promises,
writeFile: writeFileMock,
Expand Down Expand Up @@ -88,19 +89,5 @@ describe('setVersion', () => {
};

await setVersion('0.82.0-main', true);

// Make sure we don't update any react-native source or build files
writeFileMock.mock.calls.forEach(([filePath, content]) => {
if (!filePath.endsWith('package.json')) {
throw new Error(
`set-version should not update any react-native source or build files. Updated ${filePath}`,
);
}
});
});

afterAll(() => {
jest.unmock('path');
jest.unmock('fs');
});
});
84 changes: 27 additions & 57 deletions scripts/releases/set-version/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,20 @@
import type {PackageJson} from '../utils/monorepo';
*/

const {REACT_NATIVE_PACKAGE_DIR} = require('../../consts');
const {updateGradleFile, updateSourceFiles} = require('../set-rn-version');
const {setReactNativeVersion} = require('../set-rn-version');
const {getPackages} = require('../utils/monorepo');
const {parseVersion} = require('../utils/version-utils');
const {promises: fs} = require('fs');
const path = require('path');
const yargs = require('yargs');

const TEMPLATE_DIR = path.join(REACT_NATIVE_PACKAGE_DIR, 'template');

async function updatePackages(
version /*: string */,
skipReactNativeVersion /*: boolean */,
) {
const packages = await getPackages({
includePrivate: false,
includeReactNative: true,
});
const newPackageVersions = Object.fromEntries(
Object.keys(packages).map(packageName => [packageName, version]),
);
const templatePackageJson /*: PackageJson */ = JSON.parse(
await fs.readFile(path.join(TEMPLATE_DIR, 'package.json'), 'utf-8'),
);
const packagesToUpdate = [
...Object.values(packages),
{
path: TEMPLATE_DIR,
packageJson: templatePackageJson,
},
];

await Promise.all(
packagesToUpdate.map(({path: packagePath, packageJson}) =>
updatePackageJson(
packagePath,
packageJson,
newPackageVersions,
skipReactNativeVersion,
),
),
);
}

async function updatePackageJson(
packagePath /*: string */,
packageJson /*: PackageJson */,
newPackageVersions /*: $ReadOnly<{[string]: string}> */,
skipReactNativeVersion /*: boolean */,
) /*: Promise<void> */ {
const packageName = packageJson.name;

if (
packageName in newPackageVersions &&
(!skipReactNativeVersion || packageName !== 'react-native')
) {
if (packageName in newPackageVersions) {
packageJson.version = newPackageVersions[packageName];
}

Expand All @@ -81,12 +39,8 @@ async function updatePackageJson(
continue;
}

for (const dependency of Object.keys(deps)) {
if (dependency === 'react-native' && skipReactNativeVersion) {
continue;
}

if (dependency in newPackageVersions) {
for (const dependency in newPackageVersions) {
if (dependency in deps) {
deps[dependency] = newPackageVersions[dependency];
}
}
Expand Down Expand Up @@ -115,15 +69,31 @@ async function updatePackageJson(
async function setVersion(
version /*: string */,
skipReactNativeVersion /*: boolean */ = false,
) {
const parsedVersion = parseVersion(version);
) /*: Promise<void> */ {
const packages = await getPackages({
includePrivate: false,
includeReactNative: true,
});
const newPackageVersions = Object.fromEntries(
Object.keys(packages).map(packageName => [packageName, version]),
);

if (!skipReactNativeVersion) {
await updateSourceFiles(parsedVersion);
await updateGradleFile(parsedVersion.version);
}
await setReactNativeVersion(
skipReactNativeVersion ? '1000.0.0' : version,
newPackageVersions,
);

// Exclude the react-native package, since this (and the template) are
// handled by `setReactNativeVersion`.
const packagesToUpdate = Object.values(packages).filter(
pkg => pkg.name !== 'react-native',
);

await updatePackages(parsedVersion.version, skipReactNativeVersion);
await Promise.all(
packagesToUpdate.map(({path: packagePath, packageJson}) =>
updatePackageJson(packagePath, packageJson, newPackageVersions),
),
);
}

if (require.main === module) {
Expand Down
11 changes: 5 additions & 6 deletions scripts/releases/update-template-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
* @oncall react_native
*/

const {REACT_NATIVE_PACKAGE_DIR} = require('../consts');
const {applyPackageVersions} = require('../npm-utils');
const fs = require('fs');
const path = require('path');

const TEMPLATE_DIR = path.join(REACT_NATIVE_PACKAGE_DIR, 'template');

/**
* Updates the react-native template package.json with
* dependencies in `dependencyMap`.
Expand All @@ -21,12 +24,8 @@ const path = require('path');
* ex. {"react-native": "0.23.0", "other-dep": "nightly"}
*/
function updateTemplatePackage(dependencyMap /*: Record<string, string> */) {
const jsonPath = path.join(
__dirname,
'../../packages/react-native/template/package.json',
);
// $FlowFixMe[unsupported-syntax]
const templatePackageJson = require(jsonPath);
const jsonPath = path.join(TEMPLATE_DIR, 'package.json');
const templatePackageJson = JSON.parse(fs.readFileSync(jsonPath, 'utf-8'));

const updatedPackageJson = applyPackageVersions(
templatePackageJson,
Expand Down
Loading