Repository navigation
Expand file tree
/
Copy pathhelpers.js
More file actions
532 lines (450 loc) · 11.3 KB
/
Copy pathhelpers.js
File metadata and controls
532 lines (450 loc) · 11.3 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
/*!
* Copyright 2016 Google Inc. 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.
*/
'use strict';
var path = require('path');
var uniq = require('array-uniq');
var globby = require('globby');
var spawn = require('child_process').spawnSync;
require('shelljs/global');
/**
* The pull request number.
*
* @alias ci.PR_NUMBER;
*/
var PR_NUMBER = process.env.TRAVIS_PULL_REQUEST ||
process.env.APPVEYOR_PULL_REQUEST_NUMBER;
/**
* Checks to see if this is a pull request or not.
*
* @alias ci.IS_PR
*/
var IS_PR = !isNaN(parseInt(PR_NUMBER, 10));
/**
* google-cloud-node root directory.. useful in case we need to cd
*/
var ROOT_DIR = path.join(__dirname, '..');
module.exports.ROOT_DIR = ROOT_DIR;
/**
* Helper class to make install dependencies + running tests easier to read
* and less error prone.
*
* @class Module
* @param {string} name - The module name (e.g. common, bigquery, etc.)
*/
function Module(name) {
if (!(this instanceof Module)) {
return new Module(name);
}
this.name = name;
this.directory = path.join(ROOT_DIR, 'packages', name);
var pkgJson = require(path.join(this.directory, 'package.json'));
this.packageName = pkgJson.name;
this.dependencies = Object.keys(pkgJson.devDependencies || {});
}
/**
* Umbrella module name.
*
* @static
*/
Module.UMBRELLA = 'google-cloud';
/**
* Retrieves a list of modules that are ahead of origin/master. We do this by
* creating a temporary remote branch that points official master branch.
* We then do a git diff against the two to get a list of files. From there we
* only care about either JS or JSON files being changed.
*
* @static
* @return {Module[]} modules - The updated modules.
*/
Module.getUpdated = function() {
var command = 'git';
var args = ['diff'];
if (!isPushToMaster()) {
run([
'git remote add temp',
'https://github.com/GoogleCloudPlatform/google-cloud-node.git'
]);
run('git fetch -q temp');
args.push('HEAD', 'temp/master');
} else {
args.push('HEAD^');
}
args.push('--name-only');
console.log(command, args.join(' '));
// There's a Windows bug where child_process.exec exits early on `git diff`
// which in turn does not return all of the files that have changed. This can
// cause a false positive when checking for package changes on AppVeyor
var output = spawn(command, args, {
cwd: ROOT_DIR,
stdio: null
});
if (output.status || output.error) {
console.error(output.error || output.stderr.toString());
exit(output.status || 1);
}
var files = output.stdout.toString();
console.log(files);
var modules = files
.trim()
.split('\n')
.filter(function(file) {
return /^packages\/.+\.js/.test(file);
})
.map(function(file) {
return file.split('/')[1];
});
return uniq(modules).map(Module);
};
/**
* Builds docs for all modules
*
* @static
*/
Module.buildDocs = function() {
run('npm run docs', { cwd: ROOT_DIR });
};
/**
* Returns a list containing ALL the modules.
*
* @static
* @return {Module[]} modules - All of em'!
*/
Module.getAll = function() {
cd(ROOT_DIR);
return globby
.sync('*', { cwd: 'packages' })
.map(Module);
};
/**
* Returns a list of modules that are dependent on one or more of the modules
* specified.
*
* @static
* @param {Module[]} modules - The dependency modules.
* @return {Module[]} modules - The dependent modules.
*/
Module.getDependents = function(modules) {
return Module.getAll().filter(function(mod) {
return mod.hasDeps(modules);
});
};
/**
* Installs dependencies for all the modules!
*
* @static
*/
Module.installAll = function() {
run('npm run postinstall', { cwd: ROOT_DIR });
};
/**
* Generates an lcov coverage report for the specified modules.
*
* @static
*/
Module.runCoveralls = function() {
run('npm run coveralls', { cwd: ROOT_DIR });
};
/**
* Installs this modules dependencies via `npm install`
*/
Module.prototype.install = function() {
run('npm install', { cwd: this.directory });
};
/**
* Creates/uses symlink for a module (depending on if module was provided)
* via `npm link`
*
* @param {Module=} mod - The module to use with `npm link ${mod.packageName}`
*/
Module.prototype.link = function(mod) {
run(['npm link', mod && mod.packageName || ''], {
cwd: this.directory
});
};
/**
* Runs unit tests for this module via `npm run test`
*/
Module.prototype.runUnitTests = function() {
run('npm run test', { cwd: this.directory });
};
/**
* Runs snippet tests for this module.
*/
Module.prototype.runSnippetTests = function() {
process.env.TEST_MODULE = this.name;
run('npm run snippet-test', { cwd: ROOT_DIR });
delete process.env.TEST_MODULE;
};
/**
* Runs system tests for this module via `npm run system-test`
*/
Module.prototype.runSystemTests = function() {
if (this.name === 'common') {
return;
}
run('npm run system-test', { cwd: this.directory });
};
/**
* Checks to see if this module has one or more of the supplied modules
* as a dev dependency.
*
* @param {Module[]} modules - The modules to check for.
* @return {boolean}
*/
Module.prototype.hasDeps = function(modules) {
var packageName;
for (var i = 0; i < modules.length; i++) {
packageName = modules[i].packageName;
if (this.dependencies.indexOf(packageName) > -1) {
return true;
}
}
return false;
};
module.exports.Module = Module;
/**
* Exec's command via child_process.spawnSync.
* By default all output will be piped to the console unless `stdio`
* is overridden.
*
* @param {string} command - The command to run.
* @param {object=} options - Options to pass to `spawnSync`.
* @return {string|null}
*/
function run(command, options) {
options = options || {};
if (Array.isArray(command)) {
command = command.join(' ');
}
console.log(command);
var response = exec(command.trim(), options);
if (response.code) {
exit(response.code);
}
return response.stdout;
}
module.exports.run = run;
/**
* Used to make committing to git easier/etc..
*
* @param {string=} cwd - Directory to commit/add/push from.
*/
function Git(cwd) {
this.cwd = cwd || ROOT_DIR;
this.branch = {
current: IS_PR ? 'FETCH_HEAD' : run('git rev-parse --abbrev-ref HEAD', {
cwd: this.cwd
}).trim()
};
}
// ssh fails on AppVeyor
Git.REPO = 'https://github.com/GoogleCloudPlatform/google-cloud-node.git';
/**
* Checks out a branch.
*
* @param {string} branch - The branch to check out.
*/
Git.prototype.checkout = function(branch) {
// trying to checkout the same branch.. so we'll skip it
if (this.branch.current === branch) {
this.branch.previous = branch;
return;
}
// checking out previous branch.. check if they are the same and skip
if (branch === '-' && this.branch.current === this.branch.previous) {
delete this.branch.previous;
return;
}
run(['git checkout', branch], {
cwd: this.cwd
});
this.branch.previous = this.branch.current;
this.branch.current = branch;
};
/**
* Creates a submodule in the root directory in quiet mode.
*
* @param {string} branch - The branch to use.
* @param {string=} alias - Name of the folder that contains submodule.
* @return {Git}
*/
Git.prototype.submodule = function(branch, alias) {
alias = alias || branch;
run(['git submodule add -q -b', branch, Git.REPO, alias], {
cwd: this.cwd
});
var git = new Git(path.join(this.cwd, alias));
git.branch.current = branch;
git.alias = alias;
return git;
};
/**
* Check to see if git has any files it can commit.
*
* @return {boolean}
*/
Git.prototype.hasUpdates = function() {
var output = run('git status --porcelain', {
cwd: this.cwd
});
return !!output && output.trim().length > 0;
};
/**
* Sets git user
*
* @param {string} name - User name
* @param {string} email - User email
*/
Git.prototype.setUser = function(name, email) {
run(['git config --global user.name', name], {
cwd: this.cwd
});
run(['git config --global user.email', email], {
cwd: this.cwd
});
};
/**
* Adds all files passed in via git add
*
* @param {...string} file - File to add
*/
Git.prototype.add = function() {
var files = [].slice.call(arguments);
var command = ['git add'].concat(files);
run(command, {
cwd: this.cwd
});
};
/**
* Removes files via git
*
* @param {string=} options - Command line options like -rf
* @param {...string} file - File to remove.
*/
Git.prototype.remove = function() {
var files = [].slice.call(arguments);
var command = ['git rm'].concat(files);
run(command, {
cwd: this.cwd
});
};
/**
* Commits to git via commit message.
*
* @param {string} message - The commit message.
*/
Git.prototype.commit = function(message) {
run(['git commit -m', '"' + message + ' [ci skip]"'], {
cwd: this.cwd
});
};
/**
* Runs git status and pushes changes in quiet mode.
*
* @param {string} branch - The branch to push to.
*/
Git.prototype.push = function(branch) {
run('git status', {
cwd: this.cwd
});
run(['git push -q', Git.REPO, branch], {
cwd: this.cwd
});
};
/**
* Deinits a submodule.
*
* @param {git} submodule - The submodule instance.
*/
Git.prototype.deinit = function(submodule) {
var options = {
cwd: this.cwd
};
run(['git submodule deinit -f', submodule.alias], options);
run(['git rm -rf', submodule.alias], options);
run('git rm -rf .gitmodules', options);
rm('-rf', path.resolve('.git/modules', submodule.alias));
};
module.exports.git = new Git();
/**
* The name of the branch currently being tested.
*
* @alias ci.BRANCH
*/
var BRANCH = process.env.TRAVIS_BRANCH || process.env.APPVEYOR_REPO_BRANCH;
/**
* Returns the tag name (assuming this is a release)
*
* @alias ci.getTagName
* @return {string|null}
*/
function getTagName() {
return process.env.TRAVIS_TAG || process.env.APPVEYOR_REPO_TAG_NAME;
}
/**
* Let's us know whether or not this is a release.
*
* @alias ci.isReleaseBuild
* @return {string|null}
*/
function isReleaseBuild() {
return !!getTagName();
}
/**
* Returns name/version of release.
*
* @alias ci.getRelease
* @return {object|null}
*/
function getRelease() {
var tag = getTagName();
if (!tag) {
return null;
}
var parts = tag.split('-');
return {
version: parts.pop(),
name: parts.pop() || Module.UMBRELLA
};
}
/**
* Checks to see if this is a push to master.
*
* @alias ci.isPushToMaster
* @return {boolean}
*/
function isPushToMaster() {
return BRANCH === 'master' && !IS_PR;
}
/**
* Checks to see if this the CI's first pass (Travis only)
*
* @alias ci.isFirstPass
* @return {boolean}
*/
function isFirstPass() {
return /\.1$/.test(process.env.TRAVIS_JOB_NUMBER);
}
module.exports.ci = {
BRANCH: BRANCH,
IS_PR: IS_PR,
PR_NUMBER: PR_NUMBER,
getTagName: getTagName,
isReleaseBuild: isReleaseBuild,
getRelease: getRelease,
isPushToMaster: isPushToMaster,
isFirstPass: isFirstPass
};