Skip to content

Commit

Permalink
Merge pull request #7 from hanny24/master
Browse files Browse the repository at this point in the history
Add support for customizeable source sets
  • Loading branch information
alenkacz authored Jan 5, 2017
2 parents 4ad8935 + 5ff469e commit 150f443
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package cz.alenkacz.gradle.scalafmt

import org.gradle.api.tasks.SourceSet

class PluginExtension {
def String configFilePath

List<SourceSet> sourceSets

public PluginExtension() {
configFilePath = ".scalafmt.conf"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SourceSet> 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<String>()
project.sourceSets.all { sourceSet ->
sourceSets { sourceSet ->
sourceSet.allSource.filter { File f -> canBeFormatted(f) }.each { File f ->
String contents = f.text
logger.debug("Formatting '$f'")
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> filePaths) {
super("Files incorrectly formatted: " + filePaths.join(","))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ class ScalafmtPlugin implements Plugin<Project> {
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
}
}
}
}
}
18 changes: 18 additions & 0 deletions src/test/groovy/cz/alenkacz/gradle/scalafmt/ProjectMother.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}

0 comments on commit 150f443

Please sign in to comment.