Last active
January 23, 2019 11:29
-
-
Save iRevive/3742e5991d7ae0662b06c0335ea95b48 to your computer and use it in GitHub Desktop.
DependenciesUpdate sbt task
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
import com.timushev.sbt.updates.versions.{ValidVersion, Version} | |
import sbt._ | |
import scala.collection.immutable.SortedSet | |
object DependenciesUpdate { | |
def update( | |
dependencyUpdates: Map[ModuleID, SortedSet[Version]], | |
baseDirectory: File, | |
targetDirectory: File, | |
logger: Logger | |
): Unit = { | |
val updates = dependencyUpdates.flatMap { | |
case (module, versions) => | |
versions | |
.collect { | |
case ValidVersion(text, _, _, _) => text | |
} | |
.lastOption | |
.map(latestVersion => module -> latestVersion) | |
.toList | |
} | |
val file = baseDirectory / "project" / "Dependencies.scala" | |
val lines = IO.readLines(file) | |
val possibleUpdates = for ((module, v) <- updates) yield { | |
val regex = versionVariablePattern(module) | |
val reference = lines.flatMap { line => | |
val versionReference = regex.findFirstMatchIn(line).map(_.group(1)) | |
versionReference.toList | |
} | |
logger.info(s"Module $module. Reference => $reference") | |
(module, reference.headOption, v) | |
} | |
val updatedDependenciesFile = possibleUpdates.foldLeft(lines) { | |
case (acc, (_, Some(reference), newVersion)) => | |
val regex = s"""val\\s+$reference\\s{0,}=\\s{0,}"([\\w.-]*)"""".r | |
acc.map { line => | |
if (regex.pattern.matcher(line).find) { | |
val r = line.replaceFirst("\"([\\w.-]*)\"", s""""$newVersion"""") | |
logger.info(s"Replacement $r") | |
r | |
} else { | |
line | |
} | |
} | |
case (acc, (module, None, _)) => | |
logger.info(s"Versions reference wasn't found for module $module") | |
acc | |
} | |
val gitMessages = possibleUpdates.toSeq | |
.sortBy(_._1.organization) | |
.collect { | |
case (module, Some(_), newVersion) => | |
s"Updates module ${module.organization}:${module.name} from ${module.revision} to $newVersion" | |
} | |
IO.writeLines(file, updatedDependenciesFile) | |
if (gitMessages.nonEmpty) { | |
val commitMessageFile = targetDirectory / "update-dependencies-commit-message.txt" | |
IO.writeLines(commitMessageFile, gitMessages) | |
"git add project/Dependencies.scala".!! | |
s"git commit -F ${commitMessageFile.getAbsolutePath}".!! | |
"git push".!! | |
logger.info(gitMessages.mkString("\r\n")) | |
IO.delete(commitMessageFile) | |
} else { | |
logger.info("Nothing to update") | |
} | |
} | |
private def versionVariablePattern(module: ModuleID) = | |
s""""${escape(module.organization)}"\\s{0,}%{1,3}\\s{0,}"${escape(module.name)}"\\s{0,}%\\s{0,}Versions\\.([\\w]*)""".r | |
private def escape(input: String) = input.replace(".", "\\.") | |
} |
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
lazy val updateDependencies = { | |
val updateDependencies = TaskKey[Unit]("update-dependencies") | |
Seq( | |
updateDependencies := { | |
dependencyUpdates.value | |
DependenciesUpdate.update(dependencyUpdatesData.value, baseDirectory.value, target.value, streams.value.log) | |
} | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment