diff --git a/build.gradle b/build.gradle index 6144d38..8bed252 100644 --- a/build.gradle +++ b/build.gradle @@ -30,7 +30,7 @@ repositories { dependencies { compile gradleApi() - compile "org.scalameta:scalafmt-dynamic_2.12:2.2.2" + compile "org.scalameta:scalafmt-dynamic_2.12:2.3.2" compile group: 'org.scala-lang.modules', name: 'scala-xml_2.12', version: '1.1.1' testCompile group: 'junit', name: 'junit', version: '4.12' diff --git a/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtFormatBase.groovy b/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtFormatBase.groovy index e91b886..10a4ffa 100644 --- a/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtFormatBase.groovy +++ b/src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtFormatBase.groovy @@ -4,18 +4,18 @@ import org.gradle.api.DefaultTask import org.gradle.api.plugins.JavaBasePlugin import org.gradle.api.tasks.SourceSet import org.scalafmt.interfaces.Scalafmt -import org.scalafmt.interfaces.ScalafmtClassLoader -import java.nio.file.Paths + +import java.util.stream.Collectors class ScalafmtFormatBase extends DefaultTask { SourceSet sourceSet ClassLoader cl = this.class.getClassLoader() PluginExtension pluginExtension - def formatter = Scalafmt.create(cl) + + def globalFormatter = Scalafmt.create(cl) .withRespectVersion(false) .withDefaultVersion("1.5.1") - 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") @@ -24,6 +24,9 @@ class ScalafmtFormatBase extends DefaultTask { def configpath = ConfigFactory.get(logger,project,pluginExtension.configFilePath) def misformattedFiles = new ArrayList() + def formatter = globalFormatter + .withMavenRepositories(repositories()) + sourceSet.allSource.filter { File f -> canBeFormatted(f) }.each { File f -> String contents = f.text logger.debug("Formatting '$f'") @@ -42,6 +45,12 @@ class ScalafmtFormatBase extends DefaultTask { } } + private String[] repositories() { + project.getRepositories().stream().map { repository -> + repository.properties.get("url").toString() + }.collect(Collectors.toList()).toArray(new String[0]) + } + def boolean canBeFormatted(File file) { file.getAbsolutePath().endsWith(".scala") || file.getAbsolutePath().endsWith(".sbt") }