Skip to content

Commit

Permalink
Repository settings are passed directly from the UI to the Downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
maris123 committed Nov 3, 2017
1 parent ede345c commit 2939ed9
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ object HydraVersions {
scalaVersionsPerModule.values.toSeq.distinct
}

def downloadHydraVersions: Array[String] = (Versions.loadHydraVersions ++ HydraApplicationSettings.getInstance().getDownloadedHydraVersions)
def downloadHydraVersions(repoURL: String, login: String, password: String): Array[String] = (Versions.loadHydraVersions(repoURL, login, password) ++ HydraApplicationSettings.getInstance().getDownloadedHydraVersions)
.distinct
.sortWith(Version(_) >= Version(_))
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.jetbrains.plugins.hydra.caches
import java.io.File

import com.intellij.openapi.diagnostic.Logger
import org.jetbrains.plugins.hydra.compiler.HydraRepositorySettings
import org.jetbrains.plugins.hydra.settings.HydraApplicationSettings
import org.jetbrains.plugins.scala.project.template.{Downloader, FileExt}

Expand All @@ -20,12 +21,12 @@ object HydraArtifactsCache {
artifacts.forall(new File(_).exists())
}

def downloadIfNotPresent(scalaVersion: String, hydraVersion: String, listener: String => Unit): Unit = {
def downloadIfNotPresent(repositorySettings: HydraRepositorySettings, scalaVersion: String, hydraVersion: String, listener: String => Unit): Unit = {
val artifacts = hydraGlobalSettings.artifactPaths.get((scalaVersion, hydraVersion))

if (artifacts.isEmpty) {
val sbtBufferedOutput = new StringBuilder
Downloader.downloadHydra(s"${scalaVersion}_$hydraVersion", (text: String) => {
Downloader.downloadHydra(repositorySettings, s"${scalaVersion}_$hydraVersion", (text: String) => {
sbtBufferedOutput.append(text)
listener(text)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@ object HydraCredentialsManager {
new String(encode(s"${getLogin}:${getPlainPassword}"))
}

def getBasicAuthEncoding(login: String, password: String): String = {
new String(encode(s"$login:$password"))
}

private def encode(text: String) = Base64.getEncoder.encodeToString(text.getBytes(StandardCharsets.UTF_8))
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@ class ScalaHydraCompilerConfigurationPanel(project: Project, settings: HydraComp
hydraGlobalSettings.getHydraRepositoryUrl != getHydraRepository ||
hydraGlobalSettings.hydraRepositoryRealm != getHydraRepositoryRealm)) {

HydraCredentialsManager.setCredentials(getUsername, getPassword)
hydraGlobalSettings.setHydraRepositopryUrl(getHydraRepository)
hydraVersionComboBox.setItems(HydraVersions.downloadHydraVersions)
hydraVersionComboBox.setItems(HydraVersions.downloadHydraVersions(getHydraRepository, getUsername, getPassword))
hydraVersionComboBox.setSelectedItem(settings.hydraVersion)
}
}

hydraGlobalSettings.getState
setDefaultHydraVersion

hydraRepository.setText(hydraGlobalSettings.getHydraRepositoryUrl)
hydraRepository.addFocusListener(focusListener)
Expand Down Expand Up @@ -74,6 +71,7 @@ class ScalaHydraCompilerConfigurationPanel(project: Project, settings: HydraComp
}
})

setDefaultHydraVersion

def selectedVersion: String = hydraVersionComboBox.getSelectedItem.toString

Expand All @@ -99,6 +97,11 @@ class ScalaHydraCompilerConfigurationPanel(project: Project, settings: HydraComp

def setHydraRepositoryRealm(realm: String): Unit = realmTextField.setText(realm)

def getHydraRepositoryName: String = Try(new URL(getHydraRepository)) match {
case Success(url) => url.getHost
case _ => ""
}

def onDownload(): Unit = {
Try(new URL(hydraGlobalSettings.getHydraRepositoryUrl)) match {
case Success(_) => downloadHydraForProjectScalaVersions()
Expand Down Expand Up @@ -136,11 +139,15 @@ class ScalaHydraCompilerConfigurationPanel(project: Project, settings: HydraComp
}

private def downloadVersion(scalaVersions: Seq[String], hydraVersion: String): (String => Unit) => Unit =
(listener: (String) => Unit) => scalaVersions.foreach(version => HydraArtifactsCache.downloadIfNotPresent(version, hydraVersion, listener))
(listener: (String) => Unit) => scalaVersions.foreach(version =>
HydraArtifactsCache.downloadIfNotPresent(HydraRepositorySettings(getHydraRepositoryName, getHydraRepository,
getHydraRepositoryRealm, getUsername, getPassword), version, hydraVersion, listener))

private def setDefaultHydraVersion: Unit = {
val hydraVersions = HydraVersions.downloadHydraVersions
val hydraVersions = HydraVersions.downloadHydraVersions(getHydraRepository, getUsername, getPassword)
hydraVersionComboBox.setItems(hydraVersions)
setSelectedVersion(hydraVersions.head)
}
}

sealed case class HydraRepositorySettings(repositoryName: String, repositoryURL: String, repositoryRealm: String, login: String, password: String)
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ class HydraApplicationSettings extends PersistentStateComponent[HydraApplication
for { (scalaVersion, _) <- artifactPaths.keySet.toArray } yield scalaVersion
}

def getHydraRepositoryName: String = Try(new URL(hydraRepositoryUrl)) match {
case Success(url) => url.getHost
case _ => ""
}

def setHydraRepositopryUrl(url: String): Unit = hydraRepositoryUrl = url

def getHydraRepositoryUrl: String = hydraRepositoryUrl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import java.net.HttpURLConnection

import com.intellij.util.net.HttpConfigurable
import org.jetbrains.plugins.hydra.compiler.HydraCredentialsManager
import org.jetbrains.plugins.hydra.settings.HydraApplicationSettings
import org.jetbrains.plugins.scala.buildinfo.BuildInfo
import org.jetbrains.plugins.scala.project.Platform.{Dotty, Scala}

Expand All @@ -27,35 +26,39 @@ object Versions {
case Dotty => loadVersionsOf(Entity.Dotty)
}

def loadHydraVersions: Array[String] = loadVersionsOf(Entity.Hydra)

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 = entity match {
case Entity.Hydra => loadVersionsForHydra()
case _ => loadVersionsFrom(entity.url, {
case entity.pattern(number) => number
})
}
val loaded = loadVersionsFrom(entity.url, {
case entity.pattern(number) => number
})

loaded
.getOrElse(entity.hardcodedVersions)
.map(Version(_))
.filter(_ >= entity.minVersion)
filterVersionsForEntity(loaded.getOrElse(entity.hardcodedVersions), entity)
}
allVersions
.sortWith(_ >= _)
.map(_.presentation)
.toArray
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
}

private def loadVersionsFrom(url: String, filter: PartialFunction[String, String]): Try[Seq[String]] = {
loadLinesFrom(url).map { lines => lines.collect(filter) }
}

private def loadHydraVersionsFrom(url: String, filter: PartialFunction[String, String]): Try[Seq[String]] = {
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) }
}
Expand All @@ -71,16 +74,15 @@ object Versions {
}
}

private def loadVersionsForHydra() = {
private def loadVersionsForHydra(repoURL: String, login: String, password: String) = {
val entity = Entity.Hydra
val repoUrl = HydraApplicationSettings.getInstance().getHydraRepositoryUrl
val entityUrl = if (repoUrl.endsWith("/")) repoUrl + entity.url else repoUrl + "/" + entity.url
val entityUrl = if (repoURL.endsWith("/")) repoURL + entity.url else repoURL + "/" + entity.url

def downloadHydraVersions(url: String): Seq[String] =
loadHydraVersionsFrom(url, { case entity.pattern(number) => number }).getOrElse(entity.hardcodedVersions).map(Version(_))
loadHydraVersionsFrom(url, login, password, { case entity.pattern(number) => number }).getOrElse(entity.hardcodedVersions).map(Version(_))
.filter(_ >= entity.minVersion).map(_.presentation)

loadHydraVersionsFrom(entityUrl, {
loadHydraVersionsFrom(entityUrl, login, password, {
case entity.pattern(number) => number
}).map { versions =>
versions.flatMap(version => downloadHydraVersions(s"""$entityUrl$version/""")).distinct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import java.io.{File, FileNotFoundException}

import com.intellij.execution.process.{OSProcessHandler, ProcessAdapter, ProcessEvent}
import com.intellij.openapi.util.Key
import org.jetbrains.plugins.hydra.compiler.HydraCredentialsManager
import org.jetbrains.plugins.hydra.settings.HydraApplicationSettings
import org.jetbrains.plugins.hydra.compiler.HydraRepositorySettings
import org.jetbrains.plugins.scala.project.Platform

/**
Expand All @@ -16,8 +15,8 @@ object Downloader {
createTempSbtProject(platform, version, listener, sbtCommandsFor)
}

def downloadHydra(version: String, listener: String => Unit): Unit = {
createTempSbtProject(Platform.Scala, version, listener, sbtCommandsForHydra)
def downloadHydra(repositorySettings: HydraRepositorySettings, version: String, listener: String => Unit): Unit = {
createTempSbtProject(Platform.Scala, version, listener, sbtCommandsForHydra(repositorySettings))
}

private def createTempSbtProject(platform: Platform, version: String, listener: String => Unit, sbtCommands: (Platform, String) => Seq[String]): Unit = {
Expand Down Expand Up @@ -72,14 +71,11 @@ object Downloader {
"updateClassifiers")
}

private def sbtCommandsForHydra(platform: Platform, version: String) = {
val settings = HydraApplicationSettings.getInstance()
val repositoryName = settings.getHydraRepositoryName
val repositoryUrl = settings.getHydraRepositoryUrl
private def sbtCommandsForHydra(repositorySettings: HydraRepositorySettings)(platform: Platform, version: String) = {
Seq(
s"""set scalaVersion := "${version.split("_")(0)}"""",
s"""set credentials := Seq(Credentials("${settings.hydraRepositoryRealm}", "${repositoryName}", "${HydraCredentialsManager.getLogin}", "${HydraCredentialsManager.getPlainPassword}"))""",
s"""set resolvers := Seq(Resolver.url("Triplequote Plugins Ivy Releases", url("${repositoryUrl}/ivy-releases/"))(Resolver.ivyStylePatterns), Resolver.url("Triplequote sbt-plugin-relseases", url("${repositoryUrl}/sbt-plugins-release/"))(Resolver.ivyStylePatterns), "Triplequote Plugins Releases" at "${repositoryUrl}/libs-release-local/")""",
s"""set credentials := Seq(Credentials("${repositorySettings.repositoryRealm}", "${repositorySettings.repositoryName}", "${repositorySettings.login}", "${repositorySettings.password}"))""",
s"""set resolvers := Seq(Resolver.url("Triplequote Plugins Ivy Releases", url("${repositorySettings.repositoryURL}/ivy-releases/"))(Resolver.ivyStylePatterns), Resolver.url("Triplequote sbt-plugin-relseases", url("${repositorySettings.repositoryURL}/sbt-plugins-release/"))(Resolver.ivyStylePatterns), "Triplequote Plugins Releases" at "${repositorySettings.repositoryURL}/libs-release-local/")""",
s"""set libraryDependencies := Seq("com.triplequote" % "hydra_${version.split("_")(0)}" % "${version.split("_")(1)}", ("com.triplequote" % "hydra-bridge_1_0" % "${version.split("_")(1)}").sources())""",
"updateClassifiers",
"show dependencyClasspath")
Expand Down

0 comments on commit 2939ed9

Please sign in to comment.