@@ -15,7 +15,7 @@ import org.gradle.api.model.ObjectFactory
15
15
import org.gradle.api.tasks.*
16
16
17
17
@CacheableTask
18
- public open class KotlinApiCompareTask @Inject constructor(private val objects : ObjectFactory ): DefaultTask() {
18
+ public open class KotlinApiCompareTask @Inject constructor(objects : ObjectFactory ): DefaultTask() {
19
19
20
20
@get:InputFiles
21
21
@get:SkipWhenEmpty
@@ -33,122 +33,31 @@ public open class KotlinApiCompareTask @Inject constructor(private val objects:
33
33
34
34
@TaskAction
35
35
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
46
38
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
104
40
if (! projectApiDir.exists()) {
105
41
error(" Expected folder with API declarations '$projectApiDir ' does not exist.\n " +
106
42
" Please ensure that ':apiDump' was executed in order to get API dump to compare the build against" )
107
43
}
108
- val generatedApiFile_ = generatedApiFile.get().asFile
109
- val buildApiDir = generatedApiFile_.parentFile
44
+ val buildApiDir = generatedApiFile.parentFile
110
45
if (! buildApiDir.exists()) {
111
46
error(" Expected folder with generate API declarations '$buildApiDir ' does not exist." )
112
47
}
113
48
val subject = projectName
114
49
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 " +
139
52
" :$subject :apiDump task to generate one" )
140
53
}
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." )
143
56
}
144
57
145
58
// Normalize case-sensitivity
146
- val expectedApiDeclaration = expectedApiFiles.getValue(projectApiFile_.name)
147
- val actualApiDeclaration = apiBuildDirFiles.getValue(generatedApiFile_.name)
148
59
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)
152
61
if (diff != null ) diffSet.add(diff)
153
62
if (diffSet.isNotEmpty()) {
154
63
val diffText = diffSet.joinToString(" \n\n " )
0 commit comments