-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try in PythonMethodHeuristic Logic Fixed PythonMethodHeuristic
- Loading branch information
Showing
10 changed files
with
157 additions
and
8 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
2 changes: 1 addition & 1 deletion
2
core/src/main/kotlin/org/virtuslab/bazelsteward/core/library/LibraryId.kt
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,7 +1,7 @@ | ||
package org.virtuslab.bazelsteward.core.library | ||
|
||
abstract class LibraryId { | ||
abstract fun associatedStrings(): List<String> | ||
abstract fun associatedStrings(): List<List<String>> | ||
abstract val name: String | ||
final override fun toString(): String = name | ||
} |
67 changes: 67 additions & 0 deletions
67
core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/PythonMethodHeuristic.kt
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,67 @@ | ||
package org.virtuslab.bazelsteward.core.replacement | ||
|
||
import org.virtuslab.bazelsteward.core.common.FileChange | ||
import org.virtuslab.bazelsteward.core.common.TextFile | ||
import org.virtuslab.bazelsteward.core.common.UpdateSuggestion | ||
import java.nio.file.Path | ||
|
||
object PythonMethodHeuristic : VersionReplacementHeuristic { | ||
override val name: String = "python-method" | ||
|
||
override fun apply(files: List<TextFile>, updateSuggestion: UpdateSuggestion): LibraryUpdate? { | ||
val pythonMethodMatchResult = getPythonMethods(files) | ||
|
||
if (pythonMethodMatchResult.isNotEmpty()) { | ||
val libraryMatchResult = updateSuggestion.currentLibrary.id.associatedStrings().let { libAssociatedStrings -> | ||
(libAssociatedStrings.size - 1).let { associatedIndex -> | ||
findRegexInListOfMatchResults( | ||
libAssociatedStrings[associatedIndex][1], | ||
findRegexInListOfMatchResults(libAssociatedStrings[associatedIndex][0], pythonMethodMatchResult) | ||
) | ||
} | ||
} | ||
|
||
val currentVersion = updateSuggestion.currentLibrary.version.value | ||
val versionMatchResult = findRegexInListOfMatchResults(currentVersion, libraryMatchResult, false) | ||
if (versionMatchResult.isEmpty()) return null | ||
|
||
val versionOffset = libraryMatchResult.first()?.first?.range?.let { | ||
versionMatchResult.first()?.first?.range?.first?.plus( | ||
it.first) ?: return null | ||
} ?: return null | ||
|
||
return LibraryUpdate( | ||
updateSuggestion, | ||
listOf( | ||
FileChange( | ||
versionMatchResult.first()!!.second, | ||
versionOffset, | ||
updateSuggestion.currentLibrary.version.value.length, | ||
updateSuggestion.suggestedVersion.value | ||
) | ||
) | ||
) | ||
} | ||
return null | ||
} | ||
|
||
private fun getPythonMethods(files: List<TextFile>): List<Pair<MatchResult, Path>?> { | ||
val pythonMethodRegex = Regex("\\w+.\\w+\\([a-zA-Z0-1\\n\\s\".,-=]+\\)") | ||
return files | ||
.map { textFile -> pythonMethodRegex.findAll(textFile.content).map { it to textFile.path }.toList() } | ||
.flatten() | ||
} | ||
|
||
private fun findRegexInListOfMatchResults( | ||
regexString: String, | ||
listToLookFor: List<Pair<MatchResult, Path>?>, | ||
returnOriginal: Boolean = true | ||
) : List<Pair<MatchResult, Path>?> = | ||
listToLookFor.mapNotNull { | ||
it?.let { | ||
Regex(Regex.escape(regexString)).find(it.first.value)?.let { found -> | ||
if (returnOriginal) it else found to it.second | ||
} | ||
} | ||
} | ||
} |
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