/*! resol-vbus | Copyright (c) 2013-2014, Daniel Wippermann | MIT license */
'use strict';



var exec = require('child_process').exec;


require('better-stack-traces').install();
var chai = require('chai');
var gulp = require('gulp');
var plugins = require('gulp-load-plugins')();
var Q = require('q');
global.sinon = require('sinon');



chai.Assertion.includeStack = true;

global.expect = chai.expect;



global.promiseIt = function(message, callback) {
    it(message, function(done) {
        var _this = this;

        Q.fcall(function() {
            return callback.call(_this);
        }).then(function() {
            done();
        }, function(err) {
            done(err);
        }).done();
    });
};

global.xpromiseIt = function(message, callback) {
    xit(message, function() {
        // x-ed test
    });
};



var patterns = {
    src: [
        'src/**/*.js',
    ],
    test: [
        'test/specs/**/*.spec.js',
    ],
    all: [
        'src/**/*.js',
        '!src/specification-data.js',
        'test/specs/**/*.js'
    ],
    jsdoc: [
        'src/**/*.js',
        '!src/specification-data.js',
        'README.md',
    ],
    coverage: [
        'src/**/*.js',
        '!src/specification-data.js',
    ],
    docs: [
        'docs/**/*',
        '!docs/_site/**/*',
    ],
};



gulp.task('default', function() {
    return gulp.src(patterns.all)
        .pipe(plugins.jshint('.jshintrc'))
        .pipe(plugins.jshint.reporter('default'))
        .pipe(plugins.jscs())
        .on('end', function() {
            return gulp.src(patterns.test)
                .pipe(plugins.mocha({
                    ui: 'bdd',
                    reporter: 'spec',
                }));
        });
});


gulp.task('jsdoc', function() {
    return gulp.src(patterns.jsdoc)
        .pipe(plugins.jsdoc('./.docs/jsdoc', {
            'path': 'ink-docstrap',
            'cleverLinks': false,
            'monospaceLinks': false,
            'default': {
                'outputSourceFiles': true
            },
            'systemName': 'resol-vbus',
            'footer': '',
            'copyright': 'resol-vbus | Copyright (c) 2013-2014, Daniel Wippermann',
            'navType': 'inline',
            'theme': 'spacelab',
            'linenums': false,
            'collapseSymbols': true,
            'inverseNav': true
        }));
});


gulp.task('jekyll', function(done) {
    exec('jekyll build', { cwd: 'docs' }, function(err) {
        if (err) {
            done(err);
        } else {
            gulp.src('docs/_site/**/*', { base: 'docs/_site' })
                .pipe(gulp.dest('.docs/'))
                .on('error', done)
                .on('end', done);
        }
    });
});


gulp.task('publish', [ 'jekyll', 'jsdoc' ], function() {
    return gulp.src('.docs/**/*', { base: './.docs' })
        .pipe(gulp.dest('../danielwippermann.github.io/resol-vbus'));
});


gulp.task('coverage', function(done) {
    return gulp.src(patterns.coverage)
        .pipe(plugins.istanbul())
        .on('error', done)
        .on('end', function() {
            return gulp.src(patterns.test)
                .pipe(plugins.mocha({
                    ui: 'bdd',
                }))
                .pipe(plugins.istanbul.writeReports())
                .on('error', done)
                .on('end', done);
        });
});


gulp.task('coveralls', function() {
    return gulp.src('coverage/lcov.info')
        .pipe(plugins.coveralls());
});


gulp.task('watch', function() {
    gulp.watch(patterns.all, [ 'default' ]);
});

@@ -1317,22 +1317,21 @@
 p.task('
-defaul
+jshin
 t', func
@@ -1465,16 +1465,88 @@
 fault'))
+;%0A%7D);%0A%0A%0Agulp.task('jscs', function() %7B%0A    return gulp.src(patterns.all)
 %0A       
@@ -1567,33 +1567,40 @@
 .jscs())
-%0A        .on('end
+;%0A%7D);%0A%0A%0Agulp.task('mocha
 ', funct
@@ -1603,32 +1603,24 @@
 unction() %7B%0A
-        
     return g
@@ -1638,32 +1638,24 @@
 terns.test)%0A
-        
         .pip
@@ -1680,32 +1680,24 @@
             
-  
-      
 ui: 'bdd',%0A 
@@ -1707,24 +1707,16 @@
         
-     
-   
 reporter
@@ -1718,20 +1718,19 @@
 orter: '
-spec
+dot
 ',%0A     
@@ -1736,30 +1736,10 @@
     
-        %7D));%0A        
 %7D
+)
 );%0A%7D
@@ -2942,16 +2942,24 @@
 coverage
+-prepare
 ', funct
@@ -2954,36 +2954,32 @@
 pare', function(
-done
 ) %7B%0A    return g
@@ -3042,76 +3042,79 @@
 l())
-%0A        .on('error', done)%0A        .on('end', function() %7B%0A        
+;%0A%7D);%0A%0A%0Agulp.task('coverage', %5B 'coverage-prepare' %5D, function(done) %7B%0A
     
@@ -3148,32 +3148,24 @@
 st)%0A        
-   
-     
 .pipe(plugin
@@ -3182,32 +3182,24 @@
             
-       
- 
 ui: 'bdd',%0A 
@@ -3209,32 +3209,20 @@
     
-        %7D))%0A    
+%7D))%0A
         
     
@@ -3213,28 +3213,24 @@
 %7D))%0A        
-    
 .pipe(plugin
@@ -3259,88 +3259,8 @@
 s())
-%0A                .on('error', done)%0A                .on('end', done);%0A        %7D)
 ;%0A%7D)
@@ -3460,12 +3460,116 @@
 lt' %5D);%0A
-
 %7D);%0A
+%0A%0Agulp.task('default', %5B 'jshint', 'jscs', 'mocha' %5D);%0A%0A%0Agulp.task('nodemon', %5B 'coverage', 'jsdoc' %5D);%0A

Fix some async gulp task bugs.