Skip to content

Commit 6a8f03a

Browse files
committed
WIP: Bzlmod part the one hundred thirty-third
Reorganize `extensions/{,dev_}deps.bzl` logic Established the `_defaults`/`_attrs`/`tag_class` pattern. Used this to refactor `_get_scala_compiler_srcjars()` and `_get_twitter_scrooge()` and to delete `_add_if_not_empty()`.
1 parent 807d818 commit 6a8f03a

File tree

2 files changed

+91
-96
lines changed

2 files changed

+91
-96
lines changed

scala/extensions/deps.bzl

Lines changed: 81 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -12,77 +12,41 @@ _settings_defaults = {
1212
"validate_scala_version": True,
1313
}
1414

15+
_settings_attrs = {
16+
"maven_servers": attr.string_list(
17+
default = _settings_defaults["maven_servers"],
18+
),
19+
# Correct spelling of "overridden"
20+
"overridden_artifacts": attr.string_dict(
21+
default = _settings_defaults["overridden_artifacts"],
22+
),
23+
"load_scala_toolchain_dependencies": attr.bool(
24+
default = _settings_defaults["load_scala_toolchain_dependencies"],
25+
),
26+
"fetch_sources": attr.bool(
27+
default = _settings_defaults["fetch_sources"],
28+
),
29+
"validate_scala_version": attr.bool(
30+
default = _settings_defaults["validate_scala_version"],
31+
),
32+
}
33+
1534
_settings = tag_class(
16-
attrs = {
17-
"maven_servers": attr.string_list(
18-
default = _settings_defaults["maven_servers"],
19-
),
20-
# Correct spelling of "overridden"
21-
"overridden_artifacts": attr.string_dict(
22-
default = _settings_defaults["overridden_artifacts"],
23-
),
24-
"load_scala_toolchain_dependencies": attr.bool(
25-
default = _settings_defaults["load_scala_toolchain_dependencies"],
26-
),
27-
"fetch_sources": attr.bool(
28-
default = _settings_defaults["fetch_sources"],
29-
),
30-
"validate_scala_version": attr.bool(
31-
default = _settings_defaults["validate_scala_version"],
32-
),
33-
},
35+
attrs = _settings_attrs,
3436
)
3537

3638
_scalafmt_defaults = {
3739
"default_config_path": ".scalafmt.conf",
3840
}
3941

40-
_scalafmt = tag_class(
41-
attrs = {
42-
"default_config_path": attr.string(
43-
default = _scalafmt_defaults["default_config_path"],
44-
),
45-
},
46-
)
47-
48-
_compiler_srcjar = tag_class(
49-
attrs = {
50-
"version": attr.string(mandatory = True),
51-
"url": attr.string(),
52-
"urls": attr.string_list(),
53-
"label": attr.label(),
54-
"sha256": attr.string(),
55-
"integrity": attr.string(),
56-
},
57-
)
58-
59-
_toolchains_defaults = {
60-
"scalatest": False,
61-
"junit": False,
62-
"specs2": False,
63-
"testing": False,
64-
"scalafmt": False,
65-
"scala_proto": False,
66-
"scala_proto_enable_all_options": False,
67-
"twitter_scrooge": False,
68-
"jmh": False,
42+
_scalafmt_attrs = {
43+
"default_config_path": attr.string(
44+
default = _scalafmt_defaults["default_config_path"],
45+
),
6946
}
7047

71-
_toolchains = tag_class(
72-
attrs = {
73-
k: attr.bool(default = v)
74-
for k, v in _toolchains_defaults.items()
75-
},
76-
)
77-
78-
_twitter_scrooge = tag_class(
79-
attrs = {
80-
"libthrift": attr.string(),
81-
"scrooge_core": attr.string(),
82-
"scrooge_generator": attr.string(),
83-
"util_core": attr.string(),
84-
"util_logging": attr.string(),
85-
},
48+
_scalafmt = tag_class(
49+
attrs = _scalafmt_attrs,
8650
)
8751

8852
def _get_settings(root_module):
@@ -105,36 +69,61 @@ def _get_settings(root_module):
10569

10670
return settings | {"scalafmt_%s" % k: v for k, v in scalafmt.items()}
10771

108-
def _add_if_not_empty(result, name, value):
109-
if len(value) != 0:
110-
result[name] = value
72+
_compiler_srcjar_attrs = {
73+
"version": attr.string(mandatory = True),
74+
"url": attr.string(),
75+
"urls": attr.string_list(),
76+
"label": attr.label(),
77+
"sha256": attr.string(),
78+
"integrity": attr.string(),
79+
}
80+
81+
_compiler_srcjar = tag_class(
82+
attrs = _compiler_srcjar_attrs,
83+
)
11184

11285
def _get_scala_compiler_srcjars(root_module):
11386
if root_module == None:
11487
return {}
11588

11689
result = {}
117-
for srcjar in root_module.tags.compiler_srcjar:
118-
info = {}
119-
_add_if_not_empty(info, "url", srcjar.url)
120-
_add_if_not_empty(info, "urls", srcjar.urls)
121-
_add_if_not_empty(info, "sha256", srcjar.sha256)
122-
_add_if_not_empty(info, "integrity", srcjar.integrity)
12390

124-
# Label values don't have a length.
125-
if srcjar.label != None:
126-
info["label"] = srcjar.label
91+
for srcjar in root_module.tags.compiler_srcjar:
92+
values = {k: getattr(srcjar, k) for k in _compiler_srcjar_attrs}
12793

12894
# Later instances for the same version overwrite earlier ones.
129-
result[srcjar.version] = info
95+
result[srcjar.version] = {k: v for k, v in values.items() if v}
13096

13197
return result
13298

99+
_toolchains_defaults = {
100+
"scalatest": False,
101+
"junit": False,
102+
"specs2": False,
103+
"testing": False,
104+
"scalafmt": False,
105+
"scala_proto": False,
106+
"scala_proto_enable_all_options": False,
107+
"twitter_scrooge": False,
108+
"jmh": False,
109+
}
110+
111+
_toolchains_attrs = {
112+
k: attr.bool(default = v)
113+
for k, v in _toolchains_defaults.items()
114+
}
115+
116+
_toolchains = tag_class(
117+
attrs = _toolchains_attrs,
118+
)
119+
133120
def _get_toolchains(module_ctx):
134121
result = dict(_toolchains_defaults)
135122

136123
for mod in module_ctx.modules:
137124
values = get_tag_values(mod.tags.toolchains, _toolchains_defaults)
125+
126+
# Don't overwrite `True` values from one tag with `False` from another.
138127
result.update({k: True for k in values if values[k]})
139128

140129
if result["testing"]:
@@ -144,21 +133,25 @@ def _get_toolchains(module_ctx):
144133
result["junit"] = True
145134
return result
146135

147-
def _get_twitter_scrooge(module_ctx):
148-
result = {}
136+
_twitter_scrooge_attrs = {
137+
"libthrift": attr.string(),
138+
"scrooge_core": attr.string(),
139+
"scrooge_generator": attr.string(),
140+
"util_core": attr.string(),
141+
"util_logging": attr.string(),
142+
}
149143

150-
for mod in module_ctx.modules:
151-
if not mod.is_root:
152-
continue
144+
_twitter_scrooge = tag_class(
145+
attrs = _twitter_scrooge_attrs,
146+
)
153147

154-
for tag in mod.tags.twitter_scrooge:
155-
_add_if_not_empty(result, "libthrift", tag.libthrift)
156-
_add_if_not_empty(result, "scrooge_core", tag.scrooge_core)
157-
_add_if_not_empty(result, "scrooge_generator", tag.scrooge_generator)
158-
_add_if_not_empty(result, "util_core", tag.util_core)
159-
_add_if_not_empty(result, "util_logging", tag.util_logging)
148+
def _get_twitter_scrooge(root_module):
149+
if root_module == None or len(root_module.tags.twitter_scrooge) == 0:
150+
return {}
160151

161-
return result
152+
tag = root_module.tags.twitter_scrooge[-1]
153+
tag_values = {k: getattr(tag, k) for k in _twitter_scrooge_attrs}
154+
return {k: v for k, v in tag_values.items() if len(v) != 0}
162155

163156
def _scala_deps_impl(module_ctx):
164157
root_module = get_root_module(module_ctx)
@@ -168,7 +161,7 @@ def _scala_deps_impl(module_ctx):
168161
**(
169162
_get_settings(root_module) |
170163
_get_toolchains(module_ctx) |
171-
_get_twitter_scrooge(module_ctx)
164+
_get_twitter_scrooge(root_module)
172165
)
173166
)
174167

scala/private/extensions/dev_deps.bzl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,17 @@ _settings_defaults = {
1313
"fetch_sources": False,
1414
}
1515

16+
_settings_attrs = {
17+
"maven_servers": attr.string_list(
18+
default = _settings_defaults["maven_servers"],
19+
),
20+
"fetch_sources": attr.bool(
21+
default = _settings_defaults["fetch_sources"],
22+
),
23+
}
24+
1625
_settings = tag_class(
17-
attrs = {
18-
"maven_servers": attr.string_list(
19-
default = _settings_defaults["maven_servers"],
20-
),
21-
"fetch_sources": attr.bool(
22-
default = _settings_defaults["fetch_sources"],
23-
),
24-
},
26+
attrs = _settings_attrs,
2527
)
2628

2729
def dev_deps_repositories(

0 commit comments

Comments
 (0)