Skip to content

Commit

Permalink
Merge pull request #139 from dwijnand/expose-JavaMilli
Browse files Browse the repository at this point in the history
Expose JavaMilli to the whole sbt package
  • Loading branch information
dwijnand authored Apr 6, 2018
2 parents fbd1d48 + d03bb12 commit b35ee7d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
4 changes: 4 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
),
)
26 changes: 3 additions & 23 deletions io/src/main/scala/sbt/internal/io/Milli.scala
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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._

Expand Down
26 changes: 26 additions & 0 deletions io/src/main/scala/sbt/io/JavaMilli.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
8 changes: 8 additions & 0 deletions io/src/test/scala/sbt/internal/io/JavaMilliSpec.scala
Original file line number Diff line number Diff line change
@@ -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")""")
}

0 comments on commit b35ee7d

Please sign in to comment.