forked from sourcegraph/sourcegraph-public-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjs_lib.bzl
More file actions
126 lines (100 loc) · 4.48 KB
/
js_lib.bzl
File metadata and controls
126 lines (100 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""`js_binary` helper functions. Copied from rules_js internals.
"""
load("@aspect_rules_js//js:providers.bzl", "JsInfo")
load("@aspect_rules_js//npm:providers.bzl", "NpmPackageStoreInfo")
def gather_files_from_js_providers(
targets,
include_sources,
include_transitive_sources,
include_declarations,
include_npm_linked_packages):
"""Gathers files from JsInfo and NpmPackageStoreInfo providers.
Args:
targets: list of target to gather from
include_sources: see js_filegroup documentation
include_transitive_sources: see js_filegroup documentation
include_declarations: see js_filegroup documentation
include_npm_linked_packages: see js_filegroup documentation
Returns:
A depset of files
"""
files_depsets = []
files_depsets.extend([
target[DefaultInfo].default_runfiles.files
for target in targets
if DefaultInfo in target and hasattr(target[DefaultInfo], "default_runfiles")
])
if include_sources:
files_depsets.extend([
target[JsInfo].sources
for target in targets
if JsInfo in target and hasattr(target[JsInfo], "sources")
])
if include_transitive_sources:
files_depsets.extend([
target[JsInfo].transitive_sources
for target in targets
if JsInfo in target and hasattr(target[JsInfo], "transitive_sources")
])
if include_declarations:
files_depsets.extend([
target[JsInfo].transitive_declarations
for target in targets
if JsInfo in target and hasattr(target[JsInfo], "transitive_declarations")
])
if include_npm_linked_packages:
files_depsets.extend([
target[JsInfo].transitive_npm_linked_package_files
for target in targets
if JsInfo in target and hasattr(target[JsInfo], "transitive_npm_linked_package_files")
])
files_depsets.extend([
target[NpmPackageStoreInfo].transitive_files
for target in targets
if NpmPackageStoreInfo in target and hasattr(target[NpmPackageStoreInfo], "transitive_files")
])
# print(files_depsets)
return depset([], transitive = files_depsets)
def gather_runfiles(ctx, sources, data, deps):
"""Creates a runfiles object containing files in `sources`, default outputs from `data` and transitive runfiles from `data` & `deps`.
As a defense in depth against `data` & `deps` targets not supplying all required runfiles, also
gathers the transitive sources & transitive npm linked packages from the `JsInfo` &
`NpmPackageStoreInfo` providers of `data` & `deps` targets.
See https://bazel.build/extending/rules#runfiles for more info on providing runfiles in build rules.
Args:
ctx: the rule context
sources: list or depset of files which should be included in runfiles
data: list of data targets; default outputs and transitive runfiles are gather from these targets
See https://bazel.build/reference/be/common-definitions#typical.data and
https://bazel.build/concepts/dependencies#data-dependencies for more info and guidance
on common usage of the `data` attribute in build rules.
deps: list of dependency targets; only transitive runfiles are gather from these targets
Returns:
A [runfiles](https://bazel.build/rules/lib/runfiles) object created with [ctx.runfiles](https://bazel.build/rules/lib/ctx#runfiles).
"""
# Includes sources
if type(sources) == "list":
sources = depset(sources)
transitive_files_depsets = [sources]
# Gather the default outputs of data targets
transitive_files_depsets.extend([
target[DefaultInfo].files
for target in data
])
# Gather the transitive sources & transitive npm linked packages from the JsInfo &
# NpmPackageStoreInfo providers of data & deps targets.
transitive_files_depsets.append(gather_files_from_js_providers(
targets = data + deps,
include_sources = True,
include_transitive_sources = True,
include_declarations = False,
include_npm_linked_packages = True,
))
# Merge the above with the transitive runfiles of data & deps.
runfiles = ctx.runfiles(
transitive_files = depset(transitive = transitive_files_depsets),
).merge_all([
target[DefaultInfo].default_runfiles
for target in data + deps
])
return runfiles