3 var gulp
= require('gulp');
4 var runSequence
= require('run-sequence');
5 var gutil
= require('gulp-util');
7 var argv
= require('yargs')
8 .option('skip-stage', {
10 describe
: 'skip a stage'
14 function shouldSkip(stageName
) {
15 return argv
['skip-stage'] && argv
['skip-stage'].indexOf(stageName
) >= 0;
18 function findStageTasks(config
, stageName
) {
19 var items
= Object
.keys(config
)
21 return name
+ ':' + stageName
;
23 .filter(function(fullName
) {
24 var hasTask
= !!gulp
.tasks
[fullName
];
31 var stageMetaData
= {};
33 function configureTasks(config
) {
34 function createStageTask(stageName
, previousStages
) {
35 if (shouldSkip(stageName
)) return;
37 if (!previousStages
) previousStages
= [];
39 previousStages
= previousStages
.filter(function(name
) {
40 return !shouldSkip(name
);
43 var preTaskName
= 'pre-' + stageName
;
44 var postTaskName
= 'post-' + stageName
;
46 var preSteps
= findStageTasks(config
, preTaskName
);
47 var steps
= findStageTasks(config
, stageName
);
48 var postSteps
= findStageTasks(config
, postTaskName
);
50 if (preSteps
.length
) {
51 gulp
.task(preTaskName
, preSteps
, function() {
52 gutil
.log(preTaskName
+ ' complete');
56 if (postSteps
.length
) {
57 gulp
.task(postTaskName
, postSteps
, function() {
58 gutil
.log(postTaskName
+ ' complete');
66 stageMetaData
[stageName
] = metaData
;
67 if (preSteps
.length
) {
68 metaData
.pre
= preSteps
;
72 metaData
.main
= steps
;
75 if (postSteps
.length
) {
76 metaData
.post
= postSteps
;
79 gulp
.task(stageName
, previousStages
, function(callback
) {
81 if (preSteps
.length
) {
82 seq
.push(preTaskName
);
89 if (postSteps
.length
) {
90 seq
.push(postTaskName
);
93 if (seq
.length
=== 0) {
97 gutil
.log('Starting ' + stageName
+ ' stage', seq
);
99 seq
.push(function(err
) {
101 gutil
.log('Stage ' + stageName
+ ' failed');
103 gutil
.log('Stage ' + stageName
+ ' complete');
109 runSequence
.apply(null, seq
);
113 gulp
.task('display-stages', function() {
114 gutil
.log('-------------------');
116 gutil
.log('-------------------');
117 Object
.keys(stageMetaData
).forEach(function(stageName
) {
118 var meta
= stageMetaData
[stageName
];
119 gutil
.log('+', stageName
);
120 if (meta
.dep
&& meta
.dep
.length
) {
121 gutil
.log(' Dependencies:');
122 meta
.dep
.forEach(function(m
) {
126 if (meta
.pre
&& meta
.pre
.length
) {
127 gutil
.log(' Pre-steps:');
128 meta
.pre
.forEach(function(m
) {
133 if (meta
.main
&& meta
.main
.length
) {
135 meta
.main
.forEach(function(m
) {
140 if (meta
.post
&& meta
.post
.length
) {
141 gutil
.log(' Post-steps:');
142 meta
.post
.forEach(function(m
) {
150 * ------------------------------------------------
152 * ------------------------------------------------
156 * Validation stage: is everything as we expect it to be
157 * before kicking off the build
159 createStageTask('validate');
162 * Test stage: does the code do what it should?
164 createStageTask('test');
167 * Compile stage: process the code into a new form
169 createStageTask('compile', ['test', 'validate']);
172 * Assemble stage: gather all the files
174 createStageTask('assemble', ['compile']);
177 * Package stage: create packaged artifacts
179 createStageTask('package', ['assemble']);
182 * Clean stage: clean the environment
184 createStageTask('clean');
187 * Watch stage: watch the development environment and
190 createStageTask('watch');
192 gulp
.task('default', ['package']);
195 module
.exports
= configureTasks
;