|
| 1 | +// It would be nice to use sbt-mima-plugin here, but the plugin is missing |
| 2 | +// at least two features we need: |
| 3 | +// * ability to run MiMa twice, swapping `curr` and `prev`, to detect |
| 4 | +// both forwards and backwards incompatibilities (possibly fixed as of |
| 5 | +// https://github.com/typesafehub/migration-manager/commit/2844ffa48b6d2255aa64bd687703aec21dadd55e) |
| 6 | +// * ability to pass a filter file (https://github.com/typesafehub/migration-manager/issues/102) |
| 7 | +// So we invoke the MiMa CLI directly; it's also what the Ant build did. |
| 8 | + |
| 9 | +import sbt._ |
| 10 | +import sbt.Keys._ |
| 11 | +import BuildSettings.autoImport._ |
| 12 | + |
| 13 | +object MiMa { |
| 14 | + lazy val mima = |
| 15 | + taskKey[Unit]("run Migration Manager to detect binary incompatibilities") |
| 16 | + |
| 17 | + lazy val settings = |
| 18 | + Seq( |
| 19 | + mima := { |
| 20 | + val log = streams.value.log |
| 21 | + mimaReferenceVersion.value.fold { |
| 22 | + log.info(s"No reference version defined - skipping binary compatibility checks") |
| 23 | + } { refVersion => |
| 24 | + def runOnce(prev: java.io.File, curr: java.io.File, isForward: Boolean): Unit = { |
| 25 | + val direction = if (isForward) "forward" else "backward" |
| 26 | + log.info(s"Checking $direction binary compatibility") |
| 27 | + log.debug(s"prev = $prev, curr = $curr") |
| 28 | + runMima( |
| 29 | + prev = if (isForward) curr else prev, |
| 30 | + curr = if (isForward) prev else curr, |
| 31 | + // TODO: it would be nicer if each subproject had its own whitelist, but for now |
| 32 | + // for compatibility with how Ant did things, there's just one at the root. |
| 33 | + // once Ant is gone we'd be free to split it up. |
| 34 | + filter = (baseDirectory in ThisBuild).value / s"bincompat-$direction.whitelist.conf", |
| 35 | + log) |
| 36 | + } |
| 37 | + val artifact = |
| 38 | + getPreviousArtifact( |
| 39 | + "org.scala-lang" % s"${name.value}" % refVersion, |
| 40 | + ivySbt.value, streams.value) |
| 41 | + for (isForward <- Seq(false, true)) |
| 42 | + runOnce(artifact, (packageBin in Compile).value, isForward) |
| 43 | + } |
| 44 | + } |
| 45 | + ) |
| 46 | + |
| 47 | + def runMima(prev: java.io.File, curr: java.io.File, filter: java.io.File, log: Logger): Unit = { |
| 48 | + val args = Array( |
| 49 | + "--prev", prev.getAbsolutePath, |
| 50 | + "--curr", curr.getAbsolutePath, |
| 51 | + "--filters", filter.getAbsolutePath, |
| 52 | + "--generate-filters" |
| 53 | + ) |
| 54 | + val exitCode = TrapExit(com.typesafe.tools.mima.cli.Main.main(args), log) |
| 55 | + if (exitCode != 0) |
| 56 | + throw new RuntimeException(s"MiMa failed with exit code $exitCode") |
| 57 | + } |
| 58 | + |
| 59 | + // cribbed from https://github.com/typesafehub/migration-manager/blob/master/sbtplugin/src/main/scala/com/typesafe/tools/mima/plugin/SbtMima.scala |
| 60 | + def getPreviousArtifact(m: ModuleID, ivy: IvySbt, s: TaskStreams): File = { |
| 61 | + val moduleSettings = InlineConfiguration( |
| 62 | + "dummy" % "test" % "version", |
| 63 | + ModuleInfo("dummy-test-project-for-resolving"), |
| 64 | + dependencies = Seq(m)) |
| 65 | + val module = new ivy.Module(moduleSettings) |
| 66 | + val report = Deprecated.Inner.ivyUpdate(ivy)(module, s) |
| 67 | + val optFile = (for { |
| 68 | + config <- report.configurations |
| 69 | + module <- config.modules |
| 70 | + (artifact, file) <- module.artifacts |
| 71 | + // TODO - Hardcode this? |
| 72 | + if artifact.name == m.name |
| 73 | + } yield file).headOption |
| 74 | + optFile getOrElse sys.error("Could not resolve previous artifact: " + m) |
| 75 | + } |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | +// use the SI-7934 workaround to silence a deprecation warning on an sbt API |
| 80 | +// we have no choice but to call. on the lack of any suitable alternative, |
| 81 | +// see https://gitter.im/sbt/sbt-dev?at=5616e2681b0e279854bd74a4 : |
| 82 | +// "it's my intention to eventually come up with a public API" says Eugene Y |
| 83 | +object Deprecated { |
| 84 | + @deprecated("", "") class Inner { |
| 85 | + def ivyUpdate(ivy: IvySbt)(module: ivy.Module, s: TaskStreams) = |
| 86 | + IvyActions.update( |
| 87 | + module, |
| 88 | + new UpdateConfiguration( |
| 89 | + retrieve = None, |
| 90 | + missingOk = false, |
| 91 | + logging = UpdateLogging.DownloadOnly), |
| 92 | + s.log) |
| 93 | + } |
| 94 | + object Inner extends Inner |
| 95 | +} |
0 commit comments