Skip to content

Commit 36f0269

Browse files
author
Pedro Palma Ramos
committed
add openApiGenerateInputs task
1 parent f662476 commit 36f0269

File tree

4 files changed

+281
-238
lines changed

4 files changed

+281
-238
lines changed

src/main/scala/org/openapitools/generator/sbt/plugin/OpenApiGeneratorKeys.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717

1818
package org.openapitools.generator.sbt.plugin
1919

20-
import org.openapitools.codegen.CodegenConstants
20+
import org.openapitools.codegen.{ClientOptInput, CodegenConstants}
2121
import sbt.{File, settingKey, taskKey}
2222

2323
trait OpenApiGeneratorKeys {
2424
final val openApiGenerate = taskKey[Seq[File]]("Generate code via Open API Tools Generator for Open API 2.0 or 3.x specification documents.")
25+
final val openApiGenerateInputs = taskKey[Option[ClientOptInput]]("Generates the user-provided config in a format that can be fed to a org.openapitools.codegen.Generator")
2526
final val openApiGenerators = taskKey[Unit]("Print list of available generators")
2627

2728
final val openApiInputSpec = settingKey[String]("The Open API 2.0/3.x specification location.")

src/main/scala/org/openapitools/generator/sbt/plugin/OpenApiGeneratorPlugin.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@
1717

1818
package org.openapitools.generator.sbt.plugin
1919

20-
import org.openapitools.generator.sbt.plugin.tasks.{OpenApiGenerateTask, OpenApiGeneratorsTask}
20+
import org.openapitools.generator.sbt.plugin.tasks.{OpenApiGenerateInputsTask, OpenApiGenerateTask, OpenApiGeneratorsTask}
2121
import sbt.Keys.aggregate
2222
import sbt.plugins.JvmPlugin
2323
import sbt.{Def, File, config, taskKey}
2424

2525
object OpenApiGeneratorPlugin extends sbt.AutoPlugin
2626
with OpenApiGeneratorsTask
27-
with OpenApiGenerateTask {
27+
with OpenApiGenerateTask
28+
with OpenApiGenerateInputsTask {
2829
self =>
2930

3031
override def requires: JvmPlugin.type = sbt.plugins.JvmPlugin
@@ -100,7 +101,8 @@ object OpenApiGeneratorPlugin extends sbt.AutoPlugin
100101

101102
override lazy val projectSettings: Seq[Def.Setting[_]] = Seq[sbt.Setting[_]](
102103
openApiGenerators := openApiGeneratorsTask.value,
103-
openApiGenerate := openApiGenerateTask.value
104+
openApiGenerate := openApiGenerateTask.value,
105+
openApiGenerateInputs := openApiGenerateInputsTask.value
104106
) ++ baseSettings
105107

106108
}
Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
/*
2+
* Copyright (c) 2020 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
*
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.openapitools.generator.sbt.plugin.tasks
18+
19+
import org.openapitools.codegen.config.{CodegenConfigurator, GlobalSettings}
20+
import org.openapitools.codegen.{ClientOptInput, CodegenConstants, DefaultGenerator}
21+
import org.openapitools.generator.sbt.plugin.OpenApiGeneratorKeys
22+
import sbt.Keys._
23+
import sbt.{Def, _}
24+
25+
import scala.util.{Failure, Success, Try}
26+
27+
trait OpenApiGenerateInputsTask extends OpenApiGeneratorKeys {
28+
29+
protected[this] def openApiGenerateInputsTask: Def.Initialize[Task[Option[ClientOptInput]]] = Def.task {
30+
31+
val logger = sbt.Keys.streams.value.log
32+
33+
if (openApiConfigFile.value.isEmpty && openApiGeneratorName.value.isEmpty) {
34+
None
35+
} else {
36+
val configurator: CodegenConfigurator = if (openApiConfigFile.value.nonEmpty) {
37+
val config = openApiConfigFile.value
38+
logger.info(s"read configuration from $config")
39+
CodegenConfigurator.fromFile(config)
40+
} else new CodegenConfigurator()
41+
42+
if (openApiSupportingFilesConstrainedTo.value.nonEmpty) {
43+
GlobalSettings.setProperty(CodegenConstants.SUPPORTING_FILES, openApiSupportingFilesConstrainedTo.value.mkString(","))
44+
} else {
45+
GlobalSettings.clearProperty(CodegenConstants.SUPPORTING_FILES)
46+
}
47+
48+
if (openApiModelFilesConstrainedTo.value.nonEmpty) {
49+
GlobalSettings.setProperty(CodegenConstants.MODELS, openApiModelFilesConstrainedTo.value.mkString(","))
50+
} else {
51+
GlobalSettings.clearProperty(CodegenConstants.MODELS)
52+
}
53+
54+
if (openApiApiFilesConstrainedTo.value.nonEmpty) {
55+
GlobalSettings.setProperty(CodegenConstants.APIS, openApiApiFilesConstrainedTo.value.mkString(","))
56+
} else {
57+
GlobalSettings.clearProperty(CodegenConstants.APIS)
58+
}
59+
60+
openApiGenerateApiDocumentation.value.foreach { value =>
61+
GlobalSettings.setProperty(CodegenConstants.API_DOCS, value.toString)
62+
}
63+
64+
openApiGenerateModelDocumentation.value.foreach { value =>
65+
GlobalSettings.setProperty(CodegenConstants.MODEL_DOCS, value.toString)
66+
}
67+
68+
openApiGenerateModelTests.value.foreach { value =>
69+
GlobalSettings.setProperty(CodegenConstants.MODEL_TESTS, value.toString)
70+
}
71+
72+
openApiGenerateApiTests.value.foreach { value =>
73+
GlobalSettings.setProperty(CodegenConstants.API_TESTS, value.toString)
74+
}
75+
76+
openApiWithXml.value.foreach { value =>
77+
GlobalSettings.setProperty(CodegenConstants.WITH_XML, value.toString)
78+
}
79+
80+
// now override with any specified parameters
81+
openApiVerbose.value.foreach { value =>
82+
configurator.setVerbose(value)
83+
}
84+
openApiValidateSpec.value.foreach { value =>
85+
configurator.setValidateSpec(value)
86+
}
87+
88+
openApiSkipOverwrite.value.foreach { value =>
89+
configurator.setSkipOverwrite(value)
90+
}
91+
92+
if (openApiInputSpec.value.nonEmpty) {
93+
configurator.setInputSpec(openApiInputSpec.value)
94+
}
95+
96+
97+
if (openApiGeneratorName.value.nonEmpty) {
98+
configurator.setGeneratorName(openApiGeneratorName.value)
99+
}
100+
101+
if (openApiOutputDir.value.nonEmpty) {
102+
configurator.setOutputDir(openApiOutputDir.value)
103+
}
104+
105+
if (openApiAuth.value.nonEmpty) {
106+
configurator.setAuth(openApiAuth.value)
107+
}
108+
109+
if (openApiTemplateDir.value.nonEmpty) {
110+
configurator.setTemplateDir(openApiTemplateDir.value)
111+
}
112+
113+
if (openApiPackageName.value.nonEmpty) {
114+
configurator.setPackageName(openApiPackageName.value)
115+
}
116+
117+
if (openApiApiPackage.value.nonEmpty) {
118+
configurator.setApiPackage(openApiApiPackage.value)
119+
}
120+
121+
if (openApiModelPackage.value.nonEmpty) {
122+
configurator.setModelPackage(openApiModelPackage.value)
123+
}
124+
125+
if (openApiModelNamePrefix.value.nonEmpty) {
126+
configurator.setModelNamePrefix(openApiModelNamePrefix.value)
127+
}
128+
129+
if (openApiModelNameSuffix.value.nonEmpty) {
130+
configurator.setModelNameSuffix(openApiModelNameSuffix.value)
131+
}
132+
133+
if (openApiInvokerPackage.value.nonEmpty) {
134+
configurator.setInvokerPackage(openApiInvokerPackage.value)
135+
}
136+
137+
if (openApiGroupId.value.nonEmpty) {
138+
configurator.setGroupId(openApiGroupId.value)
139+
}
140+
141+
if (openApiId.value.nonEmpty) {
142+
configurator.setArtifactId(openApiId.value)
143+
}
144+
145+
if ((version in openApiGenerate).value.nonEmpty) {
146+
configurator.setArtifactVersion(version.value)
147+
}
148+
149+
if (openApiLibrary.value.nonEmpty) {
150+
configurator.setLibrary(openApiLibrary.value)
151+
}
152+
153+
if (openApiGitHost.value.nonEmpty) {
154+
configurator.setGitHost(openApiGitHost.value)
155+
}
156+
157+
if (openApiGitUserId.value.nonEmpty) {
158+
configurator.setGitUserId(openApiGitUserId.value)
159+
}
160+
161+
162+
if (openApiGitRepoId.value.nonEmpty) {
163+
configurator.setGitRepoId(openApiGitRepoId.value)
164+
}
165+
166+
if (openApiReleaseNote.value.nonEmpty) {
167+
configurator.setReleaseNote(openApiReleaseNote.value)
168+
}
169+
170+
if (openApiHttpUserAgent.value.nonEmpty) {
171+
configurator.setHttpUserAgent(openApiHttpUserAgent.value)
172+
}
173+
174+
if (openApiIgnoreFileOverride.value.nonEmpty) {
175+
configurator.setIgnoreFileOverride(openApiIgnoreFileOverride.value)
176+
}
177+
178+
openApiRemoveOperationIdPrefix.value.foreach { value =>
179+
configurator.setRemoveOperationIdPrefix(value)
180+
}
181+
182+
openApiLogToStderr.value.foreach { value =>
183+
configurator.setLogToStderr(value)
184+
}
185+
186+
openApiEnablePostProcessFile.value.foreach { value =>
187+
configurator.setEnablePostProcessFile(value)
188+
}
189+
190+
openApiSkipValidateSpec.value.foreach { value =>
191+
configurator.setValidateSpec(!value)
192+
}
193+
194+
openApiGenerateAliasAsModel.value.foreach { value =>
195+
configurator.setGenerateAliasAsModel(value)
196+
}
197+
198+
if (openApiGlobalProperties.value.nonEmpty || openApiSystemProperties.value.nonEmpty) {
199+
(openApiSystemProperties.value ++ openApiGlobalProperties.value).foreach { entry =>
200+
configurator.addGlobalProperty(entry._1, entry._2)
201+
}
202+
}
203+
204+
if (openApiInstantiationTypes.value.nonEmpty) {
205+
openApiInstantiationTypes.value.foreach { entry =>
206+
configurator.addInstantiationType(entry._1, entry._2)
207+
}
208+
}
209+
210+
if (openApiImportMappings.value.nonEmpty) {
211+
openApiImportMappings.value.foreach { entry =>
212+
configurator.addImportMapping(entry._1, entry._2)
213+
}
214+
}
215+
216+
if (openApiTypeMappings.value.nonEmpty) {
217+
openApiTypeMappings.value.foreach { entry =>
218+
configurator.addTypeMapping(entry._1, entry._2)
219+
}
220+
}
221+
222+
if (openApiAdditionalProperties.value.nonEmpty) {
223+
openApiAdditionalProperties.value.foreach { entry =>
224+
configurator.addAdditionalProperty(entry._1, entry._2)
225+
}
226+
}
227+
228+
if (openApiServerVariables.value.nonEmpty) {
229+
openApiServerVariables.value.foreach { entry =>
230+
configurator.addServerVariable(entry._1, entry._2)
231+
}
232+
}
233+
234+
if (openApiLanguageSpecificPrimitives.value.nonEmpty) {
235+
openApiLanguageSpecificPrimitives.value.foreach { it =>
236+
configurator.addLanguageSpecificPrimitive(it)
237+
}
238+
}
239+
240+
if (openApiReservedWordsMappings.value.nonEmpty) {
241+
openApiReservedWordsMappings.value.foreach { entry =>
242+
configurator.addAdditionalReservedWordMapping(entry._1, entry._2)
243+
}
244+
}
245+
246+
Try(configurator.toClientOptInput) match {
247+
case Success(clientOptInput) =>
248+
Some(clientOptInput)
249+
250+
case Failure(ex) =>
251+
logger.error(ex.getMessage)
252+
None
253+
}
254+
}
255+
}
256+
}

0 commit comments

Comments
 (0)