GUI: Added ESLint to gulp tasks. ONOS-6521
Commented out gulp tasks making this change uneffective
Fixed an error in the build script

Change-Id: I4f4f9762aa1a66304aa74b3ab208095b9c1d4515
diff --git a/tools/gui/gulp-tasks/bundles/bundle-js/index.js b/tools/gui/gulp-tasks/bundles/bundle-js/index.js
index 212034a..2292775 100644
--- a/tools/gui/gulp-tasks/bundles/bundle-js/index.js
+++ b/tools/gui/gulp-tasks/bundles/bundle-js/index.js
@@ -4,6 +4,7 @@
 import uglyfy from 'gulp-uglify';
 import sourceMaps from 'gulp-sourcemaps';
 import BundleResources from '../helpers/bundleResources';
+import { reload } from '../../dev-server';
 
 
 const GUI_BASE = '../../web/gui/src/main/webapp/';
@@ -34,6 +35,18 @@
 ];
 
 function bundle(files, exportName) {
+
+    // TODO: Use util.noop once npm issues have been resolved
+    if (process.argv.indexOf('--development') > -1) {
+        return gulp.src(BundleResources(GUI_BASE, files))
+            .pipe(sourceMaps.init())
+            .on('error', (e, file, line) => console.error(e))
+            .pipe(concat(exportName))
+            .pipe(sourceMaps.write('source-map'))
+            .pipe(gulp.dest(GUI_BASE + '/dist/'))
+            .on('end', () => { reload(); })
+    }
+
     return gulp.src(BundleResources(GUI_BASE, files))
         .pipe(sourceMaps.init())
         .pipe(strip())
@@ -45,8 +58,13 @@
 }
 
 const tasks = function () {
-    gulp.task('bundle-vendor', () => bundle(vendor, 'vendor.js'));
+    // gulp.task('bundle-vendor', () => bundle(vendor, 'vendor.js'));
     gulp.task('bundle-js', () => bundle(bundleFiles, 'onos.js'));
+    gulp.task('watch-js', () => {
+        gulp.watch([GUI_BASE + '**/*.js', `!${GUI_BASE}/dist/**/*`], ['bundle-js']);
+    }).on('change', (event) => {
+        console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
+    });
 };
 
 export default tasks();
\ No newline at end of file
diff --git a/tools/gui/gulp-tasks/dev-server/index.js b/tools/gui/gulp-tasks/dev-server/index.js
new file mode 100644
index 0000000..119c3f2
--- /dev/null
+++ b/tools/gui/gulp-tasks/dev-server/index.js
@@ -0,0 +1,73 @@
+import gulp from 'gulp';
+import browserSync from 'browser-sync';
+import fs from 'fs';
+import webserver from 'gulp-webserver';
+import proxy from 'http-proxy-middleware';
+
+let external_apps;
+let bs = null;
+
+const files = ['../../web/gui/src/main/webapp/**/*.js'];
+const defaultViews = fs.readdirSync('../../web/gui/src/main/webapp/app/view/');
+const viewNameMatcher = new RegExp(/\/onos\/ui\/app\/view\/(.+)\/.+\.(?:js|css|html)/);
+
+if (process.env.ONOS_EXTERNAL_APP_DIRS) {
+    let external_apps = process.env.ONOS_EXTERNAL_APP_DIRS.replace(/\s/,'').split(',');
+
+    external_apps = external_apps.reduce(function (dict, app) {
+        const pieces = app.split(':');
+        const appName = pieces[0];
+        const appPath = pieces[1];
+        dict[appName] = appPath;
+        return dict;
+    }, {});
+}
+
+const checkExternalApp = (url) => {
+    if(external_apps){
+        for(let i = 0; i < Object.keys(external_apps).length; i++){
+            const key = Object.keys(external_apps)[i];
+            if (url.indexOf(key) !== -1) {
+                return key;
+            }
+        }
+    }
+    return false;
+};
+
+const serve = () => {
+    bs = browserSync.init({
+        proxy: {
+            target: 'http://localhost:8181',
+            ws: true,
+            middleware: [
+                proxy(['**/*.js', '!/onos/ui/onos.js'], { target: 'http://localhost:8189' }),
+                proxy('**/*.js.map', {
+                    target: 'http://localhost:8189',
+                    changeOrigin: true,
+                    logLevel: 'debug'
+                })
+            ]
+        }
+    });
+};
+
+const tasks = () => {
+    gulp.task('serve', ['bundle-js', 'proxy-server'], serve);
+    gulp.task('proxy-server', function () {
+        gulp.src('../../web/gui/src/main/webapp')
+            .pipe(webserver({
+                port: 8189,
+                path: '/onos/ui/'
+            }));
+    });
+};
+
+export default tasks();
+
+export function reload() {
+    if (bs) {
+        bs.reload();
+    }
+}
+
diff --git a/tools/gui/gulp-tasks/index.js b/tools/gui/gulp-tasks/index.js
index 3bef084..3a31432 100644
--- a/tools/gui/gulp-tasks/index.js
+++ b/tools/gui/gulp-tasks/index.js
@@ -1,2 +1,7 @@
 export { default as BundleJS } from './bundles/bundle-js';
-export { default as BundleCSS } from './bundles/bundle-css';
\ No newline at end of file
+export { default as BundleCSS } from './bundles/bundle-css';
+
+// TODO: These are commented out because of npm issues with BUCK.
+// export { default as Lint } from './lint';
+// export { default as DevServer } from './dev-server';
+// export { default as Tests } from './unit-tests';
\ No newline at end of file
diff --git a/tools/gui/gulp-tasks/lint/esconfig.json b/tools/gui/gulp-tasks/lint/esconfig.json
new file mode 100644
index 0000000..670a48d
--- /dev/null
+++ b/tools/gui/gulp-tasks/lint/esconfig.json
@@ -0,0 +1,28 @@
+{
+    "extends": "google",
+    "globals": {
+        "angular": true,
+        "d3": true,
+        "_": true
+    },
+    "rules": {
+        "prefer-spread": 0,
+        "prefer-rest-params": 0,
+        "no-var": 0,
+        "comma-dangle": 1,
+        "brace-style": 0,
+        "no-void": 0,
+        "require-jsdoc": 0,
+        "padded-blocks": 0,
+        "quote-props": 0,
+        "no-warning-comments": 0,
+        "object-curly-spacing": ["error", "always"],
+        "indent": 0,
+        "one-var": 0,
+        "block-spacing": ["error", "always"],
+        "space-before-function-paren": ["error", { "anonymous": "always", "named": "never" }],
+        "max-len": ["error", 120],
+        "no-invalid-this": 0,
+        "switch-colon-spacing": ["error", { "after": true, "before": false }]
+    }
+}
diff --git a/tools/gui/gulp-tasks/lint/index.js b/tools/gui/gulp-tasks/lint/index.js
new file mode 100644
index 0000000..f864c6b
--- /dev/null
+++ b/tools/gui/gulp-tasks/lint/index.js
@@ -0,0 +1,33 @@
+import gulp from 'gulp';
+import eslint from 'gulp-eslint';
+import gulpIf from 'gulp-if';
+import path from 'path';
+
+const files = [
+    '../../web/gui/src/main/webapp/app/**/*.js'
+];
+
+function isFixed(file) {
+    // Has ESLint fixed the file contents?
+    return file.eslint != null && file.eslint.fixed;
+}
+
+const lint = () => {
+    return gulp.src(files)
+        .pipe(eslint({
+            configFile: path.join(__dirname, 'esconfig.json'),
+            useEslintrc: false,
+            // Automatically fix trivial issues
+            // fix: true,
+        }))
+        .pipe(eslint.format())
+        .pipe(gulpIf(isFixed,
+            gulp.dest('../../web/gui/src/main/webapp/app')
+        ));
+};
+
+const tasks = () => {
+    gulp.task('lint', () => lint());
+};
+
+export default tasks();
\ No newline at end of file
diff --git a/tools/gui/gulpfile.babel.js b/tools/gui/gulpfile.babel.js
index 7c5056d..2705872 100644
--- a/tools/gui/gulpfile.babel.js
+++ b/tools/gui/gulpfile.babel.js
@@ -1,9 +1,10 @@
 import gulp from 'gulp';
 import * as Tasks from './gulp-tasks/';
 
-gulp.task('build', ['bundle-css', 'bundle-vendor', 'bundle-js']);
+gulp.task('build', ['bundle-css', 'bundle-js']);
 gulp.task('tests', ['test']);
 
-gulp.task('default', function() {
-    // Do stuff
-});
\ No newline at end of file
+gulp.task('default', []);
+
+// TODO: Uncomment once npm and buck issues are resolved.
+// gulp.task('default', ['bundle-js', 'serve', 'watch-js']);
\ No newline at end of file
diff --git a/tools/gui/package.json b/tools/gui/package.json
index 4c2aeb0..304cd54 100644
--- a/tools/gui/package.json
+++ b/tools/gui/package.json
@@ -5,7 +5,8 @@
   "main": "index.js",
   "scripts": {
     "test": "echo \"Error: no test specified\" && exit 1",
-    "build": "./node_modules/gulp/bin/gulp.js build"
+    "build": "./node_modules/gulp/bin/gulp.js build",
+    "dev": "./node_modules/gulp/bin/gulp.js --development"
   },
   "author": "",
   "license": "ISC",
@@ -14,11 +15,18 @@
     "babel": "^6.23.0",
     "babel-core": "^6.25.0",
     "babel-preset-es2015": "^6.24.1",
+    "browser-sync": "^2.18.12",
+    "eslint": "^4.2.0",
+    "eslint-config-google": "^0.9.1",
     "gulp": "^3.9.1",
     "gulp-concat": "^2.6.1",
+    "gulp-eslint": "3.0.1",
+    "gulp-if": "^2.0.2",
     "gulp-sourcemaps": "^2.6.0",
     "gulp-strip-comments": "^2.4.5",
     "gulp-uglify": "^3.0.0",
+    "gulp-webserver": "^0.9.1",
+    "http-proxy-middleware": "^0.17.4",
     "jasmine-core": "^2.6.4",
     "karma": "^1.7.0",
     "karma-babel-preprocessor": "^6.0.1",