Create zip_file() rule

This doesn't work quite the same way as Fileset(). But it should be able
to serve as a decent replacement.

The important part of this design is that zips can depend on other zips.
Therefore definitions don't have to be monolithic. This will be
important when migrating the Domain Registry codebase, because our
Fileset() definitions are dispersed across many BUILD files. So we'll be
able to migrate to zip_file() with the least amount of intrusiveness.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=129034885
This commit is contained in:
Justine Tunney 2016-08-01 15:34:13 -07:00
parent b2d5108c0c
commit 9de287378b
8 changed files with 299 additions and 0 deletions

View file

@ -0,0 +1,92 @@
package(
default_testonly = 1,
default_visibility = ["//java/google/registry:registry_project"],
)
licenses(["notice"]) # Apache 2.0
load("//java/google/registry/builddefs:zip_file.bzl", "zip_file")
load("//javatests/google/registry/builddefs:zip_contents_test.bzl", "zip_contents_test")
genrule(
name = "generated",
outs = ["generated.txt"],
cmd = "echo generated >$@",
)
zip_file(
name = "basic",
srcs = [
"generated.txt",
"hello.txt",
"world.txt",
],
out = "basic.zip",
mappings = {"": ""},
)
zip_contents_test(
name = "zip_emptyMapping_leavesShortPathsInTact",
src = "basic.zip",
contents = {
"javatests/google/registry/builddefs/generated.txt": "generated",
"javatests/google/registry/builddefs/hello.txt": "hello",
"javatests/google/registry/builddefs/world.txt": "world",
},
)
zip_file(
name = "stripped",
srcs = ["hello.txt"],
out = "stripped.zip",
mappings = {"javatests/google/registry/builddefs": ""},
)
zip_contents_test(
name = "zip_prefixRemoval_works",
src = "stripped.zip",
contents = {"hello.txt": "hello"},
)
zip_file(
name = "repath",
srcs = [
"generated.txt",
"hello.txt",
"world.txt",
],
out = "repath.zip",
mappings = {
"javatests/google/registry/builddefs": "a/b/c",
"javatests/google/registry/builddefs/generated.txt": "x/y/z/generated.txt",
},
)
zip_contents_test(
name = "zip_pathReplacement_works",
src = "repath.zip",
contents = {
"a/b/c/hello.txt": "hello",
"a/b/c/world.txt": "world",
"x/y/z/generated.txt": "generated",
},
)
zip_file(
name = "overridden",
srcs = ["override/hello.txt"],
out = "overridden.zip",
mappings = {"javatests/google/registry/builddefs/override": "a/b/c"},
deps = [":repath"],
)
zip_contents_test(
name = "zip_fileWithSameMappingAsDependentRule_prefersMyMapping",
src = "overridden.zip",
contents = {
"a/b/c/hello.txt": "OMG IM AN OVERRIDE",
"a/b/c/world.txt": "world",
"x/y/z/generated.txt": "generated",
},
)

View file

@ -0,0 +1 @@
hello

View file

@ -0,0 +1 @@
OMG IM AN OVERRIDE

View file

@ -0,0 +1 @@
world

View file

@ -0,0 +1,61 @@
# Copyright 2016 The Domain Registry Authors. All Rights Reserved.
#
# 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.
"""Build rule for unit testing the zip_file() rule."""
load('//java/google/registry/builddefs:defs.bzl', 'ZIPPER', 'runpath')
def _impl(ctx):
"""Implementation of zip_contents_test() rule."""
cmd = [
'set -e',
'repo="$(pwd)"',
'zipper="${repo}/%s"' % runpath(ctx.file._zipper),
'archive="${repo}/%s"' % runpath(ctx.file.src),
('listing="$("${zipper}" v "${archive}"' +
' | grep -v ^d | awk \'{print $3}\' | LC_ALL=C sort)"'),
'if [[ "${listing}" != "%s" ]]; then' % (
'\n'.join(ctx.attr.contents.keys())),
' echo "archive had different file listing:"',
' "${zipper}" v "${archive}" | grep -v ^d',
' exit 1',
'fi',
'tmp="$(mktemp -d "${TMPDIR:-/tmp}/zip_contents_test.XXXXXXXXXX")"',
'cd "${tmp}"',
'"${zipper}" x "${archive}"',
]
for path, data in ctx.attr.contents.items():
cmd += [
'if [[ "$(cat "%s")" != "%s" ]]; then' % (path, data),
' echo "%s had different contents:"' % path,
' cat "%s"' % path,
' exit 1',
'fi',
]
cmd += ['cd "${repo}"',
'rm -rf "${tmp}"']
ctx.file_action(
output=ctx.outputs.executable,
content='\n'.join(cmd),
executable=True)
return struct(runfiles=ctx.runfiles([ctx.file.src, ctx.file._zipper]))
zip_contents_test = rule(
implementation=_impl,
test=True,
attrs={
'src': attr.label(allow_single_file=True),
'contents': attr.string_dict(),
'_zipper': attr.label(default=Label(ZIPPER), single_file=True),
})