load("@bazel_skylib//lib:paths.bzl", "paths")
-def _nfpm_package(
- package_name,
- config,
- exes,
- data,
- packager,
- os,
- arch,
-):
- extension_map = {
- "archlinux": "pkg.tar.zst",
- }
+def _nfpm_config_impl(ctx):
+ # ctx.file.template
distro_vars = {
"archlinux": {
"ENV_DIR": "/etc/conf.d",
"ENV_DIR": "/etc/default",
},
}
- extra_args = []
- _exes = []
- exe_suffix = ""
- if os != "linux" or arch != "amd64":
- exe_suffix = "_{}_{}".format(os, arch)
- extra_args.append("--platform={}".format(os))
- extra_args.append("--arch={}".format(arch))
-
- for exe in exes:
- if ":" not in exe:
- exe += ":" + paths.basename(exe)
- exe += exe_suffix
- _exes.append(exe)
-
- pkg_prefix = "{}_{}".format(package_name.replace("-", "_"), packager)
- if arch != "amd64":
- pkg_prefix += "_{}".format(arch)
- for target in data + _exes:
- extra_args.append('--include="$(location {})"'.format(target))
-
- for var, val in distro_vars.get(packager, {}).items():
- extra_args.append('--var="{}={}"'.format(var, val))
-
- native.genrule(
- name = "{}_nfpm_config".format(pkg_prefix),
- tools = [
- "//pkg/nfpmgen",
- ],
- srcs = [config] + data + _exes,
- outs = [
- "{}_nfpm_config.yaml".format(pkg_prefix),
- ],
- cmd_bash = """
- $(location //pkg/nfpmgen) --in="$(location {})" --out="$@" {}
- """.format(config, " ".join(extra_args)),
+
+ out_filename = ctx.file.template.basename[:-5] + "_{}_{}_{}.yaml".format(
+ ctx.attr.packager,
+ ctx.attr.os,
+ ctx.attr.arch,
+ )
+ outf = ctx.actions.declare_file(out_filename)
+
+ transitive_deps = []
+ for target in ctx.attr.data:
+ transitive_deps.append(depset(target.files.to_list()))
+
+ nfpmgen_args = [
+ "--verbose",
+ "--in", ctx.file.template.path,
+ "--out", outf.path,
+ ]
+ if ctx.attr.os != "linux" or ctx.attr.arch != "amd64":
+ nfpmgen_args.extend(["--platform", ctx.attr.os])
+ nfpmgen_args.extend(["--arch", ctx.attr.arch])
+ for dat in ctx.attr.data:
+ for f in dat.files.to_list():
+ nfpmgen_args.extend(["--include", f.path])
+
+ for var, val in distro_vars.get(ctx.attr.packager, {}).items():
+ nfpmgen_args.extend(['--var', '{}={}'.format(var, val)])
+
+ deps = depset([ctx.file.template], transitive = transitive_deps)
+
+ ctx.actions.run(
+ inputs = deps,
+ outputs = [outf],
+ executable = ctx.executable.nfpmgen,
+ arguments = nfpmgen_args,
+ )
+
+ return [
+ DefaultInfo(files = depset([outf]))
+ ]
+
+_nfpm_config = rule(
+ implementation = _nfpm_config_impl,
+ attrs = {
+ "template": attr.label(
+ doc = "base nfpm config",
+ allow_single_file = [".yaml"],
+ mandatory = True,
+ ),
+ "data": attr.label_list(
+ doc = "executables and data files to include in the package",
+ cfg = "target",
+ mandatory = False,
+ ),
+ "os": attr.string(
+ doc = "operating system (usually 'linux')",
+ default = "linux",
+ ),
+ "arch": attr.string(
+ doc = "architecture (usually 'amd64')",
+ default = "amd64",
+ ),
+ "packager": attr.string(
+ doc = "type of the package (deb, rpm, apk, archlinux)",
+ ),
+ "nfpmgen": attr.label(
+ doc = "nfpmgen executable (usually //pkg/nfpmgen)",
+ executable = True,
+ cfg = "exec",
+ default = Label("//pkg/nfpmgen:nfpmgen"),
+ ),
+ },
+)
+
+def _nfpm_package_impl(ctx):
+ pkg_ext = {
+ "archlinux": "pkg.tar.zst",
+ }.get(ctx.attr.packager, ctx.attr.packager)
+
+ name_arch_separator = {
+ "archlinux": "-",
+ "rpm": "-",
+ "deb": "_",
+ }.get(ctx.attr.packager)
+
+ pkg_arch = {
+ "archlinux": {
+ "amd64": "x86_64",
+ "arm64": "aarch64",
+ },
+ "rpm": {
+ "arm64": "aarch64",
+ },
+ }.get(ctx.attr.packager, {}).get(ctx.attr.arch, ctx.attr.arch)
+
+ output_filename = "{}{}{}.{}".format(
+ ctx.attr.package_name,
+ name_arch_separator,
+ pkg_arch,
+ pkg_ext,
)
- native.genrule(
- name = "{}_nfpm_pkg".format(pkg_prefix),
- tools = [
- "@com_github_goreleaser_nfpm_v2//cmd/nfpm:nfpm",
- ],
- srcs = [
- ":{}_nfpm_config".format(pkg_prefix),
- ] + data + _exes,
- outs = [
- "{}_{}.{}".format(package_name, arch, extension_map.get(packager, packager)),
- ],
- cmd = " ".join([
- "$(location @com_github_goreleaser_nfpm_v2//cmd/nfpm:nfpm)",
- "package",
- "-p {}".format(packager),
- "-f $(location :{}_nfpm_config)".format(pkg_prefix),
- "-t $@",
- ]),
+ output = ctx.actions.declare_file(output_filename)
+
+ nfpmgen_args = [
+ "package",
+ "-p", ctx.attr.packager,
+ "-f", ctx.file.config.path,
+ "-t", output.path,
+ ]
+
+ deps = depset([ctx.file.config], transitive = [])
+
+ ctx.actions.run(
+ inputs = deps,
+ outputs = [output],
+ executable = ctx.executable.nfpm,
+ arguments = nfpmgen_args,
)
+ return [
+ DefaultInfo(files = depset([output]))
+ ]
+
+
+_nfpm_package = rule(
+ implementation = _nfpm_package_impl,
+ attrs = {
+ "package_name": attr.string(
+ mandatory = True,
+ ),
+ "config": attr.label(
+ doc = "NFPM generated config",
+ mandatory = True,
+ allow_single_file = True,
+ ),
+ "os": attr.string(
+ doc = "operating system (usually 'linux')",
+ mandatory = True,
+ ),
+ "arch": attr.string(
+ doc = "architecture (usually 'amd64')",
+ mandatory = True,
+ ),
+ "packager": attr.string(
+ doc = "type of the package (deb, rpm, apk, archlinux)",
+ mandatory = True,
+ ),
+ "nfpm": attr.label(
+ doc = "nfpm executable",
+ executable = True,
+ cfg = "exec",
+ default = Label("@com_github_goreleaser_nfpm_v2//cmd/nfpm:nfpm"),
+ ),
+ },
+)
+
def nfpm_package(
- name, # type: str
config, # type: str
exes = [], # type: list[str]
data = [], # type: list[str]
linux_arm64 = False, # type: bool
):
- #for packager in ["deb", "archlinux", "rpm"]:
- for packager in ["archlinux", "apk", "deb"]:
- _nfpm_package(name, config, exes, data, packager, "linux", "amd64")
+ package_name = config.removesuffix(".yaml")
+ if ":" in package_name:
+ package_name = package_name[package_name.index(":")+1:]
+
+ # ensure all exe targets are fully qualified label names
+ exes = [
+ exe if ":" in exe
+ else "{}:{}".format(exe, paths.basename(exe))
+ for exe in exes
+ ]
- if linux_arm64:
- _nfpm_package(name, config, exes, data, packager, "linux", "arm64")
- _nfpm_package(name, config, exes, data, packager, "linux", "arm")
\ No newline at end of file
+ os_arch_pairs = [("linux", "amd64")]
+
+ if linux_arm64:
+ os_arch_pairs.append(("linux", "arm64"))
+
+ package_rules = []
+
+ for os, arch in os_arch_pairs:
+ for packager in ["archlinux", "deb", "rpm"]:
+ config_rule_name = "{}_{}_{}_{}_nfpm_config".format(package_name, packager, os, arch)
+ package_rule_name = "{}_{}_{}_{}_nfpm_pkg".format(package_name, packager, os, arch)
+
+ arch_exes = (
+ exes if os == "linux" and arch == "amd64"
+ else [
+ "{}_{}_{}".format(exe, os, arch)
+ for exe in exes
+ ]
+ )
+
+ _nfpm_config(
+ name = config_rule_name,
+ template = config,
+ data = arch_exes + data,
+ packager = packager,
+ os = os,
+ arch = arch,
+ )
+
+ _nfpm_package(
+ name = package_rule_name,
+ package_name = config[config.index(":")+1:-5],
+ config = ":{}".format(config_rule_name),
+ os = os,
+ arch = arch,
+ packager = packager,
+ )
+
+ package_rules.append(':{}'.format(package_rule_name))
+
+ native.filegroup(
+ name = package_name,
+ srcs = package_rules
+ )