Adding GUI2 to the Bazel build system

Change-Id: Ieb39ade1a8713a50fdf885e2bb43f53f8f725ea8
diff --git a/web/gui2/BUILD b/web/gui2/BUILD
new file mode 100644
index 0000000..c50c654
--- /dev/null
+++ b/web/gui2/BUILD
@@ -0,0 +1,353 @@
+"""
+ Copyright 2018-present Open Networking Foundation
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+"""
+
+"""
+    Rules to build the ONOS GUI 2
+
+    The GUI2 Angular 6 elements are built here with Angular CLI 'ng'
+    Some work is being done in the Bazel community to integrate Bazel and
+    Angular 6, (Angular Buildtools Convergence -
+    https://docs.google.com/document/d/1OlyiUnoTirUj4gecGxJeZBcjHcFr36RvLsvpBl2mxA8/preview)
+    but it is in the very early stages (Aug'18) and not yet fit
+    for production and at present it works as a replacement for Angular CLI
+    (which is not desirable).
+
+    There are plans to extend Bazel it to work with Angular CLI, and if works
+    well this Bazel file may be rearchiteced in future.
+
+    Bazel and npm are incompatibe in how they deal with files. npm likes to
+    follow links to get back to the original canonical path names, and bazel
+    uses links extensively when populating the sandbox. To get around these
+    problems, the rules that follow use filegroups to specify the files as
+    dependencies and then use a genrule to convert the files into a tar ball.
+    Once the tar ball is unrolled into the sandbox, the links are broken, but
+    the build is still hermetic since those files are referred to as dependencies in the genrule.
+"""
+
+COMPILE_DEPS = CORE_DEPS + JACKSON + KRYO + [
+    "@javax_ws_rs_api//jar",
+    "@servlet_api//jar",
+    "@jetty_websocket//jar",
+    "@jetty_util//jar",
+    "@jersey_media_multipart//jar",
+    "@jersey_server//jar",
+    "//utils/rest:onlab-rest",
+    "//core/store/serializers:onos-core-serializers",
+]
+
+TEST_DEPS = TEST + [
+    "//core/api:onos-api-tests",
+    "//drivers/default:onos-drivers-default",
+]
+
+"""
+    Files that get put at the top level of the tar ball
+"""
+
+filegroup(
+    name = "_root_level_files",
+    srcs =
+        [
+            ":angular.json",
+            ":karma.conf.js",
+            ":package.json",
+            ":package-lock.json",
+            ":protractor.conf.js",
+            ":src/main/tsconfig.json",
+            ":src/main/tslint.json",
+            ":tsconfig.json",
+        ],
+)
+
+filegroup(
+    name = "_e2e_test_files",
+    srcs = [
+        ":e2e/app.e2e-spec.ts",
+        ":e2e/app.po.ts",
+        ":e2e/tsconfig.e2e.json",
+    ],
+)
+
+"""
+    Files that get put into the WEB-INF directory of the tar ball
+"""
+
+filegroup(
+    name = "_web_inf_classes_files",
+    srcs =
+        [
+            ":src/main/webapp/error.html",
+            ":src/main/webapp/login.html",
+            ":src/main/webapp/nav.html",
+            ":src/main/webapp/not-ready.html",
+            ":src/main/webapp/onos.theme.css",
+        ],
+)
+
+"""
+    Install npm packages listed in package.json in web/gui2
+    See bazel-genfiles/web/gui2/onos-gui2-npm-install.log for details of the 'npm install'
+"""
+
+genrule(
+    name = "_onos-gui2-npm-install",
+    srcs = [
+        "@nodejs//:bin/npm",
+        "@nodejs//:bin/node",
+        "@nodejs//:bin/node.js",
+        "@nodejs//:bin/nodejs/bin/node",
+        "@nodejs//:bin/nodejs/bin/npm",
+        ":_root_level_files",
+    ],
+    outs = [
+        "onos-gui2-npm-install.jar",
+        "onos-gui2-npm-install.log",
+    ],
+    cmd = " ROOT=`pwd` &&" +
+          " export HOME=. &&" +
+          " export XDG_CONFIG_HOME=$(@D)/config &&" +  # npm config cache to the sandbox
+          " export BABEL_DISABLE_CACHE=1 &&" +  # turn off babel cache
+          " NPM=$$ROOT/$(location @nodejs//:bin/npm) &&" +
+          " mkdir -p web/gui2 &&" +
+          " cd web/gui2 &&" +
+          " $$NPM install  --no-cache --loglevel=error > $$ROOT/$(location onos-gui2-npm-install.log) 2>&1 &&" +
+          " jar Mcf $$ROOT/$(location onos-gui2-npm-install.jar) . &&" +
+          " touch $$ROOT/$(location onos-gui2-npm-install.log)",  # to get the log always as the 2nd file
+    visibility = ["//visibility:public"],
+)
+
+"""
+    Run ng build to create outputs in production mode
+    See bazel-genfiles/web/gui2/onos-gui2-ng-build-prod.log for details of the Angular CLI output
+"""
+
+genrule(
+    name = "_onos-gui2-ng-build",
+    srcs = [
+        "@nodejs//:bin/npm",
+        "@nodejs//:bin/node",
+        "@nodejs//:bin/node.js",
+        "@nodejs//:bin/nodejs/bin/node",
+        "@nodejs//:bin/nodejs/bin/npm",
+        ":_onos-gui2-npm-install",
+        ":_web_app_all",
+    ],
+    outs = [
+        "onos-gui2-ng-build-prod.log",
+        "onos-gui2-ng-build.jar",
+    ],
+    cmd = "ROOT=`pwd` &&" +
+          " export HOME=. &&" +
+          " export XDG_CONFIG_HOME=$(@D)/config &&" +
+          " NODE=$(location @nodejs//:bin/node) &&" +
+          " INSTALL_FILES=($(locations :_onos-gui2-npm-install)) &&" +  # An array of filenames - sorted by time created
+          " mkdir -p web/gui2 && cd web/gui2 &&" +
+          " jar xf ../../$(location :_web_app_all) &&" +
+          " jar xf $$ROOT/$${INSTALL_FILES[0]} &&" +
+          " chmod +x $$ROOT/web/gui2/node_modules/@angular/cli/bin/ng &&" +
+          " export PATH=$$ROOT/$${NODE::-5}:$$ROOT/web/gui2/node_modules/@angular/cli/bin:$$PATH &&" +
+          " node -v > ../../$(location onos-gui2-ng-build-prod.log) &&" +
+          " npm -v >> ../../$(location onos-gui2-ng-build-prod.log) &&" +
+          " ng -v >> ../../$(location onos-gui2-ng-build-prod.log) &&" +
+          # Build it in production mode - optimization is turned off because of Angular CLI 6.0.x bug https://github.com/angular/angular-cli/issues/7799
+          " ng build --extract-css --prod --optimization=false --preserve-symlinks --base-href /onos/ui2/ --deploy-url /onos/ui2/ >> $$ROOT/$(location onos-gui2-ng-build-prod.log) 2>&1 &&" +
+          " cd src/main/webapp/dist && jar Mcf $$ROOT/$(location onos-gui2-ng-build.jar) .",
+    message = "Angular CLI 6 build",
+)
+
+"""
+    Run 'ng test' to run Angular test and 'ng lint' for checkstyle
+    See bazel-genfiles/web/gui2/onos-gui2-ng-lint.log or
+    bazel-genfiles/web/gui2/onos-gui2-ng-test.log for details of the Angular CLI output
+"""
+
+genrule(
+    name = "_onos-gui2-ng-test-genrule",
+    srcs = [
+        "@nodejs//:bin/npm",
+        "@nodejs//:bin/node",
+        "@nodejs//:bin/node.js",
+        "@nodejs//:bin/nodejs/bin/node",
+        "@nodejs//:bin/nodejs/bin/npm",
+        ":_onos-gui2-npm-install",
+        ":_web_app_all",
+        ":_web_app_tests",
+        ":_angular_all",
+    ],
+    outs = [
+        "onos-gui2-ng-ver.log",
+        "onos-gui2-ng-lint.log",
+        "onos-gui2-ng-test.log",
+    ],
+    cmd = " ROOT=`pwd` &&" +
+          " export HOME=. &&" +
+          " export XDG_CONFIG_HOME=$(@D)/config &&" +
+          " NODE=$(location @nodejs//:bin/node) &&" +
+          " INSTALL_FILES=($(locations :_onos-gui2-npm-install)) &&" +  # An array of filenames - sorted by time created
+          " mkdir -p web/gui2 &&" +
+          " cd web/gui2 &&" +
+          " jar xf ../../$(location :_angular_all) &&" +
+          " jar xf ../../$(location :_web_app_all) &&" +
+          " jar xf ../../$(location :_web_app_tests) &&" +
+          " jar xf $$ROOT/$${INSTALL_FILES[0]} &&" +
+          " chmod +x $$ROOT/web/gui2/node_modules/@angular/cli/bin/ng &&" +
+          " export PATH=$$ROOT/$${NODE::-5}:$$ROOT/web/gui2/node_modules/@angular/cli/bin:$$PATH &&" +
+          " node -v > ../../$(location onos-gui2-ng-ver.log) &&" +
+          " npm -v >> ../../$(location onos-gui2-ng-ver.log) &&" +
+          " ng -v >> ../../$(location onos-gui2-ng-ver.log) &&" +
+          " ng lint > ../../$(location onos-gui2-ng-lint.log);" +
+          " if [ -f /usr/bin/chromium-browser ]; then " +  # Add to this for Mac and Chrome
+          "   export CHROME_BIN=/usr/bin/chromium-browser; " +
+          " elif [ -f /opt/google/chrome/chrome ]; then " +
+          "   export CHROME_BIN=/opt/google/chrome/chrome; " +
+          " else " +
+          "   MSG='Warning: Step onos-gui2-ng-test skipped because \\n" +
+          "   no binary for ChromeHeadless browser was found at /usr/bin/chromium-browser. \\n" +
+          "   Install Google Chrome or Chromium Browser to allow this step to run.';" +
+          "   echo -e $$MSG >&2;" +
+          "   echo -e $$MSG > ../../$(location onos-gui2-ng-test.log);" +
+          "   exit 0;" +
+          " fi;" +
+          " ng test --preserve-symlinks --code-coverage --browsers=ChromeHeadless" +
+          "     --watch=false > ../../$(location onos-gui2-ng-test.log) 2>&1 ||" +
+          " if [ $$? -eq 0 ]; then echo 'Successfully ran tests';" +
+          " else " +
+          "   echo 'Error running \'ng test\' on \'//web/gui2:onos-gui2-ng-test\'. \\\n" +
+          "     See bazel-genfiles/web/gui2/onos-gui2-ng-test.log for more details' >&2;" +
+          #"   tail -n 100 ../../$(location onos-gui2-ng-test.log) >&2;" +
+          "   exit 1;" +
+          " fi;",
+    message = "Angular CLI 6 lint and test",
+)
+
+"""
+    Make a jar file of all the webapp files. Useful for breaking symblic links in the sandbox
+"""
+
+genrule(
+    name = "_web_app_all",
+    srcs = glob(
+        [
+            "src/main/webapp/**",
+        ],
+        exclude = [
+            "src/main/webapp/**/*.spec.ts",  # Don't track tests here
+            "src/main/webapp/tests/**",
+            "src/main/webapp/node_modules/**",
+            "src/main/webapp/dist/**",
+            "src/main/webapp/doc/**",
+        ],
+    ),
+    outs = ["web_app_all.jar"],
+    cmd = "cd web/gui2 &&" +
+          " find src/main/webapp -type f -exec touch -t 201808280000 {} \; &&" +
+          " jar Mcf ../../$@ src/main/webapp",
+)
+
+"""
+    Make a jar file of all the webapp test (*.spec.ts) files.
+"""
+
+genrule(
+    name = "_web_app_tests",
+    srcs = glob(
+        [
+            "src/main/webapp/**/*.spec.ts",
+        ],
+        exclude = [
+            "src/main/webapp/tests/**",
+            "src/main/webapp/node_modules/**",
+            "src/main/webapp/dist/**",
+            "src/main/webapp/doc/**",
+        ],
+    ),
+    outs = ["web_app_tests.jar"],
+    cmd = "cd web/gui2 &&" +
+          " find src/main/webapp -type f -exec touch -t 201808280000 {} \; &&" +
+          " jar Mcf ../../$@ src/main/webapp",
+)
+
+"""
+    Make a jar file of all the supporting files. Useful for breaking symblic links in the sandbox
+"""
+
+genrule(
+    name = "_angular_all",
+    srcs = [
+        ":_e2e_test_files",
+        ":_root_level_files",
+    ],
+    outs = ["angular_all.jar"],
+    cmd = " cd web/gui2 && jar Mcf ../../$@ .",
+)
+
+"""
+    Builds the java jar for the java code provided by the GUI2
+"""
+
+osgi_jar_with_tests(
+    name = "_onos-gui2-base-jar",
+    exclude_tests = [
+        "org.onosproject.ui.impl.AbstractUiImplTest",
+        "org.onosproject.ui.impl.topo.model.AbstractTopoModelTest",
+    ],
+    test_deps = TEST_DEPS,
+    web_context = "/onos/ui2",
+    deps = COMPILE_DEPS,
+)
+
+"""
+    Builds the tar ball for the ONOS GUI2
+"""
+
+genrule(
+    name = "onos-gui2",
+    srcs = [
+        ":_onos-gui2-ng-build",
+        ":_onos-gui2-base-jar",
+        ":_web_inf_classes_files",
+    ],
+    outs = ["onos-gui2.jar"],
+    cmd = " ROOT=`pwd` &&" +
+          " mkdir -p web/gui2/WEB-INF/classes &&" +
+          " cd web/gui2 &&" +
+          " BUILD_FILES=($(locations :_onos-gui2-ng-build)) &&" +  # An array of filenames - sorted by time created
+          " for i in $(locations :_web_inf_classes_files); do cp $$ROOT/$$i ./WEB-INF/classes/; done &&" +
+          " (cd WEB-INF/classes && jar xf $$ROOT/$${BUILD_FILES[1]}) &&" +
+          " jar xf $$ROOT/$(location :_onos-gui2-base-jar) &&" +
+          " find . -type f -exec touch -t 201808280000 {} \; &&" +
+          " jar cmf META-INF/MANIFEST.MF $$ROOT/$@ WEB-INF/classes",
+    output_to_bindir = 1,
+    visibility = ["//visibility:public"],
+)
+
+"""
+    Wrap the genrule for testing in a test
+"""
+
+sh_test(
+    name = "onos-gui2-ng-tests",
+    size = "small",
+    srcs = [
+        ":ng-test.sh",
+    ],
+    data = [
+        ":_onos-gui2-ng-test-genrule",
+    ],
+    deps = [
+        "@bazel_tools//tools/bash/runfiles",
+    ],
+)