-
Notifications
You must be signed in to change notification settings - Fork 519
/
Copy pathexternal_npm_package_info.bzl
71 lines (64 loc) · 3.31 KB
/
external_npm_package_info.bzl
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
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""ExternalNpmPackageInfo providers and apsect to collect node_modules from deps.
"""
# ExternalNpmPackageInfo provider is provided by targets that are external npm packages by
# `js_library` rule when package_name is set to "node_modules", as well as other targets that
# have direct or transitive deps on `js_library` targets via the `node_modules_aspect` below.
ExternalNpmPackageInfo = provider(
doc = "Provides information about one or more external npm packages",
fields = {
"direct_sources": "Depset of direct source files in these external npm package(s)",
"has_directories": "True if any sources are directories",
"path": "The local workspace path that these external npm deps should be linked at. If empty, they will be linked at the root.",
"sources": "Depset of direct & transitive source files in these external npm package(s) and transitive dependencies",
"workspace": "The workspace name that these external npm package(s) are provided from",
},
)
def _node_modules_aspect_impl(target, ctx):
if ExternalNpmPackageInfo in target:
return []
# provide ExternalNpmPackageInfo if it is not already provided there are ExternalNpmPackageInfo deps
providers = []
# map of 'path' to [workspace, sources_depsets]
paths = {}
if hasattr(ctx.rule.attr, "deps"):
# if any deps have has_directories set then has_directories will be true in the exported ExternalNpmPackageInfo
has_directories = False
for dep in ctx.rule.attr.deps:
if ExternalNpmPackageInfo in dep:
has_directories = has_directories or dep[ExternalNpmPackageInfo].has_directories
path = dep[ExternalNpmPackageInfo].path
workspace = dep[ExternalNpmPackageInfo].workspace
sources_depsets = []
if path in paths:
path_entry = paths[path]
if path_entry[0] != workspace:
fail("All npm dependencies at the path '%s' must come from a single workspace. Found '%s' and '%s'." % (path, workspace, path_entry[0]))
sources_depsets = path_entry[1]
sources_depsets.append(dep[ExternalNpmPackageInfo].sources)
paths[path] = [workspace, sources_depsets]
for path, path_entry in paths.items():
providers.extend([ExternalNpmPackageInfo(
direct_sources = depset(),
sources = depset(transitive = path_entry[1]),
workspace = path_entry[0],
path = path,
has_directories = has_directories,
)])
return providers
node_modules_aspect = aspect(
_node_modules_aspect_impl,
attr_aspects = ["deps"],
)