Skip to content

Commit

Permalink
add detekt generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Jul 25, 2024
1 parent 59d6bde commit a769e52
Show file tree
Hide file tree
Showing 17 changed files with 366 additions and 268 deletions.
5 changes: 5 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,8 @@ updates:
schedule:
interval: 'daily'
open-pull-requests-limit: 5
- package-ecosystem: 'gradle'
directory: '/generators/detekt/resources/'
schedule:
interval: 'daily'
open-pull-requests-limit: 5
8 changes: 7 additions & 1 deletion .yo-rc.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
{
"generator-jhipster": {
"additionalSubGenerators": "ktlint,migration",
"additionalSubGenerators": "detekt,ktlint,migration",
"baseName": "kotlin",
"cli": true,
"cliName": "khipster",
"entities": [],
"generators": {
"detekt": {
"command": false,
"priorities": ["loading", "writing", "postWriting"],
"sbs": null,
"written": true
},
"ktlint": {
"command": false,
"priorities": ["default"],
Expand Down
128 changes: 128 additions & 0 deletions generators/detekt/__snapshots__/generator.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`SubGenerator detekt of kotlin JHipster blueprint > gradle > should match source calls 1`] = `
{
"addGradlePluginToBuildScript": [
{
"group": "io.gitlab.arturbosch.detekt",
"name": "detekt-gradle-plugin",
"version": "\${detekt_version}",
},
],
"addGradleProperty": [
{
"property": "detekt_version",
"value": "'DETEKT-GRADLE-VERSION'",
},
],
"applyFromGradle": [
{
"script": "gradle/detekt.gradle",
},
],
}
`;

exports[`SubGenerator detekt of kotlin JHipster blueprint > gradle > should succeed 1`] = `
{
".yo-rc.json": {
"stateCleared": "modified",
},
"detekt-config.yml": {
"stateCleared": "modified",
},
"gradle/detekt.gradle": {
"stateCleared": "modified",
},
}
`;

exports[`SubGenerator detekt of kotlin JHipster blueprint > maven > should match source calls 1`] = `
{
"addJavaDefinition": [
{
"versions": [
{
"name": "maven-antrun-plugin",
"version": "3.0.0",
},
{
"name": "detekt",
"version": "'DETEKT-CLI-VERSION'",
},
],
},
],
"addMavenDefinition": [
{
"plugins": [
{
"additionalContent": " <executions>
<execution>
<!-- This can be run separately with mvn antrun:run@detekt -->
<id>detekt</id>
<phase>verify</phase>
<configuration>
<target name="detekt">
<!-- See https://arturbosch.github.io/detekt/cli.html for more options-->
<java taskname="detekt" dir="\${basedir}"
fork="true"
failonerror="false"
classname="io.gitlab.arturbosch.detekt.cli.Main"
classpathref="maven.plugin.classpath">
<arg value="--input"/>
<arg value="\${project.basedir}/src/main/kotlin"/>
<arg value="--report"/>
<arg value="xml:\${detekt.xmlReportFile}"/>
<arg value="--config"/>
<arg value="\${detekt.configFile}"/>
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>io.gitlab.arturbosch.detekt</groupId>
<artifactId>detekt-cli</artifactId>
<version>\${detekt.version}</version>
</dependency>
<!-- additional 3rd party ruleset(s) can be specified here -->
</dependencies>",
"artifactId": "maven-antrun-plugin",
"groupId": "org.apache.maven.plugins",
"version": "\${maven-antrun-plugin.version}",
},
],
"properties": [
{
"property": "detekt.configFile",
"value": "\${project.basedir}/detekt-config.yml",
},
{
"property": "detekt.xmlReportFile",
"value": "\${project.build.directory}/detekt-reports/detekt.xml",
},
{
"property": "sonar.kotlin.detekt.reportPaths",
"value": "\${detekt.xmlReportFile}",
},
],
},
],
}
`;

exports[`SubGenerator detekt of kotlin JHipster blueprint > maven > should succeed 1`] = `
{
".yo-rc.json": {
"stateCleared": "modified",
},
"detekt-config.yml": {
"stateCleared": "modified",
},
}
`;
138 changes: 138 additions & 0 deletions generators/detekt/generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import BaseApplicationGenerator from 'generator-jhipster/generators/base-application';
import { DETEKT_CONFIG_FILE, MAVEN_ANTRUN_VERSION } from '../spring-boot/kotlin-constants.js';

export default class extends BaseApplicationGenerator {
constructor(args, opts, features) {
super(args, opts, { ...features, queueCommandTasks: true });
}

async beforeQueue() {
await this.dependsOnBootstrapApplicationServer();
}

get [BaseApplicationGenerator.LOADING]() {
return this.asLoadingTaskGroup({
async loading({ application }) {
this.loadJavaDependenciesFromGradleCatalog(application.javaDependencies);
},
});
}

get [BaseApplicationGenerator.PREPARING]() {
return this.asPreparingTaskGroup({
async preparing({ applicationDefaults }) {
applicationDefaults({
detektConfigFile: DETEKT_CONFIG_FILE,
});
},
});
}

get [BaseApplicationGenerator.WRITING]() {
return this.asWritingTaskGroup({
async writing({ application }) {
await this.writeFiles({
blocks: [
{ templates: [{ file: DETEKT_CONFIG_FILE }] },
{ condition: ctx => ctx.buildToolGradle, templates: [{ file: 'gradle/detekt.gradle' }] },
],
context: application,
});
},
});
}

get [BaseApplicationGenerator.POST_WRITING]() {
return this.asPostWritingTaskGroup({
async customizeGradle({ application, source }) {
if (application.buildToolGradle) {
source.applyFromGradle({ script: 'gradle/detekt.gradle' });

source.addGradleProperty({ property: 'detekt_version', value: application.javaDependencies['detekt-gradle'] });

source.addGradlePluginToBuildScript({
group: 'io.gitlab.arturbosch.detekt',
name: 'detekt-gradle-plugin',
version: '${detekt_version}',
});

/*
// JHipster 8 based configuration
source.addJavaDefinition({
versions: [
{ name: 'detekt', version: DETEKT_VERSION },
],
});
source.addGradleDependencyCatalogPlugins([
{ pluginName: 'detekt', id: 'io.gitlab.arturbosch.detekt', version: application.javaDependencies['detekt-gradle'], addToBuild: true },
]);
*/
}
},

async customizeMaven({ application, source }) {
if (application.buildToolMaven) {
source.addJavaDefinition({
versions: [
{ name: 'maven-antrun-plugin', version: MAVEN_ANTRUN_VERSION },
{ name: 'detekt', version: application.javaDependencies['detekt-cli'] },
],
});

const antRunOther = ` <executions>
<execution>
<!-- This can be run separately with mvn antrun:run@detekt -->
<id>detekt</id>
<phase>verify</phase>
<configuration>
<target name="detekt">
<!-- See https://arturbosch.github.io/detekt/cli.html for more options-->
<java taskname="detekt" dir="$\{basedir}"
fork="true"
failonerror="false"
classname="io.gitlab.arturbosch.detekt.cli.Main"
classpathref="maven.plugin.classpath">
<arg value="--input"/>
<arg value="$\{project.basedir}/src/main/kotlin"/>
<arg value="--report"/>
<arg value="xml:$\{detekt.xmlReportFile}"/>
<arg value="--config"/>
<arg value="$\{detekt.configFile}"/>
</java>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>io.gitlab.arturbosch.detekt</groupId>
<artifactId>detekt-cli</artifactId>
<version>$\{detekt.version}</version>
</dependency>
<!-- additional 3rd party ruleset(s) can be specified here -->
</dependencies>`;

source.addMavenDefinition({
properties: [
{ property: 'detekt.configFile', value: `$\{project.basedir}/${DETEKT_CONFIG_FILE}` },
{ property: 'detekt.xmlReportFile', value: '${project.build.directory}/detekt-reports/detekt.xml' },
{ property: 'sonar.kotlin.detekt.reportPaths', value: '${detekt.xmlReportFile}' },
],
plugins: [
{
groupId: 'org.apache.maven.plugins',
artifactId: 'maven-antrun-plugin',
version: '${maven-antrun-plugin.version}',
additionalContent: antRunOther,
},
],
});
}
},
});
}
}
33 changes: 33 additions & 0 deletions generators/detekt/generator.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { beforeAll, describe, expect, it } from 'vitest';

import { fromMatrix, defaultHelpers as helpers, result } from 'generator-jhipster/testing';

const SUB_GENERATOR = 'detekt';
const SUB_GENERATOR_NAMESPACE = `jhipster-kotlin:${SUB_GENERATOR}`;

describe('SubGenerator detekt 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();
});
});
});
});
1 change: 1 addition & 0 deletions generators/detekt/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './generator.js';
1 change: 1 addition & 0 deletions generators/detekt/resources/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// required by dependabot
5 changes: 5 additions & 0 deletions generators/detekt/resources/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[libraries]
detekt-cli = { module = 'io.gitlab.arturbosch.detekt:detekt-cli', version = '1.21.0' }

[plugins]
detekt-gradle = { id = 'io.gitlab.arturbosch.detekt', version = '1.21.0' }
36 changes: 36 additions & 0 deletions generators/detekt/templates/gradle/detekt.gradle.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<%#
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
http://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.
-%>
apply plugin: "io.gitlab.arturbosch.detekt"

detekt {
input = files("src/main/kotlin")
config = files("<%= detektConfigFile %>")
reports {
xml {
required = true
outputLocation = file("$buildDir/reports/detekt/detekt.xml")
}
}
}

sonarqube {
properties {
property "sonar.kotlin.detekt.reportPaths", detekt.reports.xml.outputLocation
}
}
Loading

0 comments on commit a769e52

Please sign in to comment.