Skip to content
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

fix: update KotlinEditor and handle GRADLE_DISTRIBUTION type. #118

Merged
merged 1 commit into from
Oct 11, 2024
Merged
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
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dagp = "1.30.0"
java = "11"
junit5 = "5.7.2"
kotlin = "1.9.24"
kotlinEditor = "0.4"
kotlinEditor = "0.5"
mavenPublish = "0.28.0"
moshi = "1.14.0"
retrofit = "2.9.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ internal class DependencyComparator : Comparator<DependencyDeclaration> {
right: DependencyDeclaration,
): Int {
if (left.isProjectDependency() && right.isProjectDependency()) return compareDependencies(
left,
right
left, right
)
if (left.isProjectDependency()) return -1
if (right.isProjectDependency()) return 1

if (left.isGradleDistributionDependency() && right.isGradleDistributionDependency()) return compareDependencies(
left, right
)
if (left.isGradleDistributionDependency()) return -1
if (right.isGradleDistributionDependency()) return 1

if (left.isFileDependency() && right.isFileDependency()) return compareDependencies(left, right)
if (left.isFileDependency()) return -1
if (right.isFileDependency()) return -1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ internal interface DependencyDeclaration {
fun isPlatformDeclaration(): Boolean
fun isTestFixturesDeclaration(): Boolean

/** TODO(tsr): what about files and fileTree? */
fun isFileDependency(): Boolean

fun isGradleDistributionDependency(): Boolean

fun isProjectDependency(): Boolean

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,13 @@ internal class GroovyDependencyDeclaration(
override fun isPlatformDeclaration() = declarationKind == DeclarationKind.PLATFORM
override fun isTestFixturesDeclaration() = declarationKind == DeclarationKind.TEST_FIXTURES

override fun isProjectDependency() = dependencyKind == DependencyKind.PROJECT
override fun isFileDependency() = dependencyKind == DependencyKind.FILE

/** TODO(tsr): implement DependencyKind.GRADLE_DISTRIBUTION */
override fun isGradleDistributionDependency(): Boolean = false

override fun isProjectDependency() = dependencyKind == DependencyKind.PROJECT

override fun hasQuotes(): Boolean {
val i = declaration.children.indexOf(dependency)
return declaration.getChild(i - 1) is QuoteContext && declaration.getChild(i + 1) is QuoteContext
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ internal class KotlinDependencyDeclaration(
return base.type == Type.FILE
}

override fun isGradleDistributionDependency(): Boolean {
return base.type == Type.GRADLE_DISTRIBUTION
}

override fun isProjectDependency(): Boolean {
return base.type == Type.PROJECT
}
Expand Down
31 changes: 31 additions & 0 deletions sort/src/test/groovy/com/squareup/sort/KotlinSorterSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,37 @@ class KotlinSorterSpec extends Specification {
)).inOrder()
}

def "can sort build script with gradleApi() dep"() {
given:
def buildScript = dir.resolve('build.gradle.kts')
Files.writeString(buildScript,
'''\
dependencies {
implementation("heart:of-gold:1.0")
api(project(":marvin"))

implementation("sad:robot:1.0")
api(gradleApi())
implementation(testFixtures(libs.magic))
implementation(platform(project(":platform")))
}'''.stripIndent())
def sorter = KotlinSorter.of(buildScript)

expect:
assertThat(trimmedLinesOf(sorter.rewritten())).containsExactlyElementsIn(trimmedLinesOf(
'''\
dependencies {
api(project(":marvin"))
api(gradleApi())

implementation(platform(project(":platform")))
implementation(testFixtures(libs.magic))
implementation("heart:of-gold:1.0")
implementation("sad:robot:1.0")
}'''.stripIndent()
)).inOrder()
}

def "can sort build script with four-space tabs"() {
given:
def buildScript = dir.resolve('build.gradle.kts')
Expand Down
Loading