Skip to content

Commit c3a6afb

Browse files
authored
feat: add support for packages JSON in Starlark code (cgrindel#289)
Related to cgrindel#276.
1 parent 702dc37 commit c3a6afb

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

swiftpkg/internal/deps_indexes.bzl

+54-1
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,18 @@ def _new_from_json(json_str):
2525
_new_product_from_dict(prod_dict)
2626
for prod_dict in orig_dict["products"]
2727
],
28+
packages = [
29+
_new_package_from_dict(pkg_dict)
30+
for pkg_dict in orig_dict.get("packages", [])
31+
],
2832
)
2933

30-
def _new(modules = [], products = []):
34+
def _new(modules = [], products = [], packages = []):
3135
modules_by_name = {}
3236
modules_by_label = {}
3337
products_by_key = {}
3438
products_by_name = {}
39+
packages_by_id = {}
3540

3641
# buildifier: disable=uninitialized
3742
def _add_module(m):
@@ -52,16 +57,22 @@ def _new(modules = [], products = []):
5257
prds.append(p)
5358
products_by_name[p.name] = prds
5459

60+
def _add_package(p):
61+
packages_by_id[p.identity] = p
62+
5563
for module in modules:
5664
_add_module(module)
5765
for product in products:
5866
_add_product(product)
67+
for package in packages:
68+
_add_package(package)
5969

6070
return struct(
6171
modules_by_name = modules_by_name,
6272
modules_by_label = modules_by_label,
6373
products_by_key = products_by_key,
6474
products_by_name = products_by_name,
75+
packages_by_id = packages_by_id,
6576
)
6677

6778
def _new_module_from_dict(mod_dict):
@@ -108,6 +119,48 @@ def _new_product(identity, name, type, target_labels):
108119
target_labels = target_labels,
109120
)
110121

122+
def _new_package(identity, name, remote_pkg = None, local_pkg = None):
123+
return struct(
124+
identity = identity,
125+
name = name,
126+
remote_pkg = remote_pkg,
127+
local_pkg = local_pkg,
128+
)
129+
130+
def _new_remote_package(remote, commit, version = None, branch = None):
131+
return struct(
132+
remote = remote,
133+
commit = commit,
134+
version = version,
135+
branch = branch,
136+
)
137+
138+
def _new_local_package(path):
139+
return struct(
140+
path = path,
141+
)
142+
143+
def _new_package_from_dict(pkg_dict):
144+
remote_pkg = None
145+
remote_dict = pkg_dict.get("remote")
146+
if remote_dict != None:
147+
remote_pkg = _new_remote_package(
148+
remote = remote_dict["remote"],
149+
commit = remote_dict["commit"],
150+
version = remote_dict.get("version"),
151+
branch = remote_dict.get("branch"),
152+
)
153+
local_pkg = None
154+
local_dict = pkg_dict.get("local")
155+
if local_dict != None:
156+
local_pkg = _new_local_package(path = local_dict["path"])
157+
return _new_package(
158+
identity = pkg_dict["identity"],
159+
name = pkg_dict["name"],
160+
remote_pkg = remote_pkg,
161+
local_pkg = local_pkg,
162+
)
163+
111164
def _modulemap_label_for_module(module):
112165
return bazel_labels.new(
113166
name = pkginfo_targets.modulemap_label_name(module.label.name),

swiftpkg/tests/deps_indexes_tests.bzl

+25
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,31 @@ _deps_index_json = """
394394
"@example_another_repo//Sources/Foo"
395395
]
396396
}
397+
],
398+
"packages": [
399+
{
400+
"name": "swiftpkg_swift_argument_parser",
401+
"identity": "swift-argument-parser",
402+
"remote": {
403+
"commit": "4ad606ba5d7673ea60679a61ff867cc1ff8c8e86",
404+
"remote": "https://github.com/apple/swift-argument-parser",
405+
"version": "1.2.1"
406+
}
407+
},
408+
{
409+
"name": "swiftpkg_example_cool_repo",
410+
"identity": "example-cool-repo",
411+
"local": {
412+
"path": "third_party/example_cool_repo"
413+
}
414+
},
415+
{
416+
"name": "swiftpkg_example_another_repo",
417+
"identity": "example-another-repo",
418+
"local": {
419+
"path": "third_party/example_another_repo"
420+
}
421+
}
397422
]
398423
}
399424
"""

0 commit comments

Comments
 (0)