Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overlay repositories: resolve labels before downloading #1349

Merged
merged 1 commit into from
Feb 23, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions go/private/tools/overlay_repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@
# recursive workspaces.

def _http_archive_impl(ctx):
overlay = _resolve_overlay(ctx, ctx.attr.overlay)
ctx.download_and_extract(
url = ctx.attr.urls,
sha256 = ctx.attr.sha256,
type = ctx.attr.type,
stripPrefix = ctx.attr.strip_prefix,
)

_apply_overlay(ctx, ctx.attr.overlay)
_apply_overlay(ctx, overlay)

http_archive = repository_rule(
implementation = _http_archive_impl,
Expand All @@ -57,12 +57,14 @@ def _git_repository_impl(ctx):
if ctx.attr.commit and ctx.attr.tag:
fail("'commit' and 'tag' may not both be specified")

overlay = _resolve_overlay(ctx, ctx.attr.overlay)

# TODO(jayconrod): sanitize inputs passed to git.
revision = ctx.attr.commit if ctx.attr.commit else ctx.attr.tag
_check_execute(ctx, ["git", "clone", "-n", ctx.attr.remote, "."], "failed to clone %s" % ctx.attr.remote)
_check_execute(ctx, ["git", "checkout", revision], "failed to checkout revision %s in remote %s" % (revision, ctx.attr.remote))

_apply_overlay(ctx, ctx.attr.overlay)
_apply_overlay(ctx, overlay)

git_repository = repository_rule(
implementation = _git_repository_impl,
Expand All @@ -74,11 +76,23 @@ git_repository = repository_rule(
},
)

def _resolve_overlay(ctx, overlay):
"""Resolve overlay labels to paths.
This should be done before downloading the repository, since it may
trigger restarts.
"""
return [(ctx.path(src_label), dst_rel) for src_label, dst_rel in overlay.items()]

def _apply_overlay(ctx, overlay):
"""Copies overlay files into the repository.
This should be done after downloading the repository, since it may replace
downloaded files.
"""
# TODO(jayconrod): sanitize destination paths.
for src_label, dst_rel in overlay.items():
src_path = ctx.path(src_label)
_check_execute(ctx, ["cp", src_path, dst_rel], "failed to copy file from %s" % src_label)
for src_path, dst_rel in overlay:
_check_execute(ctx, ["cp", src_path, dst_rel], "failed to copy file from %s" % src_path)

def _check_execute(ctx, arguments, message):
res = ctx.execute(arguments)
Expand Down