|
| 1 | +import ConditionalKeys._ |
| 2 | +import explicitdeps.ExplicitDepsPlugin.autoImport._ |
| 3 | +import sbt.Keys._ |
| 4 | +import sbt.ScriptedPlugin.autoImport.scripted |
| 5 | +import sbt.plugins.SbtPlugin |
| 6 | +import sbt.{Def, _} |
| 7 | + |
| 8 | +// This is all the crazy hacks to get cross compiling working with an sub-project that is an sbt plugin. |
| 9 | +object SbtPluginSubProjectPlugin extends AutoPlugin { |
| 10 | + |
| 11 | + override def trigger: PluginTrigger = allRequirements |
| 12 | + override def requires: Plugins = SbtPlugin |
| 13 | + |
| 14 | + object autoImport { |
| 15 | + val spspIsSbtCompatibleScalaVersion = settingKey[Boolean]("Checks if the current Scala version is 2.13") |
| 16 | + } |
| 17 | + |
| 18 | + import autoImport._ |
| 19 | + |
| 20 | + override def projectSettings: Seq[Def.Setting[_]] = |
| 21 | + List( |
| 22 | + crossScalaVersions := Nil, |
| 23 | + // Remove all library dependencies for Scala 2.13 as they will not resolve when cross building. |
| 24 | + libraryDependencies := settingDefaultIfSetting(libraryDependencies, scalaVersion, isScala213, Nil).value, |
| 25 | + // Remove all project dependencies for Scala 2.13 as they will not resolve when cross building. |
| 26 | + projectDependencies := taskDefaultIfSkipped(projectDependencies, Nil).value, |
| 27 | + scripted := inputDefaultIfSkipped(scripted, ()).evaluated, |
| 28 | + spspIsSbtCompatibleScalaVersion := isSbtCompatibleScalaVersionSetting.value, |
| 29 | + // We can't skip this as it has to run at least once or sbt complains. |
| 30 | + update / skip := false, |
| 31 | + // Skip everything else otherwise it will just fail. |
| 32 | + skip := !spspIsSbtCompatibleScalaVersion.value, |
| 33 | + undeclaredCompileDependenciesFilter -= moduleFilter() |
| 34 | + ) |
| 35 | + |
| 36 | + private def isSbtCompatibleScalaVersionSetting = Def.setting { |
| 37 | + if (isScala213(scalaVersion.value)) |
| 38 | + throw new IllegalStateException("sbt project must not use Scala 2.13. Did you force the version with '+'?") |
| 39 | + val versions = |
| 40 | + scalaVersion.all(ScopeFilter(inDependencies(ThisProject, transitive = true, includeRoot = false))).value |
| 41 | + !versions.exists(isScala213) |
| 42 | + } |
| 43 | + |
| 44 | + private def isScala213(version: String) = |
| 45 | + CrossVersion.partialVersion(version) match { |
| 46 | + case Some((2, n)) if n == 13 => true |
| 47 | + case _ => false |
| 48 | + } |
| 49 | +} |
0 commit comments