Skip to content

Commit

Permalink
- --predef is now --predef-code and --predef-file is now `--pre…
Browse files Browse the repository at this point in the history
…def`, to reflect the more common use case of specifying a predef file rather than some predef code snippet

- `ammonite.Main`'s `predef` argument has also become `predefCode`, and it has grown a new `predefFile` arg

- `--predef` now always is *in addition to* the predef in `~/.ammonite/predef.sc`. You can use `--no-home-predef` to disable `~/.ammonite/predef.sc` instead.

- `~/.ammonite/predefShared.sc` is now gone; if you want to share code between `predef.sc` and `predefScript.sc`, you can `import $exec` a third script from both of those
  • Loading branch information
lihaoyi committed Jun 18, 2017
1 parent adba7cd commit de15081
Show file tree
Hide file tree
Showing 16 changed files with 272 additions and 134 deletions.
7 changes: 3 additions & 4 deletions amm/interp/src/main/scala/ammonite/interp/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import ammonite.util._
*/
class Interpreter(val printer: Printer,
val storage: Storage,
basePredefs: Seq[PredefInfo],
customPredefs: Seq[PredefInfo],
// Allows you to set up additional "bridges" between the REPL
// world and the outside world, by passing in the full name
Expand Down Expand Up @@ -93,6 +94,7 @@ class Interpreter(val printer: Printer,
interpApi,
evalClassloader,
storage,
basePredefs,
customPredefs,
// AutoImport is false, because we do not want the predef imports to get
// bundled into the main `eval.imports`: instead we pass `predefImports`
Expand All @@ -112,10 +114,7 @@ class Interpreter(val printer: Printer,

// The ReplAPI requires some special post-Interpreter-initialization
// code to run, so let it pass it in a callback and we'll run it here
def watch(p: Path) = {
new Exception().printStackTrace()
watchedFiles.append(p -> Interpreter.pathSignature(p))
}
def watch(p: Path) = watchedFiles.append(p -> Interpreter.pathSignature(p))

def resolveSingleImportHook(source: CodeSource, tree: ImportTree) = synchronized{
val strippedPrefix = tree.prefix.takeWhile(_(0) == '$').map(_.stripPrefix("$"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ object PredefInitialization {
interpApi: InterpAPI,
evalClassloader: SpecialClassLoader,
storage: Storage,
basePredefs: Seq[PredefInfo],
customPredefs: Seq[PredefInfo],
processModule: (String, CodeSource, Boolean) => Res[Metadata],
addImports: Imports => Unit,
Expand All @@ -37,19 +38,19 @@ object PredefInitialization {
)

val predefs = {
bridgePredefs ++ customPredefs ++
(storage.loadSharedPredef ++ storage.loadPredef).map{
bridgePredefs ++
basePredefs ++
storage.loadPredef.map{
case (code, path) =>
PredefInfo(Name(path.last.stripSuffix(".sc")), code, false, Some(path))
}
} ++
customPredefs
}

predefs.filter(_.code.nonEmpty)


Res.fold((), predefs){(_, predefInfo) =>
pprint.log(predefInfo.name)
pprint.log(predefInfo.path)
predefInfo.path.foreach(watch)
if (predefInfo.code.isEmpty) Res.Success(())
else {
Expand Down
11 changes: 4 additions & 7 deletions amm/repl/src/main/scala/ammonite/repl/Repl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class Repl(input: InputStream,
output: OutputStream,
error: OutputStream,
storage: Storage,
defaultPredef: String,
mainPredef: String,
basePredefs: Seq[PredefInfo],
customPredefs: Seq[PredefInfo],
wd: ammonite.ops.Path,
welcomeBanner: Option[String],
replArgs: IndexedSeq[Bind[_]] = Vector.empty,
Expand Down Expand Up @@ -58,11 +58,8 @@ class Repl(input: InputStream,
val interp: Interpreter = new Interpreter(
printer,
storage,
Seq(
PredefInfo(Name("DefaultPredef"), defaultPredef, true, None),
PredefInfo(Name("ArgsPredef"), argString, false, None),
PredefInfo(Name("MainPredef"), mainPredef, false, None)
),
basePredefs,
customPredefs,
Seq((
"ammonite.repl.ReplBridge",
"repl",
Expand Down
8 changes: 0 additions & 8 deletions amm/runtime/src/main/scala/ammonite/runtime/Storage.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import scala.reflect.NameTransformer.encode
*/
trait Storage{
def loadPredef: Option[(String, Path)]
def loadSharedPredef: Option[(String, Path)]
val fullHistory: StableRef[History]
val ivyCache: StableRef[Storage.IvyMap]
def compileCacheSave(path: String, tag: Tag, data: Storage.CompileCache): Unit
Expand Down Expand Up @@ -55,7 +54,6 @@ object Storage{
var predef = ""
var sharedPredef = ""
def loadPredef = None
def loadSharedPredef = None
def getSessionId = 0L
var _history = new History(Vector())
val fullHistory = new StableRef[History]{
Expand Down Expand Up @@ -105,7 +103,6 @@ object Storage{

class Folder(val dir: Path, isRepl: Boolean = true) extends Storage{
def predef = if (isRepl) dir/"predef.sc" else dir/"predefScript.sc"
def predefShared = dir/"predefShared.sc"
// Each version puts its cache in a separate folder, to bust caches
// on every version bump; otherwise binary-incompatible changes to
// ReplAPI/Preprocessor/ammonite-ops will cause scripts to fail after
Expand Down Expand Up @@ -247,11 +244,6 @@ object Storage{
try Some((read(predef), predef))
catch { case e: java.nio.file.NoSuchFileException => None }
}

def loadSharedPredef = {
try Some((read(predefShared), predefShared))
catch {case e: java.nio.file.NoSuchFileException => None }
}
}
}

Expand Down
Loading

0 comments on commit de15081

Please sign in to comment.