Skip to content

Commit f76f794

Browse files
Merge pull request #119 from kellyma2/null-rpms
Add null repository rule for missing RPMs
2 parents b1e6dcb + f58234d commit f76f794

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

bazeldnf/extensions.bzl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ based on: https://github.com/bazel-contrib/rules-template/blob/0dadcb716f06f6728
77

88
load("@bazel_features//:features.bzl", "bazel_features")
99
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_jar")
10-
load("//internal:rpm.bzl", rpm_repository = "rpm")
10+
load("//internal:rpm.bzl", null_rpm_repository = "null_rpm", rpm_repository = "rpm")
1111
load(":repositories.bzl", "bazeldnf_register_toolchains")
1212

1313
_DEFAULT_NAME = "bazeldnf"
@@ -159,6 +159,10 @@ _alias_repository = repository_rule(
159159
},
160160
)
161161

162+
def _to_rpm_repo_name(prefix, rpm_name):
163+
name = rpm_name.replace("+", "plus")
164+
return "{}{}".format(prefix, name)
165+
162166
def _handle_lock_file(config, module_ctx, registered_rpms = {}):
163167
if not config.lock_file:
164168
fail("No lock file provided for %s" % config.name)
@@ -200,8 +204,7 @@ def _handle_lock_file(config, module_ctx, registered_rpms = {}):
200204
fail("invalid entry in %s for %s" % (config.lock_file, rpm_name))
201205
rpm_name = urls[0].rsplit("/", 1)[-1]
202206

203-
name = rpm_name.replace("+", "plus")
204-
name = "{}{}".format(config.rpm_repository_prefix, name)
207+
name = _to_rpm_repo_name(config.rpm_repository_prefix, rpm_name)
205208
if name in registered_rpms:
206209
continue
207210
registered_rpms[name] = 1
@@ -218,6 +221,16 @@ def _handle_lock_file(config, module_ctx, registered_rpms = {}):
218221
**rpm
219222
)
220223

224+
# if there's targets without matching RPMs we need to create a null target
225+
# so that consumers have something consistent that they can depend on
226+
for target in lock_file_json.get("targets", []):
227+
name = _to_rpm_repo_name(config.rpm_repository_prefix, target)
228+
if name in registered_rpms:
229+
continue
230+
231+
null_rpm_repository(name = name)
232+
registered_rpms[name] = 1
233+
221234
repository_args["rpms"] = ["@@%s//rpm" % x for x in registered_rpms.keys()]
222235

223236
_alias_repository(

internal/rpm.bzl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,35 @@ rpm = repository_rule(
106106
implementation = _rpm_impl,
107107
attrs = _rpm_attrs,
108108
)
109+
110+
def _null_rpm_rule_impl(_):
111+
return [
112+
RpmInfo(
113+
file = "",
114+
deps = depset(),
115+
),
116+
DefaultInfo(files = depset()),
117+
]
118+
119+
null_rpm_rule = rule(
120+
implementation = _null_rpm_rule_impl,
121+
)
122+
123+
_NULL_FILE_BUILD = """
124+
load("@bazeldnf//internal:rpm.bzl", "null_rpm_rule")
125+
package(default_visibility = ["//visibility:public"])
126+
null_rpm_rule(
127+
name = "rpm",
128+
)
129+
"""
130+
131+
def _null_rpm_impl(ctx):
132+
ctx.file("WORKSPACE", "workspace(name = \"{name}\")".format(name = ctx.name))
133+
ctx.file(
134+
"rpm/BUILD",
135+
_NULL_FILE_BUILD,
136+
)
137+
138+
null_rpm = repository_rule(
139+
implementation = _null_rpm_impl,
140+
)

0 commit comments

Comments
 (0)