Skip to content

Commit

Permalink
Merge pull request #41 from adrian-wang/source
Browse files Browse the repository at this point in the history
Read sources files from sbt instead of scalaSource
  • Loading branch information
matthewfarwell committed Oct 15, 2015
2 parents 3c49946 + 7790bfa commit 255e554
Showing 1 changed file with 10 additions and 7 deletions.
17 changes: 10 additions & 7 deletions src/main/scala/org/scalastyle/sbt/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,13 @@ object ScalastylePlugin extends Plugin {
val scalastyleFailOnError = settingKey[Boolean]("If true, Scalastyle will fail the task when an error level rule is violated")
val scalastyleConfigRefreshHours = settingKey[Integer]("How many hours until next run will fetch the scalastyle-config.xml again if location is a URI.")
val scalastyleConfigUrlCacheFile = settingKey[String]("If scalastyleConfigUrl is set, it will be cached here")
val scalastyleSources = settingKey[Seq[File]]("Which sources will scalastyle check")

def rawScalastyleSettings(): Seq[sbt.Def.Setting[_]] =
Seq(
scalastyle := {
val args: Seq[String] = spaceDelimited("<arg>").parsed
val scalaSourceV = scalaSource.value
val scalastyleSourcesV = scalastyleSources.value
val configV = scalastyleConfig.value
val configUrlV = scalastyleConfigUrl.value
val streamsV = streams.value
Expand All @@ -85,7 +86,7 @@ object ScalastylePlugin extends Plugin {
val targetV = target.value
val configCacheFileV = scalastyleConfigUrlCacheFile.value

Tasks.doScalastyle(args, configV, configUrlV, failOnErrorV, scalaSourceV, scalastyleTargetV, streamsV, configRefreshHoursV, targetV, configCacheFileV)
Tasks.doScalastyle(args, configV, configUrlV, failOnErrorV, scalastyleSourcesV, scalastyleTargetV, streamsV, configRefreshHoursV, targetV, configCacheFileV)
},
scalastyleGenerateConfig := {
val streamsValue = streams.value
Expand All @@ -107,14 +108,16 @@ object ScalastylePlugin extends Plugin {
scalastyleTarget := (target.value / "scalastyle-result.xml"),
(scalastyleTarget in Test) := target.value / "scalastyle-test-result.xml",
scalastyleFailOnError := true,
(scalastyleFailOnError in Test) := (scalastyleFailOnError in scalastyle).value
(scalastyleFailOnError in Test) := (scalastyleFailOnError in scalastyle).value,
scalastyleSources := Seq((scalaSource in Compile).value),
(scalastyleSources in Test) := Seq((scalaSource in Test).value)
) ++
Project.inConfig(Compile)(rawScalastyleSettings()) ++
Project.inConfig(Test)(rawScalastyleSettings())
}

object Tasks {
def doScalastyle(args: Seq[String], config: File, configUrl: Option[URL], failOnError: Boolean, scalaSource: File, scalastyleTarget: File,
def doScalastyle(args: Seq[String], config: File, configUrl: Option[URL], failOnError: Boolean, scalastyleSources: Seq[File], scalastyleTarget: File,
streams: TaskStreams[ScopedKey[_]], refreshHours: Integer, target: File, urlCacheFile: String): Unit = {
val logger = streams.log

Expand Down Expand Up @@ -156,7 +159,7 @@ object Tasks {
val messageConfig = ConfigFactory.load(new ScalastyleChecker().getClass().getClassLoader())
//streams.log.error("messageConfig=" + messageConfig.root().render())

val messages = runScalastyle(config, scalaSource)
val messages = runScalastyle(config, scalastyleSources)

saveToXml(messageConfig, messages, scalastyleTarget.absolutePath)

Expand Down Expand Up @@ -185,9 +188,9 @@ object Tasks {
getFileFromJar(getClass.getResource("/scalastyle-config.xml"), config.absolutePath, streams.log)
}

private[this] def runScalastyle(config: File, sourceDir: File) = {
private[this] def runScalastyle(config: File, scalastyleSources: Seq[File]) = {
val configuration = ScalastyleConfiguration.readFromXml(config.absolutePath)
new ScalastyleChecker().checkFiles(configuration, Directory.getFiles(None, List(sourceDir)))
new ScalastyleChecker().checkFiles(configuration, Directory.getFiles(None, scalastyleSources.toList))
}

private[this] def printResults(config: Config, logger: Logger, messages: List[Message[FileSpec]], quiet: Boolean = false, warnError: Boolean = false): OutputResult = {
Expand Down

0 comments on commit 255e554

Please sign in to comment.