/* jshint node: true */
"use strict";

var gulp = require("gulp"),
	g = require("gulp-load-plugins")({lazy: false}),
	rimraf = require("rimraf"),
	pkg = require("./package.json"),
	exec = require("child_process").exec;


/**
 * Helpers
 */

function shell(cmd, done) {
	exec(cmd, function (err, stdout, stderr) {
    if(stdout) {
			console.log(stdout);
		}
    if(stderr) {
			console.log(stderr);
		}
		if(done) {
			done();
		}
  });
}

/**
 * Scripts
 */
gulp.task("clean", function (done) {
	rimraf("dist/**/*.js", done);
});

gulp.task("scripts", ["clean"], function () {
	return gulp.src([
		"index.js"])
		.pipe(g.umd({
			dependencies: function() {
				return [
					{
						name: "jquery-deferred",
						amd: "jquery-deferred",
						cjs: "jquery-deferred",
						global: "$",
						param: "$"
					},
					{
						name: "lodash",
						amd: "lodash",
						cjs: "lodash",
						global: "_",
						param: "_"
					},
					{
						name: "moment",
						amd: "moment",
						cjs: "moment",
						global: "moment",
						param: "moment"
					}
				];
			},
			exports: function() {
				return "m11n";
			},
			namespace: function() {
				return "m11n";
			}
		}))
		.pipe( gulp.dest("dist/") )
		.pipe(g.uglify())
		.pipe(g.rename({
			suffix: ".min"
		}))
		.pipe( gulp.dest("dist/") );
});

/**
 * Package
 */
gulp.task("package", ["scripts"], function () {
  return gulp.src(["dist/*.js", "package.json", "bower.json"])
    .pipe(g.tar(pkg.name + ".tar"))
    .pipe(g.gzip())
    .pipe(gulp.dest('dist'));
});

/**
 * Release
 */

function githubRelease() {
	return gulp.src("./dist/" + pkg.name + ".tar.gz")
  	.pipe(g.githubRelease({
			owner: "vidakovic",
			tag: pkg.version,
      repo: pkg.name,
			manifest: pkg
	 	}));
}

gulp.task("release-start", function (done) {
	shell(
		"git flow release start " + pkg.version + " && " +
		"git flow release publish " + pkg.version + " && " +
		"git flow release track " + pkg.version,
		done);
});

gulp.task("release-finish", function (done) {
	shell(
		"git flow release finish " + pkg.version + " && " +
		"git push --tags && " +
		"git checkout master && " +
		"git push && " +
		"git checkout develop && " +
		"git push",
		done);
});

gulp.task("release-major", ["release-start", "bump-major", "changelog", "release-finish", "package"], function() {
	return githubRelease();
});

gulp.task("release-minor", ["release-start", "bump-minor", "changelog", "release-finish", "package"], function() {
	return githubRelease();
});

gulp.task("release-patch", ["release-start", "bump-patch", "changelog", "release-finish", "package"], function() {
	return githubRelease();
});

gulp.task("changelog", function () {
  return gulp.src("CHANGELOG.md")
    .pipe(g.conventionalChangelog({
      preset: "angular",
			releaseCount: 0
    }))
    .pipe(gulp.dest("./"))
		.pipe(g.git.commit("release: Update changelog"));
});

gulp.task("bump-major", function(){
	return gulp.src("./*.json")
		.pipe(g.bump({type:"major"}))
		.pipe(gulp.dest("./"))
		.pipe(g.git.commit("release: Bump major version"));
});

gulp.task("bump-minor", function(){
	return gulp.src("./*.json")
		.pipe(g.bump({type:"minor"}))
		.pipe(gulp.dest("./"))
		.pipe(g.git.commit("release: Bump minor version"));
});

gulp.task("bump-patch", function(){
	return gulp.src("./*.json")
		.pipe(g.bump({type:"patch"}))
		.pipe(gulp.dest("./"))
		.pipe(g.git.commit("release: Bump patch version"));
});

gulp.task("build", ["package"]);

/**
 * Default task
 */
gulp.task("default", ["build"]);

@@ -1539,30 +1539,46 @@
 */%0A%0A
-function githubRelease
+gulp.task(%22release-publish%22, function 
 () %7B
@@ -1748,16 +1748,18 @@
  %09%7D));%0A%7D
+);
 %0A%0Agulp.t
@@ -2047,24 +2047,49 @@
 ease finish 
+-S -m %5C%22release: Finish%5C%22
 %22 + pkg.vers
@@ -2340,50 +2340,28 @@
 age%22
-%5D
 , 
-function() %7B%0A%09return githubRelease();%0A%7D
+%22release-publish%22%5D
 );%0A%0A
@@ -2463,50 +2463,28 @@
 age%22
-%5D
 , 
-function() %7B%0A%09return githubRelease();%0A%7D
+%22release-publish%22%5D
 );%0A%0A
@@ -2589,47 +2589,25 @@
 %22%5D, 
-function() %7B%0A%09return githubRelease();%0A%7D
+%22release-publish%22
 );%0A%0A

Fix release tasks