From 685afb89c5be2cb687e24641aece8203824d3e14 Mon Sep 17 00:00:00 2001 From: Jan Strnad Date: Thu, 5 Jan 2017 09:32:06 +0100 Subject: [PATCH 1/4] Customizeable sourcesets --- .../alenkacz/gradle/scalafmt/PluginExtension.groovy | 4 ++++ .../gradle/scalafmt/ScalafmtFormatBase.groovy | 13 ++++++++++++- .../alenkacz/gradle/scalafmt/ScalafmtPlugin.groovy | 9 ++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/main/groovy/cz/alenkacz/gradle/scalafmt/PluginExtension.groovy b/src/main/groovy/cz/alenkacz/gradle/scalafmt/PluginExtension.groovy index 3f9d4bd..ea31b82 100644 --- a/src/main/groovy/cz/alenkacz/gradle/scalafmt/PluginExtension.groovy +++ b/src/main/groovy/cz/alenkacz/gradle/scalafmt/PluginExtension.groovy @@ -1,8 +1,12 @@ package cz.alenkacz.gradle.scalafmt +import org.gradle.api.tasks.SourceSet + class PluginExtension { def String configFilePath + List sourceSets + public PluginExtension() { configFilePath = ".scalafmt.conf" } diff --git a/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtFormatBase.groovy b/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtFormatBase.groovy index 510d76f..0f47a65 100644 --- a/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtFormatBase.groovy +++ b/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtFormatBase.groovy @@ -2,16 +2,27 @@ package cz.alenkacz.gradle.scalafmt import org.gradle.api.DefaultTask import org.gradle.api.plugins.JavaBasePlugin +import org.gradle.api.tasks.SourceSet import org.scalafmt.Scalafmt class ScalafmtFormatBase extends DefaultTask { + private Closure sourceSets = { closure -> + project.sourceSets.all(closure) + } + + void setSourceSets(List sourceSets) { + this.sourceSets = { closure -> + sourceSets.each(closure) + } + } + def runScalafmt(boolean testOnly = false) { if (project.plugins.withType(JavaBasePlugin).empty) { logger.info("Java or Scala gradle plugin not available in this project, nothing to format") return } def misformattedFiles = new ArrayList() - project.sourceSets.all { sourceSet -> + sourceSets { sourceSet -> sourceSet.allSource.filter { File f -> canBeFormatted(f) }.each { File f -> String contents = f.text logger.debug("Formatting '$f'") diff --git a/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtPlugin.groovy b/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtPlugin.groovy index ef73cb3..1c88047 100644 --- a/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtPlugin.groovy +++ b/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtPlugin.groovy @@ -8,6 +8,13 @@ class ScalafmtPlugin implements Plugin { void apply(Project project) { project.task('scalafmt', type: ScalafmtTask) project.task('scalafmtTest', type: ScalafmtTestTask) - project.extensions.create('scalafmt', PluginExtension) + PluginExtension extension = project.extensions.create('scalafmt', PluginExtension) + project.afterEvaluate { + if (extension.sourceSets != null) { + project.tasks.withType(ScalafmtFormatBase) { + sourceSets = extension.sourceSets + } + } + } } } From 9142b3fcad83765089f16150a633d18f02454bae Mon Sep 17 00:00:00 2001 From: Jan Strnad Date: Thu, 5 Jan 2017 09:33:29 +0100 Subject: [PATCH 2/4] Readme update --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7577020..27998b5 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ Usage scalafmt { // configFilePath = ".scalafmt.conf" // .scalafmt.conf in the project root is default value, provide only if other location is needed + // sourceSets = [project.sourceSets.main] // limit to main source set only } Tasks From 99e528f0dc2c1f0723500e290cb4fce01a586095 Mon Sep 17 00:00:00 2001 From: Jan Strnad Date: Thu, 5 Jan 2017 14:42:17 +0100 Subject: [PATCH 3/4] Use GradleException instead of Exception --- .../alenkacz/gradle/scalafmt/ScalafmtFormatException.groovy | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtFormatException.groovy b/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtFormatException.groovy index 9b41b8a..069298c 100644 --- a/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtFormatException.groovy +++ b/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtFormatException.groovy @@ -1,6 +1,8 @@ package cz.alenkacz.gradle.scalafmt -class ScalafmtFormatException extends Exception { +import org.gradle.api.GradleException + +class ScalafmtFormatException extends GradleException { public ScalafmtFormatException(List filePaths) { super("Files incorrectly formatted: " + filePaths.join(",")) } From 5ff469e1b1e023b039a1dfa23bb0178ed440ac7b Mon Sep 17 00:00:00 2001 From: Jan Strnad Date: Thu, 5 Jan 2017 15:04:45 +0100 Subject: [PATCH 4/4] Add sourceSets test case --- .../gradle/scalafmt/ProjectMother.groovy | 18 ++++++++++++++++++ .../scalafmt/ScalafmtTestTaskTest.groovy | 17 +++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/test/groovy/cz/alenkacz/gradle/scalafmt/ProjectMother.groovy b/src/test/groovy/cz/alenkacz/gradle/scalafmt/ProjectMother.groovy index 33194db..f2788ff 100644 --- a/src/test/groovy/cz/alenkacz/gradle/scalafmt/ProjectMother.groovy +++ b/src/test/groovy/cz/alenkacz/gradle/scalafmt/ProjectMother.groovy @@ -22,6 +22,23 @@ class ProjectMother { return testProject } + static def basicProjectWithIncorrectTestFile() { + TestProject testProject = null + File.createTempDir().with { + deleteOnExit() + def srcFolder = new File(absoluteFile, testSourceFilePath) + srcFolder.mkdirs() + def srcFile = Files.createFile(Paths.get(srcFolder.absolutePath, "Test.scala")) + srcFile.write """ + |object Test { + | if(true){} + |} + |""".stripMargin() + testProject = new TestProject(absoluteFile, srcFile.toFile()) + } + return testProject + } + static def basicProject() { TestProject testProject = null File.createTempDir().with { @@ -40,6 +57,7 @@ class ProjectMother { | b)} """.stripMargin() private static def sourceFilePath = "src/main/scala/cz/alenkacz/gradle/scalafmt/test" + private static def testSourceFilePath = "src/test/scala/cz/alenkacz/gradle/scalafmt/test" static def projectWithConfig() { TestProject testProject = null diff --git a/src/test/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtTestTaskTest.groovy b/src/test/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtTestTaskTest.groovy index 2e70c09..ab519dc 100644 --- a/src/test/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtTestTaskTest.groovy +++ b/src/test/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtTestTaskTest.groovy @@ -31,4 +31,21 @@ class ScalafmtTestTaskTest extends Specification { then: noExceptionThrown() } + + def "ignore source files in test"() { + given: + def testProject = ProjectMother.basicProjectWithIncorrectTestFile() + def project = ProjectBuilder.builder().withProjectDir(testProject.projectRoot).build() + + project.plugins.apply 'scalafmt' + project.plugins.apply 'scala' + project.scalafmt.sourceSets = [project.sourceSets.main] + + when: + project.evaluate() + project.tasks.scalafmtTest.format() + + then: + noExceptionThrown() + } }