forked from angular/angular
/
collect-type-definitions.bzl
41 lines (30 loc) · 1.45 KB
/
collect-type-definitions.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
# Copyright Google LLC. All Rights Reserved.
#
# Use of this source code is governed by an MIT-style license that can be
# found in the LICENSE file at https://angular.io/license
"""Collect TypeScript definition files from a rule context.
This is used to find all files that will be copied into a "ng_package".
"""
def _filter_typing_files(files):
return [file for file in files if file.path.endswith(".d.ts")]
def collect_type_definitions(ctx):
"""Returns a file tree containing only TypeScript definition files.
This is useful when packaging a "ng_package" where we only want to package specified
definition files.
Args:
ctx: ctx.
Returns:
A file tree containing only TypeScript definition files.
"""
# Add all source files and filter for TypeScript definition files
# See: https://docs.bazel.build/versions/master/skylark/lib/File.html#is_source
collected_files = _filter_typing_files([d for d in ctx.files.deps if d.is_source])
# In case source files have been explicitly specified in the attributes, just collect
# them and filter for definition files.
if hasattr(ctx.attr, "srcs"):
collected_files += _filter_typing_files(ctx.files.srcs)
# Collect all TypeScript definition files from the specified dependencies.
for dep in ctx.attr.deps:
if hasattr(dep, "typescript"):
collected_files += dep.typescript.transitive_declarations.to_list()
return collected_files