Skip to content

Commit

Permalink
Cache tweaks (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjaglin authored and poslegm committed Aug 7, 2019
1 parent 721e113 commit 9981813
Show file tree
Hide file tree
Showing 16 changed files with 158 additions and 14 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: scala
dist: trusty
jdk:
- oraclejdk8

Expand Down
41 changes: 27 additions & 14 deletions plugin/src/main/scala/org/scalafmt/sbt/ScalafmtPlugin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import sbt.util.CacheStoreFactory
import sbt.util.FileInfo
import sbt.util.FilesInfo
import sbt.util.Logger
import sbt.LoggerWriter

import scala.util.Failure
import scala.util.Success
Expand Down Expand Up @@ -110,8 +109,8 @@ object ScalafmtPlugin extends AutoPlugin {
log: Logger,
writer: PrintWriter
): Unit = {
cached(cacheDirectory, FilesInfo.lastModified) { modified =>
val changed = modified.filter(_.exists)
cached(cacheDirectory, FilesInfo.lastModified, config) { cacheMisses =>
val changed = cacheMisses.filter(_.exists)
if (changed.size > 0) {
log.info(s"Formatting ${changed.size} Scala sources...")
formatSources(changed.toSeq, config, log, writer)
Expand Down Expand Up @@ -153,8 +152,8 @@ object ScalafmtPlugin extends AutoPlugin {
log: Logger,
writer: PrintWriter
): Boolean = {
cached[Boolean](cacheDirectory, FilesInfo.lastModified) { modified =>
val changed = modified.filter(_.exists)
cached(cacheDirectory, FilesInfo.lastModified, config) { cacheMisses =>
val changed = cacheMisses.filter(_.exists)
if (changed.size > 0) {
log.info(s"Checking ${changed.size} Scala sources...")
checkSources(changed.toSeq, config, log, writer)
Expand Down Expand Up @@ -191,18 +190,32 @@ object ScalafmtPlugin extends AutoPlugin {
unformattedCount == 0
}

private def cached[T](cacheBaseDirectory: File, inStyle: FileInfo.Style)(
private def cached[T](
cacheBaseDirectory: File,
outStyle: FileInfo.Style,
config: Path
)(
action: Set[File] => T
): Set[File] => Option[T] = {
import Path._
lazy val inCache = Difference.inputs(
CacheStoreFactory(cacheBaseDirectory).make("in-cache"),
inStyle
lazy val outCache = Difference.outputs(
CacheStoreFactory(cacheBaseDirectory).make("out-cache"),
outStyle
)
inputs => {
inCache(inputs) { inReport =>
if (!inReport.modified.isEmpty) Some(action(inReport.modified))
else None
outCache(inputs + config.toFile) { outReport =>
val updatedOrAdded = outReport.modified -- outReport.removed
if (!updatedOrAdded.isEmpty) {
val cacheMisses = updatedOrAdded.intersect(inputs)
if (!cacheMisses.isEmpty) {
// incremental run
Some(action(cacheMisses))
} else {
// config file must have changed, rerun everything
Some(action(inputs))
}
} else {
None
}
}
}
}
Expand Down Expand Up @@ -244,7 +257,7 @@ object ScalafmtPlugin extends AutoPlugin {
},
scalafmtCheck :=
checkSources(
streams.value.cacheDirectory,
(streams in scalafmt).value.cacheDirectory,
(unmanagedSources in scalafmt).value,
scalaConfig.value,
streams.value.log,
Expand Down
2 changes: 2 additions & 0 deletions plugin/src/sbt-test/scalafmt-sbt/sbt/.scalafmt13.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = 2.0.0
style = IntelliJ
2 changes: 2 additions & 0 deletions plugin/src/sbt-test/scalafmt-sbt/sbt/.scalafmt14.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
version = 2.0.0
style = IntelliJ
3 changes: 3 additions & 0 deletions plugin/src/sbt-test/scalafmt-sbt/sbt/.scalafmt15.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version = 2.0.0
style = IntelliJ
maxColumn = 42
3 changes: 3 additions & 0 deletions plugin/src/sbt-test/scalafmt-sbt/sbt/.scalafmt16.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version = 2.0.0
style = IntelliJ
maxColumn = 42
16 changes: 16 additions & 0 deletions plugin/src/sbt-test/scalafmt-sbt/sbt/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,22 @@ lazy val p12 = project.settings(
scalaVersion := "2.12.1",
scalafmtConfig := file(".scalafmt12.conf")
)
lazy val p13 = project.settings(
scalaVersion := "2.12.1",
scalafmtConfig := file(".scalafmt13.conf")
)
lazy val p14 = project.settings(
scalaVersion := "2.12.1",
scalafmtConfig := file(".scalafmt14.conf")
)
lazy val p15 = project.settings(
scalaVersion := "2.12.1",
scalafmtConfig := file(".scalafmt15.conf")
)
lazy val p16 = project.settings(
scalaVersion := "2.12.1",
scalafmtConfig := file(".scalafmt16.conf")
)

def assertContentsEqual(file: File, expected: String): Unit = {
val obtained =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object
Test2
{
def foo2(a: Int, // comment
b: Double) = ???
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object
Test
{
foo(a, // comment
b)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object
Test3
{
foo(a, // comment
b)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// already formatted!
object Test {
foo(
a, // comment
b
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// already formatted!
object Test2 {
def foo2(
a: Int, // comment
b: Double
) = ???
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// already formatted!
object Test3 {
foo(
a, // comment
b
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object
Test
{
foo(a, // comment
b)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object
Test
{
foo(a, // comment
b)
}
53 changes: 53 additions & 0 deletions plugin/src/sbt-test/scalafmt-sbt/sbt/test
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,56 @@ $ newer p8/src/main/scala/Test2.scala timestamp
-> p12/scalafmtCheck

> check

> p13/scalafmt
# prevent reading the source (without changing the mtime) to detect actual/uncached invocation of scalafmt
$ exec chmod 000 p13/src/main/scala/Test.scala
######## no-op expected when nothing has changed
> p13/scalafmt
> p13/scalafmtCheck
######## no-op expected expected when no file was added nor updated
$ delete p13/src/main/scala/Test2.scala
> p13/scalafmt
> p13/scalafmtCheck
######## incremental formatting expected when a file was added
$ copy-file p13/src/test/scala/Test3.scala p13/src/main/scala/Test3.scala
> p13/scalafmt
> p13/scalafmtCheck
-$ must-mirror p13/src/test/scala/Test3.scala p13/src/main/scala/Test3.scala
######## cache invalidation expected when a file was updated
$ touch p13/src/main/scala/Test.scala
-> p13/scalafmt

> p14/scalafmtCheck
# prevent reading the source (without changing the mtime) to detect actual/uncached invocation of scalafmt
$ exec chmod 000 p14/src/main/scala/Test.scala
######## no-op expected when nothing has changed
> p14/scalafmtCheck
> p14/scalafmt
######## no-op expected expected when no file was added nor updated
$ delete p14/src/main/scala/Test2.scala
> p14/scalafmtCheck
> p14/scalafmt
######## incremental checking expected when a file was added
$ copy-file p14/src/test/scala/Test3.scala p14/src/main/scala/Test3.scala
> p14/scalafmtCheck
> p14/scalafmt
######## cache invalidation expected when a file was updated
$ touch p14/src/main/scala/Test.scala
-> p14/scalafmtCheck

> p15/scalafmt
> p15/scalafmtCheck
$ copy-file .scalafmt.conf .scalafmt15.conf
# prevent reading the source (without changing the mtime) to detect actual/uncached invocation of scalafmt
$ exec chmod 000 p15/src/main/scala/Test.scala
-> p15/scalafmt
-> p15/scalafmtCheck

> p16/scalafmt
> p16/scalafmtCheck
> set p16/scalafmtConfig := file(".scalafmt.conf")
# prevent reading the source (without changing the mtime) to detect actual/uncached invocation of scalafmt
$ exec chmod 000 p16/src/main/scala/Test.scala
-> p16/scalafmt
-> p16/scalafmtCheck

0 comments on commit 9981813

Please sign in to comment.