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

Compile then check style task #69

Open
wants to merge 8 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
1 change: 1 addition & 0 deletions scripted.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ ScriptedPlugin.scriptedSettings

scriptedLaunchOpts ++= {
Seq("-Xmx1024M",
"-XX:MaxPermSize=256M",
"-Dplugin.version=" + version.value,
"-Dsbt.version=" + (sbtVersion in pluginCrossBuild).value)
}
Expand Down
19 changes: 17 additions & 2 deletions src/main/scala/org/scalastyle/sbt/Plugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,13 @@ import org.scalastyle.ScalastyleChecker
import org.scalastyle.ScalastyleConfiguration
import org.scalastyle.XmlOutput
import sbt.ConfigKey.configurationToKey
import sbt.Def.Initialize
import sbt.Keys.compile
import sbt.Keys.streams
import sbt.Keys.target
import sbt.Keys.unmanagedSourceDirectories
import sbt._
import sbt.inc.Analysis
import sbt.std.TaskStreams

import scala.io.Codec
Expand All @@ -50,6 +53,7 @@ object ScalastylePlugin extends AutoPlugin {
object autoImport {
val scalastyle = inputKey[Unit]("Run scalastyle on your code")
val scalastyleGenerateConfig = taskKey[Unit]("Generate a default configuration files for scalastyle")
val compileThenCheckStyle = taskKey[Analysis]("Compiles sources and then runs scalastyle on your code")

val scalastyleTarget = settingKey[File]("XML output file from scalastyle")
val scalastyleConfig = settingKey[File]("Scalastyle configuration file")
Expand All @@ -59,6 +63,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 scalastyleCompileArgs = settingKey[String]("The arguments that are passed to scalastyle when using compileThenCheckStyle")
}

import autoImport._
Expand All @@ -84,7 +89,15 @@ object ScalastylePlugin extends AutoPlugin {
val streamsValue = streams.value
val configValue = scalastyleConfig.value
Tasks.doGenerateConfig(configValue, streamsValue)
}
},
compileThenCheckStyle := Def.taskDyn {
val analysis = compile.value
val args = scalastyleCompileArgs.value
Def.task {
val _ = scalastyle.toTask(args).value
analysis
}
}.value
)

override def requires: Plugins = sbt.plugins.JvmPlugin
Expand All @@ -108,7 +121,9 @@ 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,
scalastyleCompileArgs := "",
(scalastyleCompileArgs in Test) := (scalastyleCompileArgs in scalastyle).value
) ++
Project.inConfig(Compile)(rawScalastyleSettings()) ++
Project.inConfig(Test)(rawScalastyleSettings())
Expand Down
27 changes: 27 additions & 0 deletions src/sbt-test/compile-task/compiles-first/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import scala.concurrent.duration._

version := "0.1"

scalaVersion := "2.10.0"

inConfig(Compile) {
Seq(
compile := {
val log = streams.value.log
log.debug("Pausing compile task for 1 second")
Thread.sleep(1.second.toMillis)
log.debug("Checking if scalastyle has already executed")
val file = target.value / "scalastyle"
if (file.exists()) {
throw new RuntimeException("scalastyle task executed before or during the compile task")
}
compile.value
},
scalastyle := {
val log = streams.value.log
log.debug("Running scalastyle")
val file = target.value / "scalastyle"
IO.write(file, "scalastyle")
}
)
}
9 changes: 9 additions & 0 deletions src/sbt-test/compile-task/compiles-first/project/plugins.sbt
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 @@
<scalastyle/>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Hello
3 changes: 3 additions & 0 deletions src/sbt-test/compile-task/compiles-first/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# compileThenCheckStyle compiles first then if successful checks the style
> clean
> compileThenCheckStyle
3 changes: 3 additions & 0 deletions src/sbt-test/compile-task/retains-compilation-error/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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 @@
<scalastyle/>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
3 changes: 3 additions & 0 deletions src/sbt-test/compile-task/retains-compilation-error/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# compileThenCheckStyle fails if the compile task fails
> clean
-> compileThenCheckStyle
17 changes: 17 additions & 0 deletions src/sbt-test/compile-task/scoped/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version := "0.1"

scalaVersion := "2.10.0"

inConfig(Compile) {
Seq(
compile := {
val file = target.value / "compile"
IO.write(file, "compile")
compile.value
},
scalastyle := {
val file = target.value / "scalastyle"
IO.write(file, "scalastyle")
}
)
}
9 changes: 9 additions & 0 deletions src/sbt-test/compile-task/scoped/project/plugins.sbt
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)
}
1 change: 1 addition & 0 deletions src/sbt-test/compile-task/scoped/scalastyle-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<scalastyle/>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Hello
5 changes: 5 additions & 0 deletions src/sbt-test/compile-task/scoped/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# compileThenCheckStyle depends on tasks in the same scope
> clean
> compileThenCheckStyle
$ exists target/compile
$ exists target/scalastyle
6 changes: 6 additions & 0 deletions src/sbt-test/config/compile-args/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
scalastyleCompileArgs := " w"

version := "0.1"

scalaVersion := "2.10.0"

9 changes: 9 additions & 0 deletions src/sbt-test/config/compile-args/project/plugins.sbt
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)
}
8 changes: 8 additions & 0 deletions src/sbt-test/config/compile-args/scalastyle-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<scalastyle>
<name>Scalastyle standard configuration</name>
<check level="warning" class="org.scalastyle.file.FileLengthChecker" enabled="true">
<parameters>
<parameter name="maxFileLength"><![CDATA[1]]></parameter>
</parameters>
</check>
</scalastyle>
3 changes: 3 additions & 0 deletions src/sbt-test/config/compile-args/src/main/scala/hello.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object hello {

}
2 changes: 2 additions & 0 deletions src/sbt-test/config/compile-args/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# compileThenCheckStyle uses the scalastyleCompileArgs
-> compileThenCheckStyle
27 changes: 27 additions & 0 deletions src/sbt-test/test-compile-task/compiles-first/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import scala.concurrent.duration._

version := "0.1"

scalaVersion := "2.10.0"

inConfig(Test) {
Seq(
compile := {
val log = streams.value.log
log.debug("Pausing compile task for 1 second")
Thread.sleep(1.second.toMillis)
log.debug("Checking if scalastyle has already executed")
val file = target.value / "scalastyle"
if (file.exists()) {
throw new RuntimeException("scalastyle task executed before or during the compile task")
}
compile.value
},
scalastyle := {
val log = streams.value.log
log.debug("Running scalastyle")
val file = target.value / "scalastyle"
IO.write(file, "scalastyle")
}
)
}
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 @@
<scalastyle/>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Hello
3 changes: 3 additions & 0 deletions src/sbt-test/test-compile-task/compiles-first/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# compileThenCheckStyle compiles first then if successful checks the style
> clean
> test:compileThenCheckStyle
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
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 @@
<scalastyle/>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello
3 changes: 3 additions & 0 deletions src/sbt-test/test-compile-task/retains-compilation-error/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# compileThenCheckStyle fails if the compile task fails
> clean
-> test:compileThenCheckStyle
17 changes: 17 additions & 0 deletions src/sbt-test/test-compile-task/scoped/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
version := "0.1"

scalaVersion := "2.10.0"

inConfig(Test) {
Seq(
compile := {
val file = target.value / "compile"
IO.write(file, "compile")
compile.value
},
scalastyle := {
val file = target.value / "scalastyle"
IO.write(file, "scalastyle")
}
)
}
9 changes: 9 additions & 0 deletions src/sbt-test/test-compile-task/scoped/project/plugins.sbt
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 @@
<scalastyle/>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class Hello
5 changes: 5 additions & 0 deletions src/sbt-test/test-compile-task/scoped/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# compileThenCheckStyle depends on tasks in the same scope
> clean
> test:compileThenCheckStyle
$ exists target/compile
$ exists target/scalastyle
6 changes: 6 additions & 0 deletions src/sbt-test/test-config/compile-args/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
scalastyleCompileArgs in Test := " w"

version := "0.1"

scalaVersion := "2.10.0"

9 changes: 9 additions & 0 deletions src/sbt-test/test-config/compile-args/project/plugins.sbt
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)
}
8 changes: 8 additions & 0 deletions src/sbt-test/test-config/compile-args/scalastyle-config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<scalastyle>
<name>Scalastyle standard configuration</name>
<check level="warning" class="org.scalastyle.file.FileLengthChecker" enabled="true">
<parameters>
<parameter name="maxFileLength"><![CDATA[1]]></parameter>
</parameters>
</check>
</scalastyle>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
object hello {

}
2 changes: 2 additions & 0 deletions src/sbt-test/test-config/compile-args/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# compileThenCheckStyle uses the scalastyleCompileArgs
-> test:compileThenCheckStyle