blob: c50c654b14a19ba0191b64e4c6912735d94309fb [file] [log] [blame]
Sean Condonf6af2a52018-08-19 10:43:24 +01001"""
2 Copyright 2018-present Open Networking Foundation
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15"""
16
17"""
18 Rules to build the ONOS GUI 2
19
20 The GUI2 Angular 6 elements are built here with Angular CLI 'ng'
21 Some work is being done in the Bazel community to integrate Bazel and
22 Angular 6, (Angular Buildtools Convergence -
23 https://docs.google.com/document/d/1OlyiUnoTirUj4gecGxJeZBcjHcFr36RvLsvpBl2mxA8/preview)
24 but it is in the very early stages (Aug'18) and not yet fit
25 for production and at present it works as a replacement for Angular CLI
26 (which is not desirable).
27
28 There are plans to extend Bazel it to work with Angular CLI, and if works
29 well this Bazel file may be rearchiteced in future.
30
31 Bazel and npm are incompatibe in how they deal with files. npm likes to
32 follow links to get back to the original canonical path names, and bazel
33 uses links extensively when populating the sandbox. To get around these
34 problems, the rules that follow use filegroups to specify the files as
35 dependencies and then use a genrule to convert the files into a tar ball.
36 Once the tar ball is unrolled into the sandbox, the links are broken, but
37 the build is still hermetic since those files are referred to as dependencies in the genrule.
38"""
39
40COMPILE_DEPS = CORE_DEPS + JACKSON + KRYO + [
41 "@javax_ws_rs_api//jar",
42 "@servlet_api//jar",
43 "@jetty_websocket//jar",
44 "@jetty_util//jar",
45 "@jersey_media_multipart//jar",
46 "@jersey_server//jar",
47 "//utils/rest:onlab-rest",
48 "//core/store/serializers:onos-core-serializers",
49]
50
51TEST_DEPS = TEST + [
52 "//core/api:onos-api-tests",
53 "//drivers/default:onos-drivers-default",
54]
55
56"""
57 Files that get put at the top level of the tar ball
58"""
59
60filegroup(
61 name = "_root_level_files",
62 srcs =
63 [
64 ":angular.json",
65 ":karma.conf.js",
66 ":package.json",
67 ":package-lock.json",
68 ":protractor.conf.js",
69 ":src/main/tsconfig.json",
70 ":src/main/tslint.json",
71 ":tsconfig.json",
72 ],
73)
74
75filegroup(
76 name = "_e2e_test_files",
77 srcs = [
78 ":e2e/app.e2e-spec.ts",
79 ":e2e/app.po.ts",
80 ":e2e/tsconfig.e2e.json",
81 ],
82)
83
84"""
85 Files that get put into the WEB-INF directory of the tar ball
86"""
87
88filegroup(
89 name = "_web_inf_classes_files",
90 srcs =
91 [
92 ":src/main/webapp/error.html",
93 ":src/main/webapp/login.html",
94 ":src/main/webapp/nav.html",
95 ":src/main/webapp/not-ready.html",
96 ":src/main/webapp/onos.theme.css",
97 ],
98)
99
100"""
101 Install npm packages listed in package.json in web/gui2
102 See bazel-genfiles/web/gui2/onos-gui2-npm-install.log for details of the 'npm install'
103"""
104
105genrule(
106 name = "_onos-gui2-npm-install",
107 srcs = [
108 "@nodejs//:bin/npm",
109 "@nodejs//:bin/node",
110 "@nodejs//:bin/node.js",
111 "@nodejs//:bin/nodejs/bin/node",
112 "@nodejs//:bin/nodejs/bin/npm",
113 ":_root_level_files",
114 ],
115 outs = [
116 "onos-gui2-npm-install.jar",
117 "onos-gui2-npm-install.log",
118 ],
119 cmd = " ROOT=`pwd` &&" +
120 " export HOME=. &&" +
121 " export XDG_CONFIG_HOME=$(@D)/config &&" + # npm config cache to the sandbox
122 " export BABEL_DISABLE_CACHE=1 &&" + # turn off babel cache
123 " NPM=$$ROOT/$(location @nodejs//:bin/npm) &&" +
124 " mkdir -p web/gui2 &&" +
125 " cd web/gui2 &&" +
126 " $$NPM install --no-cache --loglevel=error > $$ROOT/$(location onos-gui2-npm-install.log) 2>&1 &&" +
127 " jar Mcf $$ROOT/$(location onos-gui2-npm-install.jar) . &&" +
128 " touch $$ROOT/$(location onos-gui2-npm-install.log)", # to get the log always as the 2nd file
129 visibility = ["//visibility:public"],
130)
131
132"""
133 Run ng build to create outputs in production mode
134 See bazel-genfiles/web/gui2/onos-gui2-ng-build-prod.log for details of the Angular CLI output
135"""
136
137genrule(
138 name = "_onos-gui2-ng-build",
139 srcs = [
140 "@nodejs//:bin/npm",
141 "@nodejs//:bin/node",
142 "@nodejs//:bin/node.js",
143 "@nodejs//:bin/nodejs/bin/node",
144 "@nodejs//:bin/nodejs/bin/npm",
145 ":_onos-gui2-npm-install",
146 ":_web_app_all",
147 ],
148 outs = [
149 "onos-gui2-ng-build-prod.log",
150 "onos-gui2-ng-build.jar",
151 ],
152 cmd = "ROOT=`pwd` &&" +
153 " export HOME=. &&" +
154 " export XDG_CONFIG_HOME=$(@D)/config &&" +
155 " NODE=$(location @nodejs//:bin/node) &&" +
156 " INSTALL_FILES=($(locations :_onos-gui2-npm-install)) &&" + # An array of filenames - sorted by time created
157 " mkdir -p web/gui2 && cd web/gui2 &&" +
158 " jar xf ../../$(location :_web_app_all) &&" +
159 " jar xf $$ROOT/$${INSTALL_FILES[0]} &&" +
160 " chmod +x $$ROOT/web/gui2/node_modules/@angular/cli/bin/ng &&" +
161 " export PATH=$$ROOT/$${NODE::-5}:$$ROOT/web/gui2/node_modules/@angular/cli/bin:$$PATH &&" +
162 " node -v > ../../$(location onos-gui2-ng-build-prod.log) &&" +
163 " npm -v >> ../../$(location onos-gui2-ng-build-prod.log) &&" +
164 " ng -v >> ../../$(location onos-gui2-ng-build-prod.log) &&" +
165 # 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
166 " 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 &&" +
167 " cd src/main/webapp/dist && jar Mcf $$ROOT/$(location onos-gui2-ng-build.jar) .",
168 message = "Angular CLI 6 build",
169)
170
171"""
172 Run 'ng test' to run Angular test and 'ng lint' for checkstyle
173 See bazel-genfiles/web/gui2/onos-gui2-ng-lint.log or
174 bazel-genfiles/web/gui2/onos-gui2-ng-test.log for details of the Angular CLI output
175"""
176
177genrule(
178 name = "_onos-gui2-ng-test-genrule",
179 srcs = [
180 "@nodejs//:bin/npm",
181 "@nodejs//:bin/node",
182 "@nodejs//:bin/node.js",
183 "@nodejs//:bin/nodejs/bin/node",
184 "@nodejs//:bin/nodejs/bin/npm",
185 ":_onos-gui2-npm-install",
186 ":_web_app_all",
187 ":_web_app_tests",
188 ":_angular_all",
189 ],
190 outs = [
191 "onos-gui2-ng-ver.log",
192 "onos-gui2-ng-lint.log",
193 "onos-gui2-ng-test.log",
194 ],
195 cmd = " ROOT=`pwd` &&" +
196 " export HOME=. &&" +
197 " export XDG_CONFIG_HOME=$(@D)/config &&" +
198 " NODE=$(location @nodejs//:bin/node) &&" +
199 " INSTALL_FILES=($(locations :_onos-gui2-npm-install)) &&" + # An array of filenames - sorted by time created
200 " mkdir -p web/gui2 &&" +
201 " cd web/gui2 &&" +
202 " jar xf ../../$(location :_angular_all) &&" +
203 " jar xf ../../$(location :_web_app_all) &&" +
204 " jar xf ../../$(location :_web_app_tests) &&" +
205 " jar xf $$ROOT/$${INSTALL_FILES[0]} &&" +
206 " chmod +x $$ROOT/web/gui2/node_modules/@angular/cli/bin/ng &&" +
207 " export PATH=$$ROOT/$${NODE::-5}:$$ROOT/web/gui2/node_modules/@angular/cli/bin:$$PATH &&" +
208 " node -v > ../../$(location onos-gui2-ng-ver.log) &&" +
209 " npm -v >> ../../$(location onos-gui2-ng-ver.log) &&" +
210 " ng -v >> ../../$(location onos-gui2-ng-ver.log) &&" +
211 " ng lint > ../../$(location onos-gui2-ng-lint.log);" +
212 " if [ -f /usr/bin/chromium-browser ]; then " + # Add to this for Mac and Chrome
213 " export CHROME_BIN=/usr/bin/chromium-browser; " +
214 " elif [ -f /opt/google/chrome/chrome ]; then " +
215 " export CHROME_BIN=/opt/google/chrome/chrome; " +
216 " else " +
217 " MSG='Warning: Step onos-gui2-ng-test skipped because \\n" +
218 " no binary for ChromeHeadless browser was found at /usr/bin/chromium-browser. \\n" +
219 " Install Google Chrome or Chromium Browser to allow this step to run.';" +
220 " echo -e $$MSG >&2;" +
221 " echo -e $$MSG > ../../$(location onos-gui2-ng-test.log);" +
222 " exit 0;" +
223 " fi;" +
224 " ng test --preserve-symlinks --code-coverage --browsers=ChromeHeadless" +
225 " --watch=false > ../../$(location onos-gui2-ng-test.log) 2>&1 ||" +
226 " if [ $$? -eq 0 ]; then echo 'Successfully ran tests';" +
227 " else " +
228 " echo 'Error running \'ng test\' on \'//web/gui2:onos-gui2-ng-test\'. \\\n" +
229 " See bazel-genfiles/web/gui2/onos-gui2-ng-test.log for more details' >&2;" +
230 #" tail -n 100 ../../$(location onos-gui2-ng-test.log) >&2;" +
231 " exit 1;" +
232 " fi;",
233 message = "Angular CLI 6 lint and test",
234)
235
236"""
237 Make a jar file of all the webapp files. Useful for breaking symblic links in the sandbox
238"""
239
240genrule(
241 name = "_web_app_all",
242 srcs = glob(
243 [
244 "src/main/webapp/**",
245 ],
246 exclude = [
247 "src/main/webapp/**/*.spec.ts", # Don't track tests here
248 "src/main/webapp/tests/**",
249 "src/main/webapp/node_modules/**",
250 "src/main/webapp/dist/**",
251 "src/main/webapp/doc/**",
252 ],
253 ),
254 outs = ["web_app_all.jar"],
255 cmd = "cd web/gui2 &&" +
256 " find src/main/webapp -type f -exec touch -t 201808280000 {} \; &&" +
257 " jar Mcf ../../$@ src/main/webapp",
258)
259
260"""
261 Make a jar file of all the webapp test (*.spec.ts) files.
262"""
263
264genrule(
265 name = "_web_app_tests",
266 srcs = glob(
267 [
268 "src/main/webapp/**/*.spec.ts",
269 ],
270 exclude = [
271 "src/main/webapp/tests/**",
272 "src/main/webapp/node_modules/**",
273 "src/main/webapp/dist/**",
274 "src/main/webapp/doc/**",
275 ],
276 ),
277 outs = ["web_app_tests.jar"],
278 cmd = "cd web/gui2 &&" +
279 " find src/main/webapp -type f -exec touch -t 201808280000 {} \; &&" +
280 " jar Mcf ../../$@ src/main/webapp",
281)
282
283"""
284 Make a jar file of all the supporting files. Useful for breaking symblic links in the sandbox
285"""
286
287genrule(
288 name = "_angular_all",
289 srcs = [
290 ":_e2e_test_files",
291 ":_root_level_files",
292 ],
293 outs = ["angular_all.jar"],
294 cmd = " cd web/gui2 && jar Mcf ../../$@ .",
295)
296
297"""
298 Builds the java jar for the java code provided by the GUI2
299"""
300
301osgi_jar_with_tests(
302 name = "_onos-gui2-base-jar",
303 exclude_tests = [
304 "org.onosproject.ui.impl.AbstractUiImplTest",
305 "org.onosproject.ui.impl.topo.model.AbstractTopoModelTest",
306 ],
307 test_deps = TEST_DEPS,
308 web_context = "/onos/ui2",
309 deps = COMPILE_DEPS,
310)
311
312"""
313 Builds the tar ball for the ONOS GUI2
314"""
315
316genrule(
317 name = "onos-gui2",
318 srcs = [
319 ":_onos-gui2-ng-build",
320 ":_onos-gui2-base-jar",
321 ":_web_inf_classes_files",
322 ],
323 outs = ["onos-gui2.jar"],
324 cmd = " ROOT=`pwd` &&" +
325 " mkdir -p web/gui2/WEB-INF/classes &&" +
326 " cd web/gui2 &&" +
327 " BUILD_FILES=($(locations :_onos-gui2-ng-build)) &&" + # An array of filenames - sorted by time created
328 " for i in $(locations :_web_inf_classes_files); do cp $$ROOT/$$i ./WEB-INF/classes/; done &&" +
329 " (cd WEB-INF/classes && jar xf $$ROOT/$${BUILD_FILES[1]}) &&" +
330 " jar xf $$ROOT/$(location :_onos-gui2-base-jar) &&" +
331 " find . -type f -exec touch -t 201808280000 {} \; &&" +
332 " jar cmf META-INF/MANIFEST.MF $$ROOT/$@ WEB-INF/classes",
333 output_to_bindir = 1,
334 visibility = ["//visibility:public"],
335)
336
337"""
338 Wrap the genrule for testing in a test
339"""
340
341sh_test(
342 name = "onos-gui2-ng-tests",
343 size = "small",
344 srcs = [
345 ":ng-test.sh",
346 ],
347 data = [
348 ":_onos-gui2-ng-test-genrule",
349 ],
350 deps = [
351 "@bazel_tools//tools/bash/runfiles",
352 ],
353)