Skip to content

Commit 74d8b0b

Browse files
authored
Merge pull request #19 from lrytz/crossVersion2
Convert to AutoPlugin, apply Scala version settings to the build
2 parents 1704ecd + 38b1813 commit 74d8b0b

File tree

3 files changed

+80
-35
lines changed

3 files changed

+80
-35
lines changed

README.md

+20-6
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,19 @@ addSbtPlugin("org.scala-lang.modules" % "scala-module-plugin" % "1.0.6")
1313
Then, in your `build.sbt` add:
1414

1515
```
16-
scalaModuleSettings
16+
import ScalaModulePlugin._
17+
18+
scalaModuleSettings // in a multi-project build, you might want to apply these settings only to the
19+
// main project (example: scala-parallel-collections)
1720
1821
name := "<module name>"
1922
repoName := "<GitHub repo name>" // the repo under github.com/scala/, only required if different from name
2023
organization := "<org>" // only required if different from "org.scala-lang.modules"
2124
version := "<module version>"
2225
23-
// The plugin uses `scalaVersionsByJvm` to set `crossScalaVersions` according to the JVM major version.
24-
// The `scalaVersion` is set to `crossScalaVersions.value.head`.
25-
scalaVersionsByJvm := {
26+
// The plugin uses `scalaVersionsByJvm` to set `crossScalaVersions in ThisBuild` according to the JVM major version.
27+
// The `scalaVersion in ThisBuild` is set to `crossScalaVersions.value.head`.
28+
scalaVersionsByJvm in ThisBuild := {
2629
val v211 = "2.11.11"
2730
val v212 = "2.12.2"
2831
val v213 = "2.13.0-M1"
@@ -32,8 +35,7 @@ scalaVersionsByJvm := {
3235
6 -> List(v211 -> true),
3336
7 -> List(v211 -> false),
3437
8 -> List(v212 -> true, v213 -> true, v211 -> false),
35-
9 -> List(v212, v213, v211).map(_ -> false)
36-
)
38+
9 -> List(v212, v213, v211).map(_ -> false))
3739
}
3840
3941
mimaPreviousVersion := Some("1.0.3") // enables MiMa (`None` by default, which disables it)
@@ -43,6 +45,18 @@ OsgiKeys.exportPackage := Seq(s"<exported package>;version=${version.value}")
4345
// Other settings
4446
```
4547

48+
These additional settings are enabled by `scalaModuleSettings`:
49+
- `scalacOptions in (Compile, compile) ++= Seq("-feature", "-deprecation", "-unchecked", "-Xlint")`
50+
- A `projectName.properties` file is generated and packaged
51+
- `fork in Test := true` to work around some classpath clashes with scala-xml
52+
- `publishTo` sonatype, credentials file expected in `~/.ivy2/.credentials`
53+
- POM and OSGi metadata
54+
55+
The following settings are also available:
56+
- `enableOptimizer` adds `-opt:l:classpath` or `-optimize` to `scalacOptions in (Compile, compile)`,
57+
depending on the Scala version
58+
- `disablePublishing` is useful for multi-project builds for projects that should not be published
59+
4660
## Cutting a new release
4761

4862
- Sign in to Bintray (https://bintray.com/login) or create an "Open Source" account (https://bintray.com/signup/oss)

project/build.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=0.13.13
1+
sbt.version=0.13.15

src/main/scala/ScalaModulePlugin.scala

+59-28
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,85 @@ import Keys._
33
import com.typesafe.sbt.osgi.{OsgiKeys, SbtOsgi}
44
import com.typesafe.tools.mima.plugin.{MimaPlugin, MimaKeys}, MimaKeys._
55

6-
object ScalaModulePlugin extends Plugin {
7-
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.")
9-
val scalaVersionsByJvm = settingKey[Map[Int, List[(String, Boolean)]]]("For a Java major version (6, 8, 9), a list of Scala version and a flag indicating whether to use this combination for publishing.")
6+
object ScalaModulePlugin extends AutoPlugin {
7+
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.")
9+
val scalaVersionsByJvm = settingKey[Map[Int, List[(String, Boolean)]]]("For a Java major version (6, 8, 9), a list of a Scala version and a flag indicating whether to use this combination for publishing.")
1010

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.")
11+
// Settings applied to the entire build when the plugin is loaded.
1312

14-
lazy val scalaModuleSettings: Seq[Setting[_]] = Seq(
15-
repoName := name.value,
16-
17-
mimaPreviousVersion := None,
13+
override def trigger = allRequirements
1814

19-
scalaVersionsByJvm := Map.empty,
15+
override def buildSettings: Seq[Setting[_]] = Seq(
16+
scalaVersionsByJvm := Map.empty,
2017

21-
crossScalaVersions := {
18+
crossScalaVersions in ThisBuild := {
2219
val OneDot = """1\.(\d).*""".r // 1.6, 1.8
2320
val Maj = """(\d+).*""".r // 9
2421
val javaVersion = System.getProperty("java.version") match {
2522
case OneDot(n) => n.toInt
2623
case Maj(n) => n.toInt
2724
case v => throw new RuntimeException(s"Unknown Java version: $v")
2825
}
29-
val isTravisPublishing = Option(System.getenv("TRAVIS_TAG")).getOrElse("").trim.nonEmpty
30-
val scalaVersions = scalaVersionsByJvm.value.getOrElse(javaVersion, Nil) collect {
26+
27+
val isTravis = Option(System.getenv("TRAVIS")).exists(_ == "true") // `contains` doesn't exist in Scala 2.10
28+
val isTravisPublishing = Option(System.getenv("TRAVIS_TAG")).exists(_.trim.nonEmpty)
29+
30+
val byJvm = scalaVersionsByJvm.value
31+
if (byJvm.isEmpty)
32+
throw new RuntimeException(s"Make sure to define `scalaVersionsByJvm in ThisBuild` in `build.sbt` in the root project, using the `ThisBuild` scope.")
33+
34+
val scalaVersions = byJvm.getOrElse(javaVersion, Nil) collect {
3135
case (v, publish) if !isTravisPublishing || publish => v
3236
}
3337
if (scalaVersions.isEmpty) {
34-
if (isTravisPublishing) {
38+
if (isTravis) {
3539
sLog.value.warn(s"No Scala version in `scalaVersionsByJvm` in build.sbt needs to be released on Java major version $javaVersion.")
3640
// Exit successfully, don't fail the (travis) build. This happens for example if `openjdk7`
3741
// is part of the travis configuration for testing, but it's not used for releasing against
3842
// any Scala version.
3943
System.exit(0)
4044
} else
41-
throw new RuntimeException(s"No Scala version for Java major version $javaVersion. Adjust `scalaVersionsByJvm` in build.sbt.")
45+
throw new RuntimeException(s"No Scala version for Java major version $javaVersion. Change your Java version or adjust `scalaVersionsByJvm` in build.sbt.")
4246
}
4347
scalaVersions
4448
},
4549

46-
scalaVersion := crossScalaVersions.value.head,
50+
scalaVersion in ThisBuild := crossScalaVersions.value.head
51+
)
52+
53+
/**
54+
* Enable `-opt:l:classpath` or `-optimize`, depending on the scala version.
55+
*/
56+
lazy val enableOptimizer: Setting[_] = scalacOptions in (Compile, compile) += {
57+
val ScalaMaj = """2\.(\d+)\..*""".r
58+
val ScalaMaj(scalaMaj) = scalaVersion.value
59+
if (scalaMaj.toInt >= 12) "-opt:l:classpath" else "-optimize"
60+
}
61+
62+
/**
63+
* Practical for multi-project builds.
64+
*/
65+
lazy val disablePublishing: Seq[Setting[_]] = Seq(
66+
publishArtifact := false,
67+
// The above is enough for Maven repos but it doesn't prevent publishing of ivy.xml files
68+
publish := {},
69+
publishLocal := {},
70+
publishTo := Some(Resolver.file("devnull", file("/dev/null")))
71+
)
72+
73+
/**
74+
* To be included in the main sbt project of a Scala module.
75+
*/
76+
lazy val scalaModuleSettings: Seq[Setting[_]] = Seq(
77+
repoName := name.value,
4778

48-
organization := "org.scala-lang.modules",
79+
mimaPreviousVersion := None,
4980

50-
// so we don't have to wait for sonatype to synch to maven central when deploying a new module
51-
resolvers += Resolver.sonatypeRepo("releases"),
81+
organization := "org.scala-lang.modules",
5282

5383
// don't use for doc scope, scaladoc warnings are not to be reckoned with
54-
// TODO: turn on for nightlies, but don't enable for PR validation... "-Xfatal-warnings"
55-
scalacOptions in compile ++= Seq("-optimize", "-feature", "-deprecation", "-unchecked", "-Xlint"),
84+
scalacOptions in (Compile, compile) ++= Seq("-feature", "-deprecation", "-unchecked", "-Xlint"),
5685

5786
// Generate $name.properties to store our version as well as the scala version used to build
5887
resourceGenerators in Compile += Def.task {
@@ -76,8 +105,6 @@ object ScalaModulePlugin extends Plugin {
76105
// alternatively, manage the scala instance as shown at the end of this file (commented)
77106
fork in Test := true,
78107

79-
publishArtifact in Test := false,
80-
81108
// maven publishing
82109
publishTo := Some(
83110
if (version.value.trim.endsWith("SNAPSHOT")) Resolver.sonatypeRepo("snapshots")
@@ -114,7 +141,7 @@ object ScalaModulePlugin extends Plugin {
114141
) ++ mimaSettings ++ scalaModuleOsgiSettings
115142

116143
// adapted from https://github.com/typesafehub/migration-manager/blob/0.1.6/sbtplugin/src/main/scala/com/typesafe/tools/mima/plugin/SbtMima.scala#L69
117-
def artifactExists(organization: String, name: String, scalaBinaryVersion: String, version: String, ivy: IvySbt, s: TaskStreams): Boolean = {
144+
private def artifactExists(organization: String, name: String, scalaBinaryVersion: String, version: String, ivy: IvySbt, s: TaskStreams): Boolean = {
118145
val moduleId = new ModuleID(organization, s"${name}_$scalaBinaryVersion", version)
119146
val moduleSettings = InlineConfiguration(
120147
"dummy" % "test" % "version",
@@ -135,7 +162,11 @@ object ScalaModulePlugin extends Plugin {
135162
}
136163
}
137164

138-
lazy val mimaSettings: Seq[Setting[_]] = MimaPlugin.mimaDefaultSettings ++ Seq(
165+
// Internal task keys for the MiMa settings
166+
private val canRunMima = taskKey[Boolean]("Decides if MiMa should run.")
167+
private val runMimaIfEnabled = taskKey[Unit]("Run MiMa if mimaPreviousVersion and the module can be resolved against the current scalaBinaryVersion.")
168+
169+
private lazy val mimaSettings: Seq[Setting[_]] = MimaPlugin.mimaDefaultSettings ++ Seq(
139170
// manual cross-versioning because https://github.com/typesafehub/migration-manager/issues/62
140171
mimaPreviousArtifacts := Set(organization.value % s"${name.value}_${scalaBinaryVersion.value}" % mimaPreviousVersion.value.getOrElse("dummy")),
141172

@@ -165,9 +196,9 @@ object ScalaModulePlugin extends Plugin {
165196
)
166197

167198
// a setting-transform to turn the regular version into something osgi can deal with
168-
val osgiVersion = version(_.replace('-', '.'))
199+
private val osgiVersion = version(_.replace('-', '.'))
169200

170-
lazy val scalaModuleOsgiSettings = SbtOsgi.osgiSettings ++ Seq(
201+
private lazy val scalaModuleOsgiSettings = SbtOsgi.osgiSettings ++ Seq(
171202
OsgiKeys.bundleSymbolicName := s"${organization.value}.${name.value}",
172203
OsgiKeys.bundleVersion := osgiVersion.value,
173204

0 commit comments

Comments
 (0)