|
| 1 | +/** |
| 2 | + * Copyright 2013-2024 the original author or authors from the JHipster project. |
| 3 | + * |
| 4 | + * This file is part of the JHipster project, see https://www.jhipster.tech/ |
| 5 | + * for more information. |
| 6 | + * |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + */ |
| 19 | +import BaseApplicationGenerator from '../../../base-application/index.js'; |
| 20 | +import { GRADLE_BUILD_SRC_MAIN_DIR } from '../../../generator-constants.js'; |
| 21 | +import { javaMainResourceTemplatesBlock } from '../../support/files.js'; |
| 22 | + |
| 23 | +export default class OpenapiGeneratorGenerator extends BaseApplicationGenerator { |
| 24 | + async beforeQueue() { |
| 25 | + if (!this.fromBlueprint) { |
| 26 | + await this.composeWithBlueprints(); |
| 27 | + } |
| 28 | + |
| 29 | + if (!this.delegateToBlueprint) { |
| 30 | + await this.dependsOnBootstrapApplication(); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + get writing() { |
| 35 | + return this.asWritingTaskGroup({ |
| 36 | + async writing({ application }) { |
| 37 | + await this.writeFiles({ |
| 38 | + blocks: [ |
| 39 | + { templates: ['README.md.jhi.openapi-generator'] }, |
| 40 | + javaMainResourceTemplatesBlock({ templates: ['swagger/api.yml'] }), |
| 41 | + { |
| 42 | + condition: ctx => ctx.buildToolGradle, |
| 43 | + templates: [`${GRADLE_BUILD_SRC_MAIN_DIR}/jhipster.openapi-conventions.gradle`], |
| 44 | + }, |
| 45 | + ], |
| 46 | + context: application, |
| 47 | + }); |
| 48 | + }, |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + get [BaseApplicationGenerator.WRITING]() { |
| 53 | + return this.delegateTasksToBlueprint(() => this.writing); |
| 54 | + } |
| 55 | + |
| 56 | + get postWriting() { |
| 57 | + return this.asPostWritingTaskGroup({ |
| 58 | + addDependencies({ source, application }) { |
| 59 | + const { buildToolGradle, buildToolMaven, javaDependencies } = application; |
| 60 | + source.addJavaDependencies!([ |
| 61 | + { |
| 62 | + groupId: 'org.openapitools', |
| 63 | + artifactId: 'jackson-databind-nullable', |
| 64 | + version: javaDependencies!['jackson-databind-nullable'], |
| 65 | + }, |
| 66 | + ]); |
| 67 | + |
| 68 | + if (buildToolMaven) { |
| 69 | + source.addMavenDefinition!({ |
| 70 | + plugins: [{ groupId: 'org.openapitools', artifactId: 'openapi-generator-maven-plugin' }], |
| 71 | + pluginManagement: [ |
| 72 | + { |
| 73 | + groupId: 'org.openapitools', |
| 74 | + artifactId: 'openapi-generator-maven-plugin', |
| 75 | + version: javaDependencies!['openapi-generator-maven-plugin'], |
| 76 | + additionalContent: ` <executions> |
| 77 | + <execution> |
| 78 | + <goals> |
| 79 | + <goal>generate</goal> |
| 80 | + </goals> |
| 81 | + <configuration> |
| 82 | + <inputSpec>\${project.basedir}/${application.srcMainResources}swagger/api.yml</inputSpec> |
| 83 | + <generatorName>spring</generatorName> |
| 84 | + <apiPackage>${application.packageName}.web.api</apiPackage> |
| 85 | + <modelPackage>${application.packageName}.service.api.dto</modelPackage> |
| 86 | + <supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate> |
| 87 | + <skipValidateSpec>false</skipValidateSpec> |
| 88 | + <configOptions>${ |
| 89 | + application.reactive |
| 90 | + ? ` |
| 91 | + <reactive>true</reactive> |
| 92 | +` |
| 93 | + : '' |
| 94 | + } |
| 95 | + <delegatePattern>true</delegatePattern> |
| 96 | + <title>${application.dasherizedBaseName}</title> |
| 97 | + <useSpringBoot3>true</useSpringBoot3> |
| 98 | + </configOptions> |
| 99 | + </configuration> |
| 100 | + </execution> |
| 101 | + </executions> |
| 102 | +`, |
| 103 | + }, |
| 104 | + ], |
| 105 | + }); |
| 106 | + } |
| 107 | + if (buildToolGradle) { |
| 108 | + source.addGradleBuildSrcDependencyCatalogLibraries?.([ |
| 109 | + { |
| 110 | + libraryName: 'openapi-generator', |
| 111 | + module: 'org.openapitools:openapi-generator-gradle-plugin', |
| 112 | + version: javaDependencies!['gradle-openapi-generator'], |
| 113 | + scope: 'implementation', |
| 114 | + }, |
| 115 | + ]); |
| 116 | + source.addGradlePlugin?.({ id: 'jhipster.openapi-generator-conventions' }); |
| 117 | + } |
| 118 | + }, |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + get [BaseApplicationGenerator.POST_WRITING]() { |
| 123 | + return this.delegateTasksToBlueprint(() => this.postWriting); |
| 124 | + } |
| 125 | +} |
0 commit comments