Skip to content

Commit

Permalink
Support multiple versions of ktlint standard ruleset (#487)
Browse files Browse the repository at this point in the history
* Support multiple versions of ktlint standard ruleset

The StandardRulesetProvider from each supported version of ktlint is transformed with shadow plugin to a unique class so that multiple versions of the StandardRulesetProvider class can be compiled into the plugin.

Closes #426
Closes #35

* Increase build memory

(cherry picked from commit 516955b)
  • Loading branch information
paul-dingemans committed Apr 10, 2024
1 parent 01f7d52 commit 7c9d0b3
Show file tree
Hide file tree
Showing 18 changed files with 343 additions and 56 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Contrary to the [default plugin setup](https://github.com/JetBrains/intellij-platform-plugin-template) this plugin is set up as a multi-module project. This is required as the Ktlint artifact for the KtlintRuleEngine encloses the embeddable Kotlin compiler which conflicts with the IDE compiler.

The "lib" project relocates the conflicting classes. The "plugin" project uses the "lib" to include the (modified) Ktlint artifacts.
The "ktlint-lib" project relocates the conflicting classes, and provides the different versions of the rulesets. The "plugin" project uses the "lib" to include the (modified) Ktlint artifacts.

## Installation

Expand Down
4 changes: 3 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pluginRepositoryUrl = https://github.com/nbadal/ktlint-intellij-plugin
# other value. See https://plugins.jetbrains.com/docs/intellij/publishing-plugin.html#specifying-a-release-channel
# Users need to specify an additional repository to pick up publications from the non default channel. For example:
# - https://plugins.jetbrains.com/plugins/list?channel=beta&pluginId=com.nbadal.ktlint
pluginVersion = 0.22.0
pluginVersion = 0.23.0-beta-1

# Supported build number ranges and IntelliJ Platform versions -> https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
pluginSinceBuild = 213
Expand Down Expand Up @@ -38,3 +38,5 @@ org.gradle.caching = true

# Enable Gradle Kotlin DSL Lazy Property Assignment -> https://docs.gradle.org/current/userguide/kotlin_dsl.html#kotdsl:assignment
systemProp.org.gradle.unsafe.kotlin.assignment = true

org.gradle.jvmargs=-Xmx4g -Xms1g "-XX:MaxMetaspaceSize=1g"
1 change: 0 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ ktlintRuleEngine = { group = "com.pinterest.ktlint", name = "ktlint-rule-engine
ktlintCliRulesetCore = { group = "com.pinterest.ktlint", name = "ktlint-cli-ruleset-core", version.ref = "ktlint" }
ktlintCliReporterCore = { group = "com.pinterest.ktlint", name = "ktlint-cli-reporter-core", version.ref = "ktlint" }
ktlintCliReporterBaselineCore = { group = "com.pinterest.ktlint", name = "ktlint-cli-reporter-baseline", version.ref = "ktlint" }
ktlintRulesetStandard = { group = "com.pinterest.ktlint", name = "ktlint-ruleset-standard", version.ref = "ktlint" }

[plugins]
buildconfig = { id = "com.github.gmazzo.buildconfig", version.ref = "buildconfig" } # BuildConfig - read more: https://github.com/gmazzo/gradle-buildconfig-plugin
Expand Down
11 changes: 11 additions & 0 deletions ktlint-lib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Why is this a module?

The `ktlint-lib` contains several libraries to isolate functionalities from Ktlint in such a way that it does not conflict with the plugin.

## Core

The Ktlint RuleEngine core module requires certain elements of the kotlin compiler. As of that it includes a dependency on the embedded kotlin compiler library. This clashes and conflicts with classes we use in the JetBrains Kotlin plugin.

## Ruleset

Each ruleset library transforms the StandardRuleSetProvider of that version to a unique class name so that multiple versions of the Standard rule sets can be supported by the plugin. The plugin allows the user to configure one of the supported ktlint versios. In this way, the user can keep the configuration of the ktlint intellij plugin in sync with other plugins like the ktlint gradle plugin or kotlinter.
1 change: 0 additions & 1 deletion lib/build.gradle.kts → ktlint-lib/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ dependencies {
api(libs.ktlintCliRulesetCore)
api(libs.ktlintCliReporterCore)
api(libs.ktlintCliReporterBaselineCore)
implementation(libs.ktlintRulesetStandard)
}

tasks {
Expand Down
46 changes: 46 additions & 0 deletions ktlint-lib/ruleset-0-50-0/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("java") // Java support
alias(libs.plugins.kotlin) // Kotlin support
alias(libs.plugins.shadow)
}

repositories {
mavenCentral()
}

dependencies {
// Until version 0.50.0, the "mu.Kotlin" logger was used. In 1.x version this has been replaced with
// "io.github.oshai.kotlinlogging.KLogger".
implementation("com.pinterest.ktlint:ktlint-logger:0.50.0")
implementation("com.pinterest.ktlint:ktlint-ruleset-standard:0.50.0")
}

tasks {
// Set the compatibility versions to 11
withType<JavaCompile> {
sourceCompatibility = "11"
targetCompatibility = "11"
}
withType<KotlinCompile> {
kotlinOptions.jvmTarget = "11"
}

withType<ShadowJar> {
val api = project.configurations.api.get()
val impl = project.configurations.implementation.get()

configurations = listOf(api, impl).map { it.apply { isCanBeResolved = true } }

relocate(
"com.pinterest.ktlint.logger",
"shadow.com.pinterest.ktlint-0-50-0.logger",
)
relocate(
"com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider",
"shadow.com.pinterest.ktlint.ruleset.standard.StandardRuleSetProviderV0_50_0",
)
}
}
39 changes: 39 additions & 0 deletions ktlint-lib/ruleset-1-0-1/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("java") // Java support
alias(libs.plugins.kotlin) // Kotlin support
alias(libs.plugins.shadow)
}

repositories {
mavenCentral()
}

dependencies {
implementation("com.pinterest.ktlint:ktlint-ruleset-standard:1.0.1")
}

tasks {
// Set the compatibility versions to 11
withType<JavaCompile> {
sourceCompatibility = "11"
targetCompatibility = "11"
}
withType<KotlinCompile> {
kotlinOptions.jvmTarget = "11"
}

withType<ShadowJar> {
val api = project.configurations.api.get()
val impl = project.configurations.implementation.get()

configurations = listOf(api, impl).map { it.apply { isCanBeResolved = true } }

relocate(
"com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider",
"shadow.com.pinterest.ktlint.ruleset.standard.StandardRuleSetProviderV1_00_1",
)
}
}
39 changes: 39 additions & 0 deletions ktlint-lib/ruleset-1-1-1/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("java") // Java support
alias(libs.plugins.kotlin) // Kotlin support
alias(libs.plugins.shadow)
}

repositories {
mavenCentral()
}

dependencies {
implementation("com.pinterest.ktlint:ktlint-ruleset-standard:1.1.1")
}

tasks {
// Set the compatibility versions to 11
withType<JavaCompile> {
sourceCompatibility = "11"
targetCompatibility = "11"
}
withType<KotlinCompile> {
kotlinOptions.jvmTarget = "11"
}

withType<ShadowJar> {
val api = project.configurations.api.get()
val impl = project.configurations.implementation.get()

configurations = listOf(api, impl).map { it.apply { isCanBeResolved = true } }

relocate(
"com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider",
"shadow.com.pinterest.ktlint.ruleset.standard.StandardRuleSetProviderV1_01_1",
)
}
}
39 changes: 39 additions & 0 deletions ktlint-lib/ruleset-1-2-1/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("java") // Java support
alias(libs.plugins.kotlin) // Kotlin support
alias(libs.plugins.shadow)
}

repositories {
mavenCentral()
}

dependencies {
implementation("com.pinterest.ktlint:ktlint-ruleset-standard:1.2.1")
}

tasks {
// Set the compatibility versions to 11
withType<JavaCompile> {
sourceCompatibility = "11"
targetCompatibility = "11"
}
withType<KotlinCompile> {
kotlinOptions.jvmTarget = "11"
}

withType<ShadowJar> {
val api = project.configurations.api.get()
val impl = project.configurations.implementation.get()

configurations = listOf(api, impl).map { it.apply { isCanBeResolved = true } }

relocate(
"com.pinterest.ktlint.ruleset.standard.StandardRuleSetProvider",
"shadow.com.pinterest.ktlint.ruleset.standard.StandardRuleSetProviderV1_02_1",
)
}
}
10 changes: 0 additions & 10 deletions lib/README.md

This file was deleted.

20 changes: 16 additions & 4 deletions plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,28 @@ repositories {
dependencies {
// implementation(libs.annotations)

// Shadow lib (see: ../lib/README.md)
compileOnly(project(":lib")) // Required for IDE
implementation(project(":lib", "shadow"))
// Shadow lib (see: ../ktlint-lib/README.md)
compileOnly(project(":ktlint-lib:core")) // Required for IDE
implementation(project(":ktlint-lib:core", "shadow"))

compileOnly(project(":ktlint-lib:ruleset-0-50-0")) // Required for IDE
implementation(project(":ktlint-lib:ruleset-0-50-0", "shadow"))

compileOnly(project(":ktlint-lib:ruleset-1-0-1")) // Required for IDE
implementation(project(":ktlint-lib:ruleset-1-0-1", "shadow"))

compileOnly(project(":ktlint-lib:ruleset-1-1-1")) // Required for IDE
implementation(project(":ktlint-lib:ruleset-1-1-1", "shadow"))

compileOnly(project(":ktlint-lib:ruleset-1-2-1")) // Required for IDE
implementation(project(":ktlint-lib:ruleset-1-2-1", "shadow"))

implementation("com.rollbar:rollbar-java:1.10.0") {
exclude(group = "org.slf4j") // Duplicated in IDE environment
}

// Tests:
testImplementation(project(":lib"))
testImplementation(project(":ktlint-lib:core"))
testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
testImplementation("org.junit.platform:junit-platform-launcher:1.10.2")
testImplementation("io.mockk:mockk:1.13.10")
Expand Down
33 changes: 28 additions & 5 deletions plugin/src/main/kotlin/com/nbadal/ktlint/KtlintConfigForm.form
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.nbadal.ktlint.KtlintConfigForm">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="8" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="9" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="757" height="400"/>
Expand All @@ -10,12 +10,12 @@
<children>
<vspacer id="f22cd">
<constraints>
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="909eb" class="javax.swing.JLabel">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="6" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="strings" key="rulesetLabel"/>
Expand All @@ -24,7 +24,7 @@
</component>
<component id="c98b6" class="com.intellij.openapi.ui.TextFieldWithBrowseButton" binding="externalJarPaths">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="6" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
Expand Down Expand Up @@ -84,7 +84,7 @@
</component>
<component id="15acd" class="javax.swing.JButton" binding="githubButton">
<constraints>
<grid row="7" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
<grid row="8" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="4" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="strings" key="githubButton"/>
Expand Down Expand Up @@ -112,6 +112,29 @@
<text resource-bundle="strings" key="formatLabel"/>
</properties>
</component>
<component id="d4428" class="javax.swing.JComboBox" binding="rulesetVersion">
<constraints>
<grid row="5" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<model>
<item value="default (recommended)"/>
<item value="1.2.1"/>
<item value="1.1.1"/>
<item value="1.0.1"/>
<item value="0.50.0"/>
<item value="none"/>
</model>
</properties>
</component>
<component id="21f12" class="javax.swing.JLabel">
<constraints>
<grid row="5" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text resource-bundle="strings" key="rulesetVersionLabel"/>
</properties>
</component>
</children>
</grid>
<buttonGroups>
Expand Down
Loading

0 comments on commit 7c9d0b3

Please sign in to comment.