-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replacement Heuristic #132
Merged
Merged
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
654fa81
Change replacement heuristic
mikkoziel 5c4f964
Expanded heuristic testes
mikkoziel a524118
Fixes after lint
mikkoziel 9e36f2d
Fixes after buildifier github action failed
mikkoziel daa15b0
Fixes after buildifier github action failed
mikkoziel 8e9ece9
Fixes after review
mikkoziel 4c5dcb3
Changes after lint
mikkoziel 2628ea1
Added test cases for same version of two libraries
mikkoziel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
14 changes: 14 additions & 0 deletions
14
core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/Heuristic.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,14 @@ | ||
package org.virtuslab.bazelsteward.core.replacement | ||
|
||
import org.virtuslab.bazelsteward.core.common.BazelFileSearch | ||
import org.virtuslab.bazelsteward.core.common.FileUpdateSearch | ||
import org.virtuslab.bazelsteward.core.common.UpdateLogic | ||
import org.virtuslab.bazelsteward.core.library.LibraryId | ||
|
||
interface Heuristic { | ||
val name: String | ||
fun <Lib : LibraryId> apply( | ||
files: List<BazelFileSearch.BazelFile>, | ||
updateSuggestion: UpdateLogic.UpdateSuggestion<Lib> | ||
): FileUpdateSearch.FileChangeSuggestion? | ||
} |
24 changes: 24 additions & 0 deletions
24
core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/VersionOnlyHeuristic.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,24 @@ | ||
package org.virtuslab.bazelsteward.core.replacement | ||
|
||
import org.virtuslab.bazelsteward.core.common.BazelFileSearch | ||
import org.virtuslab.bazelsteward.core.common.FileUpdateSearch | ||
import org.virtuslab.bazelsteward.core.common.UpdateLogic | ||
import org.virtuslab.bazelsteward.core.library.LibraryId | ||
|
||
class VersionOnlyHeuristic : Heuristic { | ||
override val name: String = "version-only" | ||
|
||
override fun <Lib : LibraryId> apply( | ||
files: List<BazelFileSearch.BazelFile>, | ||
updateSuggestion: UpdateLogic.UpdateSuggestion<Lib> | ||
): FileUpdateSearch.FileChangeSuggestion? { | ||
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 | ||
return FileUpdateSearch.FileChangeSuggestion( | ||
updateSuggestion.currentLibrary, updateSuggestion.suggestedVersion, matchResult.second, versionGroup.range.first | ||
) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
core/src/main/kotlin/org/virtuslab/bazelsteward/core/replacement/WholeLibraryHeuristic.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,25 @@ | ||
package org.virtuslab.bazelsteward.core.replacement | ||
|
||
import org.virtuslab.bazelsteward.core.common.BazelFileSearch | ||
import org.virtuslab.bazelsteward.core.common.FileUpdateSearch | ||
import org.virtuslab.bazelsteward.core.common.UpdateLogic | ||
import org.virtuslab.bazelsteward.core.library.LibraryId | ||
|
||
class WholeLibraryHeuristic : Heuristic { | ||
override val name: String = "whole-library" | ||
|
||
override fun <Lib : LibraryId> apply( | ||
files: List<BazelFileSearch.BazelFile>, | ||
updateSuggestion: UpdateLogic.UpdateSuggestion<Lib> | ||
): FileUpdateSearch.FileChangeSuggestion? { | ||
val markers = updateSuggestion.currentLibrary.id.associatedStrings() | ||
val currentVersion = updateSuggestion.currentLibrary.version.value | ||
val regex = | ||
(markers + currentVersion).map { """(${Regex.escape(it)})""" }.reduce { acc, s -> "$acc.*$s" }.let { Regex(it) } | ||
val matchResult = files.firstNotNullOfOrNull { regex.find(it.content)?.to(it.path) } ?: return null | ||
val versionGroup = matchResult.first.groups[3] ?: return null | ||
return FileUpdateSearch.FileChangeSuggestion( | ||
updateSuggestion.currentLibrary, updateSuggestion.suggestedVersion, matchResult.second, versionGroup.range.first | ||
) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, let's cleanup the naming.
Let BazelFileSearch expose:
So make BazelFileLazy private, just return it from the factory method.
Also rename BazelFileLazy to LazyBazelFile.
It is still not a perfect design, all of this needs to be eventually renamed to something more generic than BazelFile, but let's at least keep what we have now in some order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed