|
| 1 | +/* |
| 2 | + * Copyright 2019 OPS4J Contributors |
| 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 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 13 | + * implied. |
| 14 | + * |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.ops4j.ramler.gradle; |
| 19 | + |
| 20 | +import java.io.File; |
| 21 | + |
| 22 | +import org.gradle.api.DefaultTask; |
| 23 | +import org.gradle.api.GradleException; |
| 24 | +import org.gradle.api.plugins.JavaPluginConvention; |
| 25 | +import org.gradle.api.tasks.Input; |
| 26 | +import org.gradle.api.tasks.Optional; |
| 27 | +import org.gradle.api.tasks.OutputDirectory; |
| 28 | +import org.gradle.api.tasks.TaskAction; |
| 29 | +import org.ops4j.ramler.common.exc.RamlerException; |
| 30 | +import org.ops4j.ramler.java.JavaConfiguration; |
| 31 | +import org.ops4j.ramler.java.JavaGenerator; |
| 32 | + |
| 33 | +public abstract class AbstractRamlerJavaGenerator extends DefaultTask { |
| 34 | + |
| 35 | + /** RAML specification file, relative to <code>${project.basedir}</code>. */ |
| 36 | + private String model; |
| 37 | + |
| 38 | + /** |
| 39 | + * Output directory for generated sources. |
| 40 | + */ |
| 41 | + private String outputDir; |
| 42 | + |
| 43 | + /** |
| 44 | + * Fully qualified package name for generated Java sources. The generated classes will be |
| 45 | + * located in subpackages {@code model} and {@code api}. |
| 46 | + */ |
| 47 | + private String packageName; |
| 48 | + |
| 49 | + /** |
| 50 | + * Should discriminator properties be mutable? |
| 51 | + */ |
| 52 | + private boolean discriminatorMutable; |
| 53 | + |
| 54 | + /** |
| 55 | + * Suffix for interface names. This suffix is appended to the code name of a resource. The code |
| 56 | + * name is either specified explicitly by the {@code (codeName)} annotation, or implicitly by |
| 57 | + * the resource name, converted to camel case. |
| 58 | + */ |
| 59 | + private String interfaceNameSuffix; |
| 60 | + |
| 61 | + /** |
| 62 | + * Should Java classes include type information annotations for type hierarchies? |
| 63 | + */ |
| 64 | + private boolean jacksonTypeInfo; |
| 65 | + |
| 66 | + /** |
| 67 | + * Should Java classes include {@code JsonProperty} annotations for properties with illegal Java |
| 68 | + * names? |
| 69 | + */ |
| 70 | + private boolean jacksonPropertyName; |
| 71 | + |
| 72 | + /** |
| 73 | + * Should Jackson annotations {@code @JsonSerializer} etc. be used for union types? |
| 74 | + */ |
| 75 | + private boolean jacksonUnion; |
| 76 | + |
| 77 | + private boolean delegators; |
| 78 | + |
| 79 | + private String delegatorSuffix; |
| 80 | + |
| 81 | + private String delegateFieldName; |
| 82 | + |
| 83 | + /** |
| 84 | + * Gets the model. |
| 85 | + * |
| 86 | + * @return the model |
| 87 | + */ |
| 88 | + @Input |
| 89 | + public String getModel() { |
| 90 | + return model; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Sets the model. |
| 95 | + * |
| 96 | + * @param model |
| 97 | + * the model to set |
| 98 | + */ |
| 99 | + public void setModel(String model) { |
| 100 | + this.model = model; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Gets the outputDir. |
| 105 | + * |
| 106 | + * @return the outputDir |
| 107 | + */ |
| 108 | + @Input |
| 109 | + @OutputDirectory |
| 110 | + @Optional |
| 111 | + public String getOutputDir() { |
| 112 | + return outputDir; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Sets the outputDir. |
| 117 | + * |
| 118 | + * @param outputDir |
| 119 | + * the outputDir to set |
| 120 | + */ |
| 121 | + public void setOutputDir(String outputDir) { |
| 122 | + this.outputDir = outputDir; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Gets the packageName. |
| 127 | + * |
| 128 | + * @return the packageName |
| 129 | + */ |
| 130 | + @Input |
| 131 | + public String getPackageName() { |
| 132 | + return packageName; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Sets the packageName. |
| 137 | + * |
| 138 | + * @param packageName |
| 139 | + * the packageName to set |
| 140 | + */ |
| 141 | + public void setPackageName(String packageName) { |
| 142 | + this.packageName = packageName; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Gets the discriminatorMutable. |
| 147 | + * |
| 148 | + * @return the discriminatorMutable |
| 149 | + */ |
| 150 | + @Input |
| 151 | + public boolean isDiscriminatorMutable() { |
| 152 | + return discriminatorMutable; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Sets the discriminatorMutable. |
| 157 | + * |
| 158 | + * @param discriminatorMutable |
| 159 | + * the discriminatorMutable to set |
| 160 | + */ |
| 161 | + public void setDiscriminatorMutable(boolean discriminatorMutable) { |
| 162 | + this.discriminatorMutable = discriminatorMutable; |
| 163 | + } |
| 164 | + |
| 165 | + /** |
| 166 | + * Gets the interfaceNameSuffix. |
| 167 | + * |
| 168 | + * @return the interfaceNameSuffix |
| 169 | + */ |
| 170 | + @Input |
| 171 | + @Optional |
| 172 | + public String getInterfaceNameSuffix() { |
| 173 | + return interfaceNameSuffix; |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Sets the interfaceNameSuffix. |
| 178 | + * |
| 179 | + * @param interfaceNameSuffix |
| 180 | + * the interfaceNameSuffix to set |
| 181 | + */ |
| 182 | + public void setInterfaceNameSuffix(String interfaceNameSuffix) { |
| 183 | + this.interfaceNameSuffix = interfaceNameSuffix; |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Gets the jacksonTypeInfo. |
| 188 | + * |
| 189 | + * @return the jacksonTypeInfo |
| 190 | + */ |
| 191 | + @Input |
| 192 | + public boolean isJacksonTypeInfo() { |
| 193 | + return jacksonTypeInfo; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Sets the jacksonTypeInfo. |
| 198 | + * |
| 199 | + * @param jacksonTypeInfo |
| 200 | + * the jacksonTypeInfo to set |
| 201 | + */ |
| 202 | + public void setJacksonTypeInfo(boolean jacksonTypeInfo) { |
| 203 | + this.jacksonTypeInfo = jacksonTypeInfo; |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Gets the jacksonPropertyName. |
| 208 | + * |
| 209 | + * @return the jacksonPropertyName |
| 210 | + */ |
| 211 | + @Input |
| 212 | + public boolean isJacksonPropertyName() { |
| 213 | + return jacksonPropertyName; |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * Sets the jacksonPropertyName. |
| 218 | + * |
| 219 | + * @param jacksonPropertyName |
| 220 | + * the jacksonPropertyName to set |
| 221 | + */ |
| 222 | + public void setJacksonPropertyName(boolean jacksonPropertyName) { |
| 223 | + this.jacksonPropertyName = jacksonPropertyName; |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Gets the jacksonUnion. |
| 228 | + * |
| 229 | + * @return the jacksonUnion |
| 230 | + */ |
| 231 | + @Input |
| 232 | + public boolean isJacksonUnion() { |
| 233 | + return jacksonUnion; |
| 234 | + } |
| 235 | + |
| 236 | + /** |
| 237 | + * Sets the jacksonUnion. |
| 238 | + * |
| 239 | + * @param jacksonUnion |
| 240 | + * the jacksonUnion to set |
| 241 | + */ |
| 242 | + public void setJacksonUnion(boolean jacksonUnion) { |
| 243 | + this.jacksonUnion = jacksonUnion; |
| 244 | + } |
| 245 | + |
| 246 | + /** |
| 247 | + * Gets the delegators. |
| 248 | + * |
| 249 | + * @return the delegators |
| 250 | + */ |
| 251 | + @Input |
| 252 | + public boolean isDelegators() { |
| 253 | + return delegators; |
| 254 | + } |
| 255 | + |
| 256 | + /** |
| 257 | + * Sets the delegators. |
| 258 | + * |
| 259 | + * @param delegators |
| 260 | + * the delegators to set |
| 261 | + */ |
| 262 | + public void setDelegators(boolean delegators) { |
| 263 | + this.delegators = delegators; |
| 264 | + } |
| 265 | + |
| 266 | + /** |
| 267 | + * Gets the delegatorSuffix. |
| 268 | + * |
| 269 | + * @return the delegatorSuffix |
| 270 | + */ |
| 271 | + @Input |
| 272 | + @Optional |
| 273 | + public String getDelegatorSuffix() { |
| 274 | + return delegatorSuffix; |
| 275 | + } |
| 276 | + |
| 277 | + /** |
| 278 | + * Sets the delegatorSuffix. |
| 279 | + * |
| 280 | + * @param delegatorSuffix |
| 281 | + * the delegatorSuffix to set |
| 282 | + */ |
| 283 | + public void setDelegatorSuffix(String delegatorSuffix) { |
| 284 | + this.delegatorSuffix = delegatorSuffix; |
| 285 | + } |
| 286 | + |
| 287 | + /** |
| 288 | + * Gets the delegateFieldName. |
| 289 | + * |
| 290 | + * @return the delegateFieldName |
| 291 | + */ |
| 292 | + @Input |
| 293 | + @Optional |
| 294 | + public String getDelegateFieldName() { |
| 295 | + return delegateFieldName; |
| 296 | + } |
| 297 | + |
| 298 | + /** |
| 299 | + * Sets the delegateFieldName. |
| 300 | + * |
| 301 | + * @param delegateFieldName |
| 302 | + * the delegateFieldName to set |
| 303 | + */ |
| 304 | + public void setDelegateFieldName(String delegateFieldName) { |
| 305 | + this.delegateFieldName = delegateFieldName; |
| 306 | + } |
| 307 | + |
| 308 | + protected abstract String getDefaultOutputSubdir(); |
| 309 | + |
| 310 | + protected abstract String getSourceSet(); |
| 311 | + |
| 312 | + @TaskAction |
| 313 | + public void generate() { |
| 314 | + getLogger().info("Generating Java model from {}", model); |
| 315 | + String sourceFile = new File(getProject().getProjectDir(), model).getPath(); |
| 316 | + String outputDir = java.util.Optional.ofNullable(getOutputDir()) |
| 317 | + .orElse(new File(getProject().getBuildDir(), getDefaultOutputSubdir()).getPath()); |
| 318 | + JavaConfiguration config = new JavaConfiguration(); |
| 319 | + config.setSourceFile(sourceFile); |
| 320 | + config.setBasePackage(packageName); |
| 321 | + config.setTargetDir(new File(outputDir)); |
| 322 | + config.setDiscriminatorMutable(discriminatorMutable); |
| 323 | + config.setInterfaceNameSuffix(interfaceNameSuffix); |
| 324 | + config.setJacksonTypeInfo(jacksonTypeInfo); |
| 325 | + config.setJacksonPropertyName(jacksonPropertyName); |
| 326 | + config.setJacksonUnion(jacksonUnion); |
| 327 | + config.setDelegators(delegators); |
| 328 | + config.setDelegateFieldName(delegateFieldName); |
| 329 | + config.setDelegatorSuffix(delegatorSuffix); |
| 330 | + |
| 331 | + JavaPluginConvention javaPluginConvention = getProject().getConvention() |
| 332 | + .getPlugin(JavaPluginConvention.class); |
| 333 | + javaPluginConvention.getSourceSets() |
| 334 | + .getByName(getSourceSet()) |
| 335 | + .getJava() |
| 336 | + .srcDir(outputDir); |
| 337 | + |
| 338 | + try { |
| 339 | + JavaGenerator generator = new JavaGenerator(config); |
| 340 | + generator.generate(); |
| 341 | + } |
| 342 | + catch (RamlerException exc) { |
| 343 | + throw new GradleException("Code generation failed", exc); |
| 344 | + } |
| 345 | + } |
| 346 | +} |
0 commit comments