Search code examples
bazel

How to modify just one file when repackaging an archive?


I'm trying to take a non-Bazel produced zipfile, modify some files in it, keeping most of them alone, and then ultimately produce a new tarball with the ~original content (plus my modifications)

I'm having trouble specifying my rules in a clean way and it'd be great if there was a suggestion on how to do it.

I'm importing the original zip file via the 'new_http_archive' WORKSPACE rule. This works pretty well. I put the build file in a package one level under the root. Let's call this 'foo_repackage'.

In foo_repackage/BUILD.root_archive:

package(default_visibility = ["//visibility:public"])

filegroup(
    name = "all_files",
    srcs = glob(
        ["**"],
        exclude = ["*", "share/doc/api/**"]
    ),
)

The bigger issue is in the foo_repackage/BUILD file, I'd like to take all of the files out of the all_files group above, except for a few of them that I will modify. I can't see how to do this easily. It seems like every file that I want to modify I should exclude from the above glob and make a new rule that specifies that file. This means that I have to keep modifying the global all_files exclude rule.

If I could create a new filegroup that was all of the above files with some files excluded, that'd be ideal.

I should mention that the last step is of course to use pkg_tar to repackage the result - this is in foo_repackage/BUILD

pkg_tar(
    name = "OutputTarball",
    files = ["@root_archive//:all_files"],
    deps = [":layers_of_modified_files"],
    strip_prefix = "/../root_archive",
)

Does anyone have a better way to do this?

Thanks, Sean


Solution

  • Could you use a variable like:

    MODIFIABLE_FILES = [
        "some/file",
        "another/file",
        ...
    ]
    
    filegroup(
        name = "static-files",
        srcs = glob(["**"], exclude = MODIFIABLE_FILES)
    )
    
    filegroup(
        name = "modifiable-files",
        srcs = MODIFIABLE_FILES,
    )
    

    Then the list of static files and modifiable files will be kept in sync and you'll get a build error if you accidentally specify a non-existent modifiable file.