From 5579e10d06fa33ce386c6324f371802af5fe4575 Mon Sep 17 00:00:00 2001 From: Marcelo Boveto Shima Date: Thu, 25 Jul 2024 18:24:09 -0300 Subject: [PATCH] add kotlin generator --- .yo-rc.json | 8 +- .../__snapshots__/generator.spec.js.snap | 2 +- generators/detekt/generator.js | 5 +- .../__snapshots__/generator.spec.js.snap | 281 +++++++++++++++ generators/kotlin/generator.js | 323 ++++++++++++++++++ generators/kotlin/generator.spec.js | 33 ++ generators/kotlin/index.js | 1 + generators/kotlin/resources/build.gradle | 1 + .../resources/gradle/libs.versions.toml | 4 + generators/kotlin/support/constants.js | 2 + generators/kotlin/support/index.js | 1 + .../templates/gradle/kotlin.gradle.ejs | 11 +- generators/migration/generator.js | 34 ++ generators/spring-boot/files.js | 30 -- generators/spring-boot/generator.js | 315 +---------------- generators/spring-boot/kotlin-constants.js | 21 +- 16 files changed, 704 insertions(+), 368 deletions(-) create mode 100644 generators/kotlin/__snapshots__/generator.spec.js.snap create mode 100644 generators/kotlin/generator.js create mode 100644 generators/kotlin/generator.spec.js create mode 100644 generators/kotlin/index.js create mode 100644 generators/kotlin/resources/build.gradle create mode 100644 generators/kotlin/resources/gradle/libs.versions.toml create mode 100644 generators/kotlin/support/constants.js create mode 100644 generators/kotlin/support/index.js rename generators/{spring-boot => kotlin}/templates/gradle/kotlin.gradle.ejs (87%) delete mode 100644 generators/spring-boot/files.js diff --git a/.yo-rc.json b/.yo-rc.json index 5c09e251d..d36d62a9a 100644 --- a/.yo-rc.json +++ b/.yo-rc.json @@ -1,6 +1,6 @@ { "generator-jhipster": { - "additionalSubGenerators": "detekt,ktlint,migration", + "additionalSubGenerators": "detekt,ktlint,kotlin,migration", "baseName": "kotlin", "cli": true, "cliName": "khipster", @@ -12,6 +12,12 @@ "sbs": null, "written": true }, + "kotlin": { + "command": false, + "priorities": ["loading", "default", "postWriting"], + "sbs": null, + "written": true + }, "ktlint": { "command": false, "priorities": ["default"], diff --git a/generators/detekt/__snapshots__/generator.spec.js.snap b/generators/detekt/__snapshots__/generator.spec.js.snap index 79f2de866..2f09818a0 100644 --- a/generators/detekt/__snapshots__/generator.spec.js.snap +++ b/generators/detekt/__snapshots__/generator.spec.js.snap @@ -44,7 +44,7 @@ exports[`SubGenerator detekt of kotlin JHipster blueprint > maven > should match "versions": [ { "name": "maven-antrun-plugin", - "version": "3.0.0", + "version": "'MAVEN-ANTRUN-PLUGIN-VERSION'", }, { "name": "detekt", diff --git a/generators/detekt/generator.js b/generators/detekt/generator.js index 30e4cfc47..a6fee98b3 100644 --- a/generators/detekt/generator.js +++ b/generators/detekt/generator.js @@ -1,5 +1,6 @@ import BaseApplicationGenerator from 'generator-jhipster/generators/base-application'; -import { DETEKT_CONFIG_FILE, MAVEN_ANTRUN_VERSION } from '../spring-boot/kotlin-constants.js'; + +const DETEKT_CONFIG_FILE = 'detekt-config.yml'; export default class extends BaseApplicationGenerator { constructor(args, opts, features) { @@ -75,7 +76,7 @@ export default class extends BaseApplicationGenerator { if (application.buildToolMaven) { source.addJavaDefinition({ versions: [ - { name: 'maven-antrun-plugin', version: MAVEN_ANTRUN_VERSION }, + { name: 'maven-antrun-plugin', version: application.javaDependencies['maven-antrun-plugin'] }, { name: 'detekt', version: application.javaDependencies['detekt-cli'] }, ], }); diff --git a/generators/kotlin/__snapshots__/generator.spec.js.snap b/generators/kotlin/__snapshots__/generator.spec.js.snap new file mode 100644 index 000000000..d805af908 --- /dev/null +++ b/generators/kotlin/__snapshots__/generator.spec.js.snap @@ -0,0 +1,281 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`SubGenerator kotlin of kotlin JHipster blueprint > gradle > should match source calls 1`] = ` +{ + "addGradlePluginToBuildScript": [ + { + "group": "org.jetbrains.kotlin", + "name": "kotlin-gradle-plugin", + "version": "\${kotlin_version}", + }, + { + "group": "org.jetbrains.kotlin", + "name": "kotlin-allopen", + "version": "\${kotlin_version}", + }, + ], + "addGradleProperty": [ + { + "property": "kotlin_version", + "value": "'KOTLIN-VERSION'", + }, + ], + "applyFromGradle": [ + { + "script": "gradle/kotlin.gradle", + }, + ], +} +`; + +exports[`SubGenerator kotlin of kotlin JHipster blueprint > gradle > should succeed 1`] = ` +{ + ".yo-rc.json": { + "stateCleared": "modified", + }, + "buildSrc/build.gradle": { + "stateCleared": "modified", + }, + "buildSrc/gradle/libs.versions.toml": { + "stateCleared": "modified", + }, + "gradle/kotlin.gradle": { + "stateCleared": "modified", + }, + "gradle/libs.versions.toml": { + "stateCleared": "modified", + }, + "gradle/wrapper/gradle-wrapper.jar": { + "stateCleared": "modified", + }, + "gradle/wrapper/gradle-wrapper.properties": { + "stateCleared": "modified", + }, + "gradlew": { + "stateCleared": "modified", + }, + "gradlew.bat": { + "stateCleared": "modified", + }, +} +`; + +exports[`SubGenerator kotlin of kotlin JHipster blueprint > maven > should match source calls 1`] = ` +{ + "addJavaDefinition": [ + { + "dependencies": [ + { + "artifactId": "kotlin-stdlib-jdk8", + "groupId": "org.jetbrains.kotlin", + }, + { + "artifactId": "kotlin-reflect", + "groupId": "org.jetbrains.kotlin", + }, + { + "artifactId": "kotlin-test-junit", + "groupId": "org.jetbrains.kotlin", + "scope": "test", + }, + { + "name": "mapstruct", + "version": "'MAPSTRUCT-VERSION'", + }, + { + "artifactId": "mockito-kotlin", + "groupId": "org.mockito.kotlin", + "scope": "test", + "version": "'MOCKITO-KOTLIN-VERSION'", + }, + ], + "versions": [ + { + "name": "kotlin", + "version": "'KOTLIN-VERSION'", + }, + ], + }, + ], + "addMavenDefinition": [ + { + "dependencyManagement": [ + { + "artifactId": "kotlin-bom", + "groupId": "org.jetbrains.kotlin", + "scope": "import", + "type": "pom", + "version": "\${kotlin.version}", + }, + ], + "plugins": [ + { + "additionalContent": " + + + + default-compile + none + + + + default-testCompile + none + + + java-compile + compile + + compile + + + + java-test-compile + test-compile + + testCompile + + + + + none + ", + "artifactId": "maven-compiler-plugin", + "groupId": "org.apache.maven.plugins", + "version": "\${maven-compiler-plugin.version}", + }, + { + "additionalContent": " + + + kapt + + kapt + + + + \${project.basedir}/src/main/kotlin + \${project.basedir}/src/main/java + + + + org.mapstruct + mapstruct-processor + \${mapstruct.version} + + + + org.hibernate + hibernate-jpamodelgen + \${hibernate.version} + + + org.glassfish.jaxb + jaxb-runtime + \${jaxb-runtime.version} + + + + + + + compile + process-sources + + compile + + + + \${project.basedir}/src/main/kotlin + \${project.basedir}/src/main/java + + + + + test-compile + process-test-sources + + test-compile + + + + \${project.basedir}/src/test/kotlin + \${project.basedir}/src/test/java + + + + + + \${java.version} + true + + -Xjvm-default=all + + + spring + jpa + all-open + + + + + + + + + + org.jetbrains.kotlin + kotlin-maven-allopen + \${kotlin.version} + + + org.jetbrains.kotlin + kotlin-maven-noarg + \${kotlin.version} + + ", + "artifactId": "kotlin-maven-plugin", + "groupId": "org.jetbrains.kotlin", + "version": "\${kotlin.version}", + }, + ], + }, + ], + "mergeMavenPomContent": [ + { + "project": { + "build": { + "sourceDirectory": "src/main/kotlin/", + "testSourceDirectory": "src/test/kotlin/", + }, + }, + }, + ], +} +`; + +exports[`SubGenerator kotlin of kotlin JHipster blueprint > maven > should succeed 1`] = ` +{ + ".mvn/jvm.config": { + "stateCleared": "modified", + }, + ".mvn/wrapper/maven-wrapper.jar": { + "stateCleared": "modified", + }, + ".mvn/wrapper/maven-wrapper.properties": { + "stateCleared": "modified", + }, + ".yo-rc.json": { + "stateCleared": "modified", + }, + "mvnw": { + "stateCleared": "modified", + }, + "mvnw.cmd": { + "stateCleared": "modified", + }, + "pom.xml": { + "stateCleared": "modified", + }, +} +`; diff --git a/generators/kotlin/generator.js b/generators/kotlin/generator.js new file mode 100644 index 000000000..79fb0db9d --- /dev/null +++ b/generators/kotlin/generator.js @@ -0,0 +1,323 @@ +import BaseApplicationGenerator from 'generator-jhipster/generators/base-application'; +import { transform, passthrough } from '@yeoman/transform'; +import { SERVER_MAIN_SRC_KOTLIN_DIR, SERVER_TEST_SRC_KOTLIN_DIR } from './support/index.js'; + +export default class extends BaseApplicationGenerator { + constructor(args, opts, features) { + super(args, opts, { ...features, queueCommandTasks: true }); + } + + async beforeQueue() { + await this.dependsOnJHipster('jhipster:java:build-tool'); + } + + get [BaseApplicationGenerator.LOADING]() { + return this.asLoadingTaskGroup({ + async loadCatalog({ application }) { + this.loadJavaDependenciesFromGradleCatalog(application.javaDependencies); + }, + async applyKotlinDefaults({ application }) { + Object.assign(application, { + // We don't want to use to write any Java files + backendTypeJavaAny: false, + }); + }, + }); + } + + get [BaseApplicationGenerator.DEFAULT]() { + return this.asDefaultTaskGroup({ + async checkForJavaFiles() { + this.queueTransformStream( + { + name: 'removing remaining java files', + filter: file => file.path.endsWith('.java'), + refresh: true, + }, + transform(file => { + this.log.warn(`Remaining java file ${file.path} removed`); + }), + ); + }, + async convertGradleScripts({ application }) { + if (application.buildToolGradle) { + this.queueTransformStream( + { + name: 'updating gradle files', + filter: file => file.path.endsWith('.gradle'), + refresh: false, + }, + passthrough(file => { + file.contents = Buffer.from(file.contents.toString().replaceAll('classes/java/main', 'classes/kotlin/main')); + }), + ); + } + }, + }); + } + + get [BaseApplicationGenerator.WRITING]() { + return this.asWritingTaskGroup({ + async writeKotlinFiles({ application }) { + await this.writeFiles({ + blocks: [ + { + condition: ctx => ctx.buildToolGradle, + templates: [{ file: 'gradle/kotlin.gradle' }], + }, + ], + context: application, + }); + }, + }); + } + + get [BaseApplicationGenerator.POST_WRITING]() { + return this.asPostWritingTaskGroup({ + async customizeGradle({ application, source }) { + if (application.buildToolGradle) { + source.applyFromGradle({ + script: 'gradle/kotlin.gradle', + }); + + source.addGradleProperty({ property: 'kotlin_version', value: application.javaDependencies.kotlin }); + + source.addGradlePluginToBuildScript({ + group: 'org.jetbrains.kotlin', + name: 'kotlin-gradle-plugin', + version: '${kotlin_version}', + }); + source.addGradlePluginToBuildScript({ + group: 'org.jetbrains.kotlin', + name: 'kotlin-allopen', + version: '${kotlin_version}', + }); + + /* + // JHipster 8 based configuration + source.addJavaDefinition({ + versions: [ + { name: 'kotlin', version: KOTLIN_VERSION }, + ], + }); + + source.addGradleDependencyCatalogPlugins([ + { pluginName: 'kotlin-jvm', id: 'org.jetbrains.kotlin.jvm', 'version.ref': 'kotlin', addToBuild: true }, + { + pluginName: 'kotlin-allopen', + id: 'org.jetbrains.kotlin.plugin.allopen', + 'version.ref': 'kotlin', + addToBuild: true, + }, + ]); + */ + } + }, + async customizeMaven({ application, source }) { + if (application.buildToolMaven) { + source.mergeMavenPomContent({ + project: { + build: { + sourceDirectory: SERVER_MAIN_SRC_KOTLIN_DIR, + testSourceDirectory: SERVER_TEST_SRC_KOTLIN_DIR, + }, + }, + }); + + source.addJavaDefinition({ + versions: [{ name: 'kotlin', version: application.javaDependencies.kotlin }], + dependencies: [ + { groupId: 'org.jetbrains.kotlin', artifactId: 'kotlin-stdlib-jdk8' }, + { groupId: 'org.jetbrains.kotlin', artifactId: 'kotlin-reflect' }, + { groupId: 'org.jetbrains.kotlin', artifactId: 'kotlin-test-junit', scope: 'test' }, + { name: 'mapstruct', version: application.javaDependencies.mapstruct }, + { + groupId: 'org.mockito.kotlin', + artifactId: 'mockito-kotlin', + version: application.javaDependencies['mockito-kotlin'], + scope: 'test', + }, + ], + }); + if (application.reactive) { + source.addJavaDefinition({ + dependencies: [ + { groupId: 'org.jetbrains.kotlinx', artifactId: 'kotlinx-coroutines-debug' }, + { groupId: 'org.jetbrains.kotlinx', artifactId: 'kotlinx-coroutines-reactor' }, + { groupId: 'io.projectreactor.kotlin', artifactId: 'reactor-kotlin-extensions' }, + ], + }); + } + + source.addMavenDefinition({ + dependencyManagement: [ + { + groupId: 'org.jetbrains.kotlin', + artifactId: 'kotlin-bom', + version: '${kotlin.version}', + type: 'pom', + scope: 'import', + }, + ], + plugins: [ + { + groupId: 'org.apache.maven.plugins', + artifactId: 'maven-compiler-plugin', + version: '${maven-compiler-plugin.version}', + additionalContent: ` + + + + default-compile + none + + + + default-testCompile + none + + + java-compile + compile + + compile + + + + java-test-compile + test-compile + + testCompile + + + + + none + `, + }, + { + groupId: 'org.jetbrains.kotlin', + artifactId: 'kotlin-maven-plugin', + version: '${kotlin.version}', + additionalContent: ` + + + kapt + + kapt + + + + $\{project.basedir}/src/main/kotlin + $\{project.basedir}/src/main/java + + + + org.mapstruct + mapstruct-processor + $\{mapstruct.version} + + ${ + application.databaseTypeSql + ? ` + + org.hibernate + hibernate-jpamodelgen + $\{hibernate.version} + + + org.glassfish.jaxb + jaxb-runtime + $\{jaxb-runtime.version} + ` + : '' + } + ${ + application.databaseTypeCassandra + ? ` + + com.datastax.oss + java-driver-mapper-processor + $\{cassandra-driver.version} + ` + : '' + } + + + + + compile + process-sources + + compile + + + + $\{project.basedir}/src/main/kotlin + $\{project.basedir}/src/main/java + + + + + test-compile + process-test-sources + + test-compile + + + + $\{project.basedir}/src/test/kotlin + $\{project.basedir}/src/test/java + + + + + + $\{java.version} + true + + -Xjvm-default=all + + + spring${ + application.databaseTypeSql + ? ` + jpa + all-open` + : '' + } + ${ + application.databaseTypeSql + ? ` + + + + + ` + : '' + } + + + + org.jetbrains.kotlin + kotlin-maven-allopen + $\{kotlin.version} + + ${ + application.databaseTypeSql + ? ` + org.jetbrains.kotlin + kotlin-maven-noarg + $\{kotlin.version} + ` + : '' + } + `, + }, + ], + }); + } + }, + }); + } +} diff --git a/generators/kotlin/generator.spec.js b/generators/kotlin/generator.spec.js new file mode 100644 index 000000000..d2f2b79ac --- /dev/null +++ b/generators/kotlin/generator.spec.js @@ -0,0 +1,33 @@ +import { beforeAll, describe, expect, it } from 'vitest'; + +import { fromMatrix, defaultHelpers as helpers, result } from 'generator-jhipster/testing'; + +const SUB_GENERATOR = 'kotlin'; +const SUB_GENERATOR_NAMESPACE = `jhipster-kotlin:${SUB_GENERATOR}`; + +describe('SubGenerator kotlin of kotlin JHipster blueprint', () => { + Object.entries(fromMatrix({ buildTool: ['maven', 'gradle'] })).forEach(([name, config]) => { + describe(name, () => { + beforeAll(async function () { + await helpers + .run(SUB_GENERATOR_NAMESPACE) + .withJHipsterConfig(config) + .withOptions({ + ignoreNeedlesError: true, + skipKtlintFormat: true, + }) + .withJHipsterLookup() + .withMockedSource() + .withParentBlueprintLookup(['generators', 'generators/*/generators']); + }); + + it('should succeed', () => { + expect(result.getStateSnapshot()).toMatchSnapshot(); + }); + + it('should match source calls', () => { + expect(result.sourceCallsArg).toMatchSnapshot(); + }); + }); + }); +}); diff --git a/generators/kotlin/index.js b/generators/kotlin/index.js new file mode 100644 index 000000000..01e9dc34d --- /dev/null +++ b/generators/kotlin/index.js @@ -0,0 +1 @@ +export { default } from './generator.js'; diff --git a/generators/kotlin/resources/build.gradle b/generators/kotlin/resources/build.gradle new file mode 100644 index 000000000..fd4321c8b --- /dev/null +++ b/generators/kotlin/resources/build.gradle @@ -0,0 +1 @@ +// required by dependabot diff --git a/generators/kotlin/resources/gradle/libs.versions.toml b/generators/kotlin/resources/gradle/libs.versions.toml new file mode 100644 index 000000000..afdbd2da2 --- /dev/null +++ b/generators/kotlin/resources/gradle/libs.versions.toml @@ -0,0 +1,4 @@ +[libraries] +kotlin = { module = 'org.jetbrains.kotlin:kotlin-bom', version = '1.6.21' } + +mockito-kotlin = { module = 'org.mockito.kotlin:mockito-kotlin', version = '4.0.0' } diff --git a/generators/kotlin/support/constants.js b/generators/kotlin/support/constants.js new file mode 100644 index 000000000..9bc9fcb7f --- /dev/null +++ b/generators/kotlin/support/constants.js @@ -0,0 +1,2 @@ +export const SERVER_MAIN_SRC_KOTLIN_DIR = 'src/main/kotlin/'; +export const SERVER_TEST_SRC_KOTLIN_DIR = 'src/test/kotlin/'; diff --git a/generators/kotlin/support/index.js b/generators/kotlin/support/index.js new file mode 100644 index 000000000..4f07201dc --- /dev/null +++ b/generators/kotlin/support/index.js @@ -0,0 +1 @@ +export * from './constants.js'; diff --git a/generators/spring-boot/templates/gradle/kotlin.gradle.ejs b/generators/kotlin/templates/gradle/kotlin.gradle.ejs similarity index 87% rename from generators/spring-boot/templates/gradle/kotlin.gradle.ejs rename to generators/kotlin/templates/gradle/kotlin.gradle.ejs index facf190f5..7de755c3c 100644 --- a/generators/spring-boot/templates/gradle/kotlin.gradle.ejs +++ b/generators/kotlin/templates/gradle/kotlin.gradle.ejs @@ -29,8 +29,9 @@ apply plugin: "kotlin-jpa" // See https://kotlinlang.org/docs/reference/compiler apply plugin: "org.jlleitschuh.gradle.ktlint" dependencies { - implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlin_version}" - implementation "org.jetbrains.kotlin:kotlin-reflect:${kotlin_version}" + implementation platform "org.jetbrains.kotlin:kotlin-bom:${kotlin_version}" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8" + implementation "org.jetbrains.kotlin:kotlin-reflect" <%_ if (reactive) { _%> implementation "org.jetbrains.kotlinx:kotlinx-coroutines-debug" @@ -47,9 +48,9 @@ dependencies { kapt "com.datastax.oss:java-driver-mapper-processor:${cassandraDriverVersion}" <%_ } _%> - testImplementation "org.jetbrains.kotlin:kotlin-test-junit:${kotlin_version}" + testImplementation "org.jetbrains.kotlin:kotlin-test-junit" - testImplementation "org.mockito.kotlin:mockito-kotlin:<%= MOCKITO_KOTLIN_VERSION %>" + testImplementation "org.mockito.kotlin:mockito-kotlin:<%= javaDependencies['mockito-kotlin'] %>" }<%_ if (databaseTypeSql) { %> allOpen { @@ -60,7 +61,7 @@ allOpen { [compileKotlin, compileTestKotlin]*.with { kotlinOptions { - jvmTarget = "<%= JAVA_VERSION %>" + jvmTarget = "<%= javaVersion %>" javaParameters = true freeCompilerArgs = ["-Xjvm-default=all"] } diff --git a/generators/migration/generator.js b/generators/migration/generator.js index 9dcae9ecf..1c30e60d6 100644 --- a/generators/migration/generator.js +++ b/generators/migration/generator.js @@ -1,7 +1,33 @@ import BaseApplicationGenerator from 'generator-jhipster/generators/base-application'; import { createNeedleCallback } from 'generator-jhipster/generators/base/support'; +import { passthrough } from '@yeoman/transform'; export default class extends BaseApplicationGenerator { + get [BaseApplicationGenerator.DEFAULT]() { + return this.asDefaultTaskGroup({ + async defaultTask({ application }) { + if (application.buildToolGradle) { + this.queueTransformStream( + { + name: 'updating gradle files', + filter: file => file.path.endsWith('.gradle'), + refresh: false, + }, + passthrough(file => { + file.contents = Buffer.from( + file.contents + .toString() + .replaceAll('html.enabled =', 'html.required =') + .replaceAll('xml.enabled =', 'xml.required =') + .replaceAll('csv.enabled =', 'csv.required ='), + ); + }), + ); + } + }, + }); + } + get [BaseApplicationGenerator.PREPARING]() { return this.asPreparingTaskGroup({ async source({ application, source }) { @@ -27,6 +53,14 @@ export default class extends BaseApplicationGenerator { get [BaseApplicationGenerator.POST_WRITING]() { return this.asPostWritingTaskGroup({ + removeScripts({ application }) { + if (application.applicationTypeGateway || application.gatewayServerPort) { + // Readiness port is not correctly exposed in gateways + // Don't wait for readiness state + const scriptsStorage = this.packageJson.createStorage('scripts'); + scriptsStorage.delete('pree2e:headless'); + } + }, async postWritingTemplateTask({ application }) { if (application.buildToolGradle) { // JHipster 8 have needles fixed diff --git a/generators/spring-boot/files.js b/generators/spring-boot/files.js deleted file mode 100644 index d2925cc95..000000000 --- a/generators/spring-boot/files.js +++ /dev/null @@ -1,30 +0,0 @@ -/** - * Copyright 2013-2024 the original author or authors from the JHipster project. - * - * This file is part of the JHipster project, see https://www.jhipster.tech/ - * for more information. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -import { buildToolTypes } from 'generator-jhipster/jdl'; - -const { GRADLE } = buildToolTypes; - -export const kotlinAdditionalFiles = { - serverBuild: [ - { - condition: generator => generator.buildTool === GRADLE, - templates: [{ file: 'gradle/kotlin.gradle' }], - }, - ], -}; diff --git a/generators/spring-boot/generator.js b/generators/spring-boot/generator.js index 44e84bb2c..0519b01fd 100644 --- a/generators/spring-boot/generator.js +++ b/generators/spring-boot/generator.js @@ -1,16 +1,13 @@ import { dirname, join } from 'path'; import { fileURLToPath } from 'url'; -import { transform, passthrough } from '@yeoman/transform'; import BaseApplicationGenerator from 'generator-jhipster/generators/spring-boot'; import { prepareSqlApplicationProperties } from 'generator-jhipster/generators/spring-data-relational/support'; import { files as entityServerFiles } from 'jhipster-7-templates/esm/generators/entity-server'; import { getEnumInfo } from 'generator-jhipster/generators/base-application/support'; import { files as serverFiles } from 'jhipster-7-templates/esm/generators/server'; -import { kotlinAdditionalFiles } from './files.js'; import migration from './migration.cjs'; import { serverFiles as sqlFiles } from './files-sql.js'; -import { KOTLIN_VERSION, MAPSTRUCT_VERSION, MAVEN_ANTRUN_VERSION, MOCKITO_KOTLIN_VERSION } from './kotlin-constants.js'; import { entityCouchbaseFiles } from './entity-files-couchbase.js'; const { jhipsterConstants, jhipster7DockerContainers } = migration; @@ -22,7 +19,6 @@ const { JACOCO_VERSION, JIB_VERSION, GRADLE_VERSION, - JAVA_VERSION, JAVA_COMPATIBLE_VERSIONS, JHIPSTER_DEPENDENCIES_VERSION, JACKSON_DATABIND_NULLABLE_VERSION, @@ -62,6 +58,8 @@ const customizeSpringBootFiles = file => { return file; }; +const JAVA_VERSION = '11'; + export default class extends BaseApplicationGenerator { constructor(args, options, features) { super(args, options, { ...features, jhipster7Migration: true, checkBlueprint: true, inheritTasks: true }); @@ -74,12 +72,13 @@ export default class extends BaseApplicationGenerator { } async beforeQueue() { - await this.dependsOnJHipster('server'); await this.dependsOnJHipster('jhipster:java:build-tool', { // We want to use v7 build-tool templates generatorOptions: { skipPriorities: ['writing', 'postWriting'] }, }); await this.dependsOnJHipster('jhipster-kotlin:migration'); + await this.dependsOnJHipster('jhipster-kotlin:kotlin'); + await this.dependsOnJHipster('server'); await this.dependsOnJHipster('jhipster-kotlin:ktlint'); } @@ -130,8 +129,6 @@ export default class extends BaseApplicationGenerator { ...super.loading, async applyKotlinDefaults({ application }) { Object.assign(application, { - // We don't want to use to write any Java files - backendTypeJavaAny: false, // syncUserWithIdp disabled is not supported by kotlin blueprint syncUserWithIdp: application.authenticationType === 'oauth2', }); @@ -171,7 +168,6 @@ export default class extends BaseApplicationGenerator { javaDir: application.packageFolder, DOCKER_COMPOSE_FORMAT_VERSION, - MOCKITO_KOTLIN_VERSION, GRADLE_VERSION, SPRING_BOOT_VERSION, LIQUIBASE_VERSION, @@ -257,38 +253,6 @@ export default class extends BaseApplicationGenerator { serviceDiscoveryType: application.serviceDiscoveryType === 'no' ? false : application.serviceDiscoveryType, }); }, - async defaultTemplateTask({ application }) { - this.queueTransformStream( - { - name: 'removing remaining java files', - filter: file => file.path.endsWith('.java'), - refresh: true, - }, - transform(file => { - this.log.warn(`Remaining java file ${file.path} removed`); - }), - ); - - if (application.buildToolGradle) { - this.queueTransformStream( - { - name: 'updating gradle files', - filter: file => file.path.endsWith('.gradle'), - refresh: false, - }, - passthrough(file => { - file.contents = Buffer.from( - file.contents - .toString() - .replaceAll('classes/java/main', 'classes/kotlin/main') - .replaceAll('html.enabled =', 'html.required =') - .replaceAll('xml.enabled =', 'xml.required =') - .replaceAll('csv.enabled =', 'csv.required ='), - ); - }), - ); - } - }, }); } @@ -303,10 +267,6 @@ export default class extends BaseApplicationGenerator { context: application, customizeTemplatePath: customizeSpringBootFiles, }); - await this.writeFiles({ - sections: kotlinAdditionalFiles, - context: application, - }); }, async writeSqlFiles({ application }) { if (!application.databaseTypeSql) return; @@ -407,275 +367,10 @@ export default class extends BaseApplicationGenerator { get [BaseApplicationGenerator.POST_WRITING]() { return this.asPostWritingTaskGroup({ - removeScripts({ application }) { - if (application.applicationTypeGateway || application.gatewayServerPort) { - // Readiness port is not correctly exposed in gateways - // Don't wait for readiness state - const scriptsStorage = this.packageJson.createStorage('scripts'); - scriptsStorage.delete('pree2e:headless'); - } - }, - async customizeGradle({ application, source }) { - if (application.buildToolGradle) { - source.applyFromGradle({ - script: 'gradle/kotlin.gradle', - }); - - source.addGradleProperty({ property: 'kotlin_version', value: KOTLIN_VERSION }); - source.addGradleProperty({ property: 'mapstruct_version', value: MAPSTRUCT_VERSION }); - - source.addGradlePluginToBuildScript({ - group: 'org.jetbrains.kotlin', - name: 'kotlin-gradle-plugin', - version: '${kotlin_version}', - }); - source.addGradlePluginToBuildScript({ - group: 'org.jetbrains.kotlin', - name: 'kotlin-allopen', - version: '${kotlin_version}', - }); - if (application.databaseTypeSql) { - source.addGradlePluginToBuildScript({ - group: 'org.jetbrains.kotlin', - name: 'kotlin-noarg', - version: '${kotlin_version}', - }); - } - - /* - // JHipster 8 based configuration - source.addJavaDefinition({ - versions: [ - { name: 'kotlin', version: KOTLIN_VERSION }, - ], - }); - - source.addGradleDependencyCatalogPlugins([ - { pluginName: 'kotlin-jvm', id: 'org.jetbrains.kotlin.jvm', 'version.ref': 'kotlin', addToBuild: true }, - { - pluginName: 'kotlin-allopen', - id: 'org.jetbrains.kotlin.plugin.allopen', - 'version.ref': 'kotlin', - addToBuild: true, - }, - ]); - if (application.databaseTypeSql) { - source.addGradleDependencyCatalogPlugins([ - { - pluginName: 'kotlin-noarg', - id: 'org.jetbrains.kotlin.plugin.noarg', - 'version.ref': 'kotlin', - addToBuild: true, - }, - ]); - } - */ - } - }, - async customizeMaven({ application, source }) { if (application.buildToolMaven) { - source.mergeMavenPomContent({ - project: { - build: { - sourceDirectory: SERVER_MAIN_SRC_KOTLIN_DIR, - testSourceDirectory: SERVER_TEST_SRC_KOTLIN_DIR, - }, - }, - }); - - source.addJavaDefinition({ - versions: [ - { name: 'kotlin', version: KOTLIN_VERSION }, - { name: 'mapstruct', version: MAPSTRUCT_VERSION }, - { name: 'maven-antrun-plugin', version: MAVEN_ANTRUN_VERSION }, - { name: 'modernizer-maven-plugin', version: '2.6.0' }, - ], - dependencies: [ - { groupId: 'org.jetbrains.kotlin', artifactId: 'kotlin-stdlib-jdk8' }, - { groupId: 'org.jetbrains.kotlinx', artifactId: 'kotlinx-coroutines-debug' }, - { groupId: 'org.jetbrains.kotlinx', artifactId: 'kotlinx-coroutines-reactor' }, - { groupId: 'io.projectreactor.kotlin', artifactId: 'reactor-kotlin-extensions' }, - { groupId: 'com.fasterxml.jackson.datatype', artifactId: 'jackson-datatype-json-org' }, - { groupId: 'org.jetbrains.kotlin', artifactId: 'kotlin-reflect', versionRef: 'kotlin' }, - { groupId: 'org.jetbrains.kotlin', artifactId: 'kotlin-test-junit', versionRef: 'kotlin', scope: 'test' }, - { - groupId: 'org.mockito.kotlin', - artifactId: 'mockito-kotlin', - version: MOCKITO_KOTLIN_VERSION, - scope: 'test', - }, - ], - }); - - // NOTE: Add proper indentation of the configuration tag - const kotlinOther = ` - - kapt - - kapt - - - - $\{project.basedir}/src/main/kotlin - $\{project.basedir}/src/main/java - - - - org.mapstruct - mapstruct-processor - $\{mapstruct.version} - - ${ - application.databaseTypeSql - ? ` - - org.hibernate - hibernate-jpamodelgen - $\{hibernate.version} - - - org.glassfish.jaxb - jaxb-runtime - $\{jaxb-runtime.version} - ` - : '' - } - ${ - application.databaseTypeCassandra - ? ` - - com.datastax.oss - java-driver-mapper-processor - $\{cassandra-driver.version} - ` - : '' - } - - - - - compile - process-sources - - compile - - - - $\{project.basedir}/src/main/kotlin - $\{project.basedir}/src/main/java - - - - - test-compile - process-test-sources - - test-compile - - - - $\{project.basedir}/src/test/kotlin - $\{project.basedir}/src/test/java - - - - - - $\{java.version} - true - - -Xjvm-default=all - - - spring${ - application.databaseTypeSql - ? ` - jpa - all-open` - : '' - } - ${ - application.databaseTypeSql - ? ` - - - - - ` - : '' - } - - - - org.jetbrains.kotlin - kotlin-maven-allopen - $\{kotlin.version} - - ${ - application.databaseTypeSql - ? ` - org.jetbrains.kotlin - kotlin-maven-noarg - $\{kotlin.version} - ` - : '' - } - `; - - const defaultCompileOther = ` - - - default-compile - none - - - - default-testCompile - none - - - java-compile - compile - - compile - - - - java-test-compile - test-compile - - testCompile - - - - - none - `; - source.addMavenDefinition({ - properties: [ - { property: 'modernizer.failOnViolations', value: 'false' }, - { property: 'sonar.coverage.jacoco.xmlReportPaths', value: '${jacoco.reportFolder}/jacoco.xml' }, - ], - dependencyManagement: [ - { groupId: 'org.jetbrains.kotlin', artifactId: 'kotlin-stdlib', version: '${kotlin.version}' }, - { groupId: 'org.jetbrains.kotlin', artifactId: 'kotlin-stdlib-jdk8', version: '${kotlin.version}' }, - ], - plugins: [ - { - groupId: 'org.jetbrains.kotlin', - artifactId: 'kotlin-maven-plugin', - version: '${kotlin.version}', - additionalContent: kotlinOther, - }, - { - groupId: 'org.apache.maven.plugins', - artifactId: 'maven-compiler-plugin', - version: '${maven-compiler-plugin.version}', - additionalContent: defaultCompileOther, - }, - ], + properties: [{ property: 'modernizer.failOnViolations', value: 'false' }], }); } }, diff --git a/generators/spring-boot/kotlin-constants.js b/generators/spring-boot/kotlin-constants.js index 0684c35e8..203a356df 100644 --- a/generators/spring-boot/kotlin-constants.js +++ b/generators/spring-boot/kotlin-constants.js @@ -19,28 +19,11 @@ import { JAVA_SERVER_TEST_RESOURCES_DIR, JAVA_MAIN_RESOURCES_DIR } from 'generator-jhipster'; -export const DETEKT_VERSION = '1.21.0'; -export const KOTLIN_VERSION = '1.6.21'; -export const MAPSTRUCT_VERSION = '1.5.3.Final'; -export const MAVEN_ANTRUN_VERSION = '3.0.0'; -export const MOCKITO_KOTLIN_VERSION = '4.0.0'; - -export const DETEKT_CONFIG_FILE = 'detekt-config.yml'; - -export const MAIN_DIR = 'src/main/'; -export const TEST_DIR = 'src/test/'; +const MAIN_DIR = 'src/main/'; +const TEST_DIR = 'src/test/'; export const KOTLIN_MAIN_SRC_DIR = `${MAIN_DIR}kotlin/`; export const KOTLIN_TEST_SRC_DIR = `${TEST_DIR}kotlin/`; export const KOTLIN_TEST_RES_DIR = JAVA_SERVER_TEST_RESOURCES_DIR; export const KOTLIN_MAIN_RES_DIR = JAVA_MAIN_RESOURCES_DIR; - -export const constants = { - KOTLIN_VERSION, - MOCKITO_KOTLIN_VERSION, - DETEKT_VERSION, - MAVEN_ANTRUN_VERSION, - DETEKT_CONFIG_FILE, - MAPSTRUCT_VERSION, -};