-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefs.bzl
120 lines (112 loc) · 3.49 KB
/
defs.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
# Copyright 2018 Derivita, LLC
#
# 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.
"""Bazel BUILD rules for the Go App Engine runtime"""
def _go_appengine_app_impl(ctx):
symlinks = {}
for i in ctx.attr.srcs:
prefix = len(i.label.package) + 1
for f in i.files:
p = f.basename #short_path[prefix:]
if p in symlinks:
fail("Duplicate path " + p)
symlinks["app/"+p] = f
for i in ctx.attr.folders:
for e in i.entries:
for f in e.files:
if i.strip:
p = f.basename
else:
p = f.short_path
if p.startswith("../"):
p = "external/"+p[3:]
p = i.path + "/" + p
if p in symlinks:
fail("Duplicate path " + p)
symlinks["app/"+p] = f
extra_content = []
for (key, val) in ctx.attr.env.items():
extra_content.append("export %s=%s" % (key, val))
ctx.file_action(
output=ctx.outputs.executable,
executable=True,
content="""#!/bin/sh
cd "$0".runfiles
export GOPATH=$PWD/GOPATH
%s
dev_appserver.py app/app.yaml
"""%("\n".join(extra_content),),
)
return struct(
runfiles=ctx.runfiles(
root_symlinks=symlinks,
collect_default=True,
))
go_appengine_app = rule(
attrs = {
"srcs": attr.label_list(
allow_files = True,
allow_empty = False,
),
"folders": attr.label_list(providers = ["entries"]),
"deps": attr.label_list(),
"env": attr.string_dict(default={})
},
executable = True,
implementation = _go_appengine_app_impl,
)
def _folder_impl(ctx):
entries = []+ctx.attr.srcs
for s in ctx.attr.data:
if hasattr(s, 'data_runfiles'):
entries.append(s.data_runfiles)
return struct(
path=ctx.attr.path,
entries=entries,
strip=ctx.attr.strip
)
folder = rule(
attrs = {
"path": attr.string(mandatory = True),
"strip": attr.bool(default = True),
"srcs": attr.label_list(
allow_files = True,
),
"data": attr.label_list(),
},
implementation = _folder_impl,
)
def _go_appengine_library_impl(ctx):
symlinks = {}
for src in ctx.attr.srcs:
path = "GOPATH/src/%s/%s" % (ctx.attr.importpath, src.label.name)
if len(src.files) != 1:
print(src.files)
fail("%s should be a single file" % src.label, attr="srcs")
symlinks[path] = src.files.to_list()[0]
return struct(
runfiles=ctx.runfiles(
root_symlinks=symlinks,
collect_default=True,
))
go_appengine_library = rule(
attrs = {
"importpath": attr.string(mandatory = True),
"deps": attr.label_list(default = []),
"srcs": attr.label_list(
allow_files = True,
allow_empty = False,
),
},
implementation = _go_appengine_library_impl,
)