Skip to content

Commit 2a30a46

Browse files
committed
Update compare task after rebase on dev branch
1 parent 792a409 commit 2a30a46

File tree

2 files changed

+11
-102
lines changed

2 files changed

+11
-102
lines changed

api/binary-compatibility-validator.api

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class kotlinx/validation/KotlinApiBuildTask : kotlinx/validation/BuildTas
8282
}
8383

8484
public class kotlinx/validation/KotlinApiCompareTask : org/gradle/api/DefaultTask {
85-
public fun <init> ()V
85+
public fun <init> (Lorg/gradle/api/model/ObjectFactory;)V
8686
public final fun getGeneratedApiFile ()Lorg/gradle/api/file/RegularFileProperty;
8787
public final fun getProjectApiFile ()Lorg/gradle/api/file/RegularFileProperty;
8888
}

src/main/kotlin/KotlinApiCompareTask.kt

Lines changed: 10 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import org.gradle.api.model.ObjectFactory
1515
import org.gradle.api.tasks.*
1616

1717
@CacheableTask
18-
public open class KotlinApiCompareTask @Inject constructor(private val objects: ObjectFactory): DefaultTask() {
18+
public open class KotlinApiCompareTask @Inject constructor(objects: ObjectFactory): DefaultTask() {
1919

2020
@get:InputFiles
2121
@get:SkipWhenEmpty
@@ -33,122 +33,31 @@ public open class KotlinApiCompareTask @Inject constructor(private val objects:
3333

3434
@TaskAction
3535
internal fun verify() {
36-
val projectApiDir = projectApiFile.get().asFile.parentFile
37-
if (!projectApiDir.exists()) {
38-
error("Expected folder with API declarations '$projectApiDir' does not exist.\n" +
39-
"Please ensure that ':apiDump' was executed in order to get API dump to compare the build against")
40-
}
41-
val buildApiDir = generatedApiFile.get().asFile.parentFile
42-
if (!buildApiDir.exists()) {
43-
error("Expected folder with generate API declarations '$buildApiDir' does not exist.")
44-
}
45-
val subject = projectName
36+
val projectApiFile = projectApiFile.get().asFile
37+
val generatedApiFile = generatedApiFile.get().asFile
4638

47-
if (!projectApiFile.get().asFile.exists()) {
48-
error("File ${projectApiFile.get().asFile.name} is missing from ${projectApiDir.relativeDirPath()}, please run " +
49-
":$subject:apiDump task to generate one")
50-
}
51-
if (!generatedApiFile.get().asFile.exists()) {
52-
error("File ${generatedApiFile.get().asFile.name} is missing from dump results.")
53-
}
54-
55-
// Normalize case-sensitivity
56-
val diffSet = mutableSetOf<String>()
57-
val diff = compareFiles(projectApiFile.get().asFile, generatedApiFile.get().asFile)
58-
if (diff != null) diffSet.add(diff)
59-
if (diffSet.isNotEmpty()) {
60-
val diffText = diffSet.joinToString("\n\n")
61-
error("API check failed for project $subject.\n$diffText\n\n You can run :$subject:apiDump task to overwrite API declarations")
62-
}
63-
}
64-
65-
private fun File.relativeDirPath(): String {
66-
return toRelativeString(rootDir) + File.separator
67-
}
68-
69-
private fun compareFiles(checkFile: File, builtFile: File): String? {
70-
val checkText = checkFile.readText()
71-
val builtText = builtFile.readText()
72-
73-
// We don't compare full text because newlines on Windows & Linux/macOS are different
74-
val checkLines = checkText.lines()
75-
val builtLines = builtText.lines()
76-
if (checkLines == builtLines)
77-
return null
78-
79-
val patch = DiffUtils.diff(checkLines, builtLines)
80-
val diff = UnifiedDiffUtils.generateUnifiedDiff(checkFile.toString(), builtFile.toString(), checkLines, patch, 3)
81-
return diff.joinToString("\n")
82-
}
83-
}
84-
85-
// TODO: decide what to do with to old compare task
86-
internal abstract class KotlinApiCompareLazyTask @Inject constructor(private val objects: ObjectFactory): DefaultTask() {
87-
88-
@get:SkipWhenEmpty
89-
@get:PathSensitive(PathSensitivity.RELATIVE)
90-
public val projectApiFile: RegularFileProperty = objects.fileProperty()
91-
92-
@get:InputFiles
93-
@get:SkipWhenEmpty
94-
public val generatedApiFile: RegularFileProperty = objects.fileProperty()
95-
96-
private val projectName = project.name
97-
98-
private val rootDir = project.rootProject.rootDir
99-
100-
@TaskAction
101-
internal fun verify() {
102-
val projectApiFile_ = projectApiFile.get().asFile
103-
val projectApiDir = projectApiFile_.parentFile
39+
val projectApiDir = projectApiFile.parentFile
10440
if (!projectApiDir.exists()) {
10541
error("Expected folder with API declarations '$projectApiDir' does not exist.\n" +
10642
"Please ensure that ':apiDump' was executed in order to get API dump to compare the build against")
10743
}
108-
val generatedApiFile_ = generatedApiFile.get().asFile
109-
val buildApiDir = generatedApiFile_.parentFile
44+
val buildApiDir = generatedApiFile.parentFile
11045
if (!buildApiDir.exists()) {
11146
error("Expected folder with generate API declarations '$buildApiDir' does not exist.")
11247
}
11348
val subject = projectName
11449

115-
/*
116-
* We use case-insensitive comparison to workaround issues with case-insensitive OSes
117-
* and Gradle behaving slightly different on different platforms.
118-
* We neither know original sensitivity of existing .api files, not
119-
* build ones, because projectName that is part of the path can have any sensitvity.
120-
* To workaround that, we replace paths we are looking for the same paths that
121-
* actually exist on FS.
122-
*/
123-
fun caseInsensitiveMap() = TreeMap<String, RelativePath> { rp, rp2 ->
124-
rp.compareTo(rp2, true)
125-
}
126-
127-
val apiBuildDirFiles = caseInsensitiveMap()
128-
val expectedApiFiles = caseInsensitiveMap()
129-
130-
objects.fileTree().from(buildApiDir).visit { file ->
131-
apiBuildDirFiles[file.name] = file.relativePath
132-
}
133-
objects.fileTree().from(projectApiDir).visit { file ->
134-
expectedApiFiles[file.name] = file.relativePath
135-
}
136-
137-
if (!expectedApiFiles.containsKey(projectApiFile_.name)) {
138-
error("File ${projectApiFile_.name} is missing from ${projectApiDir.relativeDirPath()}, please run " +
50+
if (!projectApiFile.exists()) {
51+
error("File ${projectApiFile.name} is missing from ${projectApiDir.relativeDirPath()}, please run " +
13952
":$subject:apiDump task to generate one")
14053
}
141-
if (!apiBuildDirFiles.containsKey(generatedApiFile_.name)) {
142-
error("File ${generatedApiFile_.name} is missing from dump results.")
54+
if (!generatedApiFile.exists()) {
55+
error("File ${generatedApiFile.name} is missing from dump results.")
14356
}
14457

14558
// Normalize case-sensitivity
146-
val expectedApiDeclaration = expectedApiFiles.getValue(projectApiFile_.name)
147-
val actualApiDeclaration = apiBuildDirFiles.getValue(generatedApiFile_.name)
14859
val diffSet = mutableSetOf<String>()
149-
val expectedFile = expectedApiDeclaration.getFile(projectApiDir)
150-
val actualFile = actualApiDeclaration.getFile(buildApiDir)
151-
val diff = compareFiles(expectedFile, actualFile)
60+
val diff = compareFiles(projectApiFile, generatedApiFile)
15261
if (diff != null) diffSet.add(diff)
15362
if (diffSet.isNotEmpty()) {
15463
val diffText = diffSet.joinToString("\n\n")

0 commit comments

Comments
 (0)