Skip to content

Commit 4305dfd

Browse files
authored
Merge pull request #14 from tweag/ch/reduce_sh_posix_toolchain_attr_number
sh_posix_toolchain: reduce number of attributes by using attr.string_dict instead of attr.string list
2 parents 0c274ad + 36b7c52 commit 4305dfd

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

CHANGELOG.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,30 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/).
88

99
[Unreleased]: https://github.com/tweag/rules_sh/compare/v0.1.1...HEAD
1010

11+
### Changed
12+
13+
- `sh_posix_toolchain` now has a single attribute `cmds`, which
14+
is a string to string `dict`; instead of having one attribute
15+
per member of `posix.commands`. It is a breaking change if you were
16+
calling `sh_posix_toolchain` directly.
17+
18+
If you were calling this rule as follows:
19+
20+
```
21+
sh_posix_toolchain(cat = "/bin/cat", wc = "/usr/bin/wc")
22+
```
23+
24+
you should now do:
25+
26+
```
27+
sh_posix_toolchain(cmds = { "cat": "/bin/cat", "wc": "/usr/bin/wc" })
28+
```
29+
30+
See PR [#14][#14] and issue [#13][#13] for the motivation.
31+
32+
[#14]: https://github.com/tweag/rules_sh/pull/14
33+
[#13]: https://github.com/tweag/rules_sh/issues/13
34+
1135
## [0.1.1] - 2019-11-13
1236

1337
[0.1.1]: https://github.com/tweag/rules_sh/compare/v0.1.0...v0.1.1

sh/posix.bzl

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,15 @@ MAKE_VARIABLES = "@rules_sh//sh/posix:make_variables"
181181

182182
def _sh_posix_toolchain_impl(ctx):
183183
commands = {}
184+
cmds = ctx.attr.cmds
184185
for cmd in _commands:
185-
cmd_path = getattr(ctx.attr, cmd, None)
186+
cmd_path = cmds.get(cmd, None)
186187
if not cmd_path:
187188
cmd_path = None
188189
commands[cmd] = cmd_path
190+
unrecognizeds = [cmd for cmd in cmds.keys() if cmd not in _commands]
191+
if unrecognizeds:
192+
fail("Unrecognized commands in keys of sh_posix_toolchain's \"cmds\" attributes: {}. See posix.commands in @rules_sh//sh:posix.bzl for the list of recognized commands.".format(", ".join(unrecognizeds)))
189193
cmd_paths = {
190194
paths.dirname(cmd_path): None
191195
for cmd_path in commands.values()
@@ -198,11 +202,10 @@ def _sh_posix_toolchain_impl(ctx):
198202

199203
sh_posix_toolchain = rule(
200204
attrs = {
201-
cmd: attr.string(
202-
doc = "Absolute path to the {} command.".format(cmd),
203-
mandatory = False,
205+
"cmds": attr.string_dict(
206+
doc = "dict where keys are command names and values are paths",
207+
mandatory = True,
204208
)
205-
for cmd in _commands
206209
},
207210
doc = """
208211
A toolchain capturing standard Unix shell commands.
@@ -300,7 +303,9 @@ load("@rules_sh//sh:posix.bzl", "sh_posix_toolchain")
300303
sh_posix_toolchain(
301304
name = "local_posix",
302305
visibility = ["//visibility:public"],
303-
{commands}
306+
cmds = {{
307+
{commands}
308+
}}
304309
)
305310
toolchain(
306311
name = "local_posix_toolchain",
@@ -316,8 +321,8 @@ toolchain(
316321
],
317322
)
318323
""".format(
319-
commands = ",\n ".join([
320-
'{cmd} = "{path}"'.format(cmd = cmd, path = cmd_path)
324+
commands = ",\n ".join([
325+
'"{cmd}": "{path}"'.format(cmd = cmd, path = cmd_path)
321326
for (cmd, cmd_path) in commands.items()
322327
if cmd_path
323328
]),

0 commit comments

Comments
 (0)