Skip to content

Commit

Permalink
Scalafmt 2.7.5 (#40)
Browse files Browse the repository at this point in the history
* chore(deps): scalafmt 2.7.5

* chore(deps): Gradle 6.8.3

* chore: Gradle deprecation message elimindated Fixes #39

Co-authored-by: Michal Augustýn <michal.augustyn@avast.com>
  • Loading branch information
augi and augi committed Apr 12, 2021
1 parent fd461b4 commit 2991939
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 33 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.4.2'
compile 'org.scalameta:scalafmt-dynamic_2.12:2.7.5'
compile 'org.scala-lang.modules:scala-xml_2.12:1.3.0'

testCompile 'junit:junit:4.13'
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.2.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package cz.alenkacz.gradle.scalafmt

class PluginExtension {
def String configFilePath

public PluginExtension() {
configFilePath = ".scalafmt.conf"
}
String configFilePath = ".scalafmt.conf"
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@ package cz.alenkacz.gradle.scalafmt
import org.gradle.api.DefaultTask
import org.gradle.api.plugins.JavaBasePlugin
import org.gradle.api.tasks.InputFiles
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.PathSensitive
import org.gradle.api.tasks.PathSensitivity
import org.gradle.api.tasks.SourceSet
import org.scalafmt.dynamic.ScalafmtDynamicError
import org.scalafmt.interfaces.Scalafmt

class ScalafmtFormatBase extends DefaultTask {
SourceSet sourceSet
ClassLoader cl = this.class.getClassLoader()
PluginExtension pluginExtension
private ClassLoader cl = this.class.getClassLoader()
@Internal PluginExtension pluginExtension

def globalFormatter = Scalafmt.create(cl)
private Scalafmt globalFormatter = Scalafmt.create(cl)
.withRespectVersion(false)
.withDefaultVersion("1.5.1")
.withDefaultVersion("2.7.5")

@InputFiles
@PathSensitive(PathSensitivity.RELATIVE)
Expand All @@ -42,13 +44,18 @@ class ScalafmtFormatBase extends DefaultTask {
getSourceSet().filter { File f -> canBeFormatted(f) }.each { File f ->
String contents = f.text
logger.debug("Formatting '$f'")
def formattedContents = formatter.format(configpath.toPath(), f.toPath(), contents)
if (testOnly) {
if (contents != formattedContents) {
misformattedFiles.add(f.absolutePath)
try {
def formattedContents = formatter.format(configpath.toPath(), f.toPath(), contents)
if (testOnly) {
if (contents != formattedContents) {
misformattedFiles.add(f.absolutePath)
}
} else {
f.write(formattedContents)
}
} else {
f.write(formattedContents)
}
catch (ScalafmtDynamicError err) {
logger.lifecycle("Error while formatting", err)
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/main/groovy/cz/alenkacz/gradle/scalafmt/ScalafmtPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ class ScalafmtPlugin implements Plugin<Project> {
project.plugins.withType(JavaBasePlugin) {
def jpc = project.convention.getPlugin(JavaPluginConvention)
jpc.sourceSets.all { sourceSet ->
def task = project.tasks.create(sourceSet.getTaskName("", "scalafmt"), ScalafmtTask).configure { Task t ->
t.sourceSet = sourceSet
t.pluginExtension = extension
t.description = "Formats ${sourceSet.name} source set using scalafmt."
}
def task = project.tasks.register(sourceSet.getTaskName("", "scalafmt"), ScalafmtTask, {
it.sourceSet = sourceSet
it.pluginExtension = extension
it.description = "Formats ${sourceSet.name} source set using scalafmt."
})

def checkTask = project.tasks.create(sourceSet.getTaskName("check", "scalafmt"), ScalafmtCheckTask).configure { Task t ->
t.sourceSet = sourceSet
t.pluginExtension = extension
t.description = "Checks formatting of ${sourceSet.name} source set using scalafmt."
}
def checkTask = project.tasks.register(sourceSet.getTaskName("check", "scalafmt"), ScalafmtCheckTask, {
it.sourceSet = sourceSet
it.pluginExtension = extension
it.description = "Checks formatting of ${sourceSet.name} source set using scalafmt."
})
scalafmtAll.dependsOn task
checkScalafmtAll.dependsOn checkTask
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ class ScalafmtTaskTest extends Specification {
def actual = testProject.testFile.text
actual == """import java.nio.file.{Paths, Files}
|object Test {
| foo(a, // comment
| b)
| foo(
| a, // comment
| b
| )
|}
|""".stripMargin()
}
Expand Down Expand Up @@ -50,8 +52,10 @@ class ScalafmtTaskTest extends Specification {
def actual = testProject.testFile.text
actual == """import java.nio.file.{Files, Paths}
|object Test {
| foo(a, // comment
| b)
| foo(
| a, // comment
| b
| )
|}
|""".stripMargin()
}
Expand Down Expand Up @@ -86,8 +90,10 @@ class ScalafmtTaskTest extends Specification {
def actual = testProject.testFile.text
actual == """import java.nio.file.{Files, Paths}
|object Test {
| foo(a, // comment
| b)
| foo(
| a, // comment
| b
| )
|}
|""".stripMargin()
}
Expand Down

0 comments on commit 2991939

Please sign in to comment.