Repository navigation
Expand file tree
/
Copy pathbuilder.js
More file actions
473 lines (402 loc) · 11.6 KB
/
Copy pathbuilder.js
File metadata and controls
473 lines (402 loc) · 11.6 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
/*!
* 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';
require('shelljs/global');
var chalk = require('chalk');
var flatten = require('lodash.flatten');
var fs = require('fs');
var globby = require('globby');
var path = require('path');
var semver = require('semver');
var config = require('./config');
var helpers = require('../helpers');
var parser = require('./parser');
var git = helpers.git;
var DOCS_ROOT = './docs/json';
var MARKDOWN = path.resolve(DOCS_ROOT, '../*.md');
var PACKAGES_ROOT = './packages';
var UMBRELLA_PACKAGE = config.UMBRELLA_PACKAGE;
var manifestPath = path.resolve(DOCS_ROOT, '../manifest.json');
var manifest = require(manifestPath);
/**
* @class Builder
*
* This is a custom builder used to generate documentation json!
*
* @param {string} name - Name of the module to build docs for.
* @param {string=} version - Target version of the docs.
* @param {string=} cwd - The current working directory to build from.
*
* @example
* var builder = new Builder('vision', '0.5.0');
*/
function Builder(name, version, cwd) {
this.name = name;
this.version = version || config.DEFAULT_VERSION;
this.dir = path.join(cwd || '', DOCS_ROOT, name, this.version);
this.isUmbrella = name === UMBRELLA_PACKAGE;
this.isRelease = !!semver.valid(this.version);
}
/**
* This simply generates documentation for the specified module.
* All files will end up in /docs/json/{builder.name}/{builder.version}
*
* If there are any malformed docs/etc., an error will be thrown.
*
* @throws {error}
*
* @example
* builder.build();
*/
Builder.prototype.build = function() {
var self = this;
mkdir('-p', this.dir);
cp(MARKDOWN, this.dir);
var docs = globby.sync(path.join(this.name, 'src/*.js'), {
cwd: PACKAGES_ROOT,
ignore: config.IGNORE
}).map(function(file) {
var json = self.parseFile(file);
var outputFile = file.replace(/^.+src\//, '') + 'on';
json.path = path.basename(outputFile);
self.write(outputFile, json);
return json;
});
var types = parser.createTypesDictionary(docs);
var toc = parser.createToc(types);
toc.tagName = this.getTagName();
this.write(config.TYPES_DICT, types);
this.write(config.TOC, toc);
if (this.isUmbrella) {
new Bundler(this).bundle();
}
};
/**
* Retrieve the tag name for the current release. Will throw an error if
* version is not set.
*
* @throws {error}
* @return {string} tagName
*
* @example
* var tagName = builder.getTagName(); // bigtable-0.2.0
*/
Builder.prototype.getTagName = function() {
if (!semver.valid(this.version)) {
return this.version;
}
if (this.isUmbrella) {
return this.version.replace(/^v*/, 'v');
}
return [this.name, this.version].join('-');
};
/**
* Retrieves the "types" list for the the current module.
*
* @return {object[]} types
*
* @example
* var types = builder.getTypes();
*/
Builder.prototype.getTypes = function() {
return require(path.resolve(this.dir, config.TYPES_DICT));
};
/**
* Attempts to parse the specified file. Will throw an error if parsing fails.
*
* @throw {error}
* @param {string} file - The file to parse.
* @return {object} json - The parsed file JSON.
*
* @example
* var json = builder.parseFile('bigtable/src/index.js');
*/
Builder.prototype.parseFile = function(file) {
var filePath = path.join(PACKAGES_ROOT, file);
var contents = fs.readFileSync(filePath, 'utf8');
try {
return parser.parseFile(filePath, contents);
} catch (e) {
e.message =
'Unable to generate docs for file: ' + file + '. Reason: ' + e.message;
throw e;
}
};
/**
* Updates the manifest.json file to include the module and/or tagged version.
*/
Builder.prototype.updateManifest = function() {
var config = findWhere(manifest.modules, { id: this.name });
if (!config) {
config = {
id: this.name,
name: '@google-cloud/' + this.name,
defaultService: this.name,
versions: ['master']
};
manifest.modules.push(config);
manifest.modules.sort(sortByModId);
}
if (config.versions.indexOf(this.version) === -1) {
config.versions.unshift(this.version);
}
fs.writeFileSync(manifestPath, JSON.stringify(manifest, null, 2));
};
/**
* Convenience method to make writing JSON files a little less tedious.
*
* @param {string} file - The file name.
* @param {object} json - The JSON object.
*/
Builder.prototype.write = function(file, json) {
fs.writeFileSync(path.join(this.dir, file), JSON.stringify(json));
};
/**
* @class Bundler
*
* This class is used to generate/update the umbrella documentation.
*
* @param {builder|string} builder - Either a Builder instance or version of
* the umbrella docs.
*
* @example
* var builder = new Builder('google-cloud', '0.44.0');
* var bundler = new Bundler(builder);
*
* // or the shorthand version..
* var bundler = new Bundler('0.44.0');
*/
function Bundler(builder) {
this.builder = builder;
if (typeof this.builder === 'string') {
this.builder = new Builder(UMBRELLA_PACKAGE, builder);
}
}
/**
* @static
*
* This iterates over all known umbrella package versions and checks to see
* if the supplied builder is the max satisfying semver version. If it is, then
* it will update the JSON files for that particular builder.
*
* @param {builder} builder - The Builder instance.
*
* @example
* // pretend this was a patch release, it would automatically be installed
* // to umbrella packages that list 0.4.0 as the semver version.
* var builder = new Builder('bigtable', '0.4.1');
*
* Bundler.updateDep(builder);
*/
Bundler.updateDep = function(builder) {
if (!builder.isRelease) {
throw new Error('Must supply valid version to update bundles with.');
}
var bundleConfig = findWhere(manifest.modules, {
id: UMBRELLA_PACKAGE
});
var bundleVersions = bundleConfig.versions;
var config = findWhere(manifest.modules, { id: builder.name });
var versions = config.versions.filter(semver.valid);
var builderTag = builder.getTagName();
var bundler, dep, bundleTag;
for (var i = 0; i < bundleVersions.length - 1; i++) {
bundler = new Bundler(bundleVersions[i]);
bundleTag = bundler.builder.getTagName();
git.checkout(bundleTag);
dep = findWhere(bundler.getDeps(), { name: builder.name }) || {};
git.checkout('-');
if (semver.maxSatisfying(versions, dep.version) !== builder.version) {
break;
}
console.log('\n');
console.log(chalk.blue('Updating %s with %s docs'), bundleTag, builderTag);
bundler.add(builder);
}
};
/**
* Adds the supplied builder to the bundled docs.
*
* @param {builder} builder - The builder instance.
*
* @example
* var builder = new Builder('bigtable', '0.5.0');
* var bundler = new Bundler('0.45.0');
*
* bundler.add(builder);
* // The 0.45.0 bundle now contains the bigtable-0.5.0 docs
*/
Bundler.prototype.add = function(builder) {
var self = this;
var outputFolder = path.join(this.builder.dir, builder.name);
mkdir('-p', outputFolder);
globby.sync(path.resolve(builder.dir, '*.json'), {
ignore: [config.TYPES_DICT, config.TOC].map(function(file) {
return path.resolve(builder.dir, file);
})
}).forEach(function(file) {
var json = require(file);
var service = json.parent || json.id;
var outputFile = path.join(builder.name, path.basename(file));
json.overview = parser.createOverview(service, true);
self.builder.write(outputFile, json);
});
};
/**
* Generates all JSON files for the umbrella version.
*
* @example
* var bundler = new Bundler('0.45.0');
*
* bundler.bundle();
*/
Bundler.prototype.bundle = function() {
var self = this;
var baseTypes = this.builder.getTypes();
var deps = this.getDeps().map(function(dep) {
var config = findWhere(manifest.modules, { id: dep.name });
var versions = config.versions.filter(semver.valid);
return {
name: dep.name,
version: semver.maxSatisfying(versions, dep.version)
};
});
var submodule = git.submodule('master', 'bundler');
var depTypes = deps.map(function(dep) {
var builder = new Builder(dep.name, dep.version, submodule.cwd);
submodule.checkout(builder.getTagName());
builder.build();
self.add(builder);
return builder.getTypes().map(function(type) {
type.contents = path.join(dep.name, type.contents);
return type;
});
});
var types = baseTypes.concat(flatten(depTypes));
var toc = parser.createToc(types, true);
toc.tagName = this.builder.getTagName();
this.builder.write(config.TYPES_DICT, types);
this.builder.write(config.TOC, toc);
git.deinit(submodule);
};
/**
* Gets the umbrella package @google-cloud specific dependencies for the
* current state of the package.
*
* @return {object[]}
*
* @example
* var bundler = new Bundler('0.44.0');
* var deps = bundler.getDeps();
* // deps = [
* // {
* // name: 'bigtable',
* // version: '^0.4.0'
* // }
* // ]
*/
Bundler.prototype.getDeps = function() {
var jsonPath = path.resolve(PACKAGES_ROOT, UMBRELLA_PACKAGE, 'package.json');
var contents = JSON.parse(fs.readFileSync(jsonPath, 'utf8'));
var dependencies = contents.dependencies;
return Object.keys(dependencies)
.filter(function(dep) {
return /^\@google\-cloud/.test(dep);
})
.map(function(dep) {
return {
name: dep.replace('@google-cloud/', ''),
version: dependencies[dep]
};
});
};
/**
* Searches a collection to find a model containing the specified props.
*
* @param {object[]} collection - The collection to search.
* @param {object} props - The properties to match against.
* @return {object|null}
*/
function findWhere(collection, props) {
var i, key, obj;
collLoop: for (i = 0; i < collection.length; i++) {
obj = collection[i];
for (key in props) {
if (obj[key] !== props[key]) {
continue collLoop;
}
}
return obj;
}
return null;
}
/**
* Used to sort manifest modules by id.
*
* @param {object} a - module A.
* @param {object} b - module B.
* @return {number}
*/
function sortByModId(a, b) {
if (a.id === UMBRELLA_PACKAGE) {
return -1;
}
if (b.id === UMBRELLA_PACKAGE) {
return 1;
}
return +(a.id > b.id) || +(a.id === b.id) - 1;
}
/**
* Builds docs for the specified module.
*
* @param {string} name - The name of the module to build docs for.
* @param {string=} version - The version of the module.
*
* @example
* builder.build('bigtable', '0.2.0');
*/
function build(name, version) {
var builder = new Builder(name, version);
console.log('\n');
console.log(chalk.blue('Building docs for %s@%s'), name, version);
git.checkout(builder.getTagName());
builder.build();
git.checkout('-');
if (builder.isRelease) {
if (!builder.isUmbrella) {
Bundler.updateDep(builder);
}
builder.updateManifest();
}
}
module.exports.build = build;
/**
* Builds docs for all modules.
*
* @example
* builder.buildAll();
*/
function buildAll() {
var currentBranch = git.branch.current;
var modules = globby.sync('*', {
cwd: PACKAGES_ROOT,
ignore: config.IGNORE
});
modules.forEach(function(name) {
build(name, currentBranch);
});
}
module.exports.buildAll = buildAll;