Skip to content

Commit 959af7a

Browse files
authored
bazel: copy go_gapic_assembly_pkg macro impl (#332)
1 parent fe77bc7 commit 959af7a

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

rules_go_gapic/go_gapic_pkg.bzl

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Copyright 2020 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
load("@io_bazel_rules_go//go:def.bzl", "GoLibrary", "GoSource")
16+
load("@com_google_api_codegen//rules_gapic:gapic_pkg.bzl", "construct_package_dir_paths")
17+
18+
def _go_gapic_src_pkg_impl(ctx):
19+
srcjars = []
20+
srcs = []
21+
src_strs = []
22+
for dep in ctx.attr.deps:
23+
if GoSource in dep:
24+
for dep_src in dep[GoSource].srcs:
25+
if dep_src.extension == "srcjar":
26+
srcjars.append(dep_src)
27+
else:
28+
srcs.append(dep_src)
29+
src_strs.append("%s:%s" % (dep[GoLibrary].importpath, dep_src.path))
30+
elif hasattr(dep, "files"):
31+
for dep_file in dep.files.to_list():
32+
if dep_file.extension == "srcjar":
33+
srcjars.append(dep_file)
34+
35+
paths = construct_package_dir_paths(ctx.attr.package_dir, ctx.outputs.pkg, ctx.label.name)
36+
script = """
37+
for srcjar in {srcjars}; do
38+
mkdir -p {package_dir_path}
39+
unzip -q -o $srcjar -d {package_dir_path}
40+
done
41+
for src_str in {srcs_strs}; do
42+
# Split the `src_str` string in format '<dest_dir_path>:<full_file_path>' by ':' delimiter
43+
# dest_dir_path: ${{src_str%:*}}
44+
# full_file_path: ${{src_str#*:}}
45+
if [ -d "${{src_str#*:}}" ]; then
46+
mkdir -p {package_dir_path}
47+
cp -R -L ${{src_str#*:}}/* {package_dir_path}/
48+
else
49+
mkdir -p {package_dir_path}/${{src_str%:*}}
50+
cp -f ${{src_str#*:}} {package_dir_path}/${{src_str%:*}}
51+
fi
52+
chmod 644 {package_dir_path}/${{src_str%:*}}/*
53+
done
54+
cd {package_dir_path}
55+
tar -zchpf {package_dir}.tar.gz {package_dir_expr}*
56+
cd -
57+
mv {package_dir_path}/{package_dir}.tar.gz {pkg}
58+
""".format(
59+
srcjars = " ".join(["'%s'" % f.path for f in srcjars]),
60+
srcs_strs = " ".join(["'%s'" % s for s in src_strs]),
61+
package_dir_path = paths.package_dir_path,
62+
package_dir = paths.package_dir,
63+
pkg = ctx.outputs.pkg.path,
64+
package_dir_expr = paths.package_dir_expr,
65+
)
66+
67+
ctx.actions.run_shell(
68+
inputs = srcjars + srcs,
69+
command = script,
70+
outputs = [ctx.outputs.pkg],
71+
)
72+
73+
_go_gapic_src_pkg = rule(
74+
attrs = {
75+
"deps": attr.label_list(allow_files = True, mandatory = True),
76+
"package_dir": attr.string(mandatory = True),
77+
},
78+
outputs = {"pkg": "%{name}.tar.gz"},
79+
implementation = _go_gapic_src_pkg_impl,
80+
)
81+
82+
def go_gapic_assembly_pkg(name, deps, assembly_name = None):
83+
package_dir = name
84+
if assembly_name:
85+
package_dir = "gapi-cloud-%s-%s" % (assembly_name, name)
86+
_go_gapic_src_pkg(
87+
name = name,
88+
deps = deps,
89+
package_dir = package_dir,
90+
)

0 commit comments

Comments
 (0)