Skip to content

Commit

Permalink
overlay repositories: resolve labels before downloading (#1349)
Browse files Browse the repository at this point in the history
Fixes #1338
  • Loading branch information
jayconrod authored Feb 23, 2018
1 parent e508ca0 commit 3e6e9bb
Showing 1 changed file with 20 additions and 6 deletions.
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

0 comments on commit 3e6e9bb

Please sign in to comment.