-
Notifications
You must be signed in to change notification settings - Fork 24.3k
/
set-rn-version.js
executable file
·178 lines (158 loc) · 4.72 KB
/
set-rn-version.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* 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.
*
* @format
*/
'use strict';
/**
* This script updates relevant React Native files with supplied version:
* * Prepares a package.json suitable for package consumption
* * Updates package.json for template project
* * Updates the version in gradle files and makes sure they are consistent between each other
* * Creates a gemfile
*/
const fs = require('fs');
const os = require('os');
const path = require('path');
const {cat, echo, exec, exit, sed} = require('shelljs');
const yargs = require('yargs');
const {parseVersion, validateBuildType} = require('./version-utils');
const {saveFiles} = require('./scm-utils');
let argv = yargs
.option('v', {
alias: 'to-version',
type: 'string',
required: true,
})
.option('b', {
alias: 'build-type',
type: 'string',
required: true,
}).argv;
const buildType = argv.buildType;
const version = argv.toVersion;
try {
validateBuildType(buildType);
} catch (e) {
throw e;
}
let major,
minor,
patch,
prerelease = -1;
try {
({major, minor, patch, prerelease} = parseVersion(version, buildType));
} catch (e) {
throw e;
}
const tmpVersioningFolder = fs.mkdtempSync(
path.join(os.tmpdir(), 'rn-set-version'),
);
echo(`The temp versioning folder is ${tmpVersioningFolder}`);
saveFiles(
[
'packages/react-native/package.json',
'packages/react-native/template/package.json',
],
tmpVersioningFolder,
);
fs.writeFileSync(
'packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/systeminfo/ReactNativeVersion.java',
cat('scripts/versiontemplates/ReactNativeVersion.java.template')
.replace('${major}', major)
.replace('${minor}', minor)
.replace('${patch}', patch)
.replace(
'${prerelease}',
prerelease !== undefined ? `"${prerelease}"` : 'null',
),
'utf-8',
);
fs.writeFileSync(
'packages/react-native/React/Base/RCTVersion.m',
cat('scripts/versiontemplates/RCTVersion.m.template')
.replace('${major}', `@(${major})`)
.replace('${minor}', `@(${minor})`)
.replace('${patch}', `@(${patch})`)
.replace(
'${prerelease}',
prerelease !== undefined ? `@"${prerelease}"` : '[NSNull null]',
),
'utf-8',
);
fs.writeFileSync(
'packages/react-native/ReactCommon/cxxreact/ReactNativeVersion.h',
cat('scripts/versiontemplates/ReactNativeVersion.h.template')
.replace('${major}', major)
.replace('${minor}', minor)
.replace('${patch}', patch)
.replace(
'${prerelease}',
prerelease !== undefined ? `"${prerelease}"` : '""',
),
'utf-8',
);
fs.writeFileSync(
'packages/react-native/Libraries/Core/ReactNativeVersion.js',
cat('scripts/versiontemplates/ReactNativeVersion.js.template')
.replace('${major}', major)
.replace('${minor}', minor)
.replace('${patch}', patch)
.replace(
'${prerelease}',
prerelease !== undefined ? `'${prerelease}'` : 'null',
),
'utf-8',
);
const packageJson = JSON.parse(cat('packages/react-native/package.json'));
packageJson.version = version;
fs.writeFileSync(
'packages/react-native/package.json',
JSON.stringify(packageJson, null, 2),
'utf-8',
);
// Change ReactAndroid/gradle.properties
saveFiles(
['packages/react-native/ReactAndroid/gradle.properties'],
tmpVersioningFolder,
);
if (
sed(
'-i',
/^VERSION_NAME=.*/,
`VERSION_NAME=${version}`,
'packages/react-native/ReactAndroid/gradle.properties',
).code
) {
echo("Couldn't update version for Gradle");
exit(1);
}
// Change react-native version in the template's package.json
exec(`node scripts/set-rn-template-version.js ${version}`);
// Verify that files changed, we just do a git diff and check how many times version is added across files
const filesToValidate = [
'packages/react-native/package.json',
'packages/react-native/ReactAndroid/gradle.properties',
'packages/react-native/template/package.json',
];
const numberOfChangedLinesWithNewVersion = exec(
`diff -r ${tmpVersioningFolder} . | grep '^[>]' | grep -c ${version} `,
{silent: true},
).stdout.trim();
if (+numberOfChangedLinesWithNewVersion !== filesToValidate.length) {
// TODO: the logic that checks whether all the changes have been applied
// is missing several files. For example, it is not checking Ruby version nor that
// the Objecive-C files, the codegen and other files are properly updated.
// We are going to work on this in another PR.
echo('WARNING:');
echo(
`Failed to update all the files: [${filesToValidate.join(
', ',
)}] must have versions in them`,
);
echo(`These files already had version ${version} set.`);
}
exit(0);