Skip to content

Commit 056e03d

Browse files
committed
fix(2.x): alias copy_[to_]directory_bin_action to bazel_lib
1 parent ae72743 commit 056e03d

File tree

7 files changed

+37
-177
lines changed

7 files changed

+37
-177
lines changed

docs/copy_file.md

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/copy_to_bin.md

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/copy_file.bzl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,10 @@ This fork of bazel-skylib's copy_file adds `DirectoryPathInfo` support and allow
3030

3131
load(
3232
"@bazel_lib//lib:copy_file.bzl",
33-
_COPY_FILE_TOOLCHAINS = "COPY_FILE_TOOLCHAINS",
3433
_copy_file = "copy_file",
3534
_copy_file_action = "copy_file_action",
3635
)
3736

3837
copy_file = _copy_file
3938
copy_file_action = _copy_file_action
40-
COPY_FILE_TOOLCHAINS = _COPY_FILE_TOOLCHAINS
39+
COPY_FILE_TOOLCHAINS = ["@aspect_bazel_lib//lib:coreutils_toolchain_type"]

lib/private/BUILD.bazel

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ bzl_library(
3232
srcs = ["copy_directory.bzl"],
3333
visibility = ["//lib:__subpackages__"],
3434
deps = [
35-
":copy_common",
3635
":platform_utils",
36+
"@bazel_lib//lib:copy_directory",
3737
],
3838
)
3939

@@ -52,11 +52,8 @@ bzl_library(
5252
srcs = ["copy_to_directory.bzl"],
5353
visibility = ["//lib:__subpackages__"],
5454
deps = [
55-
":copy_common",
5655
":directory_path",
57-
":glob_match",
58-
":paths",
59-
":platform_utils",
56+
"@bazel_lib//lib:copy_to_directory",
6057
"@bazel_skylib//lib:paths",
6158
],
6259
)
@@ -267,12 +264,6 @@ bzl_library(
267264
visibility = ["//lib:__subpackages__"],
268265
)
269266

270-
bzl_library(
271-
name = "copy_common",
272-
srcs = ["copy_common.bzl"],
273-
visibility = ["//lib:__subpackages__"],
274-
)
275-
276267
bzl_library(
277268
name = "coreutils_toolchain",
278269
srcs = ["coreutils_toolchain.bzl"],

lib/private/copy_common.bzl

Lines changed: 0 additions & 9 deletions
This file was deleted.

lib/private/copy_directory.bzl

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This rule copies a directory to another location using a precompiled binary.
44
"""
55

6-
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS")
6+
load("@bazel_lib//lib:copy_directory.bzl", _copy_directory_bin_action = "copy_directory_bin_action")
77

88
def copy_directory_bin_action(
99
ctx,
@@ -43,32 +43,15 @@ def copy_directory_bin_action(
4343
See the caveats above about interactions with remote execution and caching.
4444
4545
"""
46-
args = [
47-
src.path,
48-
dst.path,
49-
]
50-
if verbose:
51-
args.append("--verbose")
52-
53-
if hardlink == "on":
54-
args.append("--hardlink")
55-
elif hardlink == "auto" and not src.is_source:
56-
args.append("--hardlink")
57-
58-
if preserve_mtime:
59-
args.append("--preserve-mtime")
60-
61-
ctx.actions.run(
62-
inputs = [src],
63-
outputs = [dst],
64-
executable = copy_directory_bin,
65-
arguments = args,
66-
# TODO: Drop this after https://github.com/bazel-contrib/bazel-lib/issues/1146
67-
env = {"GODEBUG": "winsymlink=0"},
68-
mnemonic = "CopyDirectory",
69-
progress_message = "Copying directory %{input}",
70-
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
71-
toolchain = copy_directory_toolchain,
46+
_copy_directory_bin_action(
47+
ctx,
48+
src = src,
49+
dst = dst,
50+
copy_directory_bin = copy_directory_bin,
51+
copy_directory_toolchain = copy_directory_toolchain,
52+
hardlink = hardlink,
53+
verbose = verbose,
54+
preserve_mtime = preserve_mtime,
7255
)
7356

7457
def _copy_directory_impl(ctx):

lib/private/copy_to_directory.bzl

Lines changed: 20 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
"copy_to_directory implementation"
22

3-
load(":copy_common.bzl", _COPY_EXECUTION_REQUIREMENTS = "COPY_EXECUTION_REQUIREMENTS")
3+
load("@bazel_lib//lib:copy_to_directory.bzl", _copy_to_directory_bin_action = "copy_to_directory_bin_action")
44
load(":directory_path.bzl", "DirectoryPathInfo")
5-
load(":paths.bzl", "paths")
65

76
_filter_transforms_order_docstring = """Filters and transformations are applied in the following order:
87
@@ -312,19 +311,6 @@ def _copy_to_directory_impl(ctx):
312311
),
313312
]
314313

315-
def _expand_src_packages_patterns(patterns, package):
316-
result = []
317-
for pattern in patterns:
318-
if pattern.startswith("."):
319-
if not package and pattern.startswith("./"):
320-
# special case in the root package
321-
result.append(pattern[2:])
322-
else:
323-
result.append(package + pattern[1:])
324-
else:
325-
result.append(pattern)
326-
return result
327-
328314
def copy_to_directory_bin_action(
329315
ctx,
330316
name,
@@ -407,115 +393,25 @@ def copy_to_directory_bin_action(
407393
408394
verbose: If true, prints out verbose logs to stdout
409395
"""
410-
411-
# Replace "." in root_paths with the package name of the target
412-
root_paths = [p if p != "." else ctx.label.package for p in root_paths]
413-
414-
# Replace a leading "." with the package name of the target in include_srcs_packages & exclude_srcs_packages
415-
include_srcs_packages = _expand_src_packages_patterns(include_srcs_packages, ctx.label.package)
416-
exclude_srcs_packages = _expand_src_packages_patterns(exclude_srcs_packages, ctx.label.package)
417-
418-
if not include_srcs_packages:
419-
fail("An empty 'include_srcs_packages' list will exclude all srcs and result in an empty directory")
420-
421-
if "**" in exclude_srcs_packages:
422-
fail("A '**' glob pattern in 'exclude_srcs_packages' will exclude all srcs and result in an empty directory")
423-
424-
if not include_srcs_patterns:
425-
fail("An empty 'include_srcs_patterns' list will exclude all srcs and result in an empty directory")
426-
427-
if "**" in exclude_srcs_patterns:
428-
fail("A '**' glob pattern in 'exclude_srcs_patterns' will exclude all srcs and result in an empty directory")
429-
430-
for replace_prefix in replace_prefixes.keys():
431-
if replace_prefix.endswith("**"):
432-
msg = "replace_prefix '{}' must not end with '**' glob expression".format(replace_prefix)
433-
fail(msg)
434-
435-
files_and_targets = []
436-
for f in files:
437-
files_and_targets.append(struct(
438-
file = f,
439-
path = f.path,
440-
root_path = f.root.path,
441-
short_path = f.short_path,
442-
workspace_path = paths.to_repository_relative_path(f),
443-
))
444-
for t in targets:
445-
if not DirectoryPathInfo in t:
446-
continue
447-
files_and_targets.append(struct(
448-
file = t[DirectoryPathInfo].directory,
449-
path = "/".join([t[DirectoryPathInfo].directory.path, t[DirectoryPathInfo].path]),
450-
root_path = t[DirectoryPathInfo].directory.root.path,
451-
short_path = "/".join([t[DirectoryPathInfo].directory.short_path, t[DirectoryPathInfo].path]),
452-
workspace_path = "/".join([paths.to_repository_relative_path(t[DirectoryPathInfo].directory), t[DirectoryPathInfo].path]),
453-
))
454-
455-
file_infos = []
456-
file_inputs = []
457-
for f in files_and_targets:
458-
if not f.file.owner:
459-
msg = "Expected an owner target label for file {} but found none".format(f)
460-
fail(msg)
461-
462-
if f.file.owner.package == None:
463-
msg = "Expected owner target label for file {} to have a package name but found None".format(f)
464-
fail(msg)
465-
466-
if f.file.owner.workspace_name == None:
467-
msg = "Expected owner target label for file {} to have a workspace name but found None".format(f)
468-
fail(msg)
469-
470-
hardlink_file = False
471-
if hardlink == "on":
472-
hardlink_file = True
473-
elif hardlink == "auto":
474-
hardlink_file = not f.file.is_source
475-
476-
file_infos.append({
477-
"package": f.file.owner.package,
478-
"path": f.path,
479-
"root_path": f.root_path,
480-
"short_path": f.short_path,
481-
"workspace": f.file.owner.workspace_name,
482-
"workspace_path": f.workspace_path,
483-
"hardlink": hardlink_file,
484-
})
485-
file_inputs.append(f.file)
486-
487-
config = {
488-
"allow_overwrites": allow_overwrites,
489-
"dst": dst.path,
490-
"exclude_srcs_packages": exclude_srcs_packages,
491-
"exclude_srcs_patterns": exclude_srcs_patterns,
492-
"files": file_infos,
493-
"include_external_repositories": include_external_repositories,
494-
"include_srcs_packages": include_srcs_packages,
495-
"include_srcs_patterns": include_srcs_patterns,
496-
"replace_prefixes": replace_prefixes,
497-
"root_paths": root_paths,
498-
"preserve_mtime": preserve_mtime,
499-
"verbose": verbose,
500-
}
501-
502-
config_file = ctx.actions.declare_file("{}_config.json".format(name))
503-
ctx.actions.write(
504-
output = config_file,
505-
content = json.encode_indent(config),
506-
)
507-
508-
ctx.actions.run(
509-
inputs = file_inputs + [config_file],
510-
outputs = [dst],
511-
executable = copy_to_directory_bin,
512-
arguments = [config_file.path, ctx.label.workspace_name],
513-
# TODO: Drop this after https://github.com/bazel-contrib/bazel-lib/issues/1146
514-
env = {"GODEBUG": "winsymlink=0"},
515-
mnemonic = "CopyToDirectory",
516-
progress_message = "Copying files to directory %{output}",
517-
execution_requirements = _COPY_EXECUTION_REQUIREMENTS,
518-
toolchain = copy_to_directory_toolchain,
396+
return _copy_to_directory_bin_action(
397+
ctx,
398+
name = name,
399+
dst = dst,
400+
copy_to_directory_bin = copy_to_directory_bin,
401+
copy_to_directory_toolchain = copy_to_directory_toolchain,
402+
files = files,
403+
targets = targets,
404+
root_paths = root_paths,
405+
include_external_repositories = include_external_repositories,
406+
include_srcs_packages = include_srcs_packages,
407+
exclude_srcs_packages = exclude_srcs_packages,
408+
include_srcs_patterns = include_srcs_patterns,
409+
exclude_srcs_patterns = exclude_srcs_patterns,
410+
replace_prefixes = replace_prefixes,
411+
allow_overwrites = allow_overwrites,
412+
hardlink = hardlink,
413+
preserve_mtime = preserve_mtime,
414+
verbose = verbose,
519415
)
520416

521417
copy_to_directory_lib = struct(

0 commit comments

Comments
 (0)