From 23d12cc666fd330567828067865c746edaa63ef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wawrzyk?= Date: Fri, 5 May 2023 15:10:04 +0200 Subject: [PATCH] Fix updating version substrings for rules --- .../heuristic/HeuristicUpdateMultipleTest.kt | 2 +- .../HeuristicUpdateMultipleUrlsTest.kt | 11 ------- .../Result_WORKSPACE.bzlignore | 17 ---------- .../WORKSPACE.bzlignore | 17 ---------- .../rules_jvm_external/WORKSPACE.bzlignore | 12 +++++++ .../rules_jvm_external/WORKSPACE.expected | 12 +++++++ .../bazel/rules/BazelRuleHeuristic.kt | 32 +++++++++++++------ 7 files changed, 47 insertions(+), 56 deletions(-) delete mode 100644 e2e/src/test/kotlin/org/virtuslab/bazelsteward/e2e/rules/heuristic/HeuristicUpdateMultipleUrlsTest.kt delete mode 100644 e2e/src/test/resources/rules/heuristic/heuristic_rules_multiple_urls/Result_WORKSPACE.bzlignore delete mode 100644 e2e/src/test/resources/rules/heuristic/heuristic_rules_multiple_urls/WORKSPACE.bzlignore diff --git a/e2e/src/test/kotlin/org/virtuslab/bazelsteward/e2e/rules/heuristic/HeuristicUpdateMultipleTest.kt b/e2e/src/test/kotlin/org/virtuslab/bazelsteward/e2e/rules/heuristic/HeuristicUpdateMultipleTest.kt index 34630f0d..290c7c81 100644 --- a/e2e/src/test/kotlin/org/virtuslab/bazelsteward/e2e/rules/heuristic/HeuristicUpdateMultipleTest.kt +++ b/e2e/src/test/kotlin/org/virtuslab/bazelsteward/e2e/rules/heuristic/HeuristicUpdateMultipleTest.kt @@ -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", diff --git a/e2e/src/test/kotlin/org/virtuslab/bazelsteward/e2e/rules/heuristic/HeuristicUpdateMultipleUrlsTest.kt b/e2e/src/test/kotlin/org/virtuslab/bazelsteward/e2e/rules/heuristic/HeuristicUpdateMultipleUrlsTest.kt deleted file mode 100644 index d9ad7605..00000000 --- a/e2e/src/test/kotlin/org/virtuslab/bazelsteward/e2e/rules/heuristic/HeuristicUpdateMultipleUrlsTest.kt +++ /dev/null @@ -1,11 +0,0 @@ -package org.virtuslab.bazelsteward.e2e.rules.heuristic - -import org.virtuslab.bazelsteward.e2e.rules.fixture.BazelRulesHeuristicUpdate - -class HeuristicUpdateMultipleUrlsTest : BazelRulesHeuristicUpdate( - "rules/heuristic/heuristic_rules_multiple_urls", - "https://github.com/bazelbuild/rules_closure/archive/0.12.0.tar.gz", - "7d206c2383811f378a5ef03f4aacbcf5f47fd8650f6abbc3fa89f3a27ddcccc", - "0.12.0", - "rules_closure", -) diff --git a/e2e/src/test/resources/rules/heuristic/heuristic_rules_multiple_urls/Result_WORKSPACE.bzlignore b/e2e/src/test/resources/rules/heuristic/heuristic_rules_multiple_urls/Result_WORKSPACE.bzlignore deleted file mode 100644 index a95fc744..00000000 --- a/e2e/src/test/resources/rules/heuristic/heuristic_rules_multiple_urls/Result_WORKSPACE.bzlignore +++ /dev/null @@ -1,17 +0,0 @@ -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -# rules_closure - -http_archive( - name = "io_bazel_rules_closure", - sha256 = "7d206c2383811f378a5ef03f4aacbcf5f47fd8650f6abbc3fa89f3a27ddcccc", - strip_prefix = "rules_closure-0.12.0", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_closure/archive/0.12.0.tar.gz", - "https://github.com/bazelbuild/rules_closure/archive/0.12.0.tar.gz", - ], -) - -load("@io_bazel_rules_closure//closure:repositories.bzl", "rules_closure_dependencies", "rules_closure_toolchains") -rules_closure_dependencies() -rules_closure_toolchains() diff --git a/e2e/src/test/resources/rules/heuristic/heuristic_rules_multiple_urls/WORKSPACE.bzlignore b/e2e/src/test/resources/rules/heuristic/heuristic_rules_multiple_urls/WORKSPACE.bzlignore deleted file mode 100644 index a742a641..00000000 --- a/e2e/src/test/resources/rules/heuristic/heuristic_rules_multiple_urls/WORKSPACE.bzlignore +++ /dev/null @@ -1,17 +0,0 @@ -load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") - -# rules_closure - -http_archive( - name = "io_bazel_rules_closure", - sha256 = "7d206c2383811f378a5ef03f4aacbcf5f47fd8650f6abbc3fa89f3a27dd8b176", - strip_prefix = "rules_closure-0.10.0", - urls = [ - "https://mirror.bazel.build/github.com/bazelbuild/rules_closure/archive/0.10.0.tar.gz", - "https://github.com/bazelbuild/rules_closure/archive/0.10.0.tar.gz", - ], -) - -load("@io_bazel_rules_closure//closure:repositories.bzl", "rules_closure_dependencies", "rules_closure_toolchains") -rules_closure_dependencies() -rules_closure_toolchains() diff --git a/e2e/src/test/resources/rules/rules_jvm_external/WORKSPACE.bzlignore b/e2e/src/test/resources/rules/rules_jvm_external/WORKSPACE.bzlignore index af1d77fd..aed82baa 100644 --- a/e2e/src/test/resources/rules/rules_jvm_external/WORKSPACE.bzlignore +++ b/e2e/src/test/resources/rules/rules_jvm_external/WORKSPACE.bzlignore @@ -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", + ], +) diff --git a/e2e/src/test/resources/rules/rules_jvm_external/WORKSPACE.expected b/e2e/src/test/resources/rules/rules_jvm_external/WORKSPACE.expected index 356031c9..ada7e7ea 100644 --- a/e2e/src/test/resources/rules/rules_jvm_external/WORKSPACE.expected +++ b/e2e/src/test/resources/rules/rules_jvm_external/WORKSPACE.expected @@ -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", + ], +) diff --git a/kinds/bazel/rules/src/main/kotlin/org/virtuslab/bazelsteward/bazel/rules/BazelRuleHeuristic.kt b/kinds/bazel/rules/src/main/kotlin/org/virtuslab/bazelsteward/bazel/rules/BazelRuleHeuristic.kt index 2ae07cc7..6234d79d 100644 --- a/kinds/bazel/rules/src/main/kotlin/org/virtuslab/bazelsteward/bazel/rules/BazelRuleHeuristic.kt +++ b/kinds/bazel/rules/src/main/kotlin/org/virtuslab/bazelsteward/bazel/rules/BazelRuleHeuristic.kt @@ -10,15 +10,21 @@ object BazelRuleHeuristic : VersionReplacementHeuristic { override val name: String = "bazel-rule-default" private class ReplaceRequest( - current: String, + private val regexes: List, val suggested: String, - forbiddenSurrounding: String? = null, ) { - val regex = if (forbiddenSurrounding != null) { - """(? = regexes.flatMap { it.findAll(content).toList() } } override fun apply(files: List, updateSuggestion: UpdateSuggestion): LibraryUpdate? { @@ -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(), + """(? 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(