Skip to content

Commit

Permalink
Fix updating version substrings for rules
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszwawrzyk committed May 5, 2023
1 parent f091892 commit 23d12cc
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.virtuslab.bazelsteward.e2e.rules.heuristic
import org.virtuslab.bazelsteward.e2e.rules.fixture.BazelRulesHeuristicUpdate

class HeuristicUpdateMultipleTest : BazelRulesHeuristicUpdate(
"rules/heuristic/heuristic_rules_multiple_urls",
"rules/heuristic/heuristic_rules_multiple",
"https://github.com/bazelbuild/rules_closure/archive/0.12.0.tar.gz",
"7d206c2383811f378a5ef03f4aacbcf5f47fd8650f6abbc3fa89f3a27ddcccc",
"0.12.0",
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ http_archive(
strip_prefix = "rules_jvm_external-{}".format(RULES_JVM_EXTERNAL_TAG),
url = "https://github.com/bazelbuild/rules_jvm_external/archive/4.5.zip",
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
artifacts = [
"net.java.dev.jna:jna:4.5.2",
"com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.5",
],
repositories = [
"https://repo1.maven.org/maven2",
],
)
12 changes: 12 additions & 0 deletions e2e/src/test/resources/rules/rules_jvm_external/WORKSPACE.expected
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,15 @@ http_archive(
strip_prefix = "rules_jvm_external-{}".format(RULES_JVM_EXTERNAL_TAG),
url = "https://github.com/bazelbuild/rules_jvm_external/releases/download/5.1/rules_jvm_external-5.1.tar.gz",
)

load("@rules_jvm_external//:defs.bzl", "maven_install")

maven_install(
artifacts = [
"net.java.dev.jna:jna:4.5.2",
"com.googlecode.concurrentlinkedhashmap:concurrentlinkedhashmap-lru:1.4.5",
],
repositories = [
"https://repo1.maven.org/maven2",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@ object BazelRuleHeuristic : VersionReplacementHeuristic {
override val name: String = "bazel-rule-default"

private class ReplaceRequest(
current: String,
private val regexes: List<Regex>,
val suggested: String,
forbiddenSurrounding: String? = null,
) {
val regex = if (forbiddenSurrounding != null) {
"""(?<!$forbiddenSurrounding)(${Regex.escape(current)})(?!$forbiddenSurrounding)""".toRegex()
} else {
"""(${Regex.escape(current)})""".toRegex()
}
constructor(current: String, suggested: String, forbiddenSurrounding: String? = null) : this(
listOf(
if (forbiddenSurrounding != null) {
"""(?<!$forbiddenSurrounding)(${Regex.escape(current)})(?!$forbiddenSurrounding)""".toRegex()
} else {
"""(${Regex.escape(current)})""".toRegex()
},
),
suggested,
)

fun allMatches(content: String): List<MatchResult> = regexes.flatMap { it.findAll(content).toList() }
}

override fun apply(files: List<TextFile>, updateSuggestion: UpdateSuggestion): LibraryUpdate? {
Expand All @@ -35,13 +41,19 @@ object BazelRuleHeuristic : VersionReplacementHeuristic {

val replaceRequests = listOf(
ReplaceRequest(currentUrl, suggestedUrl),
ReplaceRequest(currentSha, suggestedSha, "[a-z0-9]"),
ReplaceRequest(currentVersion, suggestedVersion, "[0-9]"),
ReplaceRequest(currentSha, suggestedSha, forbiddenSurrounding = "[a-z0-9]"),
ReplaceRequest(
listOf(
"""(?<=")(${Regex.escape(currentVersion)})(?=")""".toRegex(),
"""(?<![0-9]|[0-9]\.)(${Regex.escape(currentVersion)})(?="|\.tar\.gz|\.tgz|\.tar|\.zip)""".toRegex(),
),
suggestedVersion,
),
)

val groupedChanges = files.map { file ->
replaceRequests.map { request ->
val matches = request.regex.findAll(file.content)
val matches = request.allMatches(file.content)
matches.mapNotNull { match ->
match.groups.first()?.range?.let { matchRange ->
FileChange(
Expand Down

0 comments on commit 23d12cc

Please sign in to comment.