| 
									
										
										
										
											2015-08-03 17:45:58 -07:00
										 |  |  | var gulp = require('gulp'); | 
					
						
							|  |  |  | var watch = require('gulp-watch'); | 
					
						
							|  |  |  | var gutil = require('gulp-util'); | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  | var taskListing = require('gulp-task-listing'); | 
					
						
							|  |  |  | var path = require('canonical-path'); | 
					
						
							| 
									
										
										
										
											2015-08-03 17:45:58 -07:00
										 |  |  | var del = require('del'); | 
					
						
							| 
									
										
										
										
											2015-08-11 00:27:09 -07:00
										 |  |  | var _ = require('lodash'); | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  | var argv = require('yargs').argv; | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  | var Q = require("q"); | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | // delPromise is a 'promise' version of del
 | 
					
						
							|  |  |  | var delPromise =  Q.denodeify(del); | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  | var Minimatch = require("minimatch").Minimatch; | 
					
						
							| 
									
										
										
										
											2015-09-15 23:21:55 -07:00
										 |  |  | var Dgeni = require('dgeni'); | 
					
						
							|  |  |  | var fsExtra = require('fs-extra'); | 
					
						
							|  |  |  | var fs = fsExtra; | 
					
						
							| 
									
										
										
										
											2015-09-25 01:28:36 -07:00
										 |  |  | var exec = require('child_process').exec; | 
					
						
							|  |  |  | var execPromise = Q.denodeify(exec); | 
					
						
							|  |  |  | var prompt = require('prompt'); | 
					
						
							| 
									
										
										
										
											2015-09-15 23:21:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-28 11:14:12 -07:00
										 |  |  | // TODO:
 | 
					
						
							|  |  |  | //  1. Think about using runSequence
 | 
					
						
							|  |  |  | //  2. Think about using spawn instead of exec in case of long error messages.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 17:45:58 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | var docShredder = require('./public/doc-shredder/doc-shredder'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | var _devguideShredOptions =  { | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |   examplesDir: './public/docs/_examples', | 
					
						
							|  |  |  |   fragmentsDir: './public/docs/_fragments' | 
					
						
							| 
									
										
										
										
											2015-08-03 17:45:58 -07:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | var _apiShredOptions =  { | 
					
						
							|  |  |  |   examplesDir: '../angular/modules/angular2/examples', | 
					
						
							|  |  |  |   fragmentsDir: './public/docs/_fragments/_api' | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 07:20:02 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-14 17:12:31 -07:00
										 |  |  | var _excludePatterns = ['**/node_modules/**', '**/typings/**', '**/packages/**']; | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  | var _excludeMatchers = _excludePatterns.map(function(excludePattern){ | 
					
						
							|  |  |  |   return new Minimatch(excludePattern) | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-06 12:45:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | gulp.task('help', taskListing.withFilters(function(taskName) { | 
					
						
							|  |  |  |   var isSubTask = taskName.substr(0,1) == "_"; | 
					
						
							|  |  |  |   return isSubTask; | 
					
						
							|  |  |  | }, function(taskName) { | 
					
						
							|  |  |  |   var shouldRemove = taskName === 'default'; | 
					
						
							|  |  |  |   return shouldRemove; | 
					
						
							|  |  |  | })); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | gulp.task('serve-and-sync', ['build-docs'], function (cb) { | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 13:21:38 -07:00
										 |  |  |   // execCommands(['harp server'], {}, cb);
 | 
					
						
							| 
									
										
										
										
											2015-09-25 02:21:56 -07:00
										 |  |  |   execCommands(['npm run harp -- server .'], {}, cb); | 
					
						
							| 
									
										
										
										
											2015-08-06 12:45:50 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |   var browserSync = require('browser-sync').create(); | 
					
						
							|  |  |  |   browserSync.init({ | 
					
						
							|  |  |  |     proxy: 'localhost:9000', | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  |     files: ["public/docs/**/*/**/*" ], | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |     logFileChanges: true, | 
					
						
							|  |  |  |     reloadDelay: 500 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  |   devGuideExamplesWatch(_devguideShredOptions, function() { | 
					
						
							|  |  |  |     browserSync.reload(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   apiSourceWatch(function() { | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |     browserSync.reload(); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 17:45:58 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-16 09:09:44 -07:00
										 |  |  | gulp.task('build-and-serve', ['build-docs'], function (cb) { | 
					
						
							| 
									
										
										
										
											2015-09-25 02:21:56 -07:00
										 |  |  |   execCommands(['npm run harp -- server .'], {}, cb); | 
					
						
							| 
									
										
										
										
											2015-10-16 09:09:44 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   var browserSync = require('browser-sync').create(); | 
					
						
							|  |  |  |   browserSync.init({ | 
					
						
							|  |  |  |     proxy: 'localhost:9000', | 
					
						
							|  |  |  |     files: ["public/docs/**/*/**/*" ], | 
					
						
							|  |  |  |     logFileChanges: true, | 
					
						
							|  |  |  |     reloadDelay: 500 | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  | }); | 
					
						
							| 
									
										
										
										
											2015-08-03 17:45:58 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | gulp.task('build-docs', ['_shred-devguide-examples', 'build-api-docs'], function() { | 
					
						
							|  |  |  |   return buildShredMaps(true); | 
					
						
							| 
									
										
										
										
											2015-08-03 17:45:58 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | gulp.task('build-devguide-docs', ['_shred-devguide-examples'], function() { | 
					
						
							|  |  |  |   return buildShredMaps(true); | 
					
						
							| 
									
										
										
										
											2015-08-03 17:45:58 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | gulp.task('build-api-docs', ['_shred-api-examples'], function() { | 
					
						
							|  |  |  |   if (!fs.existsSync('../angular')) { | 
					
						
							|  |  |  |     throw new Error('build-api-docs task requires the angular2 repo to be at ' + path.resolve('../angular')); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return buildApiDocs(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | gulp.task('_shred-devguide-examples', ['_shred-clean-devguide'], function() { | 
					
						
							|  |  |  |   return docShredder.shred( _devguideShredOptions); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | gulp.task('_shred-clean-devguide', function(cb) { | 
					
						
							|  |  |  |   var cleanPath = path.join(_devguideShredOptions.fragmentsDir, '**/*.*') | 
					
						
							|  |  |  |   return delPromise([ cleanPath, '!**/*.ovr.*', '!**/_api/**']); | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | gulp.task('_shred-api-examples', ['_shred-clean-api'], function() { | 
					
						
							|  |  |  |   return docShredder.shred( _apiShredOptions); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | gulp.task('_shred-clean-api', function(cb) { | 
					
						
							|  |  |  |   var cleanPath = path.join(_apiShredOptions.fragmentsDir, '**/*.*') | 
					
						
							|  |  |  |   return delPromise([ cleanPath, '!**/*.ovr.*' ]); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | gulp.task('_build-shred-maps', function() { | 
					
						
							|  |  |  |   return build-shred-maps(true); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | gulp.task('git-changed-examples', ['_shred-devguide-examples'], function(){ | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |   var after, sha, messageSuffix; | 
					
						
							|  |  |  |   if (argv.after) { | 
					
						
							|  |  |  |     try { | 
					
						
							|  |  |  |       after = new Date(argv.after); | 
					
						
							|  |  |  |       messageSuffix = ' after: ' + argv.after; | 
					
						
							|  |  |  |     } catch (e) { | 
					
						
							|  |  |  |       throw argv.after + " is not a valid date."; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } else if (argv.sha) { | 
					
						
							|  |  |  |     sha = argv.sha; | 
					
						
							|  |  |  |     messageSuffix = ' on commit: ' + (argv.sha.length ? argv.sha : '[last commit]'); | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     console.log('git-changed-examples may be called with either an "--sha" argument like this:'); | 
					
						
							|  |  |  |     console.log('   gulp git-changed-examples --sha=4d2ac96fa247306ddd2d4c4e0c8dee2223502eb2'); | 
					
						
							|  |  |  |     console.log('or with an "--after" argument like this') | 
					
						
							|  |  |  |     console.log('   gulp git-changed-examples --after="August 1, 2015"'); | 
					
						
							|  |  |  |     return; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |   var jadeShredMap; | 
					
						
							|  |  |  |   return buildShredMaps(false).then(function(docs) { | 
					
						
							|  |  |  |     jadeShredMap = docs[0]; | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |     if (after) { | 
					
						
							|  |  |  |       return getChangedExamplesAfter(after); | 
					
						
							|  |  |  |     } else if (sha) { | 
					
						
							|  |  |  |       return getChangedExamples(sha); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       console.log('git-changed-examples may be called with either an "--sha" argument like this:'); | 
					
						
							|  |  |  |       console.log('   gulp git-changed-examples --sha=4d2ac96fa247306ddd2d4c4e0c8dee2223502eb2'); | 
					
						
							|  |  |  |       console.log('or with an "--after" argument like this') | 
					
						
							|  |  |  |       console.log('   gulp git-changed-examples --after="August 1, 2015"'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |   }).then(function(examplePaths) { | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |     examplePaths = filterOutExcludedPatterns(examplePaths, _excludeMatchers); | 
					
						
							|  |  |  |     console.log('\nExamples changed ' + messageSuffix); | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |     console.log(examplePaths) | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  |     console.log("\nJade files affected by changed example files " + messageSuffix); | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |     var jadeExampleMap = jadeShredMapToJadeExampleMap(jadeShredMap, examplePaths); | 
					
						
							|  |  |  |     console.log(JSON.stringify(jadeExampleMap, null, "  ")); | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |     console.log("-----"); | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |   }).catch(function(err) { | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |     console.log(err); | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |     throw err; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-10 08:58:43 -07:00
										 |  |  | gulp.task('check-deploy', ['build-docs'], function() { | 
					
						
							| 
									
										
										
										
											2015-09-28 11:14:12 -07:00
										 |  |  |   console.log('running harp compile...'); | 
					
						
							|  |  |  |   return execPromise('npm run harp -- compile . ./www', {}).then(function() { | 
					
						
							|  |  |  |     execPromise('npm run live-server ./www'); | 
					
						
							| 
									
										
										
										
											2015-09-25 01:28:36 -07:00
										 |  |  |     return askDeploy(); | 
					
						
							|  |  |  |   }).then(function(shouldDeploy) { | 
					
						
							|  |  |  |     if (shouldDeploy) { | 
					
						
							| 
									
										
										
										
											2015-09-28 11:14:12 -07:00
										 |  |  |       console.log('deploying...'); | 
					
						
							|  |  |  |       return execPromise('firebase deploy'); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return ['Not deploying']; | 
					
						
							| 
									
										
										
										
											2015-09-25 01:28:36 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-09-28 11:14:12 -07:00
										 |  |  |   }).then(function(s) { | 
					
						
							|  |  |  |     console.log(s.join('')); | 
					
						
							| 
									
										
										
										
											2015-09-25 01:28:36 -07:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // returns a promise;
 | 
					
						
							|  |  |  | function askDeploy() { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   prompt.start(); | 
					
						
							|  |  |  |   var schema = { | 
					
						
							|  |  |  |     name: 'shouldDeploy', | 
					
						
							|  |  |  |     description: 'Deploy to Firebase? (y/n): ', | 
					
						
							|  |  |  |     type: 'string', | 
					
						
							|  |  |  |     pattern: /Y|N|y|n/, | 
					
						
							|  |  |  |     message: "Respond with either a 'y' or 'n'", | 
					
						
							|  |  |  |     required: true | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   var getPromise = Q.denodeify(prompt.get); | 
					
						
							|  |  |  |   return getPromise([schema]).then(function(result) { | 
					
						
							|  |  |  |     return result.shouldDeploy.toLowerCase() === 'y'; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | gulp.task('test-api-builder', function (cb) { | 
					
						
							|  |  |  |   execCommands(['npm run test-api-builder'], {}, cb); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function filterOutExcludedPatterns(fileNames, excludeMatchers) { | 
					
						
							|  |  |  |   return fileNames.filter(function(fileName) { | 
					
						
							|  |  |  |     return !excludeMatchers.some(function(excludeMatcher) { | 
					
						
							|  |  |  |       return excludeMatcher.match(fileName); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function apiSourceWatch(postShredAction) { | 
					
						
							|  |  |  |   var srcPattern = ['../angular/modules/angular2/src/**/*.*']; | 
					
						
							|  |  |  |   watch(srcPattern, function (event, done) { | 
					
						
							|  |  |  |     console.log('Event type: ' + event.event); // added, changed, or deleted
 | 
					
						
							|  |  |  |     console.log('Event path: ' + event.path); // The path of the modified file
 | 
					
						
							|  |  |  |     // need to run just build
 | 
					
						
							|  |  |  |     buildApiDocs().then(done); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  |   var examplesPattern = ['../angular/modules/angular2/examples/**/*.*']; | 
					
						
							|  |  |  |   watch(examplesPattern, function (event, done) { | 
					
						
							|  |  |  |     console.log('Event type: ' + event.event); // added, changed, or deleted
 | 
					
						
							|  |  |  |     console.log('Event path: ' + event.path); // The path of the modified file
 | 
					
						
							|  |  |  |     // need to run shredder
 | 
					
						
							|  |  |  |     var cleanPath = path.join(_apiShredOptions.fragmentsDir, '**/*.*'); | 
					
						
							|  |  |  |     return delPromise([ cleanPath, '!**/*.ovr.*' ]).then(function() { | 
					
						
							|  |  |  |       return docShredder.shred(_apiShredOptions); | 
					
						
							|  |  |  |     }).then(function() { | 
					
						
							|  |  |  |       postShredAction && postShredAction(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function buildApiDocs() { | 
					
						
							| 
									
										
										
										
											2015-09-15 23:21:55 -07:00
										 |  |  |   try { | 
					
						
							|  |  |  |     var dgeni = new Dgeni([require('./public/api-builder/angular.io-package')]); | 
					
						
							| 
									
										
										
										
											2015-10-02 09:57:15 -07:00
										 |  |  |     return dgeni.generate().then(function() { | 
					
						
							|  |  |  |       return gulp.src('./public/docs/js/latest/api/**/*.*') | 
					
						
							|  |  |  |         .pipe(gulp.dest('./public/docs/ts/latest/api')); | 
					
						
							|  |  |  |     }) | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |   } catch(err) { | 
					
						
							|  |  |  |     console.log(err); | 
					
						
							|  |  |  |     console.log(err.stack); | 
					
						
							|  |  |  |     throw err; | 
					
						
							| 
									
										
										
										
											2015-09-15 23:21:55 -07:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-15 23:21:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | function devGuideExamplesWatch(shredOptions, postShredAction) { | 
					
						
							|  |  |  |   var pattern = path.join(shredOptions.examplesDir, "**/*.*"); | 
					
						
							|  |  |  |   watch([pattern], function (event, done) { | 
					
						
							|  |  |  |     console.log('Event type: ' + event.event); // added, changed, or deleted
 | 
					
						
							|  |  |  |     console.log('Event path: ' + event.path); // The path of the modified file
 | 
					
						
							|  |  |  |     docShredder.shredSingleDir(shredOptions, event.path).then(function () { | 
					
						
							|  |  |  |       postShredAction && postShredAction(); | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  | function buildShredMaps(shouldWrite) { | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  |   var options = { | 
					
						
							|  |  |  |     devguideExamplesDir: _devguideShredOptions.examplesDir, | 
					
						
							|  |  |  |     apiExamplesDir: _apiShredOptions.examplesDir, | 
					
						
							|  |  |  |     fragmentsDir: _devguideShredOptions.fragmentsDir, | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |     jadeDir: './public/docs', | 
					
						
							|  |  |  |     outputDir: './public/docs', | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |     writeFilesEnabled: shouldWrite | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  |   }; | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |   return docShredder.buildShredMap(options).then(function(docs) { | 
					
						
							|  |  |  |     return docs; | 
					
						
							| 
									
										
										
										
											2015-08-11 00:27:09 -07:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // returns a promise containing filePaths with any changed or added examples;
 | 
					
						
							|  |  |  | function getChangedExamples(sha) { | 
					
						
							| 
									
										
										
										
											2015-10-16 14:35:58 -07:00
										 |  |  |   var Git = require("nodegit"); | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  |   var examplesPath = _devguideShredOptions.examplesDir; | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |   var relativePath = path.relative(process.cwd(), examplesPath); | 
					
						
							|  |  |  |   return Git.Repository.open(".").then(function(repo) { | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |     if (sha.length) { | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |       return repo.getCommit(sha); | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       return repo.getHeadCommit(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }).then(function(commit) { | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |     return getChangedExamplesForCommit(commit, relativePath); | 
					
						
							|  |  |  |   }).catch(function(err) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getChangedExamplesAfter(date, relativePath) { | 
					
						
							| 
									
										
										
										
											2015-10-16 14:35:58 -07:00
										 |  |  |   var Git = require("nodegit"); | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  |   var examplesPath = _devguideShredOptions.examplesDir; | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |   var relativePath = path.relative(process.cwd(), examplesPath); | 
					
						
							|  |  |  |   return Git.Repository.open(".").then(function(repo) { | 
					
						
							|  |  |  |     return repo.getHeadCommit(); | 
					
						
							|  |  |  |   }).then(function(commit) { | 
					
						
							|  |  |  |     var repo = commit.owner(); | 
					
						
							|  |  |  |     var revWalker = repo.createRevWalk(); | 
					
						
							|  |  |  |     revWalker.sorting(Git.Revwalk.SORT.TIME); | 
					
						
							|  |  |  |     revWalker.push(commit.id()); | 
					
						
							|  |  |  |     return revWalker.getCommitsUntil(function (commit) { | 
					
						
							|  |  |  |       return commit.date().getTime() > date.getTime(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }).then(function(commits) { | 
					
						
							|  |  |  |     return Q.all(commits.map(function(commit) { | 
					
						
							|  |  |  |       return getChangedExamplesForCommit(commit, relativePath); | 
					
						
							|  |  |  |     })); | 
					
						
							|  |  |  |   }).then(function(arrayOfPaths) { | 
					
						
							|  |  |  |     var pathMap = {}; | 
					
						
							|  |  |  |     arrayOfPaths.forEach(function(paths) { | 
					
						
							|  |  |  |       paths.forEach(function(path) { | 
					
						
							|  |  |  |         pathMap[path] = true; | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     var uniqPaths = _.keys(pathMap); | 
					
						
							|  |  |  |     return uniqPaths; | 
					
						
							|  |  |  |   }).catch(function(err) { | 
					
						
							|  |  |  |     var x = err; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function getChangedExamplesForCommit(commit, relativePath) { | 
					
						
							|  |  |  |   return commit.getDiff().then(function(diffList) { | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |     var filePaths = []; | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |     diffList.forEach(function (diff) { | 
					
						
							|  |  |  |       diff.patches().forEach(function (patch) { | 
					
						
							| 
									
										
										
										
											2015-08-08 13:55:53 -07:00
										 |  |  |         if (patch.isAdded() || patch.isModified) { | 
					
						
							|  |  |  |           var filePath = path.normalize(patch.newFile().path()); | 
					
						
							|  |  |  |           var isExample = filePath.indexOf(relativePath) >= 0; | 
					
						
							|  |  |  |           // console.log(filePath + " isExample: " + isExample);
 | 
					
						
							|  |  |  |           if (isExample) { | 
					
						
							|  |  |  |             filePaths.push(filePath); | 
					
						
							|  |  |  |           } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return filePaths; | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-08-11 00:27:09 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 17:45:58 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  | function jadeShredMapToJadeExampleMap(jadeShredMap, examplePaths) { | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |   // remove dups in examplePaths
 | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |   var exampleSet = {}; | 
					
						
							|  |  |  |   examplePaths.forEach(function(examplePath) { | 
					
						
							|  |  |  |     exampleSet[examplePath] = examplePath; | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |   var basePath = path.resolve("."); | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |   var jadeToFragMap = jadeShredMap.jadeToFragMap; | 
					
						
							|  |  |  |   var jadeExampleMap = {}; | 
					
						
							|  |  |  |   for (var jadePath in jadeToFragMap) { | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |     var relativeJadePath = path.relative(basePath, jadePath); | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |     var vals = jadeToFragMap[jadePath]; | 
					
						
							|  |  |  |     vals.forEach(function(val) { | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |       var relativeExamplePath = path.relative(basePath, val.examplePath); | 
					
						
							|  |  |  |       if (exampleSet[relativeExamplePath] != null) { | 
					
						
							|  |  |  |         addKeyValue(jadeExampleMap, relativeJadePath, relativeExamplePath); | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return jadeExampleMap; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function jadeShredMapToExampleJadeMap(jadeShredMap) { | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |   var jadeToFragMap = jadeShredMap.jadeToFragMap; | 
					
						
							|  |  |  |   var exampleJadeMap = {}; | 
					
						
							|  |  |  |   for (var jadePath in jadeToFragMap) { | 
					
						
							|  |  |  |     var vals = jadeToFragMap[jadePath]; | 
					
						
							|  |  |  |     vals.forEach(function(val) { | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  |       var examplePath = val.examplePath; | 
					
						
							|  |  |  |       addKeyValue(exampleJadeMap, examplePath, jadePath); | 
					
						
							| 
									
										
										
										
											2015-09-12 00:28:01 -07:00
										 |  |  |     }); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return exampleJadeMap; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function addKeyValue(map, key, value) { | 
					
						
							|  |  |  |   var vals = map[key]; | 
					
						
							|  |  |  |   if (vals) { | 
					
						
							|  |  |  |     if (vals.indexOf(value) == -1) { | 
					
						
							|  |  |  |       vals.push(value); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } else { | 
					
						
							|  |  |  |     map[key] = [value]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 12:41:20 -07:00
										 |  |  | // Synchronously execute a chain of commands.
 | 
					
						
							|  |  |  | // cmds: an array of commands
 | 
					
						
							|  |  |  | // options: { shouldLog: true,  shouldThrow: true }
 | 
					
						
							|  |  |  | // cb: function(err, stdout, stderr)
 | 
					
						
							| 
									
										
										
										
											2015-08-03 17:45:58 -07:00
										 |  |  | function execCommands(cmds, options, cb) { | 
					
						
							|  |  |  |   options = options || {}; | 
					
						
							|  |  |  |   options.shouldThrow = options.shouldThrow == null ? true : options.shouldThrow; | 
					
						
							|  |  |  |   options.shouldLog = options.shouldLog == null ? true : options.shouldLog; | 
					
						
							|  |  |  |   if (!cmds || cmds.length == 0) cb(null, null, null); | 
					
						
							|  |  |  |   var exec = require('child_process').exec;  // just to make it more portable.
 | 
					
						
							|  |  |  |   exec(cmds[0], options, function(err, stdout, stderr) { | 
					
						
							|  |  |  |     if (err == null) { | 
					
						
							|  |  |  |       if (options.shouldLog) { | 
					
						
							|  |  |  |         gutil.log('cmd: ' + cmds[0]); | 
					
						
							|  |  |  |         gutil.log('stdout: ' + stdout); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (cmds.length == 1) { | 
					
						
							|  |  |  |         cb(err, stdout, stderr); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         execCommands(cmds.slice(1), options, cb); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       if (options.shouldLog) { | 
					
						
							|  |  |  |         gutil.log('exec error on cmd: ' + cmds[0]); | 
					
						
							|  |  |  |         gutil.log('exec error: ' + err); | 
					
						
							|  |  |  |         if (stdout) gutil.log('stdout: ' + stdout); | 
					
						
							|  |  |  |         if (stderr) gutil.log('stderr: ' + stderr); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (err && options.shouldThrow) throw err; | 
					
						
							|  |  |  |       cb(err, stdout, stderr); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-18 19:51:10 -07:00
										 |  |  | gulp.task('default', ['help']); |