1
1
import sbt ._
2
2
import Keys ._
3
3
import com .typesafe .sbt .osgi .{OsgiKeys , SbtOsgi }
4
+ import com .typesafe .tools .mima .plugin .{MimaPlugin , MimaKeys }
4
5
5
6
object ScalaModulePlugin extends Plugin {
6
7
val snapshotScalaBinaryVersion = settingKey[String ](" The Scala binary version to use when building against Scala SNAPSHOT." )
7
8
val repoName = settingKey[String ](" The name of the repository under github.com/scala/." )
9
+ val mimaPreviousVersion = settingKey[Option [String ]](" The version of this module to compare against when running MiMa." )
10
+
11
+ private val canRunMima = taskKey[Boolean ](" Decides if MiMa should run." )
12
+ private val runMimaIfEnabled = taskKey[Unit ](" Run MiMa if mimaPreviousVersion and the module can be resolved against the current scalaBinaryVersion." )
8
13
9
14
def deriveBinaryVersion (sv : String , snapshotScalaBinaryVersion : String ) = sv match {
10
15
case snap_211 if snap_211.startsWith(" 2.11" ) &&
11
16
snap_211.contains(" -SNAPSHOT" ) => snapshotScalaBinaryVersion
12
17
case sv => sbt.CrossVersion .binaryScalaVersion(sv)
13
18
}
14
19
15
- lazy val scalaModuleSettings = Seq (
20
+ lazy val scalaModuleSettings : Seq [ Setting [_]] = Seq (
16
21
repoName := name.value,
17
22
23
+ mimaPreviousVersion := None ,
24
+
18
25
organization := " org.scala-lang.modules" ,
19
26
20
27
scalaBinaryVersion := deriveBinaryVersion(scalaVersion.value, snapshotScalaBinaryVersion.value),
@@ -85,6 +92,57 @@ object ScalaModulePlugin extends Plugin {
85
92
</developer >
86
93
</developers >
87
94
)
95
+ ) ++ mimaSettings
96
+
97
+ // adapted from https://github.com/typesafehub/migration-manager/blob/0.1.6/sbtplugin/src/main/scala/com/typesafe/tools/mima/plugin/SbtMima.scala#L69
98
+ def artifactExists (organization : String , name : String , scalaBinaryVersion : String , version : String , ivy : IvySbt , s : TaskStreams ): Boolean = {
99
+ val moduleId = new ModuleID (organization, s " ${name}_ $scalaBinaryVersion" , version)
100
+ val moduleSettings = InlineConfiguration (
101
+ " dummy" % " test" % " version" ,
102
+ ModuleInfo (" dummy-test-project-for-resolving" ),
103
+ dependencies = Seq (moduleId))
104
+ val ivyModule = new ivy.Module (moduleSettings)
105
+ try {
106
+ IvyActions .update(
107
+ ivyModule,
108
+ new UpdateConfiguration (
109
+ retrieve = None ,
110
+ missingOk = false ,
111
+ logging = UpdateLogging .DownloadOnly ),
112
+ s.log)
113
+ true
114
+ } catch {
115
+ case _ : ResolveException => false
116
+ }
117
+ }
118
+
119
+ lazy val mimaSettings : Seq [Setting [_]] = MimaPlugin .mimaDefaultSettings ++ Seq (
120
+ // manual cross-versioning because https://github.com/typesafehub/migration-manager/issues/62
121
+ MimaKeys .previousArtifact := Some (organization.value % s " ${name.value}_ ${scalaBinaryVersion.value}" % mimaPreviousVersion.value.getOrElse(" dummy" )),
122
+
123
+ canRunMima := {
124
+ val mimaVer = mimaPreviousVersion.value
125
+ val s = streams.value
126
+ if (mimaVer.isEmpty) {
127
+ s.log.warn(" MiMa will NOT run because no mimaPreviousVersion is provided." )
128
+ false
129
+ } else if (! artifactExists(organization.value, name.value, scalaBinaryVersion.value, mimaVer.get, ivySbt.value, s)) {
130
+ s.log.warn(s """ MiMa will NOT run because the previous artifact " ${organization.value}" % " ${name.value}_ ${scalaBinaryVersion.value}" % " ${mimaVer.get}" could not be resolved (note the binary Scala version). """ )
131
+ false
132
+ } else {
133
+ true
134
+ }
135
+ },
136
+
137
+ runMimaIfEnabled := Def .taskDyn({
138
+ if (canRunMima.value) Def .task { MimaKeys .reportBinaryIssues.value }
139
+ else Def .task { () }
140
+ }).value,
141
+
142
+ test in Test := {
143
+ runMimaIfEnabled.value
144
+ (test in Test ).value
145
+ }
88
146
)
89
147
90
148
// a setting-transform to turn the regular version into something osgi can deal with
@@ -116,12 +174,3 @@ object ScalaModulePlugin extends Plugin {
116
174
// "org.scala-lang" % "scala-library" % scalaVersion.value,
117
175
// ("org.scala-lang" % "scala-compiler" % scalaVersion.value % "scala-tool").exclude("org.scala-lang.modules", s"scala-xml_${scalaBinaryVersion.value}")
118
176
// )
119
-
120
-
121
- /* Mima blurb:
122
- addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.1.6")
123
-
124
- import com.typesafe.tools.mima.plugin.{MimaPlugin, MimaKeys}
125
- MimaPlugin.mimaDefaultSettings
126
- MimaKeys.previousArtifact := Some(... % ... % ...)
127
- */
0 commit comments