blob: c9a4256be09f1de0b9bac2f782536b962a82af35 [file] [log] [blame]
# 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('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,
)