Skip to content

Commit

Permalink
Set up Spotless + Ktlint to check styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Goooler authored and egorikftp committed Jul 22, 2024
1 parent 3f4c945 commit 5c919e6
Show file tree
Hide file tree
Showing 98 changed files with 582 additions and 511 deletions.
24 changes: 22 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
[{*.kt,*.kts}]
root = true

[*]
charset = utf-8
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.{kt,kts}]
indent_size = 4
ij_kotlin_imports_layout = *
ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
ij_kotlin_line_break_after_multiline_when_entry = false
ij_kotlin_name_count_to_use_star_import = 2147483647
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
ij_kotlin_name_count_to_use_star_import_for_members = 2147483647
ij_kotlin_packages_to_use_import_on_demand = unset
ktlint_code_style = intellij_idea
ktlint_function_naming_ignore_when_annotated_with = Composable
ktlint_standard_function-expression-body = disabled

[*.md]
trim_trailing_whitespace = false
18 changes: 18 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
import com.diffplug.gradle.spotless.SpotlessExtension

plugins {
alias(libs.plugins.kotlin.jvm) apply false
alias(libs.plugins.kotlin.compose) apply false
alias(libs.plugins.jetbrains.compose) apply false
alias(libs.plugins.jetbrains.intellij) apply false
alias(libs.plugins.spotless) apply false
}

allprojects {
plugins.apply(rootProject.libs.plugins.spotless.get().pluginId)
extensions.configure<SpotlessExtension> {
kotlin {
target("src/**/*.kt")
// Sources copied from AndroidX Compose
targetExclude("src/main/kotlin/androidx/**")
ktlint(rootProject.libs.ktlint.get().version)
}
kotlinGradle {
ktlint(rootProject.libs.ktlint.get().version)
}
}
}
2 changes: 1 addition & 1 deletion components/generator/common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ dependencies {
implementation(libs.kotlinpoet)

testImplementation(libs.kotlin.test)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ fun Float.trimTrailingZero(): String {

fun Float.formatFloat(): String = "${trimTrailingZero()}f"

fun String.toColorHex(): String = "0x${this.uppercase()}"
fun String.toColorHex(): String = "0x${this.uppercase()}"
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,37 @@ fun FileSpec.removeDeadCode(): String = toString()

inline fun objectBuilder(
name: String,
builderAction: TypeSpec.Builder.() -> Unit = {}
builderAction: TypeSpec.Builder.() -> Unit = {},
) = TypeSpec.objectBuilder(name)
.apply(builderAction)
.build()

inline fun fileSpecBuilder(
packageName: String,
fileName: String,
builderAction: FileSpec.Builder.() -> Unit
builderAction: FileSpec.Builder.() -> Unit,
) = FileSpec
.builder(packageName = packageName, fileName = fileName)
.apply(builderAction)
.build()

inline fun funSpecBuilder(
name: String,
builderAction: FunSpec.Builder.() -> Unit
builderAction: FunSpec.Builder.() -> Unit,
) = FunSpec.builder(name)
.apply(builderAction)
.build()

inline fun getterFunSpecBuilder(
builderAction: FunSpec.Builder.() -> Unit
builderAction: FunSpec.Builder.() -> Unit,
) = FunSpec.getterBuilder()
.apply(builderAction)
.build()

inline fun propertySpecBuilder(
name: String,
type: TypeName,
builderAction: PropertySpec.Builder.() -> Unit
builderAction: PropertySpec.Builder.() -> Unit,
) = PropertySpec
.builder(name = name, type = type)
.apply(builderAction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package io.github.composegears.valkyrie.generator.ext

import com.squareup.kotlinpoet.TypeName

fun TypeName.nullable() = copy(nullable = true)
fun TypeName.nullable() = copy(nullable = true)
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.composegears.valkyrie.generator.ext

import org.junit.Test
import kotlin.test.assertEquals
import org.junit.Test

class FormatterTest {

Expand All @@ -17,4 +17,4 @@ class FormatterTest {

assertEquals("FFe676ff".toColorHex(), "0xFFE676FF")
}
}
}
2 changes: 1 addition & 1 deletion components/generator/iconpack/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ dependencies {
implementation(libs.kotlinpoet)

testImplementation(libs.kotlin.test)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ internal class IconPackFileSpec(private val config: IconPackGeneratorConfig) {
}
val fileSpec = fileSpecBuilder(
packageName = config.packageName,
fileName = config.iconPackName
fileName = config.iconPackName,
) {
addType(iconPackSpec)
setIndent()
}
return IconPackSpecOutput(
content = fileSpec.removeDeadCode(),
name = fileSpec.name
name = fileSpec.name,
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package io.github.composegears.valkyrie.generator.iconpack
data class IconPackGeneratorConfig(
val packageName: String,
val iconPackName: String,
val subPacks: List<String>
val subPacks: List<String>,
)

data class IconPackSpecOutput(
val content: String,
val name: String
val name: String,
)

object IconPackGenerator {

fun create(config: IconPackGeneratorConfig) = IconPackFileSpec(config).createSpec()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.github.composegears.valkyrie.generator.iconpack

import org.junit.Test
import kotlin.test.assertEquals
import org.junit.Test

class IconPackGeneratorTest {

Expand All @@ -11,8 +11,8 @@ class IconPackGeneratorTest {
config = IconPackGeneratorConfig(
packageName = "io.github.composegears.valkyrie.icons",
iconPackName = "ValkyrieIcons",
subPacks = emptyList()
)
subPacks = emptyList(),
),
)

val expectedContent = """
Expand All @@ -32,8 +32,8 @@ class IconPackGeneratorTest {
config = IconPackGeneratorConfig(
packageName = "io.github.composegears.valkyrie.icons",
iconPackName = "ValkyrieIcons",
subPacks = listOf("Filled", "Colored")
)
subPacks = listOf("Filled", "Colored"),
),
)

val expectedContent = """
Expand All @@ -50,4 +50,4 @@ class IconPackGeneratorTest {
assertEquals(result.content, expectedContent)
assertEquals(result.name, "ValkyrieIcons")
}
}
}
2 changes: 1 addition & 1 deletion components/generator/imagevector/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ dependencies {

testImplementation(projects.components.parser)
testImplementation(libs.kotlin.test)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ internal class ImageVectorFileSpec(private val config: ImageVectorSpecConfig) {
fun createFileFor(vector: Vector): ImageVectorSpecOutput {
val backingProperty = backingPropertySpec(
name = config.iconName.backingPropertyName(),
type = ClassNames.ImageVector
type = ClassNames.ImageVector,
)

val iconPackClassName = when {
Expand All @@ -43,12 +43,12 @@ internal class ImageVectorFileSpec(private val config: ImageVectorSpecConfig) {
if (config.iconNestedPack.isEmpty()) {
ClassName(
config.iconPackage,
config.iconPack
config.iconPack,
)
} else {
ClassName(
config.iconPackage,
config.iconPack
config.iconPack,
).nestedClass(config.iconNestedPack)
}
}
Expand All @@ -61,14 +61,14 @@ internal class ImageVectorFileSpec(private val config: ImageVectorSpecConfig) {

val fileSpec = fileSpecBuilder(
packageName = packageName,
fileName = config.iconName
fileName = config.iconName,
) {
addProperty(
propertySpec = iconProperty(
vector = vector,
iconPackClassName = iconPackClassName,
backingProperty = backingProperty
)
backingProperty = backingProperty,
),
)
addProperty(propertySpec = backingProperty)
apply {
Expand All @@ -77,13 +77,13 @@ internal class ImageVectorFileSpec(private val config: ImageVectorSpecConfig) {
funSpec = when {
iconPackClassName != null -> iconPreviewSpecForNestedPack(
iconPackClassName = iconPackClassName,
iconName = config.iconName
iconName = config.iconName,
)
else -> iconPreviewSpec(
iconPackage = packageName,
iconName = config.iconName
iconName = config.iconName,
)
}
},
)
}
}
Expand All @@ -92,14 +92,14 @@ internal class ImageVectorFileSpec(private val config: ImageVectorSpecConfig) {

return ImageVectorSpecOutput(
content = fileSpec.removeDeadCode(),
name = fileSpec.name
name = fileSpec.name,
)
}

private fun iconProperty(
vector: Vector,
iconPackClassName: ClassName?,
backingProperty: PropertySpec
backingProperty: PropertySpec,
): PropertySpec = propertySpecBuilder(name = config.iconName, type = ClassNames.ImageVector) {
receiver(iconPackClassName)
getter(iconFun(vector = vector, backingProperty = backingProperty))
Expand All @@ -112,7 +112,7 @@ internal class ImageVectorFileSpec(private val config: ImageVectorSpecConfig) {
beginControlFlow("if (%N != null)", backingProperty)
addStatement("return %N!!", backingProperty)
endControlFlow()
}
},
)
addCode(
buildCodeBlock {
Expand All @@ -126,10 +126,10 @@ internal class ImageVectorFileSpec(private val config: ImageVectorSpecConfig) {
vector = vector,
path = {
vector.nodes.forEach { node -> addVectorNode(node) }
}
)
},
),
)
}
},
)
addStatement("")
addStatement("return %N!!", backingProperty)
Expand All @@ -154,4 +154,4 @@ internal class ImageVectorFileSpec(private val config: ImageVectorSpecConfig) {
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ data class ImageVectorGeneratorConfig(
val packageName: String,
val packName: String,
val nestedPackName: String,
val generatePreview: Boolean
val generatePreview: Boolean,
)

data class ImageVectorSpecOutput(
val content: String,
val name: String
val name: String,
) {
companion object {
val empty = ImageVectorSpecOutput(
content = "",
name = ""
name = "",
)
}
}
Expand All @@ -26,14 +26,14 @@ object ImageVectorGenerator {
fun convert(
vector: Vector,
kotlinName: String,
config: ImageVectorGeneratorConfig
config: ImageVectorGeneratorConfig,
): ImageVectorSpecOutput = ImageVectorFileSpec(
config = ImageVectorSpecConfig(
iconPackage = config.packageName,
iconPack = config.packName,
iconName = kotlinName,
iconNestedPack = config.nestedPackName,
generatePreview = config.generatePreview
)
generatePreview = config.generatePreview,
),
).createFileFor(vector)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ internal fun String.backingPropertyName() = "_$this"

internal fun backingPropertySpec(
name: String,
type: TypeName
type: TypeName,
) = propertySpecBuilder(name = name, type = type.nullable()) {
mutable()
addModifiers(KModifier.PRIVATE)
initializer("null")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ internal fun imageVectorBuilderSpecs(
add("}")
}
addStatement(".build()")
}
}
Loading

0 comments on commit 5c919e6

Please sign in to comment.