Skip to content

Commit

Permalink
Don't pass Storage to CompilerLifecycleManager
Browse files Browse the repository at this point in the history
That makes it easier later on to extract an interface for CompilerLifecycleManager
  • Loading branch information
alexarchambault committed Jan 14, 2021
1 parent 593cff2 commit 00b3664
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import ammonite.runtime._
import ammonite.util.Util._
import ammonite.util._

import java.nio.file.Path

import scala.collection.mutable
import scala.reflect.io.VirtualDirectory
import scala.tools.nsc.Settings
Expand All @@ -21,7 +23,7 @@ import scala.tools.nsc.Settings
* than necessary
*/
class CompilerLifecycleManager(
storage: Storage,
rtCacheDir: Option[Path],
headFrame: => Frame,
dependencyCompleteOpt: => Option[String => (Int, Seq[String])],
classPathWhitelist: Set[Seq[String]],
Expand Down Expand Up @@ -82,9 +84,9 @@ class CompilerLifecycleManager(
val settings = Option(compiler).fold(new Settings)(_.compiler.settings.copy)
onSettingsInit.foreach(_(settings))

val initialClassPath = Classpath.classpath(initialClassLoader, storage)
val initialClassPath = Classpath.classpath(initialClassLoader, rtCacheDir)
val headFrameClassPath =
Classpath.classpath(headFrame.classloader, storage)
Classpath.classpath(headFrame.classloader, rtCacheDir)

Internal.compiler = Compiler(
headFrameClassPath,
Expand All @@ -103,13 +105,13 @@ class CompilerLifecycleManager(
// Pressy is lazy, so the actual presentation compiler won't get instantiated
// & initialized until one of the methods on it is actually used
Internal.pressy = Pressy(
Classpath.classpath(headFrame.classloader, storage),
headFrameClassPath,
dynamicClasspath,
headFrame.classloader,
settings.copy(),
dependencyCompleteOpt,
classPathWhitelist,
Classpath.classpath(initialClassLoader, storage)
initialClassPath
)

Internal.preConfiguredSettingsChanged = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Interpreter(val printer: Printer,
IvyThing.completer(repositories(), verbose = verboseOutput)

val compilerManager = new CompilerLifecycleManager(
storage,
storage.dirOpt.map(_.toNIO),
headFrame,
Some(dependencyComplete),
classPathWhitelist,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class AmmoniteBuildServer(
classOf[InterpAPI].getClassLoader
else
Thread.currentThread().getContextClassLoader
private def initialClassPath = Classpath.classpath(initialClassLoader, storage)
.map(_.toURI)
private def initialClassPath =
Classpath.classpath(initialClassLoader, storage.dirOpt.map(_.toNIO)).map(_.toURI)

private lazy val proc =
withRoot { root =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ class SingleScriptCompiler(
)
}

val initialClassPath = Classpath.classpath(initialClassLoader, storage)
val classPath = Classpath.classpath(frame.classloader, storage)
val initialClassPath = Classpath.classpath(
initialClassLoader,
storage.dirOpt.map(_.toNIO)
)
val classPath = Classpath.classpath(
frame.classloader,
storage.dirOpt.map(_.toNIO)
)

AmmCompiler(
classPath,
Expand Down
23 changes: 11 additions & 12 deletions amm/runtime/src/main/scala/ammonite/runtime/Classpath.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ammonite.runtime

import java.io.File
import java.net.URL
import java.nio.file.{Path, Paths}
import java.util.zip.{ZipFile, ZipInputStream}


Expand All @@ -25,16 +26,14 @@ object Classpath {
* memory but is better than reaching all over the filesystem every time we
* want to do something.
*/
def classpath(classLoader: ClassLoader, storage: Storage): Vector[URL] = {
def rtCacheDir(storage: Storage): Option[os.Path] = storage match {
case storage: Storage.Folder =>
// no need to cache if the storage is in tmpdir
// because it is temporary
if (storage.dir.wrapped.startsWith(
java.nio.file.Paths.get(System.getProperty("java.io.tmpdir"))))
None
else Some(storage.dir)
case _ => None
def classpath(
classLoader: ClassLoader,
rtCacheDir: Option[Path]
): Vector[URL] = {
lazy val actualRTCacheDir = rtCacheDir.filter { dir =>
// no need to cache if the storage is in tmpdir
// because it is temporary
!dir.startsWith(Paths.get(System.getProperty("java.io.tmpdir")))
}

var current = classLoader
Expand Down Expand Up @@ -73,8 +72,8 @@ object Classpath {
.loadClass("javax.script.ScriptEngineManager")
} catch {
case _: ClassNotFoundException =>
rtCacheDir(storage) match {
case Some(path) => files.append(Export.rtAt(path.toIO).toURI.toURL)
actualRTCacheDir match {
case Some(path) => files.append(Export.rtAt(path.toFile).toURI.toURL)
case _ => files.append(Export.rt().toURI.toURL)
}
}
Expand Down
4 changes: 4 additions & 0 deletions amm/runtime/src/main/scala/ammonite/runtime/Storage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ trait Storage{
tag: Tag): Unit
def classFilesListLoad(filePathPrefix: os.SubPath, tag: Tag): Option[ScriptOutput]
def getSessionId: Long

def dirOpt: Option[os.Path] = None
}

object Storage{
Expand Down Expand Up @@ -339,5 +341,7 @@ object Storage{
try Some((os.read(predef), predef))
catch { case e: java.nio.file.NoSuchFileException => Some(("", predef))}
}

override def dirOpt: Option[os.Path] = Some(dir)
}
}

0 comments on commit 00b3664

Please sign in to comment.