Skip to content

Commit

Permalink
Merge pull request #527 from fthomas/topic/replaceAllInSliding
Browse files Browse the repository at this point in the history
Add new replaceAllInSliding strategy
  • Loading branch information
fthomas authored May 28, 2019
2 parents 431a981 + 6911cd1 commit d63b982
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,21 @@ sealed trait Update extends Product with Serializable {
newerVersions.head

def replaceAllInStrict(target: String): Option[String] =
replaceAllInImpl(target, true, false)
replaceAllInImpl(target, true, identity)

def replaceAllIn(target: String): Option[String] =
replaceAllInImpl(target, false, false)
replaceAllInImpl(target, false, identity)

def replaceAllInRelaxed(target: String): Option[String] =
replaceAllInImpl(target, false, true)
replaceAllInImpl(target, false, terms => terms ++ util.string.extractWords(artifactId))

def replaceAllInSliding(target: String): Option[String] =
replaceAllInImpl(target, false, terms => terms ++ artifactId.sliding(5).take(5).toList)

def replaceAllInImpl(
target: String,
includeGroupId: Boolean,
splitArtifactId: Boolean
modifySearchTerms: Nel[String] => Nel[String]
): Option[String] = {
def replaceVersion(regex: Regex): Option[String] =
util.string.replaceSomeInOpt(
Expand All @@ -65,10 +68,7 @@ sealed trait Update extends Product with Serializable {
Some(group1 + m.group(2) + nextVersion)
}
)

val artifactIdParts = if (splitArtifactId) util.string.extractWords(artifactId) else Nil
val quotedSearchTerms = searchTerms
.concat(artifactIdParts)
val quotedSearchTerms = modifySearchTerms(searchTerms)
.map { term =>
Regex
.quoteReplacement(Update.removeCommonSuffix(term))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ object EditAlg {
log("replaceAllIn") >>
fileAlg.editSourceFiles(repoDir, update.replaceAllIn),
log("replaceAllInRelaxed") >>
fileAlg.editSourceFiles(repoDir, update.replaceAllInRelaxed)
fileAlg.editSourceFiles(repoDir, update.replaceAllInRelaxed),
log("replaceAllInSliding") >>
fileAlg.editSourceFiles(repoDir, update.replaceAllInSliding)
)

bindUntilTrue(strategies).void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ class UpdateTest extends FunSuite with Matchers {
.replaceAllInRelaxed(original) shouldBe Some(expected)
}

test("replaceAllInSliding: mongo from mongodb") {
val original = """val mongoVersion = "3.7.0" """
val expected = """val mongoVersion = "3.7.1" """
Group(
"org.mongodb",
Nel.of("mongodb-driver", "mongodb-driver-async", "mongodb-driver-core"),
"3.7.0",
Nel.of("3.7.1")
).replaceAllInSliding(original) shouldBe Some(expected)
}

test("Group.artifactId") {
Group(
"org.http4s",
Expand Down

0 comments on commit d63b982

Please sign in to comment.