Skip to content

Commit 4fc358c

Browse files
fix: update KotlinEditor and handle GRADLE_DISTRIBUTION type.
1 parent ac030c0 commit 4fc358c

File tree

6 files changed

+52
-4
lines changed

6 files changed

+52
-4
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ dagp = "1.30.0"
44
java = "11"
55
junit5 = "5.7.2"
66
kotlin = "1.9.24"
7-
kotlinEditor = "0.4"
7+
kotlinEditor = "0.5"
88
mavenPublish = "0.28.0"
99
moshi = "1.14.0"
1010
retrofit = "2.9.0"

sort/src/main/kotlin/com/squareup/sort/DependencyComparator.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ internal class DependencyComparator : Comparator<DependencyDeclaration> {
2828
right: DependencyDeclaration,
2929
): Int {
3030
if (left.isProjectDependency() && right.isProjectDependency()) return compareDependencies(
31-
left,
32-
right
31+
left, right
3332
)
3433
if (left.isProjectDependency()) return -1
3534
if (right.isProjectDependency()) return 1
3635

36+
if (left.isGradleDistributionDependency() && right.isGradleDistributionDependency()) return compareDependencies(
37+
left, right
38+
)
39+
if (left.isGradleDistributionDependency()) return -1
40+
if (right.isGradleDistributionDependency()) return 1
41+
3742
if (left.isFileDependency() && right.isFileDependency()) return compareDependencies(left, right)
3843
if (left.isFileDependency()) return -1
3944
if (right.isFileDependency()) return -1

sort/src/main/kotlin/com/squareup/sort/DependencyDeclaration.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ internal interface DependencyDeclaration {
88
fun isPlatformDeclaration(): Boolean
99
fun isTestFixturesDeclaration(): Boolean
1010

11+
/** TODO(tsr): what about files and fileTree? */
1112
fun isFileDependency(): Boolean
13+
14+
fun isGradleDistributionDependency(): Boolean
15+
1216
fun isProjectDependency(): Boolean
1317

1418
/**

sort/src/main/kotlin/com/squareup/sort/groovy/GroovyDependencyDeclaration.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ internal class GroovyDependencyDeclaration(
4747
override fun isPlatformDeclaration() = declarationKind == DeclarationKind.PLATFORM
4848
override fun isTestFixturesDeclaration() = declarationKind == DeclarationKind.TEST_FIXTURES
4949

50-
override fun isProjectDependency() = dependencyKind == DependencyKind.PROJECT
5150
override fun isFileDependency() = dependencyKind == DependencyKind.FILE
5251

52+
/** TODO(tsr): implement DependencyKind.GRADLE_DISTRIBUTION */
53+
override fun isGradleDistributionDependency(): Boolean = false
54+
55+
override fun isProjectDependency() = dependencyKind == DependencyKind.PROJECT
56+
5357
override fun hasQuotes(): Boolean {
5458
val i = declaration.children.indexOf(dependency)
5559
return declaration.getChild(i - 1) is QuoteContext && declaration.getChild(i + 1) is QuoteContext

sort/src/main/kotlin/com/squareup/sort/kotlin/KotlinDependencyDeclaration.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ internal class KotlinDependencyDeclaration(
2727
return base.type == Type.FILE
2828
}
2929

30+
override fun isGradleDistributionDependency(): Boolean {
31+
return base.type == Type.GRADLE_DISTRIBUTION
32+
}
33+
3034
override fun isProjectDependency(): Boolean {
3135
return base.type == Type.PROJECT
3236
}

sort/src/test/groovy/com/squareup/sort/KotlinSorterSpec.groovy

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,37 @@ class KotlinSorterSpec extends Specification {
115115
)).inOrder()
116116
}
117117

118+
def "can sort build script with gradleApi() dep"() {
119+
given:
120+
def buildScript = dir.resolve('build.gradle.kts')
121+
Files.writeString(buildScript,
122+
'''\
123+
dependencies {
124+
implementation("heart:of-gold:1.0")
125+
api(project(":marvin"))
126+
127+
implementation("sad:robot:1.0")
128+
api(gradleApi())
129+
implementation(testFixtures(libs.magic))
130+
implementation(platform(project(":platform")))
131+
}'''.stripIndent())
132+
def sorter = KotlinSorter.of(buildScript)
133+
134+
expect:
135+
assertThat(trimmedLinesOf(sorter.rewritten())).containsExactlyElementsIn(trimmedLinesOf(
136+
'''\
137+
dependencies {
138+
api(project(":marvin"))
139+
api(gradleApi())
140+
141+
implementation(platform(project(":platform")))
142+
implementation(testFixtures(libs.magic))
143+
implementation("heart:of-gold:1.0")
144+
implementation("sad:robot:1.0")
145+
}'''.stripIndent()
146+
)).inOrder()
147+
}
148+
118149
def "can sort build script with four-space tabs"() {
119150
given:
120151
def buildScript = dir.resolve('build.gradle.kts')

0 commit comments

Comments
 (0)