Skip to content

Commit

Permalink
Update spotless config
Browse files Browse the repository at this point in the history
  • Loading branch information
syxc committed Jul 6, 2024
1 parent f0357f3 commit 9c03018
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 89 deletions.
1 change: 1 addition & 0 deletions build-logic/convention/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
exclude(group = "org.ow2.asm")
}
implementation(libs.bundles.asm)
implementation(libs.gradlePlugin.spotless)
}

gradlePlugin {
Expand Down
97 changes: 96 additions & 1 deletion build-logic/convention/src/main/kotlin/BuildSupportPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.android.build.api.variant.AndroidComponentsExtension
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.TestExtension
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import com.diffplug.gradle.spotless.SpotlessExtension
import org.gradle.api.Project
import org.gradle.api.tasks.compile.JavaCompile
import org.gradle.kotlin.dsl.configure
Expand All @@ -24,11 +25,105 @@ class BuildSupportPlugin : BasePlugin() {
project.group = "com.jithub.app.build-support"
project.version = Versions.versionName

project.configureCommonSpotless()
project.configureCommonAndroid()
project.configureCommonKotlin()
project.configureCommonCompose()
}

private fun Project.configureCommonSpotless() {
plugins.apply("com.diffplug.spotless")
val spotless = extensions.getByName("spotless") as SpotlessExtension
spotless.apply {
java {
if (path == ":") {
target("**/*.java")
} else {
target("src/**/*.java")
}
targetExclude(
"**/build/**/*.java",
"${rootProject.rootDir}/spotless/**"
)
toggleOffOn("@formatter:off", "@formatter:on")
importOrder()
removeUnusedImports()
trimTrailingWhitespace()
endWithNewline()
// https://github.com/palantir/palantir-java-format
palantirJavaFormat("2.47.0").style("GOOGLE").formatJavadoc(true)
formatAnnotations()
licenseHeaderFile(rootProject.file("spotless/copyright.txt"))
}

// https://github.com/pinterest/ktlint
val ktlintVersion = "1.3.1"
kotlin {
if (path == ":") {
target("**/*.kt")
} else {
target("src/**/*.kt")
}
targetExclude(
"**/build/**/*.kt",
"${rootProject.rootDir}/spotless/**",
"**/src/commonMain/kotlin/Greeting.kt",
"**/src/wasmJsMain/kotlin/Platform.wasmJs.kt"
)
ktlint(ktlintVersion).customRuleSets(
// https://github.com/mrmans0n/compose-rules
listOf("io.nlopez.compose.rules:ktlint:0.4.5")
).setEditorConfigPath(
"${rootProject.rootDir}/.editorconfig"
).editorConfigOverride(
mapOf(
"android" to "true",
"continuation_indent_size" to "2",
// Lowercase names are great for grouping multiple functions and/or types.
"ktlint_standard_filename" to "disabled",
// Making something an expression body should be a choice around readability.
"ktlint_standard_function-expression-body" to "disabled",
"ktlint_function_naming_ignore_when_annotated_with" to "Composable",
"ktlint_compose_compositionlocal-allowlist" to "disabled"
)
)
licenseHeaderFile(rootProject.file("spotless/copyright.txt"))
}

kotlinGradle {
target("**/*.gradle.kts")
targetExclude(
"**/build/**/*.kts",
"${rootProject.rootDir}/spotless/**"
)
ktlint(ktlintVersion).setEditorConfigPath("${rootProject.rootDir}/.editorconfig")
// Look for the first line that doesn't have a block comment (assumed to be the license)
licenseHeaderFile(rootProject.file("spotless/copyright.txt"), "(^(?![\\/ ]\\*).*$)")
}

format("xml") {
target("**/*.xml")
targetExclude(
"**/.idea/**/*.xml",
"**/.run/**/*.xml",
"**/.kotlin/**/*.xml",
"**/build/**/*.xml",
"**/lint-config/**/*.xml",
"${rootProject.rootDir}/spotless/**"
)
indentWithSpaces(2)
trimTrailingWhitespace()
endWithNewline()
if (path == ":") {
// ignore
} else {
// Look for the first XML tag that isn't a comment (<!--) or the xml declaration (<?xml)
licenseHeaderFile(rootProject.file("spotless/copyright.xml"), "(<[^!?])")
}
}
}
}

private fun Project.configureCommonAndroid() {
plugins.withId("com.android.base") {
val android = extensions.getByName("android") as BaseExtension
Expand Down Expand Up @@ -113,7 +208,7 @@ class BuildSupportPlugin : BasePlugin() {
}

val androidComponents = extensions.getByType(AndroidComponentsExtension::class.java)
with(androidComponents) {
androidComponents.apply {
onVariants(selector().withBuildType("release")) {
// Only exclude *.version files in release mode as debug mode requires
// these files for layout inspector to work.
Expand Down
4 changes: 1 addition & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ plugins {
alias(libs.plugins.kotlin.cocoapods) apply false
// alias(libs.plugins.jetbrains.compose) apply false
alias(libs.plugins.compose.compiler) apply false
alias(libs.plugins.spotless) apply false
id("com.jithub.gradle.build-logic")
}

Expand All @@ -45,7 +44,6 @@ allprojects {
}

subprojects {
apply(from = rootProject.file("spotless/spotless.gradle"))
afterEvaluate {
tasks.withType(JavaCompile::class.java) {
dependsOn(tasks.getByName("spotlessApply"))
Expand All @@ -63,6 +61,6 @@ gradle.taskGraph.whenReady {
}
}

tasks.register<Delete>("clean") {
tasks.getByName("clean") {
delete(rootProject.layout.buildDirectory)
}
3 changes: 1 addition & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ junit = "4.13.2"
[libraries]
gradlePlugin-android = { module = "com.android.tools.build:gradle", version.ref = "agp" }
gradlePlugin-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
gradlePlugin-spotless = "com.diffplug.spotless:spotless-plugin-gradle:6.25.0"

androidx-core-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "androidx-core-ktx" }
androidx-appcompat = { group = "androidx.appcompat", name = "appcompat", version.ref = "androidx-appcompat" }
Expand Down Expand Up @@ -70,7 +71,5 @@ jetbrains-compose = { id = "org.jetbrains.compose", version.ref = "jbr-compose"

compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }

spotless = "com.diffplug.spotless:6.25.0"

[bundles]
asm = []
2 changes: 1 addition & 1 deletion shared-compose/src/commonMain/kotlin/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.jetbrains.compose.ui.tooling.preview.Preview
@OptIn(ExperimentalResourceApi::class)
@Composable
@Preview
fun App() {
private fun App() {
MaterialTheme {
var showContent by remember { mutableStateOf(false) }
Column(Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
Expand Down
4 changes: 1 addition & 3 deletions shared/src/commonMain/kotlin/Greeting.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
class Greeting {
private val platform = getPlatform()

fun greet(): String {
return "Hello, ${platform.name}!"
}
fun greet(): String = "Hello, ${platform.name}!"
}
6 changes: 0 additions & 6 deletions spotless/greclipse.properties

This file was deleted.

73 changes: 0 additions & 73 deletions spotless/spotless.gradle

This file was deleted.

0 comments on commit 9c03018

Please sign in to comment.