Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

configure ktlint with custom rules #9

Merged
merged 10 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions build-plugins/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ repositories {
dependencies {
implementation(gradleApi())
implementation(kotlin("gradle-plugin", "1.8.22"))
// make our custom lint rules available to the buildscript classpath
runtimeOnly(project(":ktlint-rules"))
}

group = "aws.sdk.kotlin"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.gradle.dsl

import aws.sdk.kotlin.gradle.util.verifyRootProject
import org.gradle.api.Project
import org.gradle.api.attributes.Bundling
import org.gradle.api.tasks.JavaExec
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.register

/**
* Configure lint rules for the project
* @param lintPaths list of paths relative to the project root to lint (or not lint).
*/
fun Project.configureLinting(lintPaths: List<String>) {
verifyRootProject { "Kotlin SDK lint configuration is expected to be configured on the root project" }

val ktlint = configurations.create("ktlint") {
attributes {
attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL))
}
}

// TODO - is there anyway to align this with the version from libs.versions.toml in this project/repo
val ktlintVersion = "0.48.1"
Comment on lines +28 to +29
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Would it work to just use `val ktlintVersion: String by project" and pull it in from the root project? That would mean potentially separate versions across the repos but that's what we have now and we're not (yet) planning to commonize the version numbers right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are planning to commonize the version numbers (for ktlint anyway) because we've moved the rules into this plugin/project. Downstream consumers won't need to set ktlint version at all, just the paths to lint. Theres no real reason to make the downstream consumers set a version, it doesn't gain us anything really.

dependencies {
ktlint("com.pinterest:ktlint:$ktlintVersion")
}

// add the buildscript classpath which should pickup our custom ktlint-rules (via runtimeOnly dep on this plugin)
// plus any custom rules added by consumer
val execKtlintClaspath = ktlint + buildscript.configurations.getByName("classpath")
tasks.register<JavaExec>("ktlint") {
description = "Check Kotlin code style."
group = "Verification"
classpath = execKtlintClaspath
mainClass.set("com.pinterest.ktlint.Main")
args = lintPaths
jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
}

tasks.register<JavaExec>("ktlintFormat") {
description = "Auto fix Kotlin code style violations"
group = "formatting"
classpath = execKtlintClaspath
mainClass.set("com.pinterest.ktlint.Main")
args = listOf("-F") + lintPaths
jvmArgs("--add-opens", "java.base/java.lang=ALL-UNNAMED")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package aws.sdk.kotlin.gradle.kmp

import aws.sdk.kotlin.gradle.util.verifyRootProject
import org.gradle.api.Plugin
import org.gradle.api.Project

Expand All @@ -15,7 +16,10 @@ import org.gradle.api.Project
*/
class KmpDefaultsPlugin : Plugin<Project> {
override fun apply(target: Project) {
target.logger.info("applying kmp defaults plugin to $target")
target.configureKmpTargets()
with(target) {
logger.info("applying kmp defaults plugin to $target")
verifyRootProject { "AWS SDK KmpDefaultsPlugin requires installation into root project" }
configureKmpTargets()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.gradle.kmp

import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.the
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension

/**
* Allows configuration from parent projects subprojects/allprojects block when they haven't configured the KMP
* plugin but the subproject has applied it. The extension is otherwise not visible.
*/
fun Project.kotlin(block: KotlinMultiplatformExtension.() -> Unit) {
configure(block)
}
val Project.kotlin: KotlinMultiplatformExtension get() = the()
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,15 @@
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.gradle.kmp
package aws.sdk.kotlin.gradle.util

import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.plugins.ExtraPropertiesExtension
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.extra
import org.gradle.kotlin.dsl.the
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import java.io.File
import java.util.*

/**
* Allows configuration from parent projects subprojects/allprojects block when they haven't configured the KMP
* plugin but the subproject has applied it. The extension is otherwise not visible.
*/
fun Project.kotlin(block: KotlinMultiplatformExtension.() -> Unit) {
configure(block)
}
val Project.kotlin: KotlinMultiplatformExtension get() = the()

public fun <T> ExtraPropertiesExtension.getOrNull(name: String): T? {
if (!has(name)) return null
@Suppress("UNCHECKED_CAST")
Expand Down Expand Up @@ -68,3 +57,12 @@ inline fun <reified T> Project.typedProp(name: String): T? {
else -> error("unknown type ${T::class} for property $name")
}
}

public inline fun Project.verifyRootProject(lazyMessage: () -> Any) {
if (rootProject != this) {
val message = lazyMessage()
throw AwsSdkGradleException(message.toString())
}
}

class AwsSdkGradleException(message: String) : GradleException(message)
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
* SPDX-License-Identifier: Apache-2.0
*/
allprojects {
group = "aws.sdk.kotlin"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it correct to have this aws.sdk.kotlin if it's meant to be shared among all our projects?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We aren't publishing this so it doesn't really matter. We have to choose some group though and the only groups we own are aws.smithy.kotlin and aws.sdk.kotlin. I don't care which it is but if you or anyone else has a preference thats fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to choose some group though

Ok, this one should be fine


repositories {
mavenCentral()
}
Expand Down
21 changes: 19 additions & 2 deletions ktlint-rules/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
Expand All @@ -11,16 +17,27 @@ plugins {

kotlin {
sourceSets {
val main by getting {
main {
dependencies {
implementation(libs.ktlint.core)
}
}

val test by getting {
test {
dependencies {
implementation(libs.ktlint.test)
}
}
}
}

tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "1.8"
}
}

tasks.withType<JavaCompile> {
sourceCompatibility = JavaVersion.VERSION_1_8.toString()
targetCompatibility = JavaVersion.VERSION_1_8.toString()
}
Loading