Skip to content

Commit

Permalink
Use ktfmt for formatting (#571)
Browse files Browse the repository at this point in the history
* Format with ktfmt, use ktlint for inspections only

* Make ktlint plugin required again, but set to manual
* Make ktlint not used for formatting in IDE
* Make ktfmt plugin required, used for formatting in IDE
* Make pre-push run config use ktfmt
* Add ktfmt Gradle plugin
* ktfmt uses kotlinlang style both in IDE and Gradle

* Upgrade ktfmt Gradle plugin, set style

Also sets a minimum version for the ktfmt plugin that supports custom
styles, and stores our style.

* Make Kotlin and KDoc formatter plugins required

* Upgrade kotlinter plugin to latest version

* Use ktfmt to reformat project in run config

* Add git hook for pre-push to ensure correct formatting

* Format with ktfmt (#572)

* Reformat everything with ktfmt

* Don't nag about ktlint suppressions in .editorconfig

* Suppress ktlint checks that clash with ktfmt

* Disable MergeSarifTask for now

* Disable upload-sarif CI step for now
  • Loading branch information
rock3r authored Sep 3, 2024
1 parent 008d969 commit fc4c9cb
Show file tree
Hide file tree
Showing 274 changed files with 4,419 additions and 8,464 deletions.
19 changes: 17 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,25 @@ indent_size = 2
indent_size = 2

[{*.kt,*.kts}]
ktlint_function_signature_body_expression_wrapping = multiline
ktlint_ignore_back_ticked_identifier = true
ij_kotlin_allow_trailing_comma = true
ktlint_function_naming_ignore_when_annotated_with = Composable
ktlint_function_signature_body_expression_wrapping = multiline
ktlint_ignore_back_ticked_identifier = true
ktlint_standard_annotation = disabled
ktlint_standard_chain-method-continuation = disabled
ktlint_standard_class-signature = disabled
ktlint_standard_condition-wrapping = disabled
ktlint_standard_function-expression-body = disabled
ktlint_standard_function-literal = disabled
ktlint_standard_function-signature = disabled
ktlint_standard_import-ordering = disabled
ktlint_standard_indent = disabled
ktlint_standard_multiline-expression-wrapping = disabled
ktlint_standard_parameter-list-wrapping = disabled
ktlint_standard_string-template-indent = disabled
ktlint_standard_trailing-comma-on-call-site = disabled
ktlint_standard_trailing-comma-on-declaration-site = disabled
ktlint_standard_try-catch-finally-spacing = disabled

[gradlew.bat]
end_of_line = crlf
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ jobs:
- name: Run :check task
run: ./gradlew check --continue --no-daemon

- uses: github/codeql-action/upload-sarif@v3
if: ${{ always() }}
with:
sarif_file: ${{ github.workspace }}/build/reports/static-analysis.sarif
checkout_path: ${{ github.workspace }}
# - uses: github/codeql-action/upload-sarif@v3
# if: ${{ always() }}
# with:
# sarif_file: ${{ github.workspace }}/build/reports/static-analysis.sarif
# checkout_path: ${{ github.workspace }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ local.properties
!.idea/icon.png
!.idea/icon_dark.png
!.idea/inspectionProfiles/
!.idea/ktfmt.xml
!.idea/ktlint.xml
!.idea/ktlint-plugin.xml
!.idea/runConfigurations/
!.idea/scopes/
!.idea/vcs.xml
Expand Down
4 changes: 4 additions & 0 deletions .idea/externalDependencies.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/ktfmt.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/ktlint-plugin.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/runConfigurations/Pre_push.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/runConfigurations/Reformat_project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 51 additions & 46 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ dependencies {
}

tasks {
val mergeSarifReports by registering(MergeSarifTask::class) {
source(configurations.outgoingSarif)
include { it.file.extension == "sarif" }
}

register("check") { dependsOn(mergeSarifReports) }
// val mergeSarifReports by
// registering(MergeSarifTask::class) {
// source(configurations.outgoingSarif)
// include { it.file.extension == "sarif" }
// }
//
// register("check") { dependsOn(mergeSarifReports) }

register("tagRelease") {
dependsOn("check")
Expand All @@ -36,9 +37,9 @@ tasks {
group = "release"

doFirst {
val rawReleaseVersion = ((project.property("jewel.release.version") as String?)
?.takeIf { it.isNotBlank() }
?: throw GradleException("Please provide a jewel.release.version in gradle.properties"))
val rawReleaseVersion =
((project.property("jewel.release.version") as String?)?.takeIf { it.isNotBlank() }
?: throw GradleException("Please provide a jewel.release.version in gradle.properties"))

val releaseName = "v$rawReleaseVersion"

Expand All @@ -47,9 +48,10 @@ tasks {
// Check we're on the main branch
logger.info("Checking current branch is main...")
exec {
commandLine = listOf("git", "rev-parse", "--abbrev-ref", "HEAD")
standardOutput = stdOut
}.assertNormalExitValue()
commandLine = listOf("git", "rev-parse", "--abbrev-ref", "HEAD")
standardOutput = stdOut
}
.assertNormalExitValue()

val currentBranch = stdOut.use { it.toString() }.trim()
if (currentBranch != "main") {
Expand All @@ -60,9 +62,10 @@ tasks {
logger.info("Checking current branch is main...")
stdOut.reset()
exec {
commandLine = listOf("git", "tag")
standardOutput = stdOut
}.assertNormalExitValue()
commandLine = listOf("git", "tag")
standardOutput = stdOut
}
.assertNormalExitValue()

if (stdOut.toString().trim().lines().any { it == releaseName }) {
throw GradleException("The tag $releaseName already exists!")
Expand All @@ -72,9 +75,10 @@ tasks {
logger.info("Checking all changes have been committed...")
stdOut.reset()
exec {
commandLine = listOf("git", "status", "--porcelain")
standardOutput = stdOut
}.assertNormalExitValue()
commandLine = listOf("git", "status", "--porcelain")
standardOutput = stdOut
}
.assertNormalExitValue()

if (stdOut.toString().isNotBlank()) {
throw GradleException("Please commit all changes before tagging a release")
Expand All @@ -84,24 +88,28 @@ tasks {
logger.info("Getting HEAD hash...")
stdOut.reset()
exec {
commandLine = listOf("git", "rev-parse", "HEAD")
standardOutput = stdOut
}.assertNormalExitValue()
commandLine = listOf("git", "rev-parse", "HEAD")
standardOutput = stdOut
}
.assertNormalExitValue()

val currentHead = stdOut.use { it.toString() }.trim()

// Enumerate the release branches
logger.info("Enumerating release branches...")
stdOut.reset()
exec {
commandLine = listOf("git", "branch")
standardOutput = stdOut
}.assertNormalExitValue()
commandLine = listOf("git", "branch")
standardOutput = stdOut
}
.assertNormalExitValue()

val releaseBranches = stdOut.use { it.toString() }
.lines()
.filter { it.trim().startsWith("releases/") }
.map { it.trim().removePrefix("releases/") }
val releaseBranches =
stdOut
.use { it.toString() }
.lines()
.filter { it.trim().startsWith("releases/") }
.map { it.trim().removePrefix("releases/") }

if (releaseBranches.isEmpty()) {
throw GradleException("No local release branches found, make sure they exist locally")
Expand All @@ -114,9 +122,10 @@ tasks {
for (branch in releaseBranches) {
stdOut.reset()
exec {
commandLine = listOf("git", "merge-base", "main", "releases/$branch")
standardOutput = stdOut
}.assertNormalExitValue()
commandLine = listOf("git", "merge-base", "main", "releases/$branch")
standardOutput = stdOut
}
.assertNormalExitValue()

val mergeBase = stdOut.use { it.toString() }.trim()
if (mergeBase != currentHead) {
Expand All @@ -126,9 +135,7 @@ tasks {

// Tag main branch
logger.lifecycle("Tagging head of main branch as $releaseName...")
exec {
commandLine = listOf("git", "tag", releaseName)
}.assertNormalExitValue()
exec { commandLine = listOf("git", "tag", releaseName) }.assertNormalExitValue()

// Tag release branches
for (branch in releaseBranches) {
Expand All @@ -138,30 +145,28 @@ tasks {

logger.info("Getting branch head commit...")
exec {
commandLine = listOf("git", "rev-parse", "releases/$branch")
standardOutput = stdOut
}.assertNormalExitValue()
commandLine = listOf("git", "rev-parse", "releases/$branch")
standardOutput = stdOut
}
.assertNormalExitValue()

val branchHead = stdOut.use { it.toString() }.trim()
logger.info("HEAD of releases/$branch is $branchHead")

logger.info("Tagging commit ${branchHead.take(7)} as $branchTagName")
stdOut.reset()
exec {
commandLine = listOf("git", "tag", branchTagName, branchHead)
standardOutput = stdOut
}.assertNormalExitValue()
commandLine = listOf("git", "tag", branchTagName, branchHead)
standardOutput = stdOut
}
.assertNormalExitValue()
}

logger.info("All done!")
}
}

register<Delete>("cleanTestPublishArtifacts") {
delete(rootProject.layout.buildDirectory.dir("maven-test"))
}
register<Delete>("cleanTestPublishArtifacts") { delete(rootProject.layout.buildDirectory.dir("maven-test")) }

register<Delete>("clean") {
delete(rootProject.layout.buildDirectory)
}
register<Delete>("clean") { delete(rootProject.layout.buildDirectory) }
}
17 changes: 6 additions & 11 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@ plugins {
}

val properties = Properties()

project.file("../gradle.properties").inputStream().use { properties.load(it) }

val jdkLevel = properties.getProperty("jdk.level") as String

kotlin {
jvmToolchain {
languageVersion = JavaLanguageVersion.of(jdkLevel)
}

sourceSets {
all {
languageSettings {
optIn("kotlinx.serialization.ExperimentalSerializationApi")
}
}
}
jvmToolchain { languageVersion = JavaLanguageVersion.of(jdkLevel) }

sourceSets { all { languageSettings { optIn("kotlinx.serialization.ExperimentalSerializationApi") } } }
}

dependencies {
Expand All @@ -30,6 +24,7 @@ dependencies {
implementation(libs.kotlinSarif)
implementation(libs.kotlinpoet)
implementation(libs.kotlinter.gradlePlugin)
implementation(libs.ktfmt.gradlePlugin)
implementation(libs.kotlinx.binaryCompatValidator.gradlePlugin)
implementation(libs.kotlinx.serialization.json)
implementation(libs.poko.gradlePlugin)
Expand Down
6 changes: 1 addition & 5 deletions buildSrc/settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,5 @@ dependencyResolutionManagement {
mavenCentral()
}

versionCatalogs {
create("libs") {
from(files("../gradle/libs.versions.toml"))
}
}
versionCatalogs { create("libs") { from(files("../gradle/libs.versions.toml")) } }
}
Loading

0 comments on commit fc4c9cb

Please sign in to comment.