Skip to content
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 8 commits into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ class BazelVersionFileSearch(config: Config) {
private val fileNames = setOf(".bazelversion", ".bazeliskrc")

val bazelVersionFiles: List<BazelFileSearch.BazelFile> by lazy {
config.path.listDirectoryEntries().filter { fileNames.contains(it.name) }.map { BazelFileSearch.BazelFile(it) }
Copy link
Collaborator

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:

  • BazelFile as interface (just like you did)
  • factory method for creating the lazy version, i.e. def createBazelFile(path: Path): BazelFile
    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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

config.path.listDirectoryEntries().filter { fileNames.contains(it.name) }.map { BazelFileSearch.createBazelFile(it) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ import java.nio.file.Path
import kotlin.io.path.readText

class BazelFileSearch(config: Config) {
data class BazelFile(val path: Path) {
interface BazelFile {
val path: Path
val content: String
}

private data class LazyBazelFile(override val path: Path) : BazelFile {
override val content: String
get() = path.readText()
}

Expand All @@ -22,5 +27,9 @@ class BazelFileSearch(config: Config) {
.toList()
}

val buildDefinitions: List<BazelFile> by lazy { buildPaths.map { BazelFile(it) } }
val buildDefinitions: List<BazelFile> by lazy { buildPaths.map { createBazelFile(it) } }

companion object {
fun createBazelFile(path: Path): BazelFile = LazyBazelFile(path)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package org.virtuslab.bazelsteward.core.common
import org.virtuslab.bazelsteward.core.library.Library
import org.virtuslab.bazelsteward.core.library.LibraryId
import org.virtuslab.bazelsteward.core.library.Version
import org.virtuslab.bazelsteward.core.replacement.VersionOnlyHeuristic
import org.virtuslab.bazelsteward.core.replacement.WholeLibraryHeuristic
import java.nio.file.Path

class FileUpdateSearch {
Expand All @@ -23,15 +25,14 @@ class FileUpdateSearch {
files: List<BazelFileSearch.BazelFile>,
updateSuggestion: UpdateLogic.UpdateSuggestion<Lib>
): 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 FileChangeSuggestion(
updateSuggestion.currentLibrary, updateSuggestion.suggestedVersion, matchResult.second, versionGroup.range.first
val allHeuristics = listOf(
WholeLibraryHeuristic(),
VersionOnlyHeuristic(),
)
for (heuristic in allHeuristics) {
heuristic.apply(files, updateSuggestion)?.let { return it }
}
return null
}

fun <Lib : LibraryId> searchBazelVersionFiles(
Expand Down
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?
}
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
)
}
}
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
)
}
}
Loading