Skip to content

Commit ad0daf3

Browse files
authored
Merge pull request #137 from scala/add-sbt-version-policy
Add sbt-version-policy
2 parents 46b422d + efe29a8 commit ad0daf3

File tree

3 files changed

+25
-45
lines changed

3 files changed

+25
-45
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ The plugin also brings in
3030
- sbt-header to automate copyright header maintenance
3131
- sbt-mima-plugin to maintain binary compatibility
3232
- sbt-osgi, if enabled with `scalaModuleOsgiSettings`
33+
- sbt-version-policy to check the versioning policy
3334

3435
## Usage
3536

@@ -53,7 +54,7 @@ name := "<module name>"
5354
repoName := "<GitHub repo name>" // the repo under github.com/scala/, only required if different from name
5455
organization := "<org>" // only required if different from "org.scala-lang.modules"
5556
56-
scalaModuleMimaPreviousVersion := Some("1.0.0") // enables MiMa (`None` by default, which disables it)
57+
versionPolicyIntention := Compatibility.BinaryAndSourceCompatible // enables MiMa (`Compatibility.None` by default, which disables it)
5758
5859
OsgiKeys.exportPackage := Seq(s"<exported package>;version=${version.value}")
5960
@@ -77,7 +78,7 @@ The following settings are also available:
7778

7879
## Set up tag-based publishing
7980

80-
The instructions here are a summary of the readme in https://github.com/olafurpg/sbt-ci-release
81+
The instructions here are a summary of the readme in https://github.com/olafurpg/sbt-ci-release and https://github.com/scalacenter/sbt-version-policy
8182

8283
- Create a fresh GPG key: `gpg --gen-key`
8384
- Real name: use "project-name bot"
@@ -97,3 +98,5 @@ The instructions here are a summary of the readme in https://github.com/olafurpg
9798
- ubuntu: `gpg --armor --export-secret-keys $LONG_ID | base64 -w0`
9899
- `SONATYPE_PASSWORD`: need that one
99100
- `SONATYPE_USERNAME`: that one too
101+
102+
- Run `versionCheck` in the publishing process: `sbt versionCheck ci-release`

build.sbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ developers := List(Developer("", "", "", url("https://scala-lang.org"))
99

1010
addSbtPlugin("com.typesafe.sbt" % "sbt-osgi" % "0.9.6")
1111
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.6.0")
12-
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.9.2")
1312
addSbtPlugin("com.dwijnand" % "sbt-travisci" % "1.2.0") // set scalaVersion and crossScalaVersions
1413
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.7") // set version, scmInfo, publishing settings
14+
addSbtPlugin("ch.epfl.scala" % "sbt-version-policy" % "1.2.1")

src/main/scala/ScalaModulePlugin.scala

+19-42
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
package com.lightbend.tools.scalamoduleplugin
22

33
import com.typesafe.sbt.osgi.{OsgiKeys, SbtOsgi}
4-
import com.typesafe.tools.mima.plugin.MimaKeys._
5-
import com.typesafe.tools.mima.plugin.MimaPlugin
64
import de.heikoseeberger.sbtheader.HeaderPlugin.autoImport.{HeaderLicense, headerLicense}
75
import sbt.Keys._
8-
import sbt._
9-
import sbt.internal.librarymanagement.IvySbt
10-
import sbt.librarymanagement.ivy.IvyDependencyResolution
11-
import sbt.librarymanagement.{UnresolvedWarningConfiguration, UpdateConfiguration}
6+
import sbt.{Def, _}
127
import sbtdynver.DynVerPlugin
138
import sbtdynver.DynVerPlugin.autoImport.dynverGitDescribeOutput
149
import xerial.sbt.Sonatype.autoImport.{sonatypeProfileName, sonatypeSessionName}
10+
import sbtversionpolicy.SbtVersionPolicyPlugin.autoImport.{Compatibility, versionPolicyCheck, versionPolicyIgnoredInternalDependencyVersions, versionPolicyIntention}
1511

1612
object ScalaModulePlugin extends AutoPlugin {
1713
object autoImport {
1814
val scalaModuleRepoName = settingKey[String]("The name of the repository under github.com/scala/.")
1915
val scalaModuleAutomaticModuleName = settingKey[Option[String]]("Automatic-Module-Name setting for manifest")
16+
@deprecated("Previous version is now automatically computed by sbt-version-policy. Setting this key has no effect", "2.4.0")
2017
val scalaModuleMimaPreviousVersion = settingKey[Option[String]]("The version of this module to compare against when running MiMa.")
2118
val scalaModuleEnableOptimizerInlineFrom = settingKey[String]("The value passed to -opt-inline-from by `enableOptimizer` on 2.13 and higher.")
2219
}
@@ -36,7 +33,7 @@ object ScalaModulePlugin extends AutoPlugin {
3633
dv.copy(ref = sbtdynver.GitRef(dv.ref.value.split('#').head)))),
3734
)
3835

39-
// Settings added to the global scope
36+
// Settings added to the project scope
4037
override def projectSettings: Seq[Setting[_]] = Seq(
4138
// The staging profile is called `org.scala-lang`, the default is `org.scala-lang.modules`
4239
sonatypeProfileName := "org.scala-lang",
@@ -53,6 +50,12 @@ object ScalaModulePlugin extends AutoPlugin {
5350
},
5451
)
5552

53+
// Global settings
54+
override def globalSettings: Seq[Def.Setting[_]] = Seq(
55+
// Since we use sbt-dynver, see https://github.com/scalacenter/sbt-version-policy#how-to-integrate-with-sbt-dynver
56+
versionPolicyIgnoredInternalDependencyVersions := Some("^\\d+\\.\\d+\\.\\d+\\+\\d+".r)
57+
)
58+
5659
/**
5760
* Enable `-opt:l:inline`, `-opt:l:project` or `-optimize`, depending on the scala version.
5861
*/
@@ -167,47 +170,21 @@ object ScalaModulePlugin extends AutoPlugin {
167170
// a setting-transform to turn the regular version into something osgi can deal with
168171
private val osgiVersion = version(_.replace('-', '.'))
169172

170-
// adapted from https://github.com/lightbend/migration-manager/blob/0.3.0/sbtplugin/src/main/scala/com/typesafe/tools/mima/plugin/SbtMima.scala#L112
171-
private def artifactExists(organization: String, name: String, scalaBinaryVersion: String, version: String, ivy: IvySbt, s: TaskStreams): Boolean = {
172-
val moduleId = ModuleID(organization, s"${name}_$scalaBinaryVersion", version)
173-
val depRes = IvyDependencyResolution(ivy.configuration)
174-
val module = depRes.wrapDependencyInModule(moduleId)
175-
val updateConf = UpdateConfiguration() withLogging UpdateLogging.DownloadOnly
176-
val reportEither = depRes.update(module, updateConf, UnresolvedWarningConfiguration(), s.log)
177-
reportEither.fold(_ => false, _ => true)
178-
}
179-
180-
// Internal task keys for the MiMa settings
181-
private val canRunMima = taskKey[Boolean]("Decides if MiMa should run.")
182-
private val runMimaIfEnabled = taskKey[Unit]("Run MiMa if mimaPreviousVersion and the module can be resolved against the current scalaBinaryVersion.")
173+
// Internal task keys for the versionPolicy settings
174+
private val runVersionPolicyCheckIfEnabled = taskKey[Unit]("Run versionPolicyCheck if versionPolicyIntention is not set to Compatibility.None.")
183175

184176
private lazy val mimaSettings: Seq[Setting[_]] = Seq(
185-
scalaModuleMimaPreviousVersion := None,
186-
187-
// We're not using `%%` here in order to support both jvm and js projects (cross version `_2.12` / `_sjs0.6_2.12`)
188-
mimaPreviousArtifacts := scalaModuleMimaPreviousVersion.value.map(v => organization.value % moduleName.value % v cross crossVersion.value).toSet,
189-
190-
canRunMima := {
191-
val log = streams.value.log
192-
scalaModuleMimaPreviousVersion.value match {
193-
case None =>
194-
log.warn("MiMa will NOT run because no mimaPreviousVersion is provided.")
195-
false
196-
case Some(mimaVer) =>
197-
val exists = artifactExists(organization.value, name.value, scalaBinaryVersion.value, mimaVer, ivySbt.value, streams.value)
198-
if (!exists)
199-
log.warn(s"""MiMa will NOT run because the previous artifact "${organization.value}" % "${name.value}_${scalaBinaryVersion.value}" % "$mimaVer" could not be resolved (note the binary Scala version).""")
200-
exists
201-
}
202-
},
177+
versionPolicyIntention := Compatibility.None,
203178

204-
runMimaIfEnabled := Def.taskDyn({
205-
if (canRunMima.value) Def.task { mimaReportBinaryIssues.value }
206-
else Def.task { () }
179+
runVersionPolicyCheckIfEnabled := Def.taskDyn({
180+
if (versionPolicyIntention.value != Compatibility.None) Def.task { versionPolicyCheck.value }
181+
else Def.task {
182+
streams.value.log.warn("versionPolicyCheck will NOT run because versionPolicyIntention is set to Compatibility.None.")
183+
}
207184
}).value,
208185

209186
Test / test := {
210-
runMimaIfEnabled.value
187+
runVersionPolicyCheckIfEnabled.value
211188
(Test / test).value
212189
}
213190
)

0 commit comments

Comments
 (0)