Major update to code structure

   * Split files into independent files based on functionality.
   * Switch to bower/gulp for build.

Change-Id: Ibc775dd9b7f6a0a49f63c22162b7582e781e2d9c
Signed-off-by: Iftekharul Islam <iislam@us.ibm.com>
diff --git a/gulp_tasks/checkstyle.js b/gulp_tasks/checkstyle.js
new file mode 100644
index 0000000..bd76a8b
--- /dev/null
+++ b/gulp_tasks/checkstyle.js
@@ -0,0 +1,31 @@
+/*eslint-env node */
+/*global require: true, module: true */
+
+'use strict';
+
+var options = require('../gulp-options.js'),
+    gulp = require('gulp'),
+    clean = require('gulp-clean'),
+    eslint = require('gulp-eslint');
+
+var runSequence = require('run-sequence'),
+    fs = require('fs');
+
+gulp.task('checkstyle:clean', function () {
+    return gulp
+        .src([options.targetFolderPath + '/eslint-report-checkstyle.xml'], {'read': false})
+        .pipe(clean({'force': true}));
+});
+
+gulp.task('checkstyle:eslint', function () {
+    return gulp
+        .src([options.srcFolderPath + '/**/*.js', options.excludePath])
+        .pipe(eslint({'useEslintrc': true}))
+        .pipe(eslint.format('checkstyle', function (output) {
+            fs.writeFileSync(options.targetFolderPath + '/eslint-report-checkstyle.xml', output);
+        }));
+});
+
+module.exports = function (callback) {
+    return runSequence('checkstyle:clean', 'checkstyle:eslint', callback);
+};
diff --git a/gulp_tasks/distribution.js b/gulp_tasks/distribution.js
new file mode 100644
index 0000000..ec5aac0
--- /dev/null
+++ b/gulp_tasks/distribution.js
@@ -0,0 +1,45 @@
+/*eslint-env node */
+/*global require: true, module: true */
+
+'use strict';
+
+var options = require('../gulp-options.js'),
+    gulp = require('gulp'),
+    clean = require('gulp-clean'),
+    webapp = require('./webapp.js'),
+    imagemin = require('gulp-imagemin');
+
+var runSequence = require('run-sequence');
+
+gulp.task('webapp', function (callback) {
+    return webapp(callback);
+});
+
+gulp.task('distribution:clean', function () {
+    return gulp
+        .src([options.dirname + '/dist'], { 'read': false })
+        .pipe(clean({'force': true}));
+});
+
+gulp.task('distribution:copy', function () {
+    return gulp
+        .src(['**/*'], { 'cwd': options.targetFolderPath + '/webapp' })
+        .pipe(gulp.dest(options.dirname + '/dist'));
+});
+
+gulp.task('imagemin', () =>
+    gulp.src([options.dirname + '/app/assets/images/*'])
+        .pipe(imagemin({
+            optimizationLevel: 3,
+            progressive: true,
+            interlaced: false,
+            svgoPlugins: [{
+                removeViewBox: false
+            }]
+        }))
+        .pipe(gulp.dest('dist/assets/images'))
+);
+
+module.exports = function (callback) {
+    return runSequence('distribution:clean', 'webapp', 'distribution:copy', 'imagemin', callback);
+};
diff --git a/gulp_tasks/server.js b/gulp_tasks/server.js
new file mode 100644
index 0000000..b965786
--- /dev/null
+++ b/gulp_tasks/server.js
@@ -0,0 +1,32 @@
+var options = require('../gulp-options.js'),
+    gulp = require('gulp'),
+    connect = require('gulp-connect'),
+    distribution = require('./distribution.js');
+
+var runSequence = require('run-sequence');
+
+gulp.task('distribution', function (callback) {
+    return distribution(callback);
+});
+
+gulp.task('connect', function() {
+  connect.server({
+    root: 'dist',
+    livereload: true
+  });
+});
+
+gulp.task('livereload', function() {
+  gulp.src(['./dist/**/*.html','./dist/**/*.js','./dist/**/*.css'])
+    .pipe(connect.reload());
+});
+
+gulp.task('watch', function () {
+  gulp.watch('./app/**/*', function(callback){
+    return runSequence('distribution', 'livereload');
+  });
+});
+
+module.exports = function (callback) {
+    return runSequence('connect', 'watch', callback);
+};
\ No newline at end of file
diff --git a/gulp_tasks/webapp.js b/gulp_tasks/webapp.js
new file mode 100644
index 0000000..e514543
--- /dev/null
+++ b/gulp_tasks/webapp.js
@@ -0,0 +1,131 @@
+/*eslint-env node */
+/*global require: true, module: true */
+
+'use strict';
+
+var options = require('../gulp-options.js'),
+    gulp = require('gulp'),
+
+    // Base dependencies
+    clean = require('gulp-clean'),
+    rename = require('gulp-rename'),
+    util = require('gulp-util'),
+
+    // Angular gulp dependencies
+    ngTemplateCache = require('gulp-angular-templatecache'),
+    ngAnnotate = require('gulp-ng-annotate'),
+    ngConstant = require('gulp-ng-constant'),
+
+    // Classical gulp dependencies
+    stripDebug = require('gulp-strip-debug'),
+    uglify = require('gulp-uglify'),
+    sass = require('gulp-sass'),
+    cleanCss = require('gulp-clean-css'),
+    rev = require('gulp-rev'),
+    revReplace = require('gulp-rev-replace'),
+    gulpIf = require('gulp-if'),
+    useref = require('gulp-useref'),
+    jsoncombine = require('gulp-jsoncombine'),
+    htmlParser = require('gulp-htmlparser');
+
+
+var runSequence = require('run-sequence'),
+    es = require('event-stream');
+
+gulp.task('webapp:clean', function () {
+    return gulp
+        .src([options.targetFolderPath + '/webapp', options.dirname + '/.temp'], { 'read': false })
+        .pipe(clean({'force': true}));
+});
+
+gulp.task('webapp:sasscompile', function () {
+    return gulp
+        .src('app/styles/index.scss')
+        .pipe(sass.sync().on('error', util.log))
+        .pipe(gulp.dest(options.srcFolderPath + '/styles'))
+});
+
+// ----- To .temp from app
+gulp.task('webapp:copyjs', function () {
+    return gulp.src(options.srcFolderPath + '/**/*.js')
+        .pipe(ngAnnotate()) // Check angular dependencies injection
+        .pipe(stripDebug()) // Remove all logs
+        .pipe(uglify({ 'mangle': false }))
+        .pipe(gulp.dest(options.dirname + '/.temp'));
+});
+
+gulp.task('webapp:copyothers', function () {
+    return gulp.src(['**/*', '!**/*.js', '!**/*.css', '!**/*.scss'], { 'cwd': options.srcFolderPath }) // All except JS files
+        .pipe(gulp.dest(options.tempFolderPath));
+});
+
+gulp.task('webapp:copycss', function () {
+    return gulp
+        .src('app/styles/index.css')
+        .pipe(cleanCss())
+        .pipe(gulp.dest(options.tempFolderPath + '/styles'));
+});
+
+gulp.task('webapp:constants', function () {
+    return gulp
+        .src('environment-constants.json')
+        .pipe(ngConstant({
+            'name': 'app.constants',
+            'deps': false
+        }))
+        .pipe(rename('environment-constants.js'))
+        .pipe(gulp.dest(options.tempFolderPath + '/constants'));
+});
+
+// ----- To target/webapp from .temp and bower_components
+gulp.task('webapp:template', function () {
+    return gulp.src([options.srcFolderPath + '/**/*.html', '!' + options.srcFolderPath + '/index.html'])
+        .pipe(ngTemplateCache('templates.js', {
+            'module': 'app.templates',
+            'standalone': true
+        }))
+        .pipe(gulp.dest(options.tempFolderPath));
+});
+
+gulp.task('webapp:useref', function () {
+    var tasks = ['index.html'].map(function (indexPage) {
+        var assets = useref.assets({ });
+
+        return gulp.src(options.tempFolderPath + '/' + indexPage)
+            .pipe(assets)
+            .pipe(assets.restore())
+            .pipe(useref())
+            .pipe(revReplace()) // Force useref to apply the 'rev' method
+            .pipe(gulp.dest(options.targetFolderPath + '/webapp'));
+    });
+
+    return es.concat.apply(null, tasks);
+});
+
+gulp.task('webapp:copyresources', function () {
+    return gulp.src(['**/*.*', '!**/*.js', '!**/*.css', '!**/*.html', '!**/*.log'], { 'cwd': options.tempFolderPath })
+        .pipe(gulp.dest(options.targetFolderPath + '/webapp'));
+});
+
+module.exports = function (callback) {
+    return runSequence(
+        'webapp:clean',
+        'webapp:sasscompile',
+        [
+            'webapp:copyjs',
+            'webapp:copycss',
+            'webapp:copyothers'
+        ],
+        [
+            'webapp:constants',
+            'webapp:template'
+        ],
+        [
+            'webapp:useref'
+        ],
+        [
+            'webapp:copyresources'
+        ],
+        callback
+    );
+};