Skip to content

Instantly share code, notes, and snippets.

@mrsheepuk
Last active November 8, 2015 12:32
Show Gist options
  • Save mrsheepuk/76c7ba50db6970ecb7bc to your computer and use it in GitHub Desktop.
Save mrsheepuk/76c7ba50db6970ecb7bc to your computer and use it in GitHub Desktop.
All-singing all-dancing gulpfile for all sorts of boilerplatey things
/// <binding BeforeBuild='build' AfterBuild='test' Clean='clean' />
var gulp = require("gulp"),
rimraf = require("rimraf"),
concat = require("gulp-concat"),
cssmin = require("gulp-minify-css"),
uglify = require("gulp-uglify"),
inject = require("gulp-inject"),
angularFilesort = require("gulp-angular-filesort"),
mainBowerFiles = require("main-bower-files"),
flatten = require("gulp-flatten"),
sourcemaps = require('gulp-sourcemaps'),
KarmaServer = require('karma').Server,
jasmine = require('gulp-jasmine'),
reporters = require('jasmine-reporters'),
templateCache = require('gulp-angular-templatecache'),
rename = require('gulp-rename'),
project = require("./project.json"),
pkgInfo = require('./package.json');
var paths = {
publicRoot: "./" + project.webroot + "/",
publicRootAbs: "/" + project.webroot + "/",
testResults: "../../../../Build/Results/Tests/" + pkgInfo.name + "/Client-Tests.junit.xml"
};
var angularApp = pkgInfo.config.angularApp;
// Include all JS in the src folder, aside from .spec.js files.
paths.jsSrcRoot = paths.publicRoot + "js/src/";
paths.js = [paths.jsSrcRoot + "**/*.js", "!" + paths.jsSrcRoot + "**/*.spec.js"];
paths.jsWithSpecs = [paths.jsSrcRoot + "**/*.js"];
paths.jsTemplates = [paths.jsSrcRoot + "**/*.html"];
paths.sharedJs = "../SharedJS/js/**/*.*";
paths.sharedCss = "../SharedJS/css/**/*.*";
// Shared then local
paths.css =[paths.publicRoot + "css/shared/**/*.css", paths.publicRoot + "css/src/**/*.css"];
paths.sharedJsDest = paths.jsSrcRoot + "shared/";
paths.sharedCssDest = paths.publicRoot + "css/shared/";
paths.concatJsDestNonMin = paths.publicRoot + "js/" + pkgInfo.name + "-" + pkgInfo.version + ".src.js";
paths.concatJsDest = paths.publicRoot + "js/" + pkgInfo.name + "-" + pkgInfo.version + ".min.js";
paths.concatCssDest = paths.publicRoot + "css/" + pkgInfo.name + "-" + pkgInfo.version + ".min.css";
paths.concatLibJsDest = paths.publicRoot + "js/" + pkgInfo.name + "-libs-" + pkgInfo.version + ".min.js";
paths.concatLibCssDest = paths.publicRoot + "css/" + pkgInfo.name + "-libs-" + pkgInfo.version + ".min.css";
paths.jsTemplatesDest = paths.jsSrcRoot;
// Inject the scripts into any view with the appropriate comments in them.
paths.htmlInject =["./Views/**/*.cshtml"];
gulp.task("clean:js", function (cb) {
return rimraf(paths.concatJsDest, cb);
});
gulp.task("clean:sharedJs", function (cb) {
return rimraf(paths.sharedJsDest, cb);
});
gulp.task("clean:css", function (cb) {
return rimraf(paths.concatCssDest, cb);
});
gulp.task("clean:sharedCss", function (cb) {
return rimraf(paths.sharedJsDest, cb);
});
gulp.task("clean:libJs", function (cb) {
return rimraf(paths.concatLibJsDest, cb);
});
gulp.task("clean:libCss", function (cb) {
return rimraf(paths.concatLibCssDest, cb);
});
gulp.task("clean:tests", function (cb) {
return rimraf(paths.testResults, cb);
});
gulp.task("clean", ["clean:js", "clean:sharedJs", "clean:css", "clean:sharedCss", "clean:libJs", "clean:libCss", "clean:tests"]);
gulp.task("build:templates", function () {
return gulp.src(paths.jsTemplates)
.pipe(templateCache({ module: angularApp }))
.pipe(gulp.dest(paths.jsTemplatesDest));
});
gulp.task("build:sharedJs", function () {
return gulp.src(paths.sharedJs)
.pipe(gulp.dest(paths.sharedJsDest));
});
gulp.task("build:sharedCss", function () {
return gulp.src(paths.sharedCss)
.pipe(gulp.dest(paths.sharedCssDest));
});
gulp.task("min:js", ["build:templates", "build:sharedJs"], function () {
return gulp.src(paths.js, { base: paths.publicRoot })
.pipe(angularFilesort())
.pipe(sourcemaps.init())
.pipe(concat(paths.concatJsDestNonMin))
.pipe(uglify())
.pipe(rename(paths.concatJsDest))
.pipe(sourcemaps.write("."))
.pipe(gulp.dest("."));
});
gulp.task("min:css", ["build:sharedCss"], function () {
return gulp.src(paths.css, { base: "."
})
.pipe(cssmin({relativeTo: paths.publicRoot + "css/", target: paths.publicRoot + "css/" }))
.pipe(concat(paths.concatCssDest))
.pipe(gulp.dest("."));
});
gulp.task("min:libJs", function () {
return gulp.src(mainBowerFiles("**/*.js"), { base: "."
})
.pipe(concat(paths.concatLibJsDest))
.pipe(uglify())
.pipe(gulp.dest("."));
});
gulp.task("min:libCss", function () {
return gulp.src(mainBowerFiles("**/*.css"), { base: "."
})
.pipe(cssmin({ relativeTo: paths.publicRoot + "css/", target : paths.publicRoot + "css/" }))
.pipe(concat(paths.concatLibCssDest))
.pipe(gulp.dest("."));
});
gulp.task("min", ["min:js", "min:css", "min:libJs", "min:libCss"]);
gulp.task("inject", ["min"], function () {
// Dev: JS libs then CSS libs then our CSS.
var sources = gulp.src(mainBowerFiles("**/*.js").concat(mainBowerFiles("**/*.css")).concat(paths.css), { base: ".", read: false
});
// Dev: Our JS files.
var appSources = gulp.src(paths.js, { base: "." }).pipe(angularFilesort());
// Prod: All minified JS (libs and ours), and CSS.
//;
var minLibSources = gulp.src([paths.concatLibCssDest, paths.concatCssDest, paths.concatLibJsDest], { base: ".", read: false
});
var minAppSources = gulp.src([paths.concatJsDest], { base: ".", read: false
});
// Substitute into the relevant view files.
return gulp.src(paths.htmlInject, { base: "."
})
.pipe(inject(sources, { ignorePath: paths.publicRootAbs }))
.pipe(inject(appSources, { name: "app", ignorePath: paths.publicRootAbs }))
.pipe(inject(minLibSources, { name: "min", ignorePath: paths.publicRootAbs }))
.pipe(inject(minAppSources, { name: "minapp", ignorePath: paths.publicRootAbs }))
.pipe(gulp.dest("."));
});
gulp.task("build", ["build:templates", "build:sharedJs", "build:sharedCss", "min", "inject"]);
gulp.task('test:client', ['build:sharedJs'], function (done) {
new KarmaServer({
files: mainBowerFiles("**/*.js").concat([paths.sharedJsDest + '**/*.module.js', paths.jsSrcRoot + '*.module.js', paths.jsSrcRoot + '**/*.js']),
configFile: __dirname + "/karma.clientjs.conf.js",
junitReporter: {
outputFile: paths.testResults,
},
singleRun: true
}, done).start();
});
gulp.task("test", ["test:client"]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment