Skip to content

Commit

Permalink
Separated Hydra versions logic from Versions object
Browse files Browse the repository at this point in the history
  • Loading branch information
maris123 committed Nov 8, 2017
1 parent ba4bc2d commit 3a5807b
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] = {
Expand Down Expand Up @@ -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
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand All @@ -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)
Expand All @@ -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
}
Expand All @@ -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")
)
}
}

0 comments on commit 3a5807b

Please sign in to comment.