Skip to content

Instantly share code, notes, and snippets.

@isimmons
Last active August 29, 2015 13:55
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save isimmons/8701121 to your computer and use it in GitHub Desktop.
Save isimmons/8701121 to your computer and use it in GitHub Desktop.
gulpfile for gulp.js + Laravel: Compile sass/coffee, run phpunit tests, livereload css, js, and blade templates
/*
See dev dependencies https://gist.github.com/isimmons/8927890
Compiles sass to compressed css with autoprefixing
Compiles coffee to javascript
Livereloads on changes to coffee, sass, and blade templates
Runs PHPUnit tests
Watches sass, coffee, blade, and phpunit
Default tasks sass, coffee, phpunit, watch
*/
var gulp = require('gulp');
var gutil = require('gulp-util');
var notify = require('gulp-notify');
var sass = require('gulp-ruby-sass');
var autoprefix = require('gulp-autoprefixer');
var coffee = require('gulp-coffee');
var phpunit = require('gulp-phpunit');//notify requires >= v 0.0.3
var fs = require('fs'); //only used for icon file with growlNotifier
// livereload
var livereload = require('gulp-livereload');
var lr = require('tiny-lr');
var server = lr();
//uncomment for growl notify for windows users
//Specify custom icon by passing object to growl() { icon: fs.readFileSync('path_to_icon_file') }
//var growl = ('gulp-notify-growl');
//var growlNotifier = growl();
//CSS directories
var sassDir = 'app/assets/sass';
var targetCSSDir = 'public/css';
//javascript directories
var coffeeDir = 'app/assets/coffee';
var targetJSDir = 'public/js';
// blade directory
var bladeDir = 'app/views';
// Tasks
/* sass compile */
gulp.task('sass', function() {
return gulp.src(sassDir + '/main.scss')
.pipe(sass({ style: 'compressed'}).on('error', gutil.log))
.pipe(autoprefix('last 10 versions'))
.pipe(gulp.dest(targetCSSDir))
.pipe(livereload(server))
.pipe(notify('CSS compiled, prefixed, and minified.'));
//growlNotifier for windows
//.pipe(notify({title: 'CSS Compiled', message: 'compiled, prefixed, and minified.', notifier: growlNotifier}));
});
/* coffee compile */
gulp.task('coffee', function() {
return gulp.src(coffeeDir + '/**/*.coffee')
.pipe(coffee().on('error', gutil.log))
.pipe(gulp.dest(targetJSDir))
.pipe(livereload(server));
});
/* Blade Templates */
gulp.task('blade', function() {
return gulp.src(bladeDir + '/**/*.blade.php')
.pipe(livereload(server));
});
/* PHPUnit */
gulp.task('phpunit', function() {
//notify defaults to false. If you don't want to use a notifier or worry with errors in this task leave it off
var options = {debug: false, notify: true}
gulp.src('app/tests/*.php')
.pipe(phpunit('', options)) //empty phpunit path defaults ./vendor/bin/phpunit for windows specify with double back slashes
//both notify and notify.onError will take optional notifier: growlNotifier for windows notifications
//if options.notify is true be sure to handle the error here or suffer the consequenses!
.on('error', notify.onError({
title: 'PHPUnit Failed',
message: 'One or more tests failed, see the cli for details.'
}))
//will fire only if no error is caught
.pipe(notify({
title: 'PHPUnit Passed',
message: 'All tests passed!'
}));
});
/* Watcher */
gulp.task('watch', function() {
server.listen(35729, function(err) {
if(err) {console.log(err);}
gulp.watch(bladeDir + '/**/*.blade.php', ['blade']);
gulp.watch(sassDir + '/**/*.scss', ['sass']);
gulp.watch(coffeeDir + '/**/*.coffee', ['coffee']);
});
gulp.watch('app/**/*.php', ['phpunit']);
});
/* Default Task */
gulp.task('default', ['sass', 'coffee', 'phpunit', 'watch']);
@isimmons
Copy link
Author

isimmons commented Feb 4, 2014

ln place of the phpunit task there is now gulp-phpunit https://github.com/mikeerickson/gulp-phpunit

I'll make the changes here but also working out how to use this recipe https://github.com/gulpjs/gulp/blob/master/docs/recipes/pass-params-from-cli.md to pass in the --testsuite parameter when calling gulp or gulp phpunit. For this to work gulp-phpunit will need to accept the option.

@BnSmth
Copy link

BnSmth commented Feb 7, 2014

This has been incredibly useful, thanks. A gulp-phpunit update would be great.

@isimmons
Copy link
Author

@benbradley Mr Erickson has updated gulp-phpunit to version 0.0.3 . It is now working with gulp-notify.

Updated the gist removing unnecessary requires, getting rid of the comma separated vars, adding in comments for windows options, and the new phpunit task I'm using now.

It is working great on my Windows system. When you have time can you let me know if I need any Mac specific comments for anything that needs to be done differently?

Thanks

@isimmons
Copy link
Author

Apparently livereload starts it's own tiny-lr now so no need to create it and pass it in. Will update the file after testing that out.

@hao1987
Copy link

hao1987 commented Jan 28, 2015

question, im a newbie in gulp, what shall i put this file in laravel project, asset/ ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment