-
Notifications
You must be signed in to change notification settings - Fork 25
/
openapi.bzl
259 lines (230 loc) 路 9.81 KB
/
openapi.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
"""Bazel rules for generating sources and libraries from openapi schemas
"""
load("@bazel_tools//tools/build_defs/repo:jvm.bzl", "jvm_maven_import_external")
# https://swagger.io/docs/open-source-tools/swagger-codegen/
# https://openapi-generator.tech/
_SUPPORTED_PROVIDERS = {
"swagger": {
"artifact": "io.swagger:swagger-codegen-cli",
"name": "io_swagger_swagger_codegen_cli"
},
"swaggerv3": {
"artifact": "io.swagger.codegen.v3:swagger-codegen-cli",
"name": "io_swagger_codegen_v3_swagger_codegen_cli"
},
"openapi": {
"artifact": "org.openapitools:openapi-generator-cli",
"name": "org_openapitools_openapi_generator_cli"
}
}
def openapi_repositories(codegen_cli_version = "2.4.16", codegen_cli_sha256 = "154b5a37254a3021a8cb669a1c57af78b45bb97e89e0425e3f055b1c79f74a93", prefix = "io_bazel_rules_openapi", codegen_cli_provider = "swagger"):
jvm_maven_import_external(
name = prefix + "_" + _SUPPORTED_PROVIDERS[codegen_cli_provider]["name"],
artifact = _SUPPORTED_PROVIDERS[codegen_cli_provider]["artifact"] + ":" + codegen_cli_version,
artifact_sha256 = codegen_cli_sha256,
server_urls = ["https://repo.maven.apache.org/maven2"],
licenses = ["notice"], # Apache 2.0 License
)
native.bind(
name = prefix + "/dependency/openapi-cli",
actual = "@" + prefix + "_" + _SUPPORTED_PROVIDERS[codegen_cli_provider]["name"] + "//jar",
)
def _comma_separated_pairs(pairs):
return ",".join([
"{}={}".format(k, v)
for k, v in pairs.items()
])
def _generator_provider(ctx):
codegen_provider = "openapi"
if "io_swagger_swagger_codegen_cli" in ctx.file.codegen_cli.path:
codegen_provider = "swagger"
if "io_swagger_codegen_v3_swagger_codegen_cli" in ctx.file.codegen_cli.path:
codegen_provider = "swaggerv3"
return codegen_provider
def _is_swagger_codegen(ctx):
return _generator_provider(ctx) == "swagger"
def _is_swagger_codegen_v3(ctx):
return _generator_provider(ctx) == "swaggerv3"
def _is_openapi_codegen(ctx):
return _generator_provider(ctx) == "openapi"
def _openapi_major_version(ctx):
name = ctx.file.codegen_cli.path.split('/').pop(-1) # Extract JAR's name
# name should look like openapi-generator-cli-5.0.0.jar
# 1. Split on - and take the last part (5.0.0.jar)
# 2. Remove the .jar and split on the '.'
# 3. Take the first element of the list (major)
version = name.split("-").pop(-1).replace(".jar", "").split(".").pop(0) #
return int(version)
def _new_generator_command(ctx, gen_dir, rjars):
java_path = ctx.attr._jdk[java_common.JavaRuntimeInfo].java_executable_exec_path
gen_cmd = str(java_path)
gen_cmd += " -cp {cli_jar}:{jars}".format(
cli_jar = ctx.file.codegen_cli.path,
jars = ":".join([j.path for j in rjars.to_list()]),
)
if _is_swagger_codegen(ctx):
gen_cmd += " io.swagger.codegen.SwaggerCodegen generate -i {spec} -l {language} -o {output}".format(
spec = ctx.file.spec.path,
language = ctx.attr.language,
output = gen_dir,
)
gen_cmd += ' -D "{properties}"'.format(
properties = _comma_separated_pairs(ctx.attr.system_properties),
)
if _is_swagger_codegen_v3(ctx):
gen_cmd += " io.swagger.codegen.v3.cli.SwaggerCodegen generate -i {spec} -l {language} -o {output}".format(
spec = ctx.file.spec.path,
language = ctx.attr.language,
output = gen_dir,
)
gen_cmd += ' -D "{properties}"'.format(
properties = _comma_separated_pairs(ctx.attr.system_properties),
)
if _is_openapi_codegen(ctx):
gen_cmd += " org.openapitools.codegen.OpenAPIGenerator generate --log-to-stderr -i {spec} -g {language} -o {output}".format(
spec = ctx.file.spec.path,
language = ctx.attr.language,
output = gen_dir,
)
if _openapi_major_version(ctx) >= 5:
gen_cmd += ' --global-property "{properties}"'.format(
properties = _comma_separated_pairs(ctx.attr.system_properties),
)
else:
gen_cmd += ' -D "{properties}"'.format(
properties = _comma_separated_pairs(ctx.attr.system_properties),
)
additional_properties = dict(ctx.attr.additional_properties)
# This is needed to ensure reproducible Java output
if ctx.attr.language == "java" and \
"hideGenerationTimestamp" not in ctx.attr.additional_properties:
additional_properties["hideGenerationTimestamp"] = "true"
gen_cmd += ' --additional-properties "{properties}"'.format(
properties = _comma_separated_pairs(additional_properties),
)
gen_cmd += ' --type-mappings "{mappings}"'.format(
mappings = _comma_separated_pairs(ctx.attr.type_mappings),
)
if ctx.attr.api_package:
gen_cmd += " --api-package {package}".format(
package = ctx.attr.api_package,
)
if ctx.attr.invoker_package:
gen_cmd += " --invoker-package {package}".format(
package = ctx.attr.invoker_package,
)
if ctx.attr.model_package:
gen_cmd += " --model-package {package}".format(
package = ctx.attr.model_package,
)
# fixme: by default, swagger-codegen is rather verbose. this helps with that but can also mask useful error messages
# when it fails. look into log configuration options. it's a java app so perhaps just a log4j.properties or something
gen_cmd += " 2>/dev/null"
return gen_cmd
def _impl(ctx):
jars = _collect_jars(ctx.attr.deps)
(cjars, rjars) = (jars.compiletime, jars.runtime)
gen_dir = "{dirname}/{rule_name}".format(
dirname = ctx.file.spec.dirname,
rule_name = ctx.attr.name,
)
commands = [
"mkdir -p {gen_dir}".format(
gen_dir = gen_dir,
),
_new_generator_command(ctx, gen_dir, rjars),
# forcing a timestamp for deterministic artifacts
"find {gen_dir} -exec touch -t 198001010000 {{}} \\;".format(
gen_dir = gen_dir,
),
"{jar} cMf {target} -C {srcs} .".format(
jar = "%s/bin/jar" % ctx.attr._jdk[java_common.JavaRuntimeInfo].java_home,
target = ctx.outputs.codegen.path,
srcs = gen_dir,
),
]
inputs = ctx.files._jdk + [
ctx.file.codegen_cli,
ctx.file.spec,
] + _collect_files(ctx.attr.spec_refs) + cjars.to_list() + rjars.to_list()
ctx.actions.run_shell(
inputs = inputs,
outputs = [ctx.actions.declare_directory("%s" % (ctx.attr.name)), ctx.outputs.codegen],
command = " && ".join(commands),
progress_message = "generating openapi sources %s" % ctx.label,
arguments = [],
)
return struct(
codegen = ctx.outputs.codegen,
)
# taken from rules_scala
def _collect_jars(targets):
"""Compute the runtime and compile-time dependencies from the given targets""" # noqa
compile_jars = depset()
runtime_jars = depset()
for target in targets:
found = False
if hasattr(target, "scala"):
if hasattr(target.scala.outputs, "ijar"):
compile_jars = depset(transitive = [compile_jars, [target.scala.outputs.ijar]])
compile_jars = depset(transitive = [compile_jars, target.scala.transitive_compile_exports])
runtime_jars = depset(transitive = [runtime_jars, target.scala.transitive_runtime_deps])
runtime_jars = depset(transitive = [runtime_jars, target.scala.transitive_runtime_exports])
found = True
if hasattr(target, "JavaInfo"):
# see JavaSkylarkApiProvider.java,
# this is just the compile-time deps
# this should be improved in bazel 0.1.5 to get outputs.ijar
# compile_jars = depset(transitive = [compile_jars, [target.java.outputs.ijar]])
compile_jars = depset(transitive = [compile_jars, target[JavaInfo].transitive_deps])
runtime_jars = depset(transitive = [runtime_jars, target[JavaInfo].transitive_runtime_deps])
found = True
if not found:
# support http_file pointed at a jar. http_jar uses ijar,
# which breaks scala macros
runtime_jars = depset(transitive = [runtime_jars, target.files])
compile_jars = depset(transitive = [compile_jars, target.files])
return struct(compiletime = compile_jars, runtime = runtime_jars)
def _collect_files(targets):
result = []
for target in targets:
result += target.files.to_list()
return result
openapi_gen = rule(
attrs = {
# downstream dependencies
"deps": attr.label_list(),
# openapi spec file
"spec": attr.label(
mandatory = True,
allow_single_file = [".json", ".yaml"],
),
"spec_refs": attr.label_list(
allow_empty = True,
allow_files = [".json", ".yaml"],
default = [],
),
# language to generate
"language": attr.string(mandatory = True),
"api_package": attr.string(),
"invoker_package": attr.string(),
"model_package": attr.string(),
"additional_properties": attr.string_dict(),
"system_properties": attr.string_dict(),
"type_mappings": attr.string_dict(),
"import_mappings": attr.string(),
"_jdk": attr.label(
default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
providers = [java_common.JavaRuntimeInfo],
),
"codegen_cli": attr.label(
cfg = "host",
default = Label("//external:io_bazel_rules_openapi/dependency/openapi-cli"),
allow_single_file = True,
),
},
outputs = {
"codegen": "%{name}_codegen.srcjar",
},
implementation = _impl,
)