diff --git a/e2e/MODULE.bazel.lock b/e2e/MODULE.bazel.lock index b9023adbc979a14356c1751a231718636aaf15c4..90d6ffea93b05d123c8ba9c54cc300447970b3ab 100644 --- a/e2e/MODULE.bazel.lock +++ b/e2e/MODULE.bazel.lock @@ -10557,7 +10557,7 @@ "moduleExtensions": { "@@ape~1.0.0-alpha.5//:MODULE.bazel%_repo_rules": { "general": { - "bzlTransitiveDigest": "6pR3uBIwJUKWxgVyaTrCm23UmfZ4GxKfaMrBvcMxfPQ=", + "bzlTransitiveDigest": "rEjsXjwdJ5htW0Z2AQPbs69xCmvYT0V2dTbImgUj3R8=", "accumulatedFileDigests": {}, "envVariables": {}, "generatedRepoSpecs": { @@ -14037,7 +14037,7 @@ }, "@@rules_coreutils~1.0.0-alpha.7//:MODULE.bazel%_repo_rules": { "general": { - "bzlTransitiveDigest": "Y6Ql6Fc54/lZohdaKTW25feZ8locHF8d8fo/tu9MIUs=", + "bzlTransitiveDigest": "2rH36ziZXnIpRhGHr1GfziDIYsMvqDzRRfkq0AL1rv4=", "accumulatedFileDigests": {}, "envVariables": {}, "generatedRepoSpecs": { diff --git a/e2e/extract/BUILD.bazel b/e2e/extract/BUILD.bazel new file mode 100644 index 0000000000000000000000000000000000000000..89b2edd712d3297d49e632cc9e10791ac62595dd --- /dev/null +++ b/e2e/extract/BUILD.bazel @@ -0,0 +1,40 @@ +load("@rules_diff//diff/file/test:defs.bzl", "diff_file_test") +load("@rules_tar//tar/extract:defs.bzl", "tar_extract") + +tar_extract( + name = "goodbye-world", + src = "//fixture:fixture.tar", + outs = [ + "fixture/goodbye-world.txt", + ], +) + +diff_file_test( + name = "goodbye-world-diff", + size = "small", + a = "//fixture:goodbye-world.txt", + b = ":goodbye-world", +) + +tar_extract( + name = "multiple", + src = "//fixture:fixture.tar", + outs = [ + "fixture/hello-world.txt", + "fixture/nested/goodbye-world.txt", + ], +) + +diff_file_test( + name = "hello-world-diff", + size = "small", + a = "//fixture:hello-world.txt", + b = ":fixture/hello-world.txt", +) + +diff_file_test( + name = "nested-goodbye-world-diff", + size = "small", + a = "//fixture/nested:goodbye-world.txt", + b = ":fixture/nested/goodbye-world.txt", +) diff --git a/tar/extract/BUILD.bazel b/tar/extract/BUILD.bazel new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/tar/extract/defs.bzl b/tar/extract/defs.bzl new file mode 100644 index 0000000000000000000000000000000000000000..6959875dc0091f8bb112b2edfbf1feb72784fe3b --- /dev/null +++ b/tar/extract/defs.bzl @@ -0,0 +1,5 @@ +load(":rule.bzl", _extract = "tar_extract") + +visibility("public") + +tar_extract = _extract diff --git a/tar/extract/rule.bzl b/tar/extract/rule.bzl new file mode 100644 index 0000000000000000000000000000000000000000..4291d543198582a007dd23617c23fd1ab5f7064d --- /dev/null +++ b/tar/extract/rule.bzl @@ -0,0 +1,61 @@ +visibility("//tar/...") + +DOC = """Extracts files from a tarball + +```py +tar_extract( + name = "extracted", + src = ":archive.tar", + outs = [ + "some/member.txt", + "some/other/member.txt", + ] +) +``` + +The `outs` labels `:some/member.txt` and `:some/other/member.txt` can be used in subsequent rules. +""" + +ATTRS = { + "src": attr.label( + doc = "An archive to extract decleared files from.", + mandatory = True, + allow_single_file = [".tar"], + ), + "outs": attr.output_list( + doc = "Paths within the archive to extract.", + mandatory = True, + allow_empty = False, + ), +} + +def _name(label): + return label.name + +def implementation(ctx): + tar = ctx.toolchains["//tar/toolchain/tar:type"] + + args = ctx.actions.args() + args.add("-C").add("{}/{}".format(ctx.bin_dir.path, ctx.label.package)) + args.add("-xvf").add(ctx.file.src.path) + args.add_all(ctx.attr.outs, map_each = _name) + + ctx.actions.run( + outputs = ctx.outputs.outs, + inputs = [ctx.file.src], + arguments = [args], + executable = tar.executable, + tools = [tar.executable], + mnemonic = "TarExtract", + progress_message = "extracting %{input} files", + toolchain = "//tar/toolchain/tar:type", + ) + + return DefaultInfo(files = depset(ctx.outputs.outs)) + +tar_extract = rule( + doc = DOC, + attrs = ATTRS, + implementation = implementation, + toolchains = ["//tar/toolchain/tar:type"], +) diff --git a/tar/unpack/file/rule.bzl b/tar/unpack/file/rule.bzl deleted file mode 100644 index aa95bb8272acd5e87758d9a801a7c4a6679ce277..0000000000000000000000000000000000000000 --- a/tar/unpack/file/rule.bzl +++ /dev/null @@ -1,48 +0,0 @@ -visibility("//tar/...") - -DOC = """unpacks files from a tarball -```py -tar_unpack_file( - - name = "extracted", - src = ":archive.tar", -) -``` -""" - -ATTRS = { - "src": attr.label( - doc = "An archive to extract into a declared directory (`TreeArtifact`).", - mandatory = True, - allow_single_file = [".tar"], - ), -} - -def implementation(ctx): - tar = ctx.toolchains["//tar/toolchain/tar:type"] - - output_dir = ctx.actions.declare_directory(ctx.attr.name) - - args = ctx.actions.args() - args.add("-xvf").add(ctx.file.src.path) - args.add("-C").add(output_dir.path) - - ctx.actions.run( - outputs = [output_dir], - inputs = [ctx.file.src], - arguments = [args], - executable = tar.executable, - tools = [tar.executable], - mnemonic = "TarUnpack", - progress_message = "unpacking %{inputs}", - ) - - depset_files = depset([output_dir]) - return DefaultInfo(files = depset_files) - -tar_unpack_file = rule( - doc = DOC, - attrs = ATTRS, - implementation = implementation, - toolchains = ["//tar/toolchain/tar:type"], -)