Added native Bazel build to GUI2. Reduced a lot of the unused Angular CLI structures

Reviewers should look at the changes in WORKSPACE, BUILD, BUILD.bazel, README.md files
This is only possible now as rules_nodejs went to 1.0.0 on December 20
gui2 has now been made the entry point (rather than gui2-fw-lib)
No tests or linting are functional yet for Typescript
Each NgModule now has its own BUILD.bazel file with ng_module
gui2-fw-lib is all one module and has been refactored to simplify the directory structure
gui2-topo-lib is also all one module - its directory structure has had 3 layers removed
The big bash script in web/gui2/BUILD has been removed - all is done through ng_module rules
in web/gui2/src/main/webapp/BUILD.bazel and web/gui2/src/main/webapp/app/BUILD.bazel

Change-Id: Ifcfcc23a87be39fe6d6c8324046cc8ebadb90551
diff --git a/web/gui2/BUILD b/web/gui2/BUILD
index 1119dd7..f315b5e 100644
--- a/web/gui2/BUILD
+++ b/web/gui2/BUILD
@@ -1,12 +1,9 @@
 """
  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.
@@ -16,7 +13,6 @@
 
 """
     Rules to build the ONOS GUI 2
-
     The GUI2 Angular 7 elements are built here with Angular CLI 'ng'
     Some work is being done in the Bazel community to integrate Bazel and
     Angular 7, (Angular Buildtools Convergence -
@@ -24,10 +20,8 @@
     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
@@ -58,263 +52,6 @@
 ]
 
 """
-    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.global.css",
-        ],
-)
-
-"""
-    Run ng build to create outputs in production mode
-    See bazel-out/k8-fastbuild/bin/web/gui2/onos-gui2-ng-build-prod.log for details of the Angular CLI output
-
-    To avoid the overhead of having several "npm install" invocations, we just do
-    it once in the //web/gui2-fw-lib which is really the core for the whole Angular 7
-    structure in ONOS. This copies files in to node_modules, but because the gui2-fw-lib
-    has not been generated at that time we copy it in separately below with the 'tar' cmd
-    and then 'mv'
-"""
-
-genrule(
-    name = "_onos-gui2-ng-build",
-    srcs = [
-        "@nodejs//:bin/npm",
-        "@nodejs//:bin/nodejs/bin/node",
-        "//web/gui2-fw-lib:onos-gui2-fw-npm-install",
-        "//web/gui2-fw-lib:onos-gui2-fw-ng-build",
-        "//web/gui2-fw-lib:gui2_fw_lib_ext_css",
-        "//web/gui2-topo-lib:gui2-topo-lib-build",
-        "//apps/faultmanagement/fm-gui2-lib:fm-gui2-lib-build",
-        ":_root_level_files",
-        ":_web_app_all",
-        "//apps/roadm/web/roadm-gui:roadm-gui-lib-build",
-    ],
-    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/nodejs/bin/node) &&" +
-          " INSTALL_FILES=($(locations //web/gui2-fw-lib:onos-gui2-fw-npm-install)) &&" +  # An array of filenames - sorted by time created
-          " FWLIB_FILES=($(locations //web/gui2-fw-lib:onos-gui2-fw-ng-build)) &&" +  # 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]} &&" +
-          " tar xf $$ROOT/$${FWLIB_FILES[0]} &&" +
-          " mv package/ node_modules/gui2-fw-lib/ &&" +
-          # Add in modules from external packages
-          " GUI2_TOPO_LIB_FILES=($(locations //web/gui2-topo-lib:gui2-topo-lib-build)) &&" +  # An array of filenames - sorted by time created
-          " tar xf $$ROOT/$${GUI2_TOPO_LIB_FILES[0]} &&" +
-          " mv package/ node_modules/gui2-topo-lib/ &&" +
-          " FM_GUI2_LIB_FILES=($(locations //apps/faultmanagement/fm-gui2-lib:fm-gui2-lib-build)) &&" +  # An array of filenames - sorted by time created
-          " tar xf $$ROOT/$${FM_GUI2_LIB_FILES[0]} &&" +
-          " mv package/ node_modules/fm-gui2-lib/ &&" +
-          " ROADM_GUI_LIB_FILES=($(locations //apps/roadm/web/roadm-gui:roadm-gui-lib-build)) &&" +  # An array of filenames - sorted by time created
-          " tar xf $$ROOT/$${ROADM_GUI_LIB_FILES[0]} &&" +
-          " mv package/ node_modules/roadm-gui-lib/ &&" +
-          # End of add in modules from external packages
-          " mkdir -p src/main/webapp/app/fw &&" +
-          " (cd src/main/webapp/app/fw &&" +
-          "  jar xf $$ROOT/$(location //web/gui2-fw-lib:gui2_fw_lib_ext_css)) &&" +
-          " chmod +x $$ROOT/web/gui2/node_modules/@angular/cli/bin/ng &&" +
-          " export PATH=$$ROOT/$$(dirname $${NODE}):$$ROOT/web/gui2/node_modules/@angular/cli/bin:$$PATH &&" +
-          " node -v > ../../$(location onos-gui2-ng-build-prod.log) &&" +
-          " $$ROOT/$(location @nodejs//:bin/npm) version >> ../../$(location onos-gui2-ng-build-prod.log) &&" +
-          " ng version >> ../../$(location onos-gui2-ng-build-prod.log) &&" +
-          " ng build --extract-css --prod --preserve-symlinks" +
-          "   --base-href /onos/ui/ --deploy-url /onos/ui/ >> $$ROOT/$(location onos-gui2-ng-build-prod.log) 2>&1 ||" +
-          " if [ $$? -eq 0 ]; then echo 'Successfully ran build';" +
-          " else " +
-          "   echo 'Error running \'ng build\' on \'//web/gui2:_onos-gui2-ng-build\'. \\\n" +
-          "     See bazel-out/k8-fastbuild/bin/web/gui2/onos-gui2-ng-build-prod.log for more details' >&2;" +
-          #"   tail -n 100 ../../$(location onos-gui2-ng-test.log) >&2;" +
-          "   exit 1;" +
-          " fi;" +
-          " cp -r node_modules/gui2-fw-lib/assets src/main/webapp/dist &&" +
-          " cd src/main/webapp/dist &&" +
-          " jar Mcf $$ROOT/$(location onos-gui2-ng-build.jar) .",
-    message = "Angular CLI 8 build",
-)
-
-"""
-    Run 'ng test' to run Angular test and 'ng lint' for checkstyle
-    See bazel-out/k8-fastbuild/bin/web/gui2/onos-gui2-ng-lint.log or
-    bazel-out/k8-fastbuild/bin/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/nodejs/bin/node",
-        "//web/gui2-fw-lib:onos-gui2-fw-npm-install",
-        "//web/gui2-fw-lib:onos-gui2-fw-ng-build",
-        "//web/gui2-fw-lib:gui2_fw_lib_ext_css",
-        ":_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/nodejs/bin/node) &&" +
-          " INSTALL_FILES=($(locations //web/gui2-fw-lib:onos-gui2-fw-npm-install)) &&" +  # An array of filenames - sorted by time created
-          " FWLIB_FILES=($(locations //web/gui2-fw-lib:onos-gui2-fw-ng-build)) &&" +  # 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]} &&" +
-          " tar xf $$ROOT/$${FWLIB_FILES[0]} &&" +
-          " mv package/ node_modules/gui2-fw-lib/ &&" +
-          " mkdir -p src/main/webapp/app/fw &&" +
-          " (cd src/main/webapp/app/fw &&" +
-          "  jar xf $$ROOT/$(location //web/gui2-fw-lib:gui2_fw_lib_ext_css)) &&" +
-          " chmod +x $$ROOT/web/gui2/node_modules/@angular/cli/bin/ng &&" +
-          " export PATH=$$ROOT/$$(dirname $${NODE}):$$ROOT/web/gui2/node_modules/@angular/cli/bin:$$PATH &&" +
-          " node -v > ../../$(location onos-gui2-ng-ver.log) &&" +
-          " $$ROOT/$(location @nodejs//:bin/npm) -v >> ../../$(location onos-gui2-ng-ver.log) &&" +
-          " ng version >> ../../$(location onos-gui2-ng-ver.log);" +
-          " ng lint > ../../$(location onos-gui2-ng-lint.log) 2>&1 ||" +
-          " if [ $$? -eq 0 ]; then echo 'Successfully ran lint';" +
-          " else " +
-          "   echo 'Error running \'ng lint\' on \'//web/gui2:onos-gui2-ng-test\'. \\\n" +
-          "     See bazel-out/k8-fastbuild/bin/web/gui2/onos-gui2-ng-lint.log for details' >&2;" +
-          "   cat ../../$(location onos-gui2-ng-lint.log) >&2 ||" +
-          "   exit 1;" +
-          " fi;" +
-          " 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-out/k8-fastbuild/bin/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 8 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/**",
-            "src/main/webapp/app/fw/**",
-        ],
-    ),
-    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
 """
 
@@ -347,50 +84,26 @@
 genrule(
     name = "onos-web-gui2",
     srcs = [
-        ":_onos-gui2-ng-build",
+        "//web/gui2/src/main/webapp:prodapp",
         ":_onos-gui2-base-jar",
-        ":_web_inf_classes_files",
         "//web/gui:onos-gui-lion-for-gui2",
-        "//web/gui:onos-gui-data-for-gui2",
-        "src/main/webapp/WEB-INF/web.xml",
+        "//web/gui2/src/main/webapp:WEB-INF/web.xml",
     ],
     outs = ["onos-gui2.jar"],
     cmd = " ROOT=`pwd` &&" +
-          " mkdir -p web/gui2/WEB-INF/classes &&" +
+          " mkdir -p web/gui2/WEB-INF/classes/org/onosproject/ui/ &&" +
           " 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) &&" +
           " unzip -q $$ROOT/$(location //web/gui:onos-gui-lion-for-gui2) web/gui/src/main/resources/**/* &&" +
           " mv web/gui/src/main/resources/org/onosproject/ui/lion* WEB-INF/classes/org/onosproject/ui/ &&" +
-          " mv web/gui/src/main/resources/core WEB-INF/classes/ &&" +
-          " unzip -q $$ROOT/$(location //web/gui:onos-gui-data-for-gui2) web/gui/src/main/webapp/data/**/* &&" +
-          " mv web/gui/src/main/webapp/data WEB-INF/classes/ &&" +
-          " find . -type f -exec touch -t 201903200000 {} \; &&" +
+          " cp -R $$ROOT/$(location //web/gui2/src/main/webapp:prodapp)/* WEB-INF/classes/ &&" +
+          " mv $$ROOT/$(location //web/gui2/src/main/webapp:WEB-INF/web.xml) WEB-INF &&" +
+          " find . -type f -exec touch -t 202001010001 {} \; &&" +
           " jar cmf META-INF/MANIFEST.MF $$ROOT/$@ WEB-INF/web.xml WEB-INF/classes OSGI-INF/*.xml",
     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",
-    ],
-)
-
 onos_app(
     category = "Graphical User Interface",
     description = "ONOS GUI2 - a reengineered version of the original ONOS GUI " +