aspect-build / bazel-lib

Common useful functions for writing BUILD files and Starlark macros/rules
https://docs.aspect.build/rules/aspect_bazel_lib
Apache License 2.0
128 stars 71 forks source link

[FR]: Better support for external directory files in copy_to_directory #347

Open pullard opened 1 year ago

pullard commented 1 year ago

What is the current behavior?

Here's a simple use case where I would like to copy an external folder from our node_modules: <npm_pkg_dir>/dist/fonts => <package_name>/material-design-icons-iconfont.

Currently, I seem to be unable to figure out which combinations of props will give me the desired result. Let's say I use the package material-design-icons-iconfont@6.7.0 the prefix would need to be hardcoded:

Currently:

copy_to_directory(
    name = "material-design-icons",
    srcs = [
        "//:node_modules/material-design-icons-iconfont/dir",
    ],
    out = "material-design-icons-iconfont",
    replace_prefixes = {
        # copies an empty `node_modules` folder, not sure where this comes from
        "node_modules": "",
        # would expect to use $(location ...) here
        "node_modules/.aspect_rules_js/material-design-icons-iconfont@6.7.0/node_modules/material-design-icons-iconfont/dist/fonts": "",
    },
)

Expected:

copy_to_directory(
    name = "material-design-icons",
    srcs = [
        "//:node_modules/material-design-icons-iconfont/dir",
    ],
    out = "material-design-icons-iconfont",
    replace_prefixes = {
        "$(location //:node_modules/material-design-icons-iconfont/dir)/dist/fonts": "",
    },
)

# or

copy_to_directory(
    name = "material-design-icons",
    srcs = [
        "//:node_modules/material-design-icons-iconfont/dir",
    ],
    out = "material-design-icons-iconfont",
    root_paths = [
        "$(location //:node_modules/material-design-icons-iconfont/dir)",
    ],
    replace_prefixes = {
        "dist/fonts": "",
    },
)

Describe the feature

I would like to avoid having to compute the path to strip or hardcoding the value. Possibly there is a better way to approach this copy operation? I have tried using root_paths as well with no success, either way, it would be great to have a convenient way of performing this operation.

Fund our work

pullard commented 1 month ago

Any ideas or workarounds in the wild to clean this macro up?