Skip to content

Kotlin: Set up ktfmt in OSS #52064

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
39 changes: 39 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ plugins {
alias(libs.plugins.kotlin.android) apply false
alias(libs.plugins.binary.compatibility.validator) apply true
alias(libs.plugins.android.test) apply false
alias(libs.plugins.ktfmt) apply true
}

val reactAndroidProperties = java.util.Properties()
Expand Down Expand Up @@ -130,3 +131,41 @@ if (project.findProperty("react.internal.useHermesNightly")?.toString()?.toBoole
}
}
}

ktfmt {
blockIndent.set(2)
continuationIndent.set(4)
maxWidth.set(100)
removeUnusedImports.set(false)
manageTrailingCommas.set(false)
}

// Configure ktfmt tasks to include gradle-plugin
listOf("ktfmtCheck", "ktfmtFormat").forEach { taskName ->
tasks.named(taskName) {
dependsOn(gradle.includedBuild("gradle-plugin").task(":$taskName"))
}
}

allprojects {
// Apply exclusions for specific files that should not be formatted
val excludePatterns = listOf(
"**/build/**",
"**/hermes-engine/**",
"**/internal/featureflags/**",
"**/systeminfo/ReactNativeVersion.kt"
)
listOf(
com.ncorti.ktfmt.gradle.tasks.KtfmtCheckTask::class,
com.ncorti.ktfmt.gradle.tasks.KtfmtFormatTask::class
).forEach {
tasks.withType(it) { exclude(excludePatterns) }
}

// Disable the problematic ktfmt script tasks due to symbolic link issues in subprojects
afterEvaluate {
listOf("ktfmtCheckScripts", "ktfmtFormatScripts").forEach {
tasks.findByName(it)?.enabled = false
}
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"featureflags": "yarn --cwd packages/react-native featureflags",
"lint-ci": "./.github/workflow-scripts/analyze_code.sh && yarn shellcheck",
"lint-java": "node ./scripts/lint-java.js",
Copy link
Contributor

Choose a reason for hiding this comment

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

BTW We can nuke this and also remove the script

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Tackling this separately in #52092

"lint-kotlin-check": "./gradlew ktfmtCheck",
"lint-kotlin": "./gradlew ktfmtFormat",
"lint-markdown": "markdownlint-cli2 2>&1",
"lint": "eslint --max-warnings 0 .",
"prettier": "prettier --write \"./**/*.{js,md,yml,ts,tsx}\"",
Expand Down
22 changes: 21 additions & 1 deletion packages/gradle-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* LICENSE file in the root directory of this source tree.
*/

plugins { alias(libs.plugins.kotlin.jvm).apply(false) }
plugins {
alias(libs.plugins.kotlin.jvm).apply(false)
alias(libs.plugins.ktfmt).apply(true)
}

tasks.register("build") {
dependsOn(
Expand All @@ -24,3 +27,20 @@ tasks.register("clean") {
":shared:clean",
)
}

// Configure ktfmt tasks to run on all subprojects
tasks.named("ktfmtCheck") {
subprojects.forEach { subproject ->
if (subproject.plugins.hasPlugin("com.ncorti.ktfmt.gradle")) {
dependsOn(subproject.tasks.named("ktfmtCheck"))
}
}
}

tasks.named("ktfmtFormat") {
subprojects.forEach { subproject ->
if (subproject.plugins.hasPlugin("com.ncorti.ktfmt.gradle")) {
dependsOn(subproject.tasks.named("ktfmtFormat"))
}
}
}
2 changes: 2 additions & 0 deletions packages/gradle-plugin/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ javapoet = "1.13.0"
junit = "4.13.2"
kotlin = "2.1.20"
assertj = "3.25.1"
ktfmt = "0.22.0"

[libraries]
kotlin-gradle-plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
Expand All @@ -18,3 +19,4 @@ assertj = { module = "org.assertj:assertj-core", version.ref = "assertj" }

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
ktfmt = { id = "com.ncorti.ktfmt.gradle", version.ref = "ktfmt" }
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ktfmt)
id("java-gradle-plugin")
}

Expand Down
1 change: 1 addition & 0 deletions packages/gradle-plugin/settings-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ktfmt)
id("java-gradle-plugin")
}

Expand Down
5 changes: 4 additions & 1 deletion packages/gradle-plugin/shared-testutil/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins { alias(libs.plugins.kotlin.jvm) }
plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ktfmt)
}

repositories { mavenCentral() }

Expand Down
5 changes: 4 additions & 1 deletion packages/gradle-plugin/shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins { alias(libs.plugins.kotlin.jvm) }
plugins {
alias(libs.plugins.kotlin.jvm)
alias(libs.plugins.ktfmt)
}

repositories { mavenCentral() }

Expand Down
1 change: 1 addition & 0 deletions packages/react-native/ReactAndroid/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ plugins {
alias(libs.plugins.android.library)
alias(libs.plugins.download)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.ktfmt)
}

version = project.findProperty("VERSION_NAME")?.toString()!!
Expand Down
2 changes: 2 additions & 0 deletions packages/react-native/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jsc-android = "2026004.0.1"
jsr305 = "3.0.2"
junit = "4.13.2"
kotlin = "2.1.20"
ktfmt = "0.22.0"
mockito = "3.12.4"
mockito-kotlin = "3.2.0"
nexus-publish = "1.3.0"
Expand Down Expand Up @@ -90,6 +91,7 @@ thoughtworks = {module = "com.thoughtworks.xstream:xstream", version.ref = "xstr
android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }
download = { id = "de.undercouch.download", version.ref = "download" }
ktfmt = { id = "com.ncorti.ktfmt.gradle", version.ref = "ktfmt" }
nexus-publish = { id = "io.github.gradle-nexus.publish-plugin", version.ref = "nexus-publish" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
binary-compatibility-validator = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binary-compatibility-validator" }
Expand Down
Loading