Skip to content

Commit

Permalink
Fixes after review
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkoziel authored and lukaszwawrzyk committed Feb 22, 2023
1 parent 73d343a commit a0d7307
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ class VersionReplacementHeuristicTest {
result?.offset shouldBe correctPositionFor3212
}

@Test
fun `should return null for wrong scala dep`() {
val library = library("org.scalactic", "scalactic", "3.2.90")
val suggestedVersion = version("4.0.0")

val result = resolveUpdates(library, suggestedVersion, PythonFunctionCallHeuristic)

result shouldBe null
}

@Test
fun `should return correct position offset scala dep named parameters`() {
val library = library("junit", "junit", "4.13.2")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,27 @@ object PythonFunctionCallHeuristic : VersionReplacementHeuristic {
override val name: String = "python-function-call"

override fun apply(files: List<TextFile>, updateSuggestion: UpdateSuggestion): LibraryUpdate? {
val pythonFunctionMatchResultList = getPythonFunctionCalls(files)
val functionCalls = getFunctionCalls(files)

if (pythonFunctionMatchResultList.isNotEmpty()) {

val libAssociatedStrings = updateSuggestion.currentLibrary.id.associatedStrings()
val libraryMatchResultList = libAssociatedStrings.flatMap {
var listToLookFor = pythonFunctionMatchResultList
it.flatMap { stringForRegex ->
filterRegexInListOfMatchResults(stringForRegex, listToLookFor).also { res -> listToLookFor = res }
if (functionCalls.isNotEmpty()) {
val associatedStringVariants = updateSuggestion.currentLibrary.id.associatedStrings()
val functionCallsWithAssociatedStrings = functionCalls.filter { call ->
associatedStringVariants.any { strings ->
strings.all { call.matchedText.contains(it) }
}
}

val currentVersion = updateSuggestion.currentLibrary.version.value
val versionMatchResultList = filterRegexInListOfMatchResults(currentVersion, libraryMatchResultList, false)
if (versionMatchResultList.isEmpty()) return null

val versionOffset = libraryMatchResultList.first().getRangeStart() + versionMatchResultList.first().getRangeStart()
val (parentMatch, matchedVersion) = functionCallsWithAssociatedStrings.firstNotNullOfOrNull { call ->
Regex.fromLiteral(currentVersion).find(call.matchedText)?.let { call.subMatch(it) }
} ?: return null

return LibraryUpdate(
updateSuggestion,
listOf(
FileChange(
versionMatchResultList.first().path,
versionOffset,
matchedVersion.origin,
parentMatch.offset + matchedVersion.offset,
updateSuggestion.currentLibrary.version.value.length,
updateSuggestion.suggestedVersion.value
)
Expand All @@ -41,21 +38,10 @@ object PythonFunctionCallHeuristic : VersionReplacementHeuristic {
return null
}

private fun getPythonFunctionCalls(files: List<TextFile>): List<MatchResultPath> {
private fun getFunctionCalls(files: List<TextFile>): List<MatchedText> {
val pythonMethodRegex = Regex("""\w+\([\w\n\s".\-,=]+\)""")
return files.flatMap { textFile ->
pythonMethodRegex.findAll(textFile.content).map { MatchResultPath(it, textFile.path) }
pythonMethodRegex.findAll(textFile.content).map { MatchedText(it, textFile.path) }
}
}

private fun filterRegexInListOfMatchResults(
stringForRegex: String,
listToLookFor: List<MatchResultPath>,
returnOriginal: Boolean = true
): List<MatchResultPath> =
listToLookFor.mapNotNull {
Regex(Regex.escape(stringForRegex)).find(it.getValue())?.let { found ->
if (returnOriginal) it else MatchResultPath(found, it.path)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ object VersionOnlyHeuristic : VersionReplacementHeuristic {
override fun apply(files: List<TextFile>, updateSuggestion: UpdateSuggestion): LibraryUpdate? {
val currentVersion = updateSuggestion.currentLibrary.version.value
val regex = Regex(Regex.escape(currentVersion))
val matchResult = files.firstNotNullOfOrNull { regex.find(it.content)?.to(it.path) } ?: return null
matchResult.first.next()?.let { return null }
val versionGroup = matchResult.first.groups[0] ?: return null
val matchResult = files.firstNotNullOfOrNull { f ->
regex.find(f.content)?.let {
MatchedText(it, f.path)
}
} ?: return null
matchResult.match.next()?.let { return null }
val versionOffset = matchResult.offsetLastMatchGroup ?: return null

return LibraryUpdate(
updateSuggestion,
listOf(
FileChange(
matchResult.second,
versionGroup.range.first,
matchResult.origin,
versionOffset,
updateSuggestion.currentLibrary.version.value.length,
updateSuggestion.suggestedVersion.value
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ data class LibraryUpdate(
val fileChanges: List<FileChange>
)

data class MatchResultPath(
val matchResult: MatchResult,
val path: Path
data class MatchedText(
val match: MatchResult,
val origin: Path
) {
fun getRangeStart(): Int = matchResult.range.start
fun getGroup(index: Int): MatchGroup? = matchResult.groups[index]
val offset: Int
get() = match.range.start

fun getValue(): String = matchResult.value
val offsetLastMatchGroup: Int?
get() = match.groups.last()?.range?.start
val matchedText: String
get() = match.value

fun subMatch(other: MatchResult): Pair<MatchedText, MatchedText> = this to MatchedText(other, origin)
}

interface VersionReplacementHeuristic {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@ object WholeLibraryHeuristic : VersionReplacementHeuristic {
val regexes = markers.map { marker ->
(marker + currentVersion).map { """(${Regex.escape(it)})""" }.reduce { acc, s -> "$acc.*$s" }.toRegex()
}
val matchResultsList = regexes.mapNotNull { regex ->
val matchResult = regexes.firstNotNullOfOrNull { regex ->
files.firstNotNullOfOrNull { textFile ->
regex.find(textFile.content)?.let {
MatchResultPath(it, textFile.path)
MatchedText(it, textFile.path)
}
}
}
if (matchResultsList.isEmpty()) return null
val versionGroup = matchResultsList.first().getGroup(3) ?: return null
} ?: return null
val versionOffset = matchResult.offsetLastMatchGroup ?: return null

return LibraryUpdate(
updateSuggestion,
listOf(
FileChange(
matchResultsList.first().path,
versionGroup.range.start,
matchResult.origin,
versionOffset,
updateSuggestion.currentLibrary.version.value.length,
updateSuggestion.suggestedVersion.value
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import org.virtuslab.bazelsteward.core.library.VersioningSchema
private val logger = KotlinLogging.logger {}

object BazelLibraryId : LibraryId() {
override fun associatedStrings(): List<List<String>> = listOf(listOf("", "USE_BAZEL_VERSION"))
override fun associatedStrings(): List<List<String>> = listOf(listOf("USE_BAZEL_VERSION"))

override val name: String
get() = "bazel"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.virtuslab.bazelsteward.core.library.Version

data class MavenLibraryId(val group: String, val artifact: String) : LibraryId() {
override fun associatedStrings(): List<List<String>> {
return Regex("""_[\d.]+${'$'}""").find(artifact)?.let {
return Regex("""_[\d.]+$""").find(artifact)?.let {
listOf(listOf(group, artifact), listOf(group, artifact.removeRange(it.range)))
} ?: listOf(listOf(group, artifact))
}
Expand Down

0 comments on commit a0d7307

Please sign in to comment.