gerrit/lib/js.defs
Han-Wen Nienhuys 28e7a6d3fe bazel: bower support
* npm_binary (download tarball packaged npm apps)

* bower_archive (download a zip file, to be put in WORKSPACE)

* bower_component (defining a bower library, with dependency )

* bower_component_bundle (zipping up libraries together)

* js_component (insert plain js file into bower component bundle)

* bower2bazel.py: run bower to find dependencies, generate a .bzl to
  define archives and define components

Tested:

 python tools/js/bower2bazel.py -w lib/js/bower_archives.bzl -b \
    lib/js/bower_components.bzl

 bazel build polygerrit-ui:components
 unzip -v bazel-bin/polygerrit-ui/components.zip > /tmp/baz

 buck build polygerrit-ui:polygerrit_components
 unzip -v buck-out/gen/polygerrit-ui/polygerrit_components/polygerrit_components.bower_components.zip > /tmp/buck

 diff /tmp/buck /tmp/baz

The diff corresponds to newer file versions pinned through bower2bazel.

Change-Id: I4f33914d4853bcf8afe78b4719d0e0e83b139031
2016-09-29 13:18:19 +02:00

173 lines
4.4 KiB
Plaintext

# Copyright (C) 2015 The Android Open Source Project
#
# 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.
NPMJS = 'NPMJS'
GERRIT = 'GERRIT'
# NOTE: npm_binary rules do not get their licenses checked by gen_licenses.py,
# as we would have to cut too many edges. DO NOT include these binaries in
# build outputs. Using them in the build _process_ is ok.
def npm_binary(
name,
version,
sha1 = '',
repository = NPMJS,
visibility = ['PUBLIC']):
dir = '%s-%s' % (name, version)
filename = '%s.tgz' % dir
dest = '%s@%s.npm_binary.tgz' % (name, version)
if repository == GERRIT:
url = 'http://gerrit-maven.storage.googleapis.com/npm-packages/%s' % filename
elif repository == NPMJS:
url = 'http://registry.npmjs.org/%s/-/%s' % (name, filename)
else:
raise ValueError('invalid repository: %s' % repository)
cmd = ['$(exe //tools:download_file)', '-o', '$OUT', '-u', url]
if sha1:
cmd.extend(['-v', sha1])
genrule(
name = name,
cmd = ' '.join(cmd),
out = dest,
visibility = visibility,
)
def run_npm_binary(target):
return '$(location //tools/js:run_npm_binary) $(location %s)' % target
def bower_component(
name,
package,
version,
license,
deps = [],
semver = None,
sha1 = '',
visibility = ['PUBLIC']):
download_name = '%s__download_bower' % name
genrule(
name = download_name,
cmd = ' '.join([
'$(exe //tools/js:download_bower)',
'-b', '"%s"' % run_npm_binary('//lib/js:bower'),
'-n', name,
'-p', package,
'-v', version,
'-s', sha1,
'-o', '$OUT',
]),
out = '%s.zip' % download_name,
license = license,
visibility = [],
)
renamed_name = '%s__renamed' % name
genrule(
name = renamed_name,
cmd = ' && '.join([
'cd $TMP',
'mkdir bower_components',
'cd bower_components',
'unzip $(location :%s)' % download_name,
'cd ..',
'zip -r $OUT bower_components',
]),
out = '%s.zip' % renamed_name,
visibility = [],
)
genrule(
name = name,
cmd = _combine_components([':%s' % renamed_name] + deps),
out = '%s-%s.zip' % (name, version),
visibility = visibility,
)
version_name = '%s__bower_version' % name
dep_version = semver if semver is not None else version
deps_json = '{"%s": "%s#%s"}' % (name, package, dep_version)
genrule(
name = version_name,
cmd = "echo '%s' > $OUT" % deps_json,
out = version_name,
visibility = visibility,
)
def bower_components(
name,
deps,
visibility = ['PUBLIC']):
genrule(
name = name,
cmd = _combine_components(deps),
out = '%s.bower_components.zip' % name,
visibility = visibility,
)
def _combine_components(deps):
cmds = ['cd $TMP']
for d in deps:
cmds.append('unzip -qo $(location %s)' % d)
cmds.append("find bower_components -exec touch -t 198001010000 '{}' ';'")
cmds.append('zip -r $OUT bower_components/*')
return ' && '.join(cmds)
VULCANIZE_FLAGS = [
'--inline-scripts',
'--inline-css',
'--strip-comments',
]
def vulcanize(
name,
app,
srcs,
components,
extra_flags = [],
visibility = ['PUBLIC']):
genrule(
name = '%s__vulcanized' % name,
cmd = ' '.join([
'unzip', '-qd', '$TMP', '$(location %s)' % components,
'&&', 'rm', '-rf', '$SRCDIR/bower_components',
'&&', 'ln', '-s', '-f', '$TMP/bower_components', '.',
'&&', run_npm_binary('//lib/js:vulcanize')
] + VULCANIZE_FLAGS + extra_flags + [
'--out-html', '$OUT',
'$SRCDIR/%s' % app,
]),
srcs = srcs,
out = '%s.vulcanized.html' % name,
visibility = visibility,
)
genrule(
name = name,
cmd = ' '.join([
'cd', '$TMP',
'&&', run_npm_binary('//lib/js:crisper'), '--always-write-script',
'--source', '$(location :%s__vulcanized)' % name,
'--html', '%s.html' % name,
'--js', '%s.js' % name,
'&&', 'zip', '$OUT', '%s.html' % name, '%s.js' % name,
]),
out = '%s.vulcanized.zip' % name,
)