|
1 | 1 | "copy_to_directory implementation" |
2 | 2 |
|
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") |
4 | 4 | load(":directory_path.bzl", "DirectoryPathInfo") |
5 | | -load(":paths.bzl", "paths") |
6 | 5 |
|
7 | 6 | _filter_transforms_order_docstring = """Filters and transformations are applied in the following order: |
8 | 7 |
|
@@ -312,19 +311,6 @@ def _copy_to_directory_impl(ctx): |
312 | 311 | ), |
313 | 312 | ] |
314 | 313 |
|
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 | | - |
328 | 314 | def copy_to_directory_bin_action( |
329 | 315 | ctx, |
330 | 316 | name, |
@@ -407,115 +393,25 @@ def copy_to_directory_bin_action( |
407 | 393 |
|
408 | 394 | verbose: If true, prints out verbose logs to stdout |
409 | 395 | """ |
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, |
519 | 415 | ) |
520 | 416 |
|
521 | 417 | copy_to_directory_lib = struct( |
|
0 commit comments