-
Notifications
You must be signed in to change notification settings - Fork 31
/
Plugin.scala
149 lines (124 loc) · 5.64 KB
/
Plugin.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package aether
import sbt._
import Keys.{version, _}
import org.eclipse.aether.deployment.DeployRequest
import org.eclipse.aether.installation.InstallRequest
import org.eclipse.aether.repository.{Proxy, RemoteRepository}
import java.net.URI
import org.eclipse.aether.repository.RemoteRepository.Builder
import org.eclipse.aether.util.repository.AuthenticationBuilder
import internal._
object AetherKeys {
val aetherArtifact = taskKey[AetherArtifact]("Main artifact")
val aetherCoordinates = settingKey[MavenCoordinates]("Internal coordinates")
val aetherDeploy = TaskKey[Unit]("aether-deploy", "Deploys to a maven repository.")
val aetherInstall = TaskKey[Unit]("aether-install", "Installs to a local maven repository.")
val aetherPackageMain = taskKey[File]("package main Artifact")
val aetherLocalRepo = settingKey[File]("Local maven repository.")
val aetherOldVersionMethod = settingKey[Boolean]("Flag for using the old method of getting the version")
}
import AetherKeys._
object AetherPlugin extends AetherPlugin {
override def trigger = allRequirements
override def requires = sbt.plugins.IvyPlugin
override def projectSettings = aetherBaseSettings ++ Seq(
aetherArtifact := {
createArtifact((packagedArtifacts in Compile).value, aetherCoordinates.value, aetherPackageMain.value)
}
)
object autoImport {
def overridePublishSettings: Seq[Setting[_]] = Seq(publish := aetherDeploy.value)
def overridePublishLocalSettings: Seq[Setting[_]] = Seq(publishLocal := {
publishLocal.value
aetherInstall.value
})
def overridePublishBothSettings: Seq[Setting[_]] = overridePublishSettings ++ overridePublishLocalSettings
}
}
trait AetherPlugin extends AutoPlugin {
lazy val aetherBaseSettings: Seq[Setting[_]] = Seq(
//aetherWagons := Seq.empty,
aetherLocalRepo := Path.userHome / ".m2" / "repository",
defaultCoordinates,
deployTask,
installTask,
aetherPackageMain := {
(Keys.`package` in Compile).value
},
aetherOldVersionMethod := false,
logLevel in aetherDeploy := Level.Debug
)
def defaultCoordinates = aetherCoordinates := {
val art = artifact.value
val theVersion = if (aetherOldVersionMethod.value) version.value else (version in ThisBuild).value
val artifactId = if (!sbtPlugin.value) CrossVersion(crossVersion.value, scalaVersion.value, scalaBinaryVersion.value).map(_(art.name)) getOrElse art.name else art.name
val coords = MavenCoordinates(organization.value, artifactId, theVersion, None, art.extension)
if (sbtPlugin.value) coords.withSbtVersion(sbtBinaryVersion.value).withScalaVersion(scalaBinaryVersion.value) else coords
}
lazy val deployTask = aetherDeploy := (Def.taskDyn{
if ((publishArtifact in Compile).value) {
Def.task {
deployIt(publishTo.value, aetherLocalRepo.value, aetherArtifact.value, sbtPlugin.value, credentials.value)(streams.value)
}
} else {
Def.task(())
}
}).value
lazy val installTask = aetherInstall := {
installIt(aetherArtifact.value, aetherLocalRepo.value, sbtPlugin.value)(streams.value)
}
def createArtifact(artifacts: Map[Artifact, sbt.File], coords: MavenCoordinates, mainArtifact: File): AetherArtifact = {
val subArtifacts = artifacts
.filterNot { case (a, f) => a.classifier.isEmpty && f == mainArtifact }
.map { case (a, f) => AetherSubArtifact(f, a.classifier, a.extension) }
.toSeq
val realCoords = coords.withExtension(mainArtifact)
AetherArtifact(mainArtifact, realCoords, subArtifacts)
}
private def toRepository(repo: MavenRepository, plugin: Boolean, credentials: Option[DirectCredentials]): RemoteRepository = {
val builder: Builder = new Builder(repo.name, if (plugin) "sbt-plugin" else "default", repo.root)
credentials.foreach{c => builder.setAuthentication(new AuthenticationBuilder().addUsername(c.userName).addPassword(c.passwd).build()) }
val proxy: Option[Proxy] = Option(SystemPropertyProxySelector.selector.getProxy(builder.build()))
proxy.foreach(p => builder.setProxy(p))
builder.build()
}
def deployIt(repo: Option[Resolver], localRepo: File, artifact: AetherArtifact, plugin: Boolean, cred: Seq[Credentials])(implicit strem: TaskStreams) {
val repository = repo.collect{
case x: MavenRepository => x
case x => sys.error("The configured repo MUST be a maven repo, but was: " + x)
}.getOrElse(sys.error("There MUST be a configured publish repo"))
val maybeCred = scala.util.control.Exception.allCatch.apply {
val href = URI.create(repository.root)
val c = Credentials.forHost(cred, href.getHost)
if (c.isEmpty && href.getHost != null) {
strem.log.warn("No credentials supplied for %s".format(href.getHost))
}
c
}
val request = new DeployRequest()
request.setRepository(toRepository(repository, plugin, maybeCred))
val parent = artifact.toArtifact
request.addArtifact(parent)
artifact.subartifacts.foreach(s => request.addArtifact(s.toArtifact(parent)))
try {
val (system, session) = Booter(localRepo, strem, plugin)
system.deploy(session, request)
}
catch {
case e: Exception => e.printStackTrace(); throw e
}
}
def installIt(artifact: AetherArtifact, localRepo: File, plugin: Boolean)(implicit streams: TaskStreams) {
val request = new InstallRequest()
val parent = artifact.toArtifact
request.addArtifact(parent)
artifact.subartifacts.foreach(s => request.addArtifact(s.toArtifact(parent)))
try {
val (system, session) = Booter(localRepo, streams, plugin)
system.install(session, request)
}
catch {
case e: Exception => e.printStackTrace(); throw e
}
}
}