From 3a5807b1b134e0f546212f51d33b8aac583b83f1 Mon Sep 17 00:00:00 2001 From: Maris Date: Wed, 8 Nov 2017 16:56:56 +0200 Subject: [PATCH] Separated Hydra versions logic from Versions object --- .../plugins/hydra/HydraVersions.scala | 36 +++++++++++- .../plugins/scala/project/Versions.scala | 55 ++++--------------- 2 files changed, 45 insertions(+), 46 deletions(-) diff --git a/scala/scala-impl/src/org/jetbrains/plugins/hydra/HydraVersions.scala b/scala/scala-impl/src/org/jetbrains/plugins/hydra/HydraVersions.scala index c6732243e07..7bd7a7ebcaa 100644 --- a/scala/scala-impl/src/org/jetbrains/plugins/hydra/HydraVersions.scala +++ b/scala/scala-impl/src/org/jetbrains/plugins/hydra/HydraVersions.scala @@ -3,9 +3,11 @@ package org.jetbrains.plugins.hydra import com.intellij.openapi.diagnostic.Logger import org.jetbrains.plugins.scala.project.{ProjectExt, ScalaModule, Version, Versions} import com.intellij.openapi.project.Project +import org.jetbrains.plugins.hydra.compiler.HydraCredentialsManager import org.jetbrains.plugins.hydra.settings.HydraApplicationSettings import scala.collection.breakOut +import scala.util.Try /** * @author Maris Alexandru @@ -16,6 +18,11 @@ object HydraVersions { private val UnsupportedScalaVersion = "2.12.0" private val CompilerRegex = """.*scala-compiler-(\d+\.\d+\.\d+)(-SNAPSHOT)?\.jar""".r + private val Pattern = ".+>(.*\\d+\\.\\d+\\.\\d+.*)/<.*".r + private val HydraURL = "ivy-releases/com.triplequote/" + private val MinHydraVersion = Version("0.9.5") + private val HardcodedHydraVersions = Seq("0.9.5") + private final val Log: Logger = Logger.getInstance(this.getClass.getName) def getSupportedScalaVersions(project: Project): Seq[String] = { @@ -43,7 +50,34 @@ object HydraVersions { } def downloadHydraVersions(repoURL: String, login: String, password: String): Array[String] = - (Versions.loadHydraVersions(repoURL, login, password) ++ HydraApplicationSettings.getInstance().hydraVersions) + (loadHydraVersions(repoURL, login, password) ++ HydraApplicationSettings.getInstance().hydraVersions) .distinct .sortWith(Version(_) >= Version(_)) + + private def loadHydraVersions(repoURL: String, login: String, password: String): Array[String] = { + val loadedVersions = loadVersionsForHydra(repoURL, login, password) + val hydraVersions = loadedVersions.getOrElse(HardcodedHydraVersions) + .map(Version(_)) + .filter(_ >= MinHydraVersion) + hydraVersions.map(_.presentation).toArray + } + + private def loadHydraVersionsFrom(url: String, login:String, password: String, filter: PartialFunction[String, String]): Try[Seq[String]] = { + val loadedLines = Versions.loadLinesFrom(url) { connection => connection.setRequestProperty("Authorization", "Basic " + HydraCredentialsManager.getBasicAuthEncoding()) } + loadedLines.map { lines => lines.collect(filter) } + } + + private def loadVersionsForHydra(repoURL: String, login: String, password: String) = { + val entityUrl = if (repoURL.endsWith("/")) repoURL + HydraURL else repoURL + "/" + HydraURL + + def downloadHydraVersions(url: String): Seq[String] = + loadHydraVersionsFrom(url, login, password, { case Pattern(number) => number }).getOrElse(HardcodedHydraVersions).map(Version(_)) + .filter(_ >= MinHydraVersion).map(_.presentation) + + loadHydraVersionsFrom(entityUrl, login, password, { + case Pattern(number) => number + }).map { versions => + versions.flatMap(version => downloadHydraVersions(s"""$entityUrl$version/""")).distinct + } + } } diff --git a/scala/scala-impl/src/org/jetbrains/plugins/scala/project/Versions.scala b/scala/scala-impl/src/org/jetbrains/plugins/scala/project/Versions.scala index a4189b157d0..bbcd9a912bf 100644 --- a/scala/scala-impl/src/org/jetbrains/plugins/scala/project/Versions.scala +++ b/scala/scala-impl/src/org/jetbrains/plugins/scala/project/Versions.scala @@ -3,7 +3,6 @@ package org.jetbrains.plugins.scala.project import java.net.HttpURLConnection import com.intellij.util.net.HttpConfigurable -import org.jetbrains.plugins.hydra.compiler.HydraCredentialsManager import org.jetbrains.plugins.scala.buildinfo.BuildInfo import org.jetbrains.plugins.scala.project.Platform.{Dotty, Scala} @@ -28,42 +27,29 @@ object Versions { def loadSbtVersions: Array[String] = loadVersionsOf(Entity.Sbt013, Entity.Sbt1) - def loadHydraVersions(repoURL: String, login: String, password: String): Array[String] = { - val entity = Entity.Hydra - val loadedVersions = loadVersionsForHydra(repoURL, login, password) - val hydraVersions = filterVersionsForEntity(loadedVersions.getOrElse(entity.hardcodedVersions), entity) - sortVersions(hydraVersions) - } - private def loadVersionsOf(entities: Entity*): Array[String] = { val allVersions = entities.flatMap { entity => val loaded = loadVersionsFrom(entity.url, { case entity.pattern(number) => number }) - filterVersionsForEntity(loaded.getOrElse(entity.hardcodedVersions), entity) + loaded + .getOrElse(entity.hardcodedVersions) + .map(Version(_)) + .filter(_ >= entity.minVersion) } - sortVersions(allVersions) - } - private def filterVersionsForEntity(versions: Seq[String], entity: Entity) = { - versions.map(Version(_)).filter(_ >= entity.minVersion) - } - - private def sortVersions(versions: Seq[Version]) = { - versions.sortWith(_ >= _).map(_.presentation).toArray + allVersions + .sortWith(_ >= _) + .map(_.presentation) + .toArray } private def loadVersionsFrom(url: String, filter: PartialFunction[String, String]): Try[Seq[String]] = { - loadLinesFrom(url).map { lines => lines.collect(filter) } - } - - private def loadHydraVersionsFrom(url: String, login:String, password: String, filter: PartialFunction[String, String]): Try[Seq[String]] = { - val loadedLines = loadLinesFrom(url){ connection => connection.setRequestProperty("Authorization", "Basic " + HydraCredentialsManager.getBasicAuthEncoding()) } - loadedLines.map { lines => lines.collect(filter) } + loadLinesFrom(url)().map { lines => lines.collect(filter) } } - private def loadLinesFrom(url: String)(implicit prepareConnection: HttpURLConnection => Unit = _ => ()): Try[Seq[String]] = { + def loadLinesFrom(url: String)(prepareConnection: HttpURLConnection => Unit = _ => ()): Try[Seq[String]] = { Try(HttpConfigurable.getInstance().openHttpConnection(url)).map { connection => try { prepareConnection(connection) @@ -74,21 +60,6 @@ object Versions { } } - private def loadVersionsForHydra(repoURL: String, login: String, password: String) = { - val entity = Entity.Hydra - val entityUrl = if (repoURL.endsWith("/")) repoURL + entity.url else repoURL + "/" + entity.url - - def downloadHydraVersions(url: String): Seq[String] = - loadHydraVersionsFrom(url, login, password, { case entity.pattern(number) => number }).getOrElse(entity.hardcodedVersions).map(Version(_)) - .filter(_ >= entity.minVersion).map(_.presentation) - - loadHydraVersionsFrom(entityUrl, login, password, { - case entity.pattern(number) => number - }).map { versions => - versions.flatMap(version => downloadHydraVersions(s"""$entityUrl$version/""")).distinct - } - } - private case class Entity(url: String, pattern: Regex, minVersion: Version, hardcodedVersions: Seq[String]) { def defaultVersion: String = hardcodedVersions.last } @@ -113,11 +84,5 @@ object Versions { """.+>(\d+.\d+.+)/<.*""".r, Version("0.2.0"), Seq("0.2.0-RC1")) - - val Hydra = Entity("ivy-releases/com.triplequote/", - ".+>(.*\\d+\\.\\d+\\.\\d+.*)/<.*".r, - Version("0.9.5"), - Seq("0.9.5") - ) } } \ No newline at end of file