-
Notifications
You must be signed in to change notification settings - Fork 521
/
Copy pathweb_package.bzl
140 lines (128 loc) · 4.75 KB
/
web_package.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# Copyright 2019 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.
"""Contains the web_package rule.
"""
def html_asset_inject(index_html, action_factory, injector, root_dirs, assets, output):
"""Injects JS and CSS resources into the index.html.
Args:
index_html: The input html file
action_factory: Bazel's actions module from ctx.actions - see https://docs.bazel.build/versions/master/skylark/lib/actions.html
injector: The injector executable
root_dirs: Path prefixes to strip off all assets. Longest wins.
assets: Asset files to inject
output: The output html file
Returns:
The output html file
"""
args = action_factory.args()
args.add(output.path)
args.add(index_html.path)
args.add_all(root_dirs)
args.add("--assets")
args.add_all(assets)
args.use_param_file("%s", use_always = True)
action_factory.run(
inputs = [index_html],
outputs = [output],
executable = injector,
arguments = [args],
)
return output
def move_files(output_name, files, action_factory, assembler, root_paths):
"""Moves files into an output directory
Args:
output_name: The name of the output directory
files: The files to move
action_factory: Bazel's actions module from ctx.actions - see https://docs.bazel.build/versions/master/skylark/lib/actions.html
assembler: The assembler executable
root_paths: Path prefixes to strip off all assets. Longest wins.
Returns:
The output directory tree-artifact
"""
www_dir = action_factory.declare_directory(output_name)
args = action_factory.args()
args.add(www_dir.path)
args.add_all(root_paths)
args.add("--assets")
args.add_all([f.path for f in files])
args.use_param_file("%s", use_always = True)
action_factory.run(
inputs = files,
outputs = [www_dir],
executable = assembler,
arguments = [args],
execution_requirements = {"local": "1"},
)
return depset([www_dir])
def _web_package(ctx):
root_paths = ctx.attr.additional_root_paths + [
ctx.label.package,
"/".join([ctx.bin_dir.path, ctx.label.package]),
"/".join([ctx.genfiles_dir.path, ctx.label.package]),
]
# Create the output file in a re-rooted subdirectory so it doesn't collide with the input file
html = ctx.actions.declare_file("_%s/%s" % (ctx.label.name, ctx.file.index_html.path))
# Move that index file back into place inside the package
root_paths.append("/".join([p for p in [ctx.bin_dir.path, ctx.label.package, "_" + ctx.label.name, ctx.label.package] if p]))
populated_index = html_asset_inject(
ctx.file.index_html,
ctx.actions,
ctx.executable._injector,
root_paths,
[f.path for f in ctx.files.assets],
html,
)
package_layout = move_files(
ctx.label.name,
ctx.files.data + ctx.files.assets + [html],
ctx.actions,
ctx.executable._assembler,
root_paths,
)
return [
DefaultInfo(files = package_layout),
]
web_package = rule(
implementation = _web_package,
attrs = {
"additional_root_paths": attr.string_list(
doc = """Path prefixes to strip off all assets, in addition to the current package. Longest wins.""",
),
"assets": attr.label_list(
allow_files = True,
doc = """Files which should be referenced from the index_html""",
),
"data": attr.label_list(
allow_files = True,
doc = """Additional files which should be served on request""",
),
"index_html": attr.label(
allow_single_file = True,
doc = """The entry point of the application""",
),
"_assembler": attr.label(
default = "@build_bazel_rules_nodejs//internal/web_package:assembler",
executable = True,
cfg = "host",
),
"_injector": attr.label(
default = "@build_bazel_rules_nodejs//internal/web_package:injector",
executable = True,
cfg = "host",
),
},
doc = """Assembles a web application from source files.
Injects JS and CSS resources into the index.html.
""",
)