forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelease-util.ts
executable file
·368 lines (317 loc) · 11.7 KB
/
release-util.ts
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/env node
/**
* @license
* Copyright 2020 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import chalk from 'chalk';
import * as mkdirp from 'mkdirp';
import * as readline from 'readline';
import * as shell from 'shelljs';
export interface Phase {
// The list of packages that will be updated with this change.
packages: string[];
// The list of dependencies that all of the packages will update to.
deps?: string[];
// An ordered map of scripts, key is package name, value is an object with two
// optional fields: `before-yarn` with scripts to run before `yarn`, and
// `after-yarn` with scripts to run after yarn is called and before the pull
// request is sent out.
scripts?: {[key: string]: {[key: string]: string[]}};
// Whether to leave the version of the package alone. Defaults to false
// (change the version).
leaveVersion?: boolean;
title?: string;
}
export interface ReleaseUnit {
// A human-readable name. Used for generating release branch.
name: string;
// The phases in this release unit.
phases: Phase[];
// The repository *only if it is not the same as tfjs*.
repo?: string;
}
export const CORE_PHASE: Phase = {
packages: ['tfjs-core'],
// Do not mark tfjs-backend-cpu as a dependency during releases. As a
// devDependency it should keep the link:// path. Once tests have passed in CI
// building and releasing core should not depend on the cpu backend
};
export const CPU_PHASE: Phase = {
packages: ['tfjs-backend-cpu'],
deps: ['tfjs-core']
};
export const WEBGL_PHASE: Phase = {
packages: ['tfjs-backend-webgl'],
deps: ['tfjs-core', 'tfjs-backend-cpu']
};
export const LAYERS_CONVERTER_PHASE: Phase = {
packages: ['tfjs-layers', 'tfjs-converter'],
deps: ['tfjs-core', 'tfjs-backend-cpu', 'tfjs-backend-webgl']
};
export const DATA_PHASE: Phase = {
packages: ['tfjs-data'],
deps: ['tfjs-core', 'tfjs-layers', 'tfjs-backend-cpu']
}
export const UNION_PHASE: Phase = {
packages: ['tfjs'],
deps: [
'tfjs-core', 'tfjs-layers', 'tfjs-converter', 'tfjs-data',
'tfjs-backend-cpu', 'tfjs-backend-webgl'
]
};
// We added tfjs-core and tfjs-layers because Node has unit tests that directly
// use tf.core and tf.layers to test serialization of models. Consider moving
// the test to tf.layers.
export const NODE_PHASE: Phase = {
packages: ['tfjs-node', 'tfjs-node-gpu'],
deps: ['tfjs', 'tfjs-core'],
scripts: {'tfjs-node-gpu': {'before-yarn': ['yarn prep-gpu']}}
};
export const WASM_PHASE: Phase = {
packages: ['tfjs-backend-wasm'],
deps: ['tfjs-core']
};
export const VIS_PHASE: Phase = {
packages: ['tfjs-vis']
};
export const REACT_NATIVE_PHASE: Phase = {
packages: ['tfjs-react-native'],
deps: ['tfjs-core', 'tfjs-backend-cpu', 'tfjs-backend-webgl']
};
export const WEBSITE_PHASE: Phase = {
packages: ['tfjs-website'],
deps: ['tfjs', 'tfjs-node', 'tfjs-vis', 'tfjs-react-native'],
scripts: {'tfjs-website': {'after-yarn': ['yarn prep && yarn build-prod']}},
leaveVersion: true,
title: 'Update website to latest dependencies.'
};
export const TFJS_RELEASE_UNIT: ReleaseUnit = {
name: 'tfjs',
phases: [
CORE_PHASE, CPU_PHASE, WEBGL_PHASE, LAYERS_CONVERTER_PHASE, DATA_PHASE,
UNION_PHASE, NODE_PHASE, WASM_PHASE
]
};
export const VIS_RELEASE_UNIT: ReleaseUnit = {
name: 'vis',
phases: [VIS_PHASE]
};
export const REACT_NATIVE_RELEASE_UNIT: ReleaseUnit = {
name: 'react-native',
phases: [REACT_NATIVE_PHASE]
};
export const WEBSITE_RELEASE_UNIT: ReleaseUnit = {
name: 'website',
phases: [WEBSITE_PHASE],
repo: 'tfjs-website'
};
export const RELEASE_UNITS: ReleaseUnit[] = [
TFJS_RELEASE_UNIT, VIS_RELEASE_UNIT, REACT_NATIVE_RELEASE_UNIT,
WEBSITE_RELEASE_UNIT
];
export const TMP_DIR = '/tmp/tfjs-release';
const rl =
readline.createInterface({input: process.stdin, output: process.stdout});
export async function question(questionStr: string): Promise<string> {
console.log(chalk.bold(questionStr));
return new Promise<string>(
resolve => rl.question('> ', response => resolve(response)));
}
/**
* A wrapper around shell.exec for readability.
* @param cmd The bash command to execute.
* @returns stdout returned by the executed bash script.
*/
export function $(cmd: string) {
const result = shell.exec(cmd, {silent: true});
if (result.code > 0) {
console.log('$', cmd);
console.log(result.stderr);
process.exit(1);
}
return result.stdout.trim();
}
export function printReleaseUnit(id: number) {
const releaseUnit = RELEASE_UNITS[id];
console.log(chalk.green(`Release unit ${id}:`));
console.log(` packages: ${
chalk.blue(releaseUnit.phases.map(phase => phase.packages.join(', '))
.join(', '))}`);
}
export function printPhase(phases: Phase[], phaseId: number) {
const phase = phases[phaseId];
console.log(chalk.green(`Phase ${phaseId}:`));
console.log(` packages: ${chalk.blue(phase.packages.join(', '))}`);
if (phase.deps != null) {
console.log(` deps: ${phase.deps.join(', ')}`);
}
}
export function makeReleaseDir(dir: string) {
mkdirp(TMP_DIR, err => {
if (err) {
console.log('Error creating temp dir', TMP_DIR);
process.exit(1);
}
});
$(`rm -f -r ${dir}/*`);
$(`rm -f -r ${dir}`);
$(`mkdir ${dir}`);
}
export async function updateDependency(
deps: string[], pkg: string, parsedPkg: any): Promise<string> {
console.log(chalk.magenta.bold(`~~~ Update dependency versions ~~~`));
if (deps != null) {
const depsLatestVersion: string[] =
deps.map(dep => $(`npm view @tensorflow/${dep} dist-tags.latest`));
for (let j = 0; j < deps.length; j++) {
const dep = deps[j];
let version = '';
const depNpmName = `@tensorflow/${dep}`;
if (parsedPkg['dependencies'] != null &&
parsedPkg['dependencies'][depNpmName] != null) {
version = parsedPkg['dependencies'][depNpmName];
} else if (
parsedPkg['peerDependencies'] != null &&
parsedPkg['peerDependencies'][depNpmName] != null) {
version = parsedPkg['peerDependencies'][depNpmName];
} else if (
parsedPkg['devDependencies'] != null &&
parsedPkg['devDependencies'][depNpmName] != null) {
version = parsedPkg['devDependencies'][depNpmName];
}
if (version == null) {
throw new Error(`No dependency found for ${dep}.`);
}
let relaxedVersionPrefix = '';
if (version.startsWith('~') || version.startsWith('^')) {
relaxedVersionPrefix = version.substr(0, 1);
}
const depVersionLatest = relaxedVersionPrefix + depsLatestVersion[j];
let depVersion = await question(
`Updated version for ` +
`${dep} (current is ${version}, leave empty for latest ${
depVersionLatest}): `);
if (depVersion === '') {
depVersion = depVersionLatest;
}
console.log(chalk.blue(`Using version ${depVersion}`));
pkg = `${pkg}`.replace(
new RegExp(`"${depNpmName}": "${version}"`, 'g'),
`"${depNpmName}": "${depVersion}"`);
}
}
return pkg;
}
// Update package.json dependencies of tfjs packages. This method is different
// than `updateDependency`, it does not rely on published versions, instead it
// assumes all the packages have the same version and use that to update.
export function updateTFJSDependencyVersions(
deps: string[], pkg: string, parsedPkg: any, tfjsVersion: string): string {
console.log(chalk.magenta.bold(`~~~ Update dependency versions ~~~`));
if (deps != null) {
for (let j = 0; j < deps.length; j++) {
const dep = deps[j];
// Get the current dependency package version.
let version = '';
const depNpmName = `@tensorflow/${dep}`;
if (parsedPkg['dependencies'] != null &&
parsedPkg['dependencies'][depNpmName] != null) {
version = parsedPkg['dependencies'][depNpmName];
} else if (
parsedPkg['peerDependencies'] != null &&
parsedPkg['peerDependencies'][depNpmName] != null) {
version = parsedPkg['peerDependencies'][depNpmName];
} else if (
parsedPkg['devDependencies'] != null &&
parsedPkg['devDependencies'][depNpmName] != null) {
version = parsedPkg['devDependencies'][depNpmName];
}
if (version == null) {
throw new Error(`No dependency found for ${dep}.`);
}
let relaxedVersionPrefix = '';
if (version.startsWith('~') || version.startsWith('^')) {
relaxedVersionPrefix = version.substr(0, 1);
}
const versionLatest = relaxedVersionPrefix + tfjsVersion;
pkg = `${pkg}`.replace(
new RegExp(`"${depNpmName}": "${version}"`, 'g'),
`"${depNpmName}": "${versionLatest}"`);
}
}
return pkg;
}
export function prepareReleaseBuild(phase: Phase, packageName: string) {
console.log(chalk.magenta.bold(`~~~ Prepare release build ~~~`));
console.log(chalk.bold('Prepare before-yarn'));
if (phase.scripts != null && phase.scripts[packageName] != null &&
phase.scripts[packageName]['before-yarn'] != null) {
phase.scripts[packageName]['before-yarn'].forEach(script => $(script));
}
console.log(chalk.bold('yarn'));
$(`yarn`);
console.log(chalk.bold('Prepare after-yarn'));
if (phase.scripts != null && phase.scripts[packageName] != null &&
phase.scripts[packageName]['after-yarn'] != null) {
phase.scripts[packageName]['after-yarn'].forEach(script => $(script));
}
}
export async function getReleaseBranch(name: string): Promise<string> {
// Infer release branch name.
let releaseBranch = '';
// Get a list of branches sorted by timestamp in descending order.
const branchesStr = $(
`git branch -r --sort=-authordate --format='%(HEAD) %(refname:lstrip=-1)'`);
const branches =
Array.from(branchesStr.split(/\n/)).map(line => line.toString().trim());
// Find the latest matching branch, e.g. tfjs_1.7.1
// It will not match temprary generated branches such as tfjs_1.7.1_phase0.
const exp = '^' + name + '_([^_]+)$';
const regObj = new RegExp(exp);
const maybeBranch = branches.find(branch => branch.match(regObj));
releaseBranch = await question(
`Which release branch (leave empty for ` +
`${maybeBranch}):`);
if (releaseBranch === '') {
releaseBranch = maybeBranch;
}
return releaseBranch;
}
export function checkoutReleaseBranch(
releaseBranch: string, git_protocol: string, dir: string) {
console.log(chalk.magenta.bold(
`~~~ Checking out release branch ${releaseBranch} ~~~`));
$(`rm -f -r ${dir}`);
mkdirp(dir, err => {
if (err) {
console.log('Error creating temp dir', dir);
process.exit(1);
}
});
const urlBase = git_protocol ? 'git@github.com:' : 'https://github.com/';
$(`git clone -b ${releaseBranch} ${urlBase}tensorflow/tfjs ${dir} --depth=1`);
}
export function createPR(
devBranchName: string, releaseBranch: string, message: string) {
console.log(
chalk.magenta.bold('~~~ Creating PR to update release branch ~~~'));
$(`git checkout -b ${devBranchName}`);
$(`git push -u origin ${devBranchName}`);
$(`git add .`);
$(`git commit -a -m "${message}"`);
$(`git push`);
$(`hub pull-request -b ${releaseBranch} -m "${message}" -l INTERNAL -o`);
console.log();
}