Skip to content

ScalastyleConfiguration without XML #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
49 changes: 29 additions & 20 deletions src/main/scala/org/scalastyle/sbt/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ object ScalastylePlugin extends Plugin {
val scalastyleGenerateConfig = taskKey[Unit]("Generate a default configuration files for scalastyle")

val scalastyleTarget = settingKey[File]("XML output file from scalastyle")
val scalastyleConfig = settingKey[File]("Scalastyle configuration file")
val scalastyleConfig = settingKey[Option[ScalastyleConfiguration]]("Scalastyle configuration object")
val scalastyleConfigFile = settingKey[File]("Scalastyle configuration file")
val scalastyleConfigUrl = settingKey[Option[URL]]("Scalastyle configuration file as a URL")
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.")
Expand All @@ -78,6 +79,7 @@ object ScalastylePlugin extends Plugin {
val args: Seq[String] = spaceDelimited("<arg>").parsed
val scalastyleSourcesV = scalastyleSources.value
val configV = scalastyleConfig.value
val configFileV = scalastyleConfigFile.value
val configUrlV = scalastyleConfigUrl.value
val streamsV = streams.value
val failOnErrorV = scalastyleFailOnError.value
Expand All @@ -86,19 +88,21 @@ object ScalastylePlugin extends Plugin {
val targetV = target.value
val configCacheFileV = scalastyleConfigUrlCacheFile.value

Tasks.doScalastyle(args, configV, configUrlV, failOnErrorV, scalastyleSourcesV, scalastyleTargetV, streamsV, configRefreshHoursV, targetV, configCacheFileV)
Tasks.doScalastyle(args, configV, configFileV, configUrlV, failOnErrorV, scalastyleSourcesV, scalastyleTargetV, streamsV, configRefreshHoursV, targetV, configCacheFileV)
},
scalastyleGenerateConfig := {
val streamsValue = streams.value
val configValue = scalastyleConfig.value
Tasks.doGenerateConfig(configValue, streamsValue)
val configFileValue = scalastyleConfigFile.value
Tasks.doGenerateConfig(configFileValue, streamsValue)
}
)

override def projectSettings =
Seq(
scalastyleConfig := file("scalastyle-config.xml"),
(scalastyleConfig in Test) := (scalastyleConfig in scalastyle).value,
scalastyleConfig := None,
(scalastyleConfig in Test) := None,
scalastyleConfigFile := file("scalastyle-config.xml"),
(scalastyleConfigFile in Test) := (scalastyleConfigFile in scalastyle).value,
scalastyleConfigUrl := None,
(scalastyleConfigUrl in Test) := None,
scalastyleConfigUrlCacheFile := "scalastyle-config.xml",
Expand All @@ -117,7 +121,7 @@ object ScalastylePlugin extends Plugin {
}

object Tasks {
def doScalastyle(args: Seq[String], config: File, configUrl: Option[URL], failOnError: Boolean, scalastyleSources: Seq[File], scalastyleTarget: File,
def doScalastyle(args: Seq[String], config: Option[ScalastyleConfiguration], configFile: 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
val quietArg = "q"
Expand All @@ -135,7 +139,7 @@ object Tasks {
}
}

def getConfigFile(targetDirectory: File, configUrl: Option[URL], config: File, outputFile: String): File = {
def getConfigFile(targetDirectory: File, configUrl: Option[URL], configFile: File, outputFile: String): File = {
val f = configUrl match {
case Some(url) => {
val targetConfigFile = target / outputFile
Expand All @@ -149,7 +153,7 @@ object Tasks {
}
targetConfigFile
}
case None => config
case None => configFile
}

if (!quiet) {
Expand All @@ -165,7 +169,7 @@ object Tasks {
validFile
}

def doScalastyleWithConfig(config: File): Unit = {
def doScalastyleWithConfig(config: ScalastyleConfiguration): Unit = {
val messageConfig = ConfigFactory.load(new ScalastyleChecker().getClass().getClassLoader())
//streams.log.error("messageConfig=" + messageConfig.root().render())

Expand All @@ -190,21 +194,26 @@ object Tasks {
}
}

val configFileToUse = getConfigFile(target, configUrl, config, urlCacheFile)
if (configFileToUse.exists) {
doScalastyleWithConfig(configFileToUse)
} else {
sys.error("config does not exist: %s".format(configFileToUse))
val c = config match {
case Some(config) => config
case None => {
val configFileToUse = getConfigFile(target, configUrl, configFile, urlCacheFile)
if (configFileToUse.exists) {
ScalastyleConfiguration.readFromXml(configFileToUse.absolutePath)
} else {
sys.error("config does not exist: %s".format(configFileToUse))
}
}
}
doScalastyleWithConfig(c)
}

def doGenerateConfig(config: File, streams: TaskStreams[ScopedKey[_]]): Unit = {
getFileFromJar(getClass.getResource("/scalastyle-config.xml"), config.absolutePath, streams.log)
def doGenerateConfig(configFile: File, streams: TaskStreams[ScopedKey[_]]): Unit = {
getFileFromJar(getClass.getResource("/scalastyle-config.xml"), configFile.absolutePath, streams.log)
}

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

private[this] def printResults(config: Config, logger: Logger, messages: List[Message[FileSpec]], quiet: Boolean = false, warnError: Boolean = false): OutputResult = {
Expand Down
6 changes: 6 additions & 0 deletions src/sbt-test/config/scalastyle-config-file/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
scalastyleConfigFile := file("alternative-config.xml")

version := "0.1"

scalaVersion := "2.10.0"

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=0.13.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resolvers += "sonatype-snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"

{
val pluginVersion = System.getProperty("plugin.version")
if(pluginVersion == null)
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
else addSbtPlugin("org.scalastyle" % "scalastyle-sbt-plugin" % pluginVersion)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

object Main extends App {
println("hello")
}

object foo {
println("hello")
}

3 changes: 3 additions & 0 deletions src/sbt-test/config/scalastyle-config-file/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# scalastyle alternative config file
> clean
-> scalastyle w
16 changes: 15 additions & 1 deletion src/sbt-test/config/scalastyle-config/build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
scalastyleConfig := file("alternative-config.xml")
import org.scalastyle.{ScalastyleConfiguration, ConfigurationChecker}

scalastyleConfig := Some(ScalastyleConfiguration(
name = "Scalastyle standard configuration",
checks = List(
ConfigurationChecker[org.scalastyle.file.FileLengthChecker](
enabled = true,
parameters = Map("maxFileLength" -> "5")
),
ConfigurationChecker[org.scalastyle.scalariform.ObjectNamesChecker](
enabled = true,
parameters = Map("regex" -> """[A-Z][A-Za-z]*""")
)
)
))

version := "0.1"

Expand Down
2 changes: 1 addition & 1 deletion src/sbt-test/generate/generate-directory/build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
scalastyleConfig := file("foo/scalastyle-config.xml")
scalastyleConfigFile := file("foo/scalastyle-config.xml")

version := "0.1"

Expand Down
4 changes: 2 additions & 2 deletions src/sbt-test/generate/test-generate/build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
scalastyleConfig := file("scalastyle-config.xml")
scalastyleConfigFile := file("scalastyle-config.xml")

(scalastyleConfig in Test) := file("test-scalastyle-config.xml")
(scalastyleConfigFile in Test) := file("test-scalastyle-config.xml")

version := "0.1"

Expand Down
6 changes: 6 additions & 0 deletions src/sbt-test/test-config/scalastyle-config-file/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(scalastyleConfigFile in Test) := file("gggalternative-config.xml")

version := "0.1"

scalaVersion := "2.10.0"

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version=0.13.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
resolvers += "sonatype-snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/"

{
val pluginVersion = System.getProperty("plugin.version")
if(pluginVersion == null)
throw new RuntimeException("""|The system property 'plugin.version' is not defined.
|Specify this property using the scriptedLaunchOpts -D.""".stripMargin)
else addSbtPlugin("org.scalastyle" % "scalastyle-sbt-plugin" % pluginVersion)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

object Main extends App {
println("hello")
}

object foo {
println("hello")
}

3 changes: 3 additions & 0 deletions src/sbt-test/test-config/scalastyle-config-file/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# scalastyle alternative config file
> clean
-> test:scalastyle w
16 changes: 15 additions & 1 deletion src/sbt-test/test-config/scalastyle-config/build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
(scalastyleConfig in Test) := file("gggalternative-config.xml")
import org.scalastyle.{ScalastyleConfiguration, ConfigurationChecker}

(scalastyleConfig in Test) := Some(ScalastyleConfiguration(
name = "Scalastyle standard configuration",
checks = List(
ConfigurationChecker[org.scalastyle.file.FileLengthChecker](
enabled = true,
parameters = Map("maxFileLength" -> "5")
),
ConfigurationChecker[org.scalastyle.scalariform.ObjectNamesChecker](
enabled = true,
parameters = Map("regex" -> """[A-Z][A-Za-z]*""")
)
)
))

version := "0.1"

Expand Down
2 changes: 1 addition & 1 deletion src/sbt-test/test-scalastyle/config/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version := "0.1"

scalaVersion := "2.10.0"

scalastyleConfig in Test := file("scalastyle-test-config.xml")
scalastyleConfigFile in Test := file("scalastyle-test-config.xml")

val containsMessage = taskKey[Boolean]("contains message")

Expand Down