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 {
6- val snapshotScalaBinaryVersion = settingKey[String ](" The Scala binary version to use when building against Scala SNAPSHOT." )
77 val repoName = settingKey[String ](" The name of the repository under github.com/scala/." )
8+ val mimaPreviousVersion = settingKey[Option [String ]](" The version of this module to compare against when running MiMa." )
89
9- def deriveBinaryVersion (sv : String , snapshotScalaBinaryVersion : String ) = sv match {
10- case snap_211 if snap_211.startsWith(" 2.11" ) &&
11- snap_211.contains(" -SNAPSHOT" ) => snapshotScalaBinaryVersion
12- case sv => sbt.CrossVersion .binaryScalaVersion(sv)
13- }
10+ private val canRunMima = taskKey[Boolean ](" Decides if MiMa should run." )
11+ private val runMimaIfEnabled = taskKey[Unit ](" Run MiMa if mimaPreviousVersion and the module can be resolved against the current scalaBinaryVersion." )
1412
15- lazy val scalaModuleSettings = Seq (
13+ lazy val scalaModuleSettings : Seq [ Setting [_]] = Seq (
1614 repoName := name.value,
1715
18- organization := " org.scala-lang.modules " ,
16+ mimaPreviousVersion := None ,
1917
20- scalaBinaryVersion := deriveBinaryVersion(scalaVersion.value, snapshotScalaBinaryVersion.value) ,
18+ organization := " org.scala-lang.modules " ,
2119
2220 // so we don't have to wait for sonatype to synch to maven central when deploying a new module
2321 resolvers += Resolver .sonatypeRepo(" releases" ),
@@ -85,6 +83,57 @@ object ScalaModulePlugin extends Plugin {
8583 </developer >
8684 </developers >
8785 )
86+ ) ++ mimaSettings
87+
88+ // adapted from https://github.com/typesafehub/migration-manager/blob/0.1.6/sbtplugin/src/main/scala/com/typesafe/tools/mima/plugin/SbtMima.scala#L69
89+ def artifactExists (organization : String , name : String , scalaBinaryVersion : String , version : String , ivy : IvySbt , s : TaskStreams ): Boolean = {
90+ val moduleId = new ModuleID (organization, s " ${name}_ $scalaBinaryVersion" , version)
91+ val moduleSettings = InlineConfiguration (
92+ " dummy" % " test" % " version" ,
93+ ModuleInfo (" dummy-test-project-for-resolving" ),
94+ dependencies = Seq (moduleId))
95+ val ivyModule = new ivy.Module (moduleSettings)
96+ try {
97+ IvyActions .update(
98+ ivyModule,
99+ new UpdateConfiguration (
100+ retrieve = None ,
101+ missingOk = false ,
102+ logging = UpdateLogging .DownloadOnly ),
103+ s.log)
104+ true
105+ } catch {
106+ case _ : ResolveException => false
107+ }
108+ }
109+
110+ lazy val mimaSettings : Seq [Setting [_]] = MimaPlugin .mimaDefaultSettings ++ Seq (
111+ // manual cross-versioning because https://github.com/typesafehub/migration-manager/issues/62
112+ MimaKeys .previousArtifact := Some (organization.value % s " ${name.value}_ ${scalaBinaryVersion.value}" % mimaPreviousVersion.value.getOrElse(" dummy" )),
113+
114+ canRunMima := {
115+ val mimaVer = mimaPreviousVersion.value
116+ val s = streams.value
117+ if (mimaVer.isEmpty) {
118+ s.log.warn(" MiMa will NOT run because no mimaPreviousVersion is provided." )
119+ false
120+ } else if (! artifactExists(organization.value, name.value, scalaBinaryVersion.value, mimaVer.get, ivySbt.value, s)) {
121+ 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). """ )
122+ false
123+ } else {
124+ true
125+ }
126+ },
127+
128+ runMimaIfEnabled := Def .taskDyn({
129+ if (canRunMima.value) Def .task { MimaKeys .reportBinaryIssues.value }
130+ else Def .task { () }
131+ }).value,
132+
133+ test in Test := {
134+ runMimaIfEnabled.value
135+ (test in Test ).value
136+ }
88137 )
89138
90139 // a setting-transform to turn the regular version into something osgi can deal with
@@ -116,12 +165,3 @@ object ScalaModulePlugin extends Plugin {
116165// "org.scala-lang" % "scala-library" % scalaVersion.value,
117166// ("org.scala-lang" % "scala-compiler" % scalaVersion.value % "scala-tool").exclude("org.scala-lang.modules", s"scala-xml_${scalaBinaryVersion.value}")
118167// )
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