-
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.
The big refactor — step 1: prep work (#192)
* Cleanup code, remove compiler warnings * Add API validation to check task 1. Running binary-compatibility-validator on code 2. Flagging public data classes and failing build 3. Running Poko to autogen equals/hashcode/toString * Remove all public data classes, add API dumps
- Loading branch information
Showing
52 changed files
with
5,119 additions
and
155 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
4 changes: 2 additions & 2 deletions
4
.idea/runConfigurations/Run_checks.xml → .idea/runConfigurations/Reformat_project.xml
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -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, | ||
) |
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
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") | ||
} | ||
|
||
poko { | ||
pokoAnnotation.set("org.jetbrains.jewel.GenerateDataFunctions") | ||
} | ||
|
||
tasks { | ||
val validatePublicApi = register<ValidatePublicApiTask>("validatePublicApi") { | ||
include { it.file.extension == "api" } | ||
source(project.fileTree("api")) | ||
dependsOn(named("apiCheck")) | ||
} | ||
|
||
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
Oops, something went wrong.