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

New upgrading process, relying on Git #11110

Closed
wants to merge 11 commits into from
82 changes: 77 additions & 5 deletions docs/Upgrading.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,80 @@ iOS project and a JavaScript project, all combined under an npm package, upgradi
tricky. But we try to make it easy for you. Here's what you need to do to upgrade from an older
version of React Native:

## 1. Upgrade the `react-native` dependency
## Upgrade based on Git

**IMPORTANT:** You don't have to install the new version of React Native, it will be installed automatically.

The module `react-native-git-upgrade` provides a one-step operation to upgrade the source files with
a minimum of conflicts. Under the hood, it consists in 2 phases:

* First, it computes a Git patch between both old and new template files,
* Then, the patch is applied on the user's sources.

### 1. Install Git
Your project doesn't have to be handled by the Git versioning sytem (could be Mercurial, SVN or none)
but Git has to be installed and available in the `PATH`. You can download Git here:
https://git-scm.com/downloads

### 2. Install the `react-native-git-upgrade` module

It's a CLI tool and must be installed globally:

```sh
$ npm install -g react-native-git-upgrade
```

### 3. Run the command

Run the command to start the process:

```sh
$ react-native-git-upgrade
# Upgrade React Native to the latest version

# Or:

$ react-native-git-upgrade X.Y.Z
# Upgrade React Native to the X.Y.Z version
```

The templates are upgraded in a optimized way. You still may encounter conflicts but only where the Git
3-way merge have failed, depending on the version and how you modified your sources.

### 4. Resolve the conflicts

Conflicted files include delimiters which make very clear where the changes come from. For example:

```
13B07F951A680F5B00A75B9A /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
<<<<<<< ours
CODE_SIGN_IDENTITY = "iPhone Developer";
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/HockeySDK.embeddedframework",
"$(PROJECT_DIR)/HockeySDK-iOS/HockeySDK.embeddedframework",
);
=======
CURRENT_PROJECT_VERSION = 1;
>>>>>>> theirs
HEADER_SEARCH_PATHS = (
"$(inherited)",
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include,
"$(SRCROOT)/../node_modules/react-native/React/**",
"$(SRCROOT)/../node_modules/react-native-code-push/ios/CodePush/**",
);
```

You can think of "ours" as "your team" and "theirs" as "the React Native dev team".

## Alternative

Use this only in case the above didn't work.

### 1. Upgrade the `react-native` dependency

Note the latest version of the `react-native` npm package from here (or use `npm info react-native` to check):

Expand All @@ -23,11 +96,11 @@ Note the latest version of the `react-native` npm package from here (or use `npm
Now install that version of `react-native` in your project with `npm install --save`.

```sh
$ npm install --save react-native@X.Y
$ npm install --save react-native@X.Y
# where X.Y is the semantic version you are upgrading to
```

## 2. Upgrade your project templates
### 2. Upgrade your project templates

The new npm package may contain updates to the files that are normally generated when you
run `react-native init`, like the iOS and the Android sub-projects.
Expand All @@ -46,8 +119,7 @@ This will check your files against the latest template and perform the following
* If there is a new file in the template, it is simply created.
* If a file in the template is identical to your file, it is skipped.
* If a file is different in your project than the template, you will be prompted; you have options
to view a diff between your file and the template file, keep your file or overwrite it with the
template version. If you are unsure, press `h` to get a list of possible commands.
to keep your file or overwrite it with the template version.


# Manual Upgrades
Expand Down
10 changes: 10 additions & 0 deletions local-cli/upgrade/upgrade.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ function validateAndUpgrade() {
fs.readFileSync(path.resolve(projectDir, 'package.json'), 'utf8')
);

warn(
'You should consider using the new upgrade tool based on Git. It ' +
'makes upgrades easier by resolving most conflicts automatically.\n' +
'To use it:\n' +
'- Go back to the old version of React Native\n' +
'- Run "npm install -g react-native-git-upgrade"\n' +
'- Run "react-native-git-upgrade"\n' +
'See https://facebook.github.io/react-native/docs/upgrading.html'
);

const projectName = packageJSON.name;
if (!projectName) {
warn(
Expand Down
72 changes: 72 additions & 0 deletions react-native-git-upgrade/checks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

const {execSync} = require('child_process');
const semver = require('semver');

function checkDeclaredVersion(declaredVersion) {
if (!declaredVersion) {
throw new Error(
'Your "package.json" file doesn\'t seem to have "react-native" as a dependency.'
);
}
}

function checkMatchingVersions(currentVersion, declaredVersion) {
if (!semver.satisfies(currentVersion, declaredVersion)) {
throw new Error(
'react-native version in "package.json" doesn\'t match ' +
'the installed version in "node_modules".\n' +
'Try running "npm install" to fix this.'
);
}
}

function checkReactPeerDependency(currentVersion, declaredReactVersion) {
if (semver.lt(currentVersion, '0.21.0') && !declaredReactVersion) {
throw new Error(
'Your "package.json" file doesn\'t seem to have "react" as a dependency.\n' +
'"react" was changed from a dependency to a peer dependency in react-native v0.21.0.\n' +
'Therefore, it\'s necessary to include "react" in your project\'s dependencies.\n' +
'Please run "npm install --save react", then re-run ' +
'"react-native upgrade".'
);
}
}

function checkGitAvailable() {
try {
execSync('git --version');
} catch (error) {
throw new Error(
'"react-native-git-upgrade" requires "git" to be available in path. ' +
'Please install Git (https://git-scm.com)"'
);
}
}

function checkNewVersion(newVersion, requiredVersion) {
if (!semver.valid(newVersion) && requiredVersion) {
throw new Error(
'The specified version of React Native ' + requiredVersion + ' doesn\'t exist.\n' +
'Re-run the react-native-git-upgrade command with an existing version,\n' +
'for example: "react-native-git-upgrade 0.38.0",\n' +
'or without arguments to upgrade to the latest: "react-native-git-upgrade".'
);
}
}

module.exports = {
checkDeclaredVersion,
checkMatchingVersions,
checkReactPeerDependency,
checkGitAvailable,
checkNewVersion,
};
22 changes: 22 additions & 0 deletions react-native-git-upgrade/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';

require('babel-register')({
presets: [
require('babel-preset-es2015-node'),
require('babel-preset-stage-3')
],
// Enable transpiling for react-native-git-upgrade AND the generator, just like the upgrade CLI command does
only: /(react-native-git-upgrade\/(?!(node_modules)))|(local-cli\/generator)/
});

var cliEntry = require('./cliEntry');

module.exports = cliEntry;