Skip to content
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: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ libs/

# all build dirs at any level
**/build

**/*.hprof
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fun Project2.statementOrNullIn(
.parse(dependentBuildFile)
.firstNotNullOfOrNull { block ->
block.getOrEmpty(path, configuration.value)
.takeIf { it.isNotEmpty() }
}
?.firstOrNull()
?.statementWithSurroundingText
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,69 @@ abstract class DependenciesBlock(var contentString: String) {
.orEmpty()
}

protected abstract fun findOriginalStringIndex(parsedString: String): Int
/**
* Compares the target parsed string to the un-parsed lines of the original dependencies block,
* and returns the index of **the last row** which matches the parsed string.
*
* So, given the target:
* ```
* api(projects.foo.bar) {
* exclude(group = "androidx.appcompat")
* }
* ```
* And given the dependencies lines:
* ```
* <blank line>
* // Remove leaking AppCompat dependency
* api(projects.foo.bar) {
* exclude(group = "androidx.appcompat")
* } // this is index 4
* api(libs.junit)
* ```
*
* This function would return index `4`, because rows 2-4 match the target parsed string.
*
* From this value, [getOriginalString] will return a multi-line string which includes
* the blank line and the comment.
*/
private fun findLastMatchingRowIndex(parsedString: String): Int {
val targetLines = parsedString.lines()
.map { it.trimStart() }

// index is incremented at least once (if the first line is a match), so start at -1
var index = -1

var matched: Boolean

do {
val candidates = originalLines
.drop(index + 1)
.take(targetLines.size)
.map { it.trimStart() }

matched = candidates.zip(targetLines)
.all { (candidate, target) ->
originalLineMatchesParsed(candidate, target)
}

if (matched) {
index += targetLines.size
} else {

index++
}
} while (!matched)

return index
}

protected abstract fun originalLineMatchesParsed(
originalLine: String,
parsedString: String
): Boolean

protected fun getOriginalString(parsedString: String): String {
val originalStringIndex = findOriginalStringIndex(parsedString)
private fun getOriginalString(parsedString: String): String {
val originalStringIndex = findLastMatchingRowIndex(parsedString)

val originalStringLines = List(originalStringIndex + 1) {
originalLines.removeFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ class GroovyDependenciesBlock(
contentString: String
) : DependenciesBlock(contentString) {

override fun findOriginalStringIndex(parsedString: String) = originalLines
.indexOfFirst { originalLine ->
originalLine.collapseBlockComments()
.trimEachLineStart()
.trimLinesLikeAntlr()
.lines()
.any { it.startsWith(parsedString) }
override fun originalLineMatchesParsed(
originalLine: String,
parsedString: String
) = originalLine.collapseBlockComments()
.trimEachLineStart()
.trimLinesLikeAntlr()
.lines()
.any { str ->

str.startsWith(parsedString)
}

override fun toString(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class GroovyDependencyBlockParser {
val blockStatementVisitor = object : GroovyParserBaseVisitor<Unit>() {

override fun visitBlockStatement(ctx: BlockStatementContext) {
super.visitBlockStatement(ctx)

val config = ctx.start.text

Expand Down
Loading