var gulp = require('gulp');
var bower = require('bower');
var sh = require('shelljs');
var args = require('yargs').argv;
var config = require('./gulp.config')();
var merge = require('merge-stream');
var plugins = require('gulp-load-plugins')({lazy: true});

/**
 * Compiles, autoprefixes, minifies and concats all styles.
 * Produces a css for each origin scss and a minified and a concatenated styles.min.css for production.
 * @return {Stream}
 */
gulp.task('sass', function () {
    plugins.util.log(plugins.util.colors.blue('Compiling SASS'));
    return gulp.src(config.origin.mainSass)
        .pipe(plugins.plumber())
        .pipe(plugins.sass())
        .pipe(plugins.autoprefixer({browsers: ['last 2 version', 'safari 5', 'ios 6', 'android 4']}))
        .pipe(gulp.dest(config.dest.css))
        .pipe(plugins.cleanCss())
        .pipe(plugins.concat('styles.min.css'))
        .pipe(gulp.dest(config.dest.css));
});

gulp.task('watch', function () {
    gulp.watch(config.origin.allSass, ['sass']);
});

/**
 * Lints and anaylzes javascript code style.
 * @return {Stream}
 */
gulp.task('analyze', function () {
    var jshintAnalysis = analyzeJshint();
    var jscsAnalysis = analyzeJscs();

    return merge(jshintAnalysis, jscsAnalysis);
});

function analyzeJshint() {
    plugins.util.log(plugins.util.colors.blue('Analyzing - JSHint'));
    return gulp.src(config.origin.alljs)
        .pipe(plugins.if(args.verbose, plugins.print()))
        .pipe(plugins.jshint())
        .pipe(plugins.jshint.reporter('jshint-stylish', {verbose: true}))
        .pipe(plugins.jshint.reporter('fail'));
}

function analyzeJscs() {
    plugins.util.log(plugins.util.colors.blue('Analyzing - JSCS'));
    return gulp.src(config.origin.alljs)
        .pipe(plugins.jscs())
        .pipe(plugins.jscsStylish());
}

gulp.task('install', ['git-check'], function () {
    return bower.commands.install()
        .on('log', function (data) {
            plugins.util.log('bower', plugins.util.colors.cyan(data.id), data.message);
        });
});

gulp.task('git-check', function (done) {
    if (!sh.which('git')) {
        console.log(
            '  ' + plugins.util.colors.red('Git is not installed.'),
            '\n  Git, the version control system, is required to download Ionic.',
            '\n  Download git here:', plugins.util.colors.cyan('http://git-scm.com/downloads') + '.',
            '\n  Once git is installed, run \'' + plugins.util.colors.cyan('gulp install') + '\' again.'
        );
        process.exit(1);
    }
    done();
});

@@ -914,32 +914,33 @@
 est.css));%0A%7D);%0A%0A
+%0A
 gulp.task('watch
@@ -1114,24 +1114,101 @@
 nction () %7B%0A
+    plugins.util.log(plugins.util.colors.blue('Analyzing JSHint and JSCS'));%0A
     var jshi
@@ -1328,17 +1328,16 @@
 s);%0A%7D);%0A
-%0A
 function
@@ -1359,78 +1359,8 @@
 ) %7B%0A
-    plugins.util.log(plugins.util.colors.blue('Analyzing - JSHint'));%0A
     
@@ -1610,16 +1610,16 @@
 '));%0A%7D%0A%0A
+
 function
@@ -1639,76 +1639,8 @@
 ) %7B%0A
-    plugins.util.log(plugins.util.colors.blue('Analyzing - JSCS'));%0A
     

Fix code style revision.