diff --git a/.gitignore b/.gitignore index 0f52f66..c059326 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ thumbs.db # Blue Jay Files env.json *.exe +*.exe~ blueprint \ No newline at end of file diff --git a/gulpfile.js b/gulpfile.js index 4a30d5b..5ca4aa3 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -1,17 +1,26 @@ // Modules -var gulp = require('gulp'); -var favicon = require ('gulp-real-favicon'); -var fs = require('fs'); -var runSequence = require('run-sequence'); // Using until gulp v4 is released +var gulp = require('gulp'); +var favicon = require('gulp-real-favicon'); +var fs = require('fs'); +var runSequence = require('run-sequence'); // Using until gulp v4 is released +var reload = require('gulp-livereload'); +var sync = require('gulp-sync')(gulp).sync; +var child = require('child_process'); +var util = require('gulp-util'); +var path = require('path'); +var os = require('os'); // Enviroment variables var env = JSON.parse(fs.readFileSync('./env.json')) var folderAsset = env.Asset.Folder; -var folderView = env.View.Folder; +var folderView = env.View.Folder; // Other variables var faviconData = folderAsset + '/dynamic/favicon/data.json'; +// Application server +var server = null; + // SASS Task gulp.task('sass', function() { var sass = require('gulp-sass'); @@ -24,7 +33,8 @@ gulp.task('sass', function() { // Available for outputStyle: expanded, nested, compact, compressed .pipe(sass({outputStyle: 'compressed'}).on('error', sass.logError)) .pipe(ext('.min.css')) - .pipe(gulp.dest(folderAsset + '/static/css/')); + .pipe(gulp.dest(folderAsset + '/static/css/')) + .pipe(reload()); }); // JavaScript Task @@ -39,7 +49,8 @@ gulp.task('javascript', function() { min:'.min.js' } })) - .pipe(gulp.dest(folderAsset + '/static/js/')); + .pipe(gulp.dest(folderAsset + '/static/js/')) + .pipe(reload()); }); // jQuery Task @@ -145,14 +156,95 @@ gulp.task('favicon-update', function(done) { }); }); -// Watch -gulp.task('watch', function() { +// Monitor Go files for changes +gulp.task('server:watch', function() { + // Restart application + gulp.watch([ + '*/**/*.tmpl', + 'env.json' + ], ['server:spawn']); + + // Rebuild and restart application server + gulp.watch([ + '*.go', + '*/**/*.go' + ], [ + 'server:build', + 'server:spawn' + ]); +}); + +// Build application from source +gulp.task('server:build', function() { + var build = child.spawnSync('go', ['build']); + if (build.stderr.length) { + var lines = build.stderr.toString() + .split('\n').filter(function(line) { + return line.length + }); + for (var l in lines) + util.log(util.colors.red( + 'Error (go build): ' + lines[l] + )); + notifier.notify({ + title: 'Error (go build)', + message: lines + }); + } + return build; +}); + +// Spawn an application process +gulp.task('server:spawn', function() { + if (server) + server.kill(); + + // Get the application name based on the folder + var appname = path.basename(__dirname); + + // Spawn application server + if (os.platform() == 'win32') { + server = child.spawn(appname + '.exe'); + } else { + server = child.spawn('./' + appname); + } + + // Trigger reload upon server start + server.stdout.once('data', function() { + reload.reload('/'); + }); + + // Pretty print server log output + server.stdout.on('data', function(data) { + var lines = data.toString().split('\n') + for (var l in lines) + if (lines[l].length) + util.log(lines[l]); + }); + + // Print errors to stdout + server.stderr.on('data', function(data) { + process.stdout.write(data.toString()); + }); +}); + +// Main watch function. +gulp.task('watch', ['server:build'], function() { + // Start the listener (use with the LiveReload Chrome Extension) + reload.listen(); + + // Watch the assets gulp.watch(folderAsset + '/dynamic/sass/**/*.scss', ['sass']); gulp.watch(folderAsset + '/dynamic/js/*.js', ['javascript']); + + return gulp.start(sync([ + 'server:watch', + 'server:spawn' + ])); }); // Init - every task -gulp.task('init', ['sass', 'javascript', 'jquery', 'bootstrap', 'underscore', 'favicon']); +gulp.task('init', ['sass', 'javascript', 'jquery', 'bootstrap', 'underscore', 'favicon', 'server:build']); // Default - only run the tasks that change often -gulp.task('default', ['sass', 'javascript']); \ No newline at end of file +gulp.task('default', ['sass', 'javascript', 'server:build']); diff --git a/package.json b/package.json index 98c30d5..87c86b3 100644 --- a/package.json +++ b/package.json @@ -2,13 +2,19 @@ "private": true, "devDependencies": { "bootstrap": "^3.3.6", + "child_process": "^1.0.2", "gulp": "^3.9.1", "gulp-concat": "^2.6.0", "gulp-ext-replace": "^0.3.0", + "gulp-livereload": "^3.8.1", "gulp-minify": "0.0.13", "gulp-real-favicon": "^0.2.1", "gulp-sass": "^2.3.2", + "gulp-sync": "^0.1.4", + "gulp-util": "^3.0.8", "jquery": "1.9.1 - 2", + "os": "^0.1.1", + "path": "^0.12.7", "run-sequence": "^1.2.2", "underscore": "^1.8.3" }