Skip to content
This repository has been archived by the owner on Apr 22, 2020. It is now read-only.

Read the Scala cross versions from travis file for checking the isLastScalaVersion #709

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions core/src/main/scala/sbtorgpolicies/github/GitHubOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ import sbtorgpolicies.github.syntax._
import sbtorgpolicies.io.syntax._
import sbtorgpolicies.io.{FileReader, IO, IOResult}

class GitHubOps(owner: String, repo: String, accessToken: Option[String]) {
class GitHubOps(owner: String, repo: String, accessToken: Option[String], fileReader: FileReader = FileReader) {

val fileReader: FileReader = new FileReader

val gh = Github(accessToken)
val gh: Github = Github(accessToken)

def fetchContributors: Either[GitHubException, List[User]] = {

Expand Down
18 changes: 8 additions & 10 deletions core/src/main/scala/sbtorgpolicies/io/FileHelper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ import cats.syntax.traverseFilter._
import sbtorgpolicies.exceptions.IOException
import sbtorgpolicies.io.syntax._
import sbtorgpolicies.templates._
import FileReader._
import FileWriter._

class FileHelper {

val fileReader: FileReader = new FileReader

val fileWriter: FileWriter = new FileWriter

val templatesEngine: TemplatesEngine = new TemplatesEngine

def getPluginUrl: IOResult[URL] =
Expand All @@ -49,8 +47,8 @@ class FileHelper {

for {
pluginUrl <- getPluginUrl
_ <- fileWriter.copyJARResourcesTo(pluginUrl, templatesOutput, "templates")
_ <- fileWriter.copyFilesRecursively(userTemplatesDir.getAbsolutePath, s"${templatesOutput}templates")
_ <- copyJARResourcesTo(pluginUrl, templatesOutput, "templates")
_ <- copyFilesRecursively(userTemplatesDir.getAbsolutePath, s"${templatesOutput}templates")
} yield ()
}

Expand All @@ -64,16 +62,16 @@ class FileHelper {

def checkFiles(): IOResult[Unit] =
fileList.traverseU_ { f =>
if (!fileReader.exists(templatePath(f)))
if (!exists(templatePath(f)))
IOException(s"File not found: ${f.templatePath}").asLeft
else ().asRight
}

def prepareFileContent(file: FileType): IOResult[Option[String]] =
if (!fileReader.exists(file.outputPath) || file.overWritable) {
if (!exists(file.outputPath) || file.overWritable) {
templatesEngine.replaceFileContentsWith(templatePath(file), file.replacements) map (Option(_))
} else if (file.fileSections.nonEmpty) {
fileReader.getFileContent(file.outputPath) map (Option(_))
getFileContent(file.outputPath) map (Option(_))
} else Right(None)

def replaceSection(fileContent: String, fileSection: FileSection): IOResult[String] =
Expand All @@ -94,7 +92,7 @@ class FileHelper {

def writeToFileIfWritable(maybeContent: Option[String], fileType: FileType): IOResult[Option[FileType]] =
maybeContent map { c =>
fileWriter.writeContentToFile(c, outputPath(fileType)) map (_ => Option(fileType))
writeContentToFile(c, outputPath(fileType)) map (_ => Option(fileType))
} getOrElse None.asRight

def processFile(fileType: FileType): IOResult[Option[FileType]] =
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/sbtorgpolicies/io/FileReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,5 @@ class FileReader {
}
.leftMap(e => IOException(s"Error fetching files recursively", Some(e)))
}

object FileReader extends FileReader
2 changes: 2 additions & 0 deletions core/src/main/scala/sbtorgpolicies/io/FileWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,5 @@ class FileWriter {
}
.leftMap(e => IOException(s"Error copying resources from $jarUrl to directory $output", Some(e)))
}

object FileWriter extends FileWriter
2 changes: 1 addition & 1 deletion core/src/main/scala/sbtorgpolicies/libraries.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ object libraries {
"monocle" -> "1.4.0",
"mockito" -> "2.11.0",
"moultingyaml" -> "0.4.0",
"newrelic" -> "3.43.0",
"newrelic" -> "3.44.0",
"paradise" -> "2.1.0",
"pbdirect" -> "0.0.6",
"pcplod" -> "1.2.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ import cats.syntax.validated._
import sbtorgpolicies.exceptions.ValidationException
import sbtorgpolicies.io.FileReader

class FileValidation {

val fileReader: FileReader = new FileReader
class FileValidation(fileReader: FileReader = FileReader) {

def validateFile(inputPath: String, validations: ValidationFunction*): ValidationResult =
fileReader.getFileContent(inputPath) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import scala.util.matching.Regex

trait ValidationFunctions {

val yamlOps = new YamlOps

def requiredStrings(list: List[String]): ValidationFunction = {

def validateList(content: String, list: List[String])(
Expand Down Expand Up @@ -77,7 +75,7 @@ trait ValidationFunctions {

def validateCrossScalaVersions(content: String): ValidationResult = {

val travisCrossScalaVersion: List[String] = yamlOps.getFields(content, "scala").toList.sorted
val travisCrossScalaVersion: List[String] = YamlOps.getFields(content, "scala").toList.sorted
if (travisCrossScalaVersion == crossScalaVersions.sorted) ().valid
else
ValidationException(
Expand All @@ -86,7 +84,7 @@ trait ValidationFunctions {
}

def validateTasks(content: String, section: String, expectedTasks: Seq[String]): ValidationResult = {
val tasksInTravisFile: List[String] = yamlOps.getFields(content, section).toList
val tasksInTravisFile: List[String] = YamlOps.getFields(content, section).toList

if (expectedTasks.forall(expectedTsk => tasksInTravisFile.exists(_.contains(expectedTsk))))
().valid
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/scala/sbtorgpolicies/rules/YamlOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ class YamlOps {
} yield yamlValues

}

object YamlOps extends YamlOps
8 changes: 4 additions & 4 deletions core/src/main/scala/sbtorgpolicies/rules/syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ object syntax {

final class YamlResultOps(result: YamlResult[Seq[YamlValue]]) {

def toList: List[String] = result match {
case Right(yamlValues) => yamlValues.flatMap(_.convertTo[List[String]]).toList
case _ => Nil
}
def toList: List[String] = mapToString.getOrElse(Nil)

def mapToString: YamlResult[List[String]] =
result.map(_.flatMap(_.convertTo[List[String]]).toList)

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ import sbtorgpolicies.templates.utils._

import scala.util.matching.Regex

class TemplatesEngine {

val fileReader: FileReader = new FileReader

val fileWriter: FileWriter = new FileWriter
class TemplatesEngine(fileReader: FileReader = FileReader, fileWriter: FileWriter = FileWriter) {

def replaceFileContentsWith(inputPath: String, replacements: Replacements): IOResult[String] =
fileReader.withFileContent(inputPath, replaceWith(_, replacements))
Expand Down
5 changes: 2 additions & 3 deletions core/src/test/scala/sbtorgpolicies/github/GitHubOpsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ class GitHubOpsTest extends TestOps {
override lazy val pullRequests: GHPullRequests = ghPullRequests
}

val gitHubOps = new GitHubOps(owner, repo, None) {
override val fileReader: FileReader = fileReaderMock
override val gh: Github = githubMock
val gitHubOps = new GitHubOps(owner, repo, None, fileReaderMock) {
override val gh: Github = githubMock
}
(gitHubOps, fileReaderMock, ghGitData, ghPullRequests, ghRepos, ghUsers)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ class FileValidationTest extends TestOps with OneInstancePerTest {

val mockFileReader: FileReader = mock[FileReader]

val fileValidation: FileValidation = new FileValidation {

override val fileReader: FileReader = mockFileReader
}
val fileValidation: FileValidation = new FileValidation(mockFileReader)

test("FileValidation.validateFile works as expected") {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@ class TemplatesEngineTest extends TestOps {
val mockFileReader: FileReader = stub[FileReader]
val mockFileWriter: FileWriter = stub[FileWriter]

val templatesEngine: TemplatesEngine = new TemplatesEngine {

override val fileReader: FileReader = mockFileReader

override val fileWriter: FileWriter = mockFileWriter
}
val templatesEngine: TemplatesEngine = new TemplatesEngine(mockFileReader, mockFileWriter)

(templatesEngine, mockFileReader, mockFileWriter)
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/sbtorgpolicies/settings/DefaultSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@ trait DefaultSettings extends AllSettings {
getEnvVarOrElse("TRAVIS_PULL_REQUEST") == "false"
},
orgAfterCISuccessTaskListSetting := List(
orgUpdateDocFiles.asRunnableItem,
depUpdateDependencyIssues.asRunnableItem,
orgPublishReleaseTask.asRunnableItem(allModules = true, aggregated = false, crossScalaVersions = true)
orgPublishReleaseTask.asRunnableItem(allModules = true, aggregated = false, crossScalaVersions = true),
orgUpdateDocFiles.asRunnableItem
) ++ guard(((baseDirectory in LocalRootProject).value / "docs").exists() && !version.value.endsWith("-SNAPSHOT"))(
defaultPublishMicrosite),
orgScriptTaskListSetting := List(
Expand Down
69 changes: 60 additions & 9 deletions src/main/scala/sbtorgpolicies/settings/bash.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@

package sbtorgpolicies.settings

import com.typesafe.sbt.pgp.PgpKeys._
import cats.syntax.either._
import sbt.Keys._
import sbt._
import sbt.complete.DefaultParsers.OptNotSpace
import sbtorgpolicies.OrgPoliciesKeys._
import sbtorgpolicies.io.FileReader
import sbtorgpolicies.model.Dev
import sbtorgpolicies.rules.YamlOps
import sbtorgpolicies.rules.syntax._
import sbtorgpolicies.runnable._
import sbtorgpolicies.templates._
import sbtorgpolicies.utils._
import sbtrelease.ReleaseStateTransformations.reapply

Expand All @@ -32,12 +36,14 @@ trait bash extends bashCompat {
val st: State = deferredFetchContributorsState(inputState)
val extracted = Project.extract(st)

val buildV = extracted.get(version in ThisBuild)
val scalaV = extracted.get(scalaVersion)
val crossV = extracted.get(crossScalaVersions)
val orgBranch = extracted.get(orgCommitBranchSetting)
val buildV = extracted.get(version in ThisBuild)
val scalaV = extracted.get(scalaVersion)
val crossBuild = extracted.get(crossScalaVersions).toList
val orgBranch = extracted.get(orgCommitBranchSetting)
val baseDir = extracted.get(baseDirectory in LocalRootProject)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, niche catch, I'll rename this to rootDir and use the rootDir here


val isLastScalaV = crossV.lastOption.exists(_ == scalaV)
val crossV = readCrossScalaFromYaml(baseDir, crossBuild, st.log)
val isLastScalaV = isLastScalaVersion(scalaV, crossV, st.log)
val isSnapshotV = buildV.endsWith("-SNAPSHOT")

st.log.info(s"""orgPublishRelease Command Initiated
Expand All @@ -55,6 +61,7 @@ trait bash extends bashCompat {
val ref = extracted.get(thisProjectRef)

extracted.runAggregated[Unit](publish in Global in ref, st)
st
case (false, true) =>
st.log.info("Release Version detected, starting the release process...")

Expand Down Expand Up @@ -94,6 +101,50 @@ trait bash extends bashCompat {
}
}

private[this] def isLastScalaVersion(scalaV: String, crossV: List[String], logger: Logger): Boolean = {
crossV.sorted.lastOption match {
case None =>
logger.warn("crossScalaVersions is empty")
false
case Some(v) if v < scalaV =>
logger.warn(
s"Current Scala version ($scalaV) is greater than the major Scala version supported in crossScalaVersions ($crossV)")
false
case Some(v) if v == scalaV => true
case Some(v) => false
}
}

private[this] def readCrossScalaFromYaml(baseDir: File, defaultCrossV: List[String], logger: Logger): List[String] = {

import FileReader._
import YamlOps._

def verifyVersionsConsistency(yamlVersions: List[String]): List[String] = {
val notInConfig = yamlVersions.filterNot(defaultCrossV.contains)
val notInYaml = defaultCrossV.filterNot(yamlVersions.contains)

if (notInConfig.nonEmpty) {
logger.warn(
s"The Scala versions $notInConfig are defined in $travisFilePath but not in your project configuration")
}

if (notInYaml.nonEmpty) {
logger.warn(
s"The Scala versions $notInYaml are defined in your project configuration but not in $travisFilePath")
}
yamlVersions
}

getFileContent((baseDir / travisFilePath).getAbsolutePath) flatMap { content =>
getFields(content, "scala").mapToString map (_.sorted) map verifyVersionsConsistency
} valueOr { e =>
logger.warn(s"Can't read crossScalaVersion from yaml file $travisFilePath")
logger.trace(e)
defaultCrossV
}
}

private[this] def runTaskListCommand(
commandName: String,
runnableItemListSettingKey: SettingKey[List[RunnableItemConfigScope[_]]],
Expand All @@ -103,11 +154,11 @@ trait bash extends bashCompat {
val extracted = Project.extract(st)

val scalaV = extracted.get(scalaVersion)
val crossV = extracted.get(crossScalaVersions)
val baseDir = extracted.get(baseDirectory)
val rootDir = extracted.get(baseDirectory in LocalRootProject)

val isLastScalaV = crossV.lastOption.exists(_ == scalaV)
val crossV = readCrossScalaFromYaml(baseDir, extracted.get(crossScalaVersions).toList, st.log)
val isLastScalaV = isLastScalaVersion(scalaV, crossV, st.log)
val isRootModule = baseDir.getAbsolutePath == rootDir.getAbsolutePath

val runnableItemList: List[RunnableItemConfigScope[_]] = extracted.get(runnableItemListSettingKey)
Expand Down Expand Up @@ -178,7 +229,7 @@ trait bash extends bashCompat {
isRootModule: Boolean,
runnableItemList: List[RunnableItemConfigScope[_]],
filteredRunnableItemList: List[RunnableItemConfigScope[_]],
st: State) = {
st: State): Unit = {

val nonRunnableItems: Set[RunnableItemConfigScope[_]] = runnableItemList.toSet -- filteredRunnableItemList.toSet

Expand Down
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "0.8.11-SNAPSHOT"
version in ThisBuild := "0.8.11"