Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 7e6ff55

Browse files
jtrabandnaomiblack
authored andcommitted
shred and watch build tooling ( dgeni, gulp, jade)
1 parent 5fbd16f commit 7e6ff55

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+8335
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ Angular.io is currently the preview site for Angular 2. This site also includes
1212
3. run `harp server`
1313
4. Open this url in the browser: [http://localhost:9000/](http://localhost:9000/)
1414

15+
## Development setup with watches
16+
1. cd into root directory `angular.io/`
17+
2. run `gulp serve-and-watch`
18+
3. Open this url in the browser: [http://localhost:9000/](http://localhost:9000/)
19+
4. Refresh your browser to see any changes.
20+
21+
## Development setup with watches and browser reload
22+
1. cd into root directory `angular.io/`
23+
2. install `browser-sync`
24+
25+
`npm install -g browser-sync`<br/>
26+
27+
*or on Windows*<br/>
28+
29+
`npm install -g browser-sync --msvs_version=2013`
30+
31+
3. run `gulp serve-and-watch`
32+
4. run `browser-sync start --proxy localhost:9000 --files "public/docs/**/*/**/*" --reloadDelay 500`
33+
5. browser will launch and stay refreshed automatically.
1534

1635
## Technology Used
1736
- Angular 1.x: The production ready version of Angular

gulpfile.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var gulp = require('gulp');
2+
var watch = require('gulp-watch');
3+
var gutil = require('gulp-util');
4+
var Dgeni = require('dgeni');
5+
var path = require('path');
6+
var del = require('del');
7+
8+
var docShredder = require('./public/doc-shredder/doc-shredder');
9+
10+
var shredOptions = {
11+
basePath: path.resolve('./public/docs'),
12+
sourceDir: "_examples",
13+
destDir: "_fragments"
14+
};
15+
16+
gulp.task('shred-full', ['shred-clean'], function() {
17+
docShredder.shred( shredOptions);
18+
});
19+
20+
gulp.task('serve-and-watch', function (cb) {
21+
var pattern = path.join(shredOptions.basePath, shredOptions.sourceDir, "**/*.*");
22+
23+
execCommands(['harp server'])
24+
25+
watch([ pattern], function(event, done) {
26+
console.log('Event type: ' + event.event); // added, changed, or deleted
27+
console.log('Event path: ' + event.path); // The path of the modified file
28+
docShredder.shredSingleDir(shredOptions, event.path);
29+
});
30+
31+
});
32+
33+
gulp.task('shred-clean', function(cb) {
34+
var cleanPath = path.join(shredOptions.basePath, shredOptions.destDir, '**/*.*')
35+
del([ cleanPath, '!**/*.ovr.*'], function (err, paths) {
36+
// console.log('Deleted files/folders:\n', paths.join('\n'));
37+
cb();
38+
});
39+
});
40+
41+
42+
// added options are: shouldLog
43+
// cb is function(err, stdout, stderr);
44+
function execCommands(cmds, options, cb) {
45+
options = options || {};
46+
options.shouldThrow = options.shouldThrow == null ? true : options.shouldThrow;
47+
options.shouldLog = options.shouldLog == null ? true : options.shouldLog;
48+
if (!cmds || cmds.length == 0) cb(null, null, null);
49+
var exec = require('child_process').exec; // just to make it more portable.
50+
exec(cmds[0], options, function(err, stdout, stderr) {
51+
if (err == null) {
52+
if (options.shouldLog) {
53+
gutil.log('cmd: ' + cmds[0]);
54+
gutil.log('stdout: ' + stdout);
55+
}
56+
if (cmds.length == 1) {
57+
cb(err, stdout, stderr);
58+
} else {
59+
execCommands(cmds.slice(1), options, cb);
60+
}
61+
} else {
62+
if (options.shouldLog) {
63+
gutil.log('exec error on cmd: ' + cmds[0]);
64+
gutil.log('exec error: ' + err);
65+
if (stdout) gutil.log('stdout: ' + stdout);
66+
if (stderr) gutil.log('stderr: ' + stderr);
67+
}
68+
if (err && options.shouldThrow) throw err;
69+
cb(err, stdout, stderr);
70+
}
71+
});
72+
}
73+
74+
75+
76+
gulp.task('default', ['shred']);

package.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"name": "angular.io",
3+
"version": "0.0.0",
4+
"private": true,
5+
"description": "Angular 2 documentation",
6+
"main": "index.js",
7+
"scripts": {
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"repository": {
11+
"type": "git"
12+
},
13+
"licenses": [
14+
{
15+
"type": "Apache",
16+
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
17+
}
18+
],
19+
"bugs": {
20+
"url": ""
21+
},
22+
"devDependencies": {
23+
"canonical-path": "0.0.2",
24+
"del": "^1.2.0",
25+
"dgeni": "^0.4.0",
26+
"dgeni-packages": "^0.10.0",
27+
"gulp": "^3.5.6",
28+
"gulp-util": "^3.0.6",
29+
"gulp-watch": "^4.3.4",
30+
"lodash": "^3.10.1",
31+
"path": "^0.11.14"
32+
},
33+
"contributors": [
34+
"Jay Traband <[email protected]>"
35+
]
36+
}

public/_includes/_util-fns.jade

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
- var getFrag = function(fileName) {
2+
- var frag = partial(fileName);
3+
- if (frag == null) {
4+
- return "BAD FILENAME: " + fileName + " Current path: " + current.path;
5+
- } else {
6+
- // ``` gets translated to <pre><code>.....</code></pre> and we need
7+
- // to remove this from the fragment prefix is 11 long and suffix is 13 long
8+
- var r = frag.substring(11, frag.length-13);
9+
- return r;
10+
- }
11+
- }
12+
13+
- var getExtn = function(fileName) {
14+
- var ix = fileName.lastIndexOf('.');
15+
- return ix > 0 ? fileName.substr(ix+1) : "";
16+
- }
17+
18+
// HACK: to avoid having to include a path in makeTabs calls
19+
- var currentPath = current.path;
20+
// need to back up to 'docs'
21+
- var pathToFrags = "../../../../../../../../../../../".substr(0, (currentPath.length-2)*3) + "_fragments/";
22+
23+
mixin makeTabs(path, fileNames, tabNames)
24+
- fileNames = fileNames.split(",");
25+
- tabNames = tabNames.split(",")
26+
// .p Length #{currentPath.length}
27+
code-tabs
28+
each fileName,index in fileNames
29+
- var tabName = tabNames[index].trim();
30+
- var fileName = fileNames[index].trim();
31+
- var extn = getExtn(fileName);
32+
// - var extPath = pathToFrags + (path.length ? path + "/" : "");
33+
- var extPath = pathToFrags + path + "/";
34+
code-pane(language="#{extn}" name="#{tabName}" format="linenums")
35+
!= getFrag(extPath + fileName + ".md")

public/doc-shredder/doc-shredder.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
2+
// Canonical path provides a consistent path (i.e. always forward slashes) across different OSes
3+
var path = require('canonical-path');
4+
// var path = require('path');
5+
var del = require('del');
6+
var Dgeni = require('dgeni');
7+
var _ = require('lodash');
8+
9+
var createPackage = function(shredOptions) {
10+
var shredder = new Dgeni.Package('doc-shredder', [
11+
// require('dgeni-packages/base') - doesn't work
12+
]);
13+
shredder.options = resolveOptions(shredOptions);
14+
return configure(shredder);
15+
};
16+
17+
var resolveOptions = function(shredOptions) {
18+
return _.defaults({}, shredOptions, {
19+
basePath: path.resolve('.'),
20+
// read files from any subdir under here
21+
sourceDir: "docs/_examples",
22+
// shredded files get copied here with same subdir structure.
23+
destDir: "docs/_fragments",
24+
// whether to include subdirectories when shredding.
25+
includeSubdirs: true
26+
});
27+
}
28+
29+
var shred = function(shredOptions) {
30+
try {
31+
var pkg = createPackage(shredOptions);
32+
var dgeni = new Dgeni([ pkg]);
33+
return dgeni.generate();
34+
} catch(x) {
35+
console.log(x.stack);
36+
throw x;
37+
}
38+
}
39+
40+
var shredSingleDir = function(shredOptions, filePath) {
41+
shredOptions = resolveOptions(shredOptions);
42+
var root = path.resolve(shredOptions.basePath, shredOptions.sourceDir);
43+
var fileDir = path.dirname(filePath);
44+
var relativePath = path.relative(root, fileDir);
45+
var sourceDir = path.join(shredOptions.sourceDir, relativePath);
46+
var destDir = path.join(shredOptions.destDir, relativePath);
47+
var options = {
48+
basePath: shredOptions.basePath,
49+
includeSubdirs: false,
50+
sourceDir: sourceDir,
51+
destDir: destDir
52+
}
53+
var cleanPath = path.join(shredOptions.basePath, destDir, '*.*')
54+
del([ cleanPath, '!**/*.ovr.*'], function (err, paths) {
55+
// console.log('Deleted files/folders:\n', paths.join('\n'));
56+
return shred(options);
57+
});
58+
59+
}
60+
61+
module.exports = {
62+
shred: shred,
63+
shredSingleDir: shredSingleDir,
64+
createPackage: createPackage,
65+
resolveOptions: resolveOptions
66+
};
67+
68+
function configure(shredder) {
69+
var options = shredder.options;
70+
shredder
71+
.processor(require('dgeni-packages/base/processors/read-files'))
72+
.processor(require('dgeni-packages/base/processors/write-files'))
73+
.factory(require('dgeni-packages/base/services/writefile'))
74+
75+
// Ugh... Boilerplate that dgeni needs to sequence operations
76+
.processor({ name: 'reading-files' })
77+
.processor({ name: 'files-read', $runAfter: ['reading-files'] })
78+
.processor({ name: 'processing-docs', $runAfter: ['files-read'] })
79+
.processor({ name: 'docs-processed', $runAfter: ['processing-docs'] })
80+
.processor({ name: 'adding-extra-docs', $runAfter: ['docs-processed'] })
81+
.processor({ name: 'extra-docs-added', $runAfter: ['adding-extra-docs'] })
82+
.processor({ name: 'computing-ids', $runAfter: ['extra-docs-added'] })
83+
.processor({ name: 'ids-computed', $runAfter: ['computing-ids'] })
84+
.processor({ name: 'computing-paths', $runAfter: ['ids-computed'] })
85+
.processor({ name: 'paths-computed', $runAfter: ['computing-paths'] })
86+
.processor({ name: 'rendering-docs', $runAfter: ['paths-computed'] })
87+
.processor({ name: 'docs-rendered', $runAfter: ['rendering-docs'] })
88+
.processor({ name: 'writing-files', $runAfter: ['docs-rendered'] })
89+
.processor({ name: 'files-written', $runAfter: ['writing-files'] })
90+
91+
.factory(require('./fileShredder'))
92+
.factory(require('./regionExtractor'))
93+
.processor(require('./mdWrapperProcessor'))
94+
95+
.config(function(log) {
96+
// Set logging level
97+
log.level = 'info';
98+
})
99+
100+
101+
.config(function(readFilesProcessor, fileShredder ) {
102+
readFilesProcessor.fileReaders = [ fileShredder];
103+
})
104+
105+
// default configs - may be overriden
106+
.config(function(readFilesProcessor) {
107+
108+
// Specify the base path used when resolving relative paths to source and output files
109+
readFilesProcessor.basePath = options.basePath;
110+
111+
// Specify collections of source files that should contain the documentation to extract
112+
var extns = ['*.js', '*.html', '*.ts', '*.css' ];
113+
var includeFiles = extns.map(function(extn) {
114+
if (options.includeSubdirs) {
115+
return path.join(options.sourceDir, '**', extn);
116+
} else {
117+
return path.join(options.sourceDir, extn);
118+
}
119+
});
120+
readFilesProcessor.sourceFiles = [
121+
{
122+
// Process all candidate files in `src` and its subfolders ...
123+
include: includeFiles,
124+
125+
// When calculating the relative path to these files use this as the base path.
126+
// So `src/foo/bar.js` will have relative path of `foo/bar.js`
127+
basePath: options.sourceDir
128+
}
129+
];
130+
})
131+
.config(function(writeFilesProcessor) {
132+
// Specify where the writeFilesProcessor will write our generated doc files
133+
writeFilesProcessor.outputFolder = options.destDir;
134+
});
135+
return shredder;
136+
}

public/doc-shredder/fileShredder.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @dgService htmlFileShredder
3+
* @description
4+
*/
5+
module.exports = function fileShredder(log, regionExtractor) {
6+
return {
7+
name: 'fileShredder',
8+
9+
getDocs: function (fileInfo) {
10+
var commentMarkers;
11+
switch (fileInfo.extension) {
12+
case 'ts':
13+
case 'js':
14+
commentMarkers = ['//'];
15+
break;
16+
case 'html':
17+
commentMarkers = ['<!--'];
18+
break;
19+
case 'css':
20+
commentMarkers = ['/*'];
21+
break;
22+
default:
23+
return [];
24+
}
25+
log.info("fileShredder processing: " + fileInfo.projectRelativePath);
26+
return regionExtractor(fileInfo.content, commentMarkers);
27+
}
28+
}
29+
}
30+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Not currently used - but wanted to leave it as an example
2+
// var _ = require('lodash');
3+
4+
/**
5+
* dgProcessor shredderProcessor
6+
* @description
7+
*
8+
*/
9+
module.exports = function mdWrapperProcessor(log) {
10+
return {
11+
$runAfter: ['readFilesProcessor'],
12+
$runBefore: ['writing-files'],
13+
$process: function(docs) {
14+
return docs.map(function(doc) {
15+
var fileInfo = doc.fileInfo;
16+
doc.renderedContent = '```\n' + doc.content + '\n```';
17+
var regionSuffix = (doc.regionName && doc.regionName.length) ? "-" + doc.regionName.trim() : "";
18+
var origName = fileInfo.baseName + "." + fileInfo.extension;
19+
20+
var newName = fileInfo.baseName + regionSuffix + "." + fileInfo.extension + ".md";
21+
doc.outputPath = fileInfo.relativePath.replace(origName, newName);
22+
return doc;
23+
})
24+
}
25+
};
26+
};

0 commit comments

Comments
 (0)