-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Running binary-compatibility-validator on code 2. Flagging public data classes and failing build 3. Running Poko to autogen equals/hashcode/toString
- Loading branch information
Showing
12 changed files
with
152 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import org.gradle.api.GradleException | ||
import org.gradle.api.tasks.CacheableTask | ||
import org.gradle.api.tasks.SourceTask | ||
import org.gradle.api.tasks.TaskAction | ||
import java.io.File | ||
import java.util.Stack | ||
|
||
@CacheableTask | ||
open class ValidatePublicApiTask : SourceTask() { | ||
|
||
init { | ||
group = "verification" | ||
} | ||
|
||
private val classFqnRegex = "public (?:\\w+ )*class (\\S+)\\b".toRegex() | ||
|
||
@Suppress("ConvertToStringTemplate") // The odd concatenation is needed because of $; escapes get confused | ||
private val copyMethodRegex = ("public static synthetic fun copy(-\\w+)" + "\\$" + "default\\b").toRegex() | ||
|
||
@TaskAction | ||
fun validatePublicApi() { | ||
logger.info("Validating ${source.files.size} API file(s)...") | ||
|
||
val violations = mutableMapOf<File, Set<String>>() | ||
inputs.files.forEach { apiFile -> | ||
logger.lifecycle("Validating public API from file ${apiFile.path}") | ||
|
||
apiFile.useLines { lines -> | ||
val actualDataClasses = findDataClasses(lines) | ||
|
||
if (actualDataClasses.isNotEmpty()) { | ||
violations[apiFile] = actualDataClasses | ||
} | ||
} | ||
} | ||
|
||
if (violations.isNotEmpty()) { | ||
val message = buildString { | ||
appendLine("Data classes found in public API.") | ||
appendLine() | ||
|
||
for ((file, dataClasses) in violations.entries) { | ||
appendLine("In file ${file.path}:") | ||
for (dataClass in dataClasses) { | ||
appendLine(" * ${dataClass.replace("/", ".")}") | ||
} | ||
appendLine() | ||
} | ||
} | ||
|
||
throw GradleException(message) | ||
} else { | ||
logger.lifecycle("No public API violations found.") | ||
} | ||
} | ||
|
||
private fun findDataClasses(lines: Sequence<String>): Set<String> { | ||
val currentClassStack = Stack<String>() | ||
val dataClasses = mutableMapOf<String, DataClassInfo>() | ||
|
||
for (line in lines) { | ||
if (line.isBlank()) continue | ||
|
||
val matchResult = classFqnRegex.find(line) | ||
if (matchResult != null) { | ||
val classFqn = matchResult.groupValues[1] | ||
currentClassStack.push(classFqn) | ||
continue | ||
} | ||
|
||
if (line.contains("}")) { | ||
currentClassStack.pop() | ||
continue | ||
} | ||
|
||
val fqn = currentClassStack.peek() | ||
if (copyMethodRegex.find(line) != null) { | ||
val info = dataClasses.getOrPut(fqn) { DataClassInfo(fqn) } | ||
info.hasCopyMethod = true | ||
} else if (line.contains("public static final synthetic fun box-impl")) { | ||
val info = dataClasses.getOrPut(fqn) { DataClassInfo(fqn) } | ||
info.isLikelyValueClass = true | ||
} | ||
} | ||
|
||
val actualDataClasses = dataClasses.filterValues { it.hasCopyMethod && !it.isLikelyValueClass } | ||
.keys | ||
return actualDataClasses | ||
} | ||
} | ||
|
||
@Suppress("DataClassShouldBeImmutable") // Only used in a loop, saves memory and is faster | ||
private data class DataClassInfo( | ||
val fqn: String, | ||
var hasCopyMethod: Boolean = false, | ||
var isLikelyValueClass: Boolean = false, | ||
) |
24 changes: 24 additions & 0 deletions
24
buildSrc/src/main/kotlin/jewel-check-public-api.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
plugins { | ||
id("org.jetbrains.kotlinx.binary-compatibility-validator") | ||
id("dev.drewhamilton.poko") | ||
} | ||
|
||
apiValidation { | ||
/** | ||
* Set of annotations that exclude API from being public. Typically, it is | ||
* all kinds of `@InternalApi` annotations that mark effectively private | ||
* API that cannot be actually private for technical reasons. | ||
*/ | ||
nonPublicMarkers.add("org.jetbrains.jewel.InternalJewelApi") | ||
} | ||
|
||
tasks { | ||
val validatePublicApi = register<ValidatePublicApiTask>("validatePublicApi") { | ||
include { it.file.extension == "api" } | ||
source(project.fileTree("api")) | ||
} | ||
|
||
named("check") { | ||
dependsOn(validatePublicApi) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
plugins { | ||
jewel | ||
`jewel-publish` | ||
`jewel-check-public-api` | ||
alias(libs.plugins.composeDesktop) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
plugins { | ||
jewel | ||
`jewel-publish` | ||
`jewel-check-public-api` | ||
alias(libs.plugins.composeDesktop) | ||
} | ||
|
||
|