Skip to content

Commit

Permalink
configure ktlint with custom rules (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
aajtodd authored Aug 14, 2023
1 parent e0e8a5d commit 39192c7
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 17 deletions.
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"
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"

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()
}

0 comments on commit 39192c7

Please sign in to comment.