diff --git a/git/src/main/scala/com/thoughtworks/sbtBestPractice/git/Git.scala b/git/src/main/scala/com/thoughtworks/sbtBestPractice/git/Git.scala index 592bde8..614b6ba 100644 --- a/git/src/main/scala/com/thoughtworks/sbtBestPractice/git/Git.scala +++ b/git/src/main/scala/com/thoughtworks/sbtBestPractice/git/Git.scala @@ -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) { @@ -34,4 +34,8 @@ object Git extends AutoPlugin { gitDir := Option(gitRepositoryBuilder.value.getGitDir) ) + override def buildSettings = settings + + override def projectSettings = settings + } diff --git a/git/src/main/scala/com/thoughtworks/sbtBestPractice/git/GitDevelopers.scala b/git/src/main/scala/com/thoughtworks/sbtBestPractice/git/GitDevelopers.scala index 273dc2d..2a3bc3c 100644 --- a/git/src/main/scala/com/thoughtworks/sbtBestPractice/git/GitDevelopers.scala +++ b/git/src/main/scala/com/thoughtworks/sbtBestPractice/git/GitDevelopers.scala @@ -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() @@ -43,4 +43,8 @@ object GitDevelopers extends AutoPlugin { } ) + override def buildSettings = settings + + override def projectSettings = settings + } diff --git a/travis/src/main/scala/com/thoughtworks/sbtBestPractice/travis/DetectScalaVersionsFromTravisYml.scala b/travis/src/main/scala/com/thoughtworks/sbtBestPractice/travis/DetectScalaVersionsFromTravisYml.scala index a01f378..7704b7f 100644 --- a/travis/src/main/scala/com/thoughtworks/sbtBestPractice/travis/DetectScalaVersionsFromTravisYml.scala +++ b/travis/src/main/scala/com/thoughtworks/sbtBestPractice/travis/DetectScalaVersionsFromTravisYml.scala @@ -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 => _, _} @@ -15,7 +16,6 @@ import sbt.{Node => _, _} * @author 杨博 (Yang Bo) */ object DetectScalaVersionsFromTravisYml extends AutoPlugin { - override def requires = JvmPlugin && Git override def trigger = allRequirements @@ -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 @@ -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 @@ -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 => @@ -104,8 +131,10 @@ object DetectScalaVersionsFromTravisYml extends AutoPlugin { crossScalaVersions.value } } - } ) + override def projectSettings = crossScalaVersionsSetting + + override def buildSettings = Seq(crossScalaVersionsSetting, scalaVersionSetting) }