Skip to content

Commit

Permalink
Setup scalaVersion automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
Atry committed Dec 25, 2019
1 parent a3fca67 commit c477a0e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object Git extends AutoPlugin {

val gitRepositoryBuilder = SettingKey[RepositoryBuilder]("git-repository-builder", "")

override def projectSettings = Seq(
private def settings = Seq(
gitRepositoryBuilder := {
val builder = (new RepositoryBuilder).findGitDir(baseDirectory.value)
if (builder.getWorkTree != null || builder.getGitDir != null) {
Expand All @@ -34,4 +34,8 @@ object Git extends AutoPlugin {
gitDir := Option(gitRepositoryBuilder.value.getGitDir)
)

override def buildSettings = settings

override def projectSettings = settings

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ object GitDevelopers extends AutoPlugin {

import Git._

override def projectSettings = Seq(
private def settings = Seq(
developers := {
if (gitDir.value.isDefined) {
val repository = gitRepositoryBuilder.value.build()
Expand Down Expand Up @@ -43,4 +43,8 @@ object GitDevelopers extends AutoPlugin {
}
)

override def buildSettings = settings

override def projectSettings = settings

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.nodes._

import scala.collection.JavaConverters._
import sbt.Keys.scalaVersion
import sbt.Keys.crossScalaVersions
import sbt.plugins.JvmPlugin
import sbt.{Node => _, _}
Expand All @@ -15,7 +16,6 @@ import sbt.{Node => _, _}
* @author 杨博 (Yang Bo)
*/
object DetectScalaVersionsFromTravisYml extends AutoPlugin {

override def requires = JvmPlugin && Git
override def trigger = allRequirements

Expand All @@ -42,7 +42,8 @@ object DetectScalaVersionsFromTravisYml extends AutoPlugin {
!Return(valueNode.getValue)
case _ =>
throw new MessageOnlyException(
"The value of `matrix/include/scala` field in .travis.yml should be a scalar.")
"The value of `matrix/include/scala` field in .travis.yml should be a scalar."
)
}
case _ =>
!Continue
Expand All @@ -69,11 +70,13 @@ object DetectScalaVersionsFromTravisYml extends AutoPlugin {
!Return(valueNode.getValue)
case _ =>
throw new MessageOnlyException(
"The value of `scala` field in .travis.yml should be a scalar or a sequence of scalars.")
"The value of `scala` field in .travis.yml should be a scalar or a sequence of scalars."
)
}
case _ =>
throw new MessageOnlyException(
"The value of `scala` field in .travis.yml should be a scalar or a sequence of scalars.")
"The value of `scala` field in .travis.yml should be a scalar or a sequence of scalars."
)
}
case _ =>
!Continue
Expand All @@ -83,7 +86,31 @@ object DetectScalaVersionsFromTravisYml extends AutoPlugin {
}
}

override def projectSettings = Seq(
private def scalaVersionSetting = {
scalaVersion := {
Git.gitWorkTree.value match {
case None =>
scalaVersion.value
case Some(gitWorkTree) =>
val travisYmlPath = (gitWorkTree / ".travis.yml").toPath
if (Files.exists(travisYmlPath)) {
val versions = extractScalaVersions {
val reader = Files.newBufferedReader(travisYmlPath, scala.io.Codec.UTF8.charSet)
try {
new Yaml().compose(reader)
} finally {
reader.close()
}
}
versions.headOption.getOrElse(scalaVersion.value)
} else {
scalaVersion.value
}
}
}
}

private def crossScalaVersionsSetting = (
crossScalaVersions := {
Git.gitWorkTree.value match {
case None =>
Expand All @@ -104,8 +131,10 @@ object DetectScalaVersionsFromTravisYml extends AutoPlugin {
crossScalaVersions.value
}
}

}
)

override def projectSettings = crossScalaVersionsSetting

override def buildSettings = Seq(crossScalaVersionsSetting, scalaVersionSetting)
}

0 comments on commit c477a0e

Please sign in to comment.