Skip to content
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

Add input file encoding setting. #67

Open
wants to merge 2 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
16 changes: 10 additions & 6 deletions src/main/scala/org/scalastyle/sbt/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ object ScalastylePlugin extends AutoPlugin {
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")
val scalastyleInputEncoding = settingKey[Option[String]]("The encoding that scalastyle will load the input files using")
}

import autoImport._
Expand All @@ -77,8 +78,10 @@ object ScalastylePlugin extends AutoPlugin {
val configRefreshHoursV = scalastyleConfigRefreshHours.value
val targetV = target.value
val configCacheFileV = scalastyleConfigUrlCacheFile.value
val inputEncodingV = scalastyleInputEncoding.value

Tasks.doScalastyle(args, configV, configUrlV, failOnErrorV, failOnWarningV, scalastyleSourcesV, scalastyleTargetV, streamsV, configRefreshHoursV, targetV, configCacheFileV)
Tasks.doScalastyle(args, configV, configUrlV, failOnErrorV, failOnWarningV, scalastyleSourcesV, scalastyleTargetV,
streamsV, configRefreshHoursV, targetV, configCacheFileV, inputEncodingV)
},
scalastyleGenerateConfig := {
val streamsValue = streams.value
Expand Down Expand Up @@ -108,15 +111,16 @@ object ScalastylePlugin extends AutoPlugin {
scalastyleFailOnWarning := false,
(scalastyleFailOnWarning in Test) := (scalastyleFailOnWarning in scalastyle).value,
scalastyleSources := (unmanagedSourceDirectories in Compile).value,
(scalastyleSources in Test) := (unmanagedSourceDirectories in Test).value
(scalastyleSources in Test) := (unmanagedSourceDirectories in Test).value,
scalastyleInputEncoding := None
) ++
Project.inConfig(Compile)(rawScalastyleSettings()) ++
Project.inConfig(Test)(rawScalastyleSettings())
}

object Tasks {
def doScalastyle(args: Seq[String], config: File, configUrl: Option[URL], failOnError: Boolean, failOnWarning: Boolean, scalastyleSources: Seq[File], scalastyleTarget: File,
streams: TaskStreams[ScopedKey[_]], refreshHours: Integer, target: File, urlCacheFile: String): Unit = {
streams: TaskStreams[ScopedKey[_]], refreshHours: Integer, target: File, urlCacheFile: String, inputEncoding: Option[String]): Unit = {
val logger = streams.log
val quietArg = "q"
val silentArg = "s"
Expand Down Expand Up @@ -175,7 +179,7 @@ object Tasks {
case files => files
}

val messages = runScalastyle(config, filesToProcess)
val messages = runScalastyle(config, filesToProcess, inputEncoding)

saveToXml(messageConfig, messages, scalastyleTarget.absolutePath)

Expand All @@ -199,9 +203,9 @@ object Tasks {
extractFileFromJar(getClass.getResource("/scalastyle-config.xml"), config.absolutePath, streams.log)
}

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

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

version := "0.1"

scalaVersion := "2.10.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,13 @@
<scalastyle>
<name>Scalastyle standard configuration</name>
<check level="warning" class="org.scalastyle.file.FileLengthChecker" enabled="true">
<parameters>
<parameter name="maxFileLength"><![CDATA[5]]></parameter>
</parameters>
</check>
<check level="warning" class="org.scalastyle.scalariform.ObjectNamesChecker" enabled="true">
<parameters>
<parameter name="regex"><![CDATA[[A-Z][A-Za-z]*]]></parameter>
</parameters>
</check>
</scalastyle>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

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

object foo {
println("hello")
}

5 changes: 5 additions & 0 deletions src/sbt-test/config/scalastyle-input-encoding/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# scalastyle with UTF-8 input file
-> scalastyle
> 'set scalastyleInputEncoding := Some(scala.io.Codec.UTF8.name)'
> scalastyle
$ exists target/scalastyle-output.xml