diff --git a/src/main/scala/org/scalastyle/sbt/Plugin.scala b/src/main/scala/org/scalastyle/sbt/Plugin.scala index 17a5b27..e2f7a19 100644 --- a/src/main/scala/org/scalastyle/sbt/Plugin.scala +++ b/src/main/scala/org/scalastyle/sbt/Plugin.scala @@ -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._ @@ -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 @@ -108,7 +111,8 @@ 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()) @@ -116,7 +120,7 @@ object ScalastylePlugin extends AutoPlugin { 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" @@ -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) @@ -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, diff --git a/src/sbt-test/config/scalastyle-input-encoding/build.sbt b/src/sbt-test/config/scalastyle-input-encoding/build.sbt new file mode 100644 index 0000000..6de7e85 --- /dev/null +++ b/src/sbt-test/config/scalastyle-input-encoding/build.sbt @@ -0,0 +1,6 @@ +scalastyleTarget := file("target/scalastyle-output.xml") + +version := "0.1" + +scalaVersion := "2.10.0" + diff --git a/src/sbt-test/config/scalastyle-input-encoding/project/plugins.sbt b/src/sbt-test/config/scalastyle-input-encoding/project/plugins.sbt new file mode 100644 index 0000000..2c582ca --- /dev/null +++ b/src/sbt-test/config/scalastyle-input-encoding/project/plugins.sbt @@ -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) +} \ No newline at end of file diff --git a/src/sbt-test/config/scalastyle-input-encoding/scalastyle-config.xml b/src/sbt-test/config/scalastyle-input-encoding/scalastyle-config.xml new file mode 100644 index 0000000..bd579b5 --- /dev/null +++ b/src/sbt-test/config/scalastyle-input-encoding/scalastyle-config.xml @@ -0,0 +1,13 @@ + + Scalastyle standard configuration + + + + + + + + + + + diff --git a/src/sbt-test/config/scalastyle-input-encoding/src/main/scala/hello.scala b/src/sbt-test/config/scalastyle-input-encoding/src/main/scala/hello.scala new file mode 100644 index 0000000..4aa0d02 --- /dev/null +++ b/src/sbt-test/config/scalastyle-input-encoding/src/main/scala/hello.scala @@ -0,0 +1,10 @@ + +object Main extends App { + println("hello") + println("网址") +} + +object foo { + println("hello") +} + diff --git a/src/sbt-test/config/scalastyle-input-encoding/test b/src/sbt-test/config/scalastyle-input-encoding/test new file mode 100644 index 0000000..e32658c --- /dev/null +++ b/src/sbt-test/config/scalastyle-input-encoding/test @@ -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 \ No newline at end of file