Skip to content

Commit

Permalink
feat: introduce release_archive rule (#211)
Browse files Browse the repository at this point in the history
- The `release_archive` rule produces an archive file that preserves the
permissions on the provided files. The `pkg_tar` rule does not.
- Remove `rules_pkg`.
- Update `bazel-starlib` release to use `release_archive`.

Related to #200.
  • Loading branch information
cgrindel authored Feb 1, 2023
1 parent aacd773 commit c9e7856
Show file tree
Hide file tree
Showing 36 changed files with 166 additions and 223 deletions.
17 changes: 0 additions & 17 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load("@bazel_gazelle//:def.bzl", "DEFAULT_LANGUAGES", "gazelle", "gazelle_binary")
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files")
load(
"//bzlformat:defs.bzl",
"bzlformat_missing_pkgs",
Expand Down Expand Up @@ -115,12 +114,6 @@ filegroup(
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
visibility = ["//:__subpackages__"],
)

# All of the packages that should be included in the release archive and made
# available to the child workspaces.
_RUNTIME_PKGS = [
Expand Down Expand Up @@ -161,16 +154,6 @@ filegroup(
],
visibility = [
":integration_test_visibility",
],
)

pkg_filegroup(
name = "release_filegroup",
srcs = [
pkg + ":release_files"
for pkg in _RUNTIME_PKGS
],
visibility = [
"//:__subpackages__",
],
)
Expand Down
4 changes: 0 additions & 4 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories")

stardoc_repositories()

load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")

rules_pkg_dependencies()

# MARK: - Prebuilt Buildtools Deps

load("@buildifier_prebuilt//:deps.bzl", "buildifier_prebuilt_deps")
Expand Down
8 changes: 0 additions & 8 deletions bazeldoc/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("//bzlformat:defs.bzl", "bzlformat_pkg")

package(default_visibility = ["//visibility:public"])
Expand Down Expand Up @@ -30,10 +29,3 @@ filegroup(
srcs = glob(["*"]),
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
prefix = package_name(),
visibility = ["//:__subpackages__"],
)
8 changes: 0 additions & 8 deletions bazeldoc/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("//bzlformat:defs.bzl", "bzlformat_pkg")

package(default_visibility = ["//bazeldoc:__subpackages__"])
Expand Down Expand Up @@ -57,13 +56,6 @@ filegroup(
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
prefix = package_name(),
visibility = ["//:__subpackages__"],
)

bzl_library(
name = "doc_utilities",
srcs = ["doc_utilities.bzl"],
Expand Down
8 changes: 0 additions & 8 deletions build/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_pkg")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")

bzlformat_pkg(name = "bzlformat")

Expand All @@ -10,13 +9,6 @@ filegroup(
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
prefix = package_name(),
visibility = ["//:__subpackages__"],
)

string_flag(
name = "execution_env",
build_setting_default = "normal",
Expand Down
8 changes: 0 additions & 8 deletions bzlformat/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("//bzlformat/private:bzlformat_pkg.bzl", "bzlformat_pkg")

package(default_visibility = ["//visibility:public"])
Expand Down Expand Up @@ -30,10 +29,3 @@ filegroup(
srcs = glob(["*"]),
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
prefix = package_name(),
visibility = ["//:__subpackages__"],
)
8 changes: 0 additions & 8 deletions bzlformat/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load(":bzlformat_pkg.bzl", "bzlformat_pkg")

package(default_visibility = ["//bzlformat:__subpackages__"])
Expand Down Expand Up @@ -52,10 +51,3 @@ filegroup(
srcs = glob(["*"]),
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
prefix = package_name(),
visibility = ["//:__subpackages__"],
)
8 changes: 0 additions & 8 deletions bzlformat/tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("//bzlformat:defs.bzl", "bzlformat_pkg")

# MARK: - Binaries
Expand All @@ -24,10 +23,3 @@ filegroup(
srcs = glob(["*"]),
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
prefix = package_name(),
visibility = ["//:__subpackages__"],
)
8 changes: 0 additions & 8 deletions bzlformat/tools/missing_pkgs/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("//bzlformat:defs.bzl", "bzlformat_pkg")

bzlformat_pkg(name = "bzlformat")
Expand Down Expand Up @@ -46,10 +45,3 @@ filegroup(
srcs = glob(["*"]),
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
prefix = package_name(),
visibility = ["//:__subpackages__"],
)
8 changes: 0 additions & 8 deletions bzllib/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("//bzlformat:defs.bzl", "bzlformat_pkg")

package(default_visibility = ["//visibility:public"])
Expand Down Expand Up @@ -31,10 +30,3 @@ filegroup(
srcs = glob(["*"]),
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
prefix = package_name(),
visibility = ["//:__subpackages__"],
)
8 changes: 0 additions & 8 deletions bzllib/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("//bzlformat:defs.bzl", "bzlformat_pkg")

package(default_visibility = ["//bzllib:__subpackages__"])
Expand All @@ -14,13 +13,6 @@ filegroup(
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
prefix = package_name(),
visibility = ["//:__subpackages__"],
)

bzl_library(
name = "bazel_labels",
srcs = ["bazel_labels.bzl"],
Expand Down
9 changes: 1 addition & 8 deletions bzlrelease/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("//bzlformat:defs.bzl", "bzlformat_pkg")

package(default_visibility = ["//visibility:public"])
Expand All @@ -20,6 +19,7 @@ bzl_library(
"//bzlrelease/private:generate_release_notes",
"//bzlrelease/private:generate_workspace_snippet",
"//bzlrelease/private:hash_sha256",
"//bzlrelease/private:release_archive",
"//bzlrelease/private:update_readme",
],
)
Expand All @@ -31,10 +31,3 @@ filegroup(
srcs = glob(["*"]),
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
prefix = package_name(),
visibility = ["//:__subpackages__"],
)
6 changes: 6 additions & 0 deletions bzlrelease/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ load(
_generate_workspace_snippet = "generate_workspace_snippet",
)
load("//bzlrelease/private:hash_sha256.bzl", _hash_sha256 = "hash_sha256")
load(
"//bzlrelease/private:release_archive.bzl",
_release_archive = "release_archive",
)
load(
"//bzlrelease/private:update_readme.bzl",
_update_readme = "update_readme",
Expand All @@ -27,3 +31,5 @@ generate_release_notes = _generate_release_notes
hash_sha256 = _hash_sha256

update_readme = _update_readme

release_archive = _release_archive
13 changes: 5 additions & 8 deletions bzlrelease/private/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("//bzlformat:defs.bzl", "bzlformat_pkg")

package(default_visibility = ["//bzlrelease:__subpackages__"])
Expand Down Expand Up @@ -46,14 +45,12 @@ filegroup(
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
prefix = package_name(),
visibility = ["//:__subpackages__"],
)

bzl_library(
name = "hash_sha256",
srcs = ["hash_sha256.bzl"],
)

bzl_library(
name = "release_archive",
srcs = ["release_archive.bzl"],
)
63 changes: 63 additions & 0 deletions bzlrelease/private/release_archive.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
"""Definition for `release_archive` rule."""

def _release_archive_impl(ctx):
out_basename = ctx.attr.out
if out_basename == "":
out_basename = "{name}{ext}".format(
name = ctx.label.name,
ext = ctx.attr.ext,
)
out = ctx.actions.declare_file(out_basename)

# Write the file list to a file
file_list_out = ctx.actions.declare_file(ctx.label.name + "_files")
file_list_args = ctx.actions.args()
file_list_args.add_all(ctx.files.srcs)
ctx.actions.write(
output = file_list_out,
content = file_list_args,
)

# Create the archive
args = ctx.actions.args()
args.add(out)
args.add(file_list_out)
ctx.actions.run_shell(
outputs = [out],
inputs = [file_list_out] + ctx.files.srcs,
arguments = [args],
command = """\
archive="$1"
file_list="$2"
shift 1
tar 2>/dev/null -hczvf "$archive" -T "${file_list}"
""",
)
return DefaultInfo(
files = depset([out]),
runfiles = ctx.runfiles(files = [out]),
)

release_archive = rule(
implementation = _release_archive_impl,
attrs = {
"ext": attr.string(
default = ".tar.gz",
doc = "The extension for the archive.",
),
"out": attr.string(
doc = "The name of the output file.",
),
"srcs": attr.label_list(
allow_files = True,
mandatory = True,
),
},
doc = """\
Create a source release archive.
This rule uses `tar` to collect and compress files into an archive file \
suitable for use as a release artifact. Any permissions on the source files \
will be preserved.
""",
)
8 changes: 0 additions & 8 deletions bzlrelease/tools/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")
load("//bzlformat:defs.bzl", "bzlformat_pkg")

bzlformat_pkg(name = "bzlformat")
Expand Down Expand Up @@ -115,10 +114,3 @@ filegroup(
srcs = glob(["*"]),
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
prefix = package_name(),
visibility = ["//:__subpackages__"],
)
3 changes: 2 additions & 1 deletion bzlrelease/tools/generate_sha256.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ case "${utility}" in
openssl)
function sumsha256() {
# On Ubuntu, we can see a prefix of '(stdin)= '
openssl dgst -sha256 | sed -E 's|^\(stdin\)= (.*)|\1|g'
# 2023-02-01: This has recently changed to be 'SHA2-256(stdin)='.
openssl dgst -sha256 | sed -E 's|^.*\(stdin\)= (.*)|\1|g'
}
;;
*)
Expand Down
8 changes: 0 additions & 8 deletions bzltidy/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@cgrindel_bazel_starlib//bzlformat:defs.bzl", "bzlformat_pkg")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")

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

Expand All @@ -26,10 +25,3 @@ filegroup(
srcs = glob(["*"]),
visibility = ["//:__subpackages__"],
)

pkg_files(
name = "release_files",
srcs = [":all_files"],
prefix = package_name(),
visibility = ["//:__subpackages__"],
)
Loading

0 comments on commit c9e7856

Please sign in to comment.