From d03bb126234305d4332dffc9fd54c06f4acb5845 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 5 Apr 2018 10:01:05 +0100 Subject: [PATCH] Expose JavaMilli into the sbt.io package Allows for reuse, such as in sbt/sbt#4067. --- build.sbt | 4 +++ io/src/main/scala/sbt/internal/io/Milli.scala | 26 +++---------------- io/src/main/scala/sbt/io/JavaMilli.scala | 26 +++++++++++++++++++ .../scala/sbt/internal/io/JavaMilliSpec.scala | 8 ++++++ 4 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 io/src/main/scala/sbt/io/JavaMilli.scala create mode 100644 io/src/test/scala/sbt/internal/io/JavaMilliSpec.scala diff --git a/build.sbt b/build.sbt index 91654a01..319c4939 100644 --- a/build.sbt +++ b/build.sbt @@ -50,5 +50,9 @@ val io = (project in file("io")) // MiMa doesn't understand private inner classes? // method this(sbt.io.PollingWatchService,sbt.io.PollingWatchService#PollingThread,java.nio.file.Watchable,java.util.List)Unit in class sbt.io.PollingWatchService#PollingWatchKey does not have a correspondent in current version exclude[DirectMissingMethodProblem]("sbt.io.PollingWatchService#PollingWatchKey.this"), + + // moved JavaMilli to sbt.io + exclude[MissingClassProblem]("sbt.internal.io.JavaMilli$"), + exclude[MissingClassProblem]("sbt.internal.io.JavaMilli"), ), ) diff --git a/io/src/main/scala/sbt/internal/io/Milli.scala b/io/src/main/scala/sbt/internal/io/Milli.scala index 8e291601..ea38a313 100644 --- a/io/src/main/scala/sbt/internal/io/Milli.scala +++ b/io/src/main/scala/sbt/internal/io/Milli.scala @@ -1,15 +1,11 @@ package sbt.internal.io -import java.lang.UnsupportedOperationException import java.io.IOException import java.io.FileNotFoundException import java.io.File import java.util.Date import java.nio.ByteBuffer import java.nio.ByteOrder -import java.nio.file.{ Files, NoSuchFileException } -import java.nio.file.{ Paths => JPaths } -import java.nio.file.attribute.FileTime import scala.reflect.{ ClassTag, classTag } import scala.collection.JavaConverters.mapAsJavaMapConverter @@ -33,6 +29,7 @@ import com.sun.jna.platform.win32.WinBase.FILETIME import com.sun.jna.platform.win32.WinError.ERROR_FILE_NOT_FOUND import com.sun.jna.platform.win32.WinError.ERROR_PATH_NOT_FOUND +import sbt.io.JavaMilli import sbt.internal.io.MacJNA._ private abstract class Stat[Time_T](size: Int) extends NativeMapped { @@ -56,7 +53,7 @@ private abstract class StatInt(size: Int, mtimeOffset: Int, mtimensecOffset: Int def getModifiedTimeNative = (buffer.getInt(mtimeOffset), buffer.getInt(mtimensecOffset)) } -private abstract class Milli { +private[sbt] abstract class Milli { def getModifiedTime(filePath: String): Long def setModifiedTime(filePath: String, mtime: Long): Unit def copyModifiedTime(fromFilePath: String, toFilePath: String): Unit @@ -326,28 +323,11 @@ private object WinMilli extends MilliNative[FILETIME] { } // No native time information? Copy just the milliseconds -private abstract class MilliMilliseconds extends Milli { +private[sbt] abstract class MilliMilliseconds extends Milli { def copyModifiedTime(fromFilePath: String, toFilePath: String): Unit = setModifiedTime(toFilePath, getModifiedTime(fromFilePath)) } -private object JavaMilli extends MilliMilliseconds { - def getModifiedTime(filePath: String): Long = - mapNoSuchFileException(Files.getLastModifiedTime(JPaths.get(filePath)).toMillis) - def setModifiedTime(filePath: String, mtime: Long): Unit = - mapNoSuchFileException { - Files.setLastModifiedTime(JPaths.get(filePath), FileTime.fromMillis(mtime)) - () - } - - private def mapNoSuchFileException[A](f: => A): A = - try { - f - } catch { - case e: NoSuchFileException => throw new FileNotFoundException(e.getFile).initCause(e) - } -} - object Milli { import Platform._ diff --git a/io/src/main/scala/sbt/io/JavaMilli.scala b/io/src/main/scala/sbt/io/JavaMilli.scala new file mode 100644 index 00000000..3a35d298 --- /dev/null +++ b/io/src/main/scala/sbt/io/JavaMilli.scala @@ -0,0 +1,26 @@ +package sbt.io + +import java.io.FileNotFoundException +import java.nio.file.{ Files, NoSuchFileException } +import java.nio.file.{ Paths => JPaths } +import java.nio.file.attribute.FileTime + +import sbt.internal.io.MilliMilliseconds + +object JavaMilli extends MilliMilliseconds { + def getModifiedTime(filePath: String): Long = + mapNoSuchFileException(Files.getLastModifiedTime(JPaths.get(filePath)).toMillis) + + def setModifiedTime(filePath: String, mtime: Long): Unit = + mapNoSuchFileException { + Files.setLastModifiedTime(JPaths.get(filePath), FileTime.fromMillis(mtime)) + () + } + + private def mapNoSuchFileException[A](f: => A): A = + try { + f + } catch { + case e: NoSuchFileException => throw new FileNotFoundException(e.getFile).initCause(e) + } +} diff --git a/io/src/test/scala/sbt/internal/io/JavaMilliSpec.scala b/io/src/test/scala/sbt/internal/io/JavaMilliSpec.scala new file mode 100644 index 00000000..b6c50c23 --- /dev/null +++ b/io/src/test/scala/sbt/internal/io/JavaMilliSpec.scala @@ -0,0 +1,8 @@ +package sbt.io + +import org.scalatest.FlatSpec + +final class JavaMilliSpec extends FlatSpec { + "JavaMilli" should "be exposed to the sbt.io package" in + assertCompiles("""sbt.io.JavaMilli.getModifiedTime("/tmp")""") +}