Skip to content

Commit

Permalink
passing defined repositories to scalafmt (#28) (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
slivkamiro authored and augi committed Dec 10, 2019
1 parent 9170b25 commit 2e59716
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -24,6 +24,9 @@ class ScalafmtFormatBase extends DefaultTask {
def configpath = ConfigFactory.get(logger,project,pluginExtension.configFilePath)
def misformattedFiles = new ArrayList<String>()

def formatter = globalFormatter
.withMavenRepositories(repositories())

sourceSet.allSource.filter { File f -> canBeFormatted(f) }.each { File f ->
String contents = f.text
logger.debug("Formatting '$f'")
Expand All @@ -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")
}
Expand Down

0 comments on commit 2e59716

Please sign in to comment.