-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSbtPluginSubProjectPlugin.scala
61 lines (53 loc) · 2.68 KB
/
SbtPluginSubProjectPlugin.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import ConditionalKeys._
import com.typesafe.tools.mima.plugin.MimaKeys.mimaPreviousArtifacts
import explicitdeps.ExplicitDepsPlugin
import explicitdeps.ExplicitDepsPlugin.autoImport._
import sbt.Keys._
import sbt.ScriptedPlugin.autoImport.scripted
import sbt._
import sbt.plugins.SbtPlugin
import sbtversionpolicy.SbtVersionPolicyMima
import sbtversionpolicy.SbtVersionPolicyPlugin.autoImport.versionPolicyDependencySchemes
// This is all the crazy hacks to get cross compiling working with an sub-project that is an sbt plugin.
object SbtPluginSubProjectPlugin extends AutoPlugin {
override def trigger: PluginTrigger = allRequirements
override def requires: Plugins = SbtPlugin && SbtVersionPolicyMima && ExplicitDepsPlugin
object autoImport {
val spspCanBuild = settingKey[Boolean]("Checks if the project dependencies are using a compatible Scala version.")
}
import autoImport._
override def projectSettings: Seq[Def.Setting[_]] =
List(
crossScalaVersions := Nil,
mimaPreviousArtifacts := defaultIfCannotBuild(mimaPreviousArtifacts, Set.empty[ModuleID]).value,
// Remove all project dependencies for Scala 2.13 as they will not resolve when cross building.
projectDependencies := taskDefaultIfSkipped(projectDependencies, Nil).value,
scripted := inputDefaultIfSkipped(scripted, ()).evaluated,
spspCanBuild := canBuildSetting.value,
// We can't skip this as it has to run at least once or sbt complains.
update / skip := false,
// Skip everything else otherwise it will just fail.
skip := !spspCanBuild.value,
undeclaredCompileDependenciesFilter -= moduleFilter(),
// These transitive dependencies that have been "vendored" by sbt appear as though they are
// pre-release as they contain build metadata.
// Trust that sbt correctly manages its own version policy in accordance with its dependencies.
versionPolicyDependencySchemes += "org.scala-sbt.jline" % "*" % "always"
)
def defaultIfCannotBuild[A](setting: Def.Initialize[A], default: => A): Def.Initialize[A] =
settingDefaultIfSetting(setting, spspCanBuild, default)(!_)
private def canBuildSetting = Def.setting {
if (!isScala212(scalaVersion.value))
throw new IllegalStateException(
"sbt project must use Scala 2.12. Check that you have not forced the version with '+'."
)
val versions =
scalaVersion.all(ScopeFilter(inDependencies(ThisProject, transitive = true, includeRoot = false))).value
versions.forall(isScala212)
}
private def isScala212(version: String) =
CrossVersion.partialVersion(version) match {
case Some((2, n)) if n == 12 => true
case _ => false
}
}