11import sbt ._
22import Keys ._
33import com .typesafe .sbt .osgi .{OsgiKeys , SbtOsgi }
4+ import com .typesafe .tools .mima .plugin .{MimaPlugin , MimaKeys }
45
56object ScalaModulePlugin extends Plugin {
67 val snapshotScalaBinaryVersion = settingKey[String ](" The Scala binary version to use when building against Scala SNAPSHOT." )
78 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." )
813
914 def deriveBinaryVersion (sv : String , snapshotScalaBinaryVersion : String ) = sv match {
1015 case snap_211 if snap_211.startsWith(" 2.11" ) &&
1116 snap_211.contains(" -SNAPSHOT" ) => snapshotScalaBinaryVersion
1217 case sv => sbt.CrossVersion .binaryScalaVersion(sv)
1318 }
1419
15- lazy val scalaModuleSettings = Seq (
20+ lazy val scalaModuleSettings : Seq [ Setting [_]] = Seq (
1621 repoName := name.value,
1722
23+ mimaPreviousVersion := None ,
24+
1825 organization := " org.scala-lang.modules" ,
1926
2027 scalaBinaryVersion := deriveBinaryVersion(scalaVersion.value, snapshotScalaBinaryVersion.value),
@@ -85,6 +92,57 @@ object ScalaModulePlugin extends Plugin {
8592 </developer >
8693 </developers >
8794 )
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+ }
88146 )
89147
90148 // a setting-transform to turn the regular version into something osgi can deal with
@@ -116,12 +174,3 @@ object ScalaModulePlugin extends Plugin {
116174// "org.scala-lang" % "scala-library" % scalaVersion.value,
117175// ("org.scala-lang" % "scala-compiler" % scalaVersion.value % "scala-tool").exclude("org.scala-lang.modules", s"scala-xml_${scalaBinaryVersion.value}")
118176// )
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