Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REPL Autoload file #1009

Merged
merged 7 commits into from
Aug 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ tutorial/data/rightDiff.tsv
tutorial/data/tmp3.tsv
tutorial/data/jsonoutput0.tsv
tutorial/data/avrooutput0.avro
.scalding_repl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

package com.twitter.scalding

import scala.tools.nsc.Settings
import java.io.File

import scala.tools.nsc.interpreter.ILoop

/**
Expand All @@ -24,13 +25,17 @@ import scala.tools.nsc.interpreter.ILoop
class ScaldingILoop
extends ILoop {
override def printWelcome() {
echo(" ( \n" +
val fc = Console.YELLOW
val wc = Console.RED
def wrapFlames(s: String) = s.replaceAll("[()]+", fc + "$0" + wc)
echo(fc +
" ( \n" +
" )\\ ) ( ( \n" +
"(()/( ) )\\ )\\ ) ( ( ( \n" +
" /(_)) ( ( /( ((_)(()/( )\\ ( )\\))( \n" +
"(_)) )\\ )(_)) _ ((_)((_) )\\ ) ((_))\\ \n" +
"/ __| ((_)((_)_ | | _| | (_) _(_/( (()(_) \n" +
"\\__ \\/ _| / _` || |/ _` | | || ' \\))/ _` | \n" +
"(_)) )\\ )( )) _ ((_)(( ) )\\ ) (( ))\\ \n".replaceAll("_", wc + "_" + fc) + wc +
wrapFlames("/ __|((_) ((_)_ | | _| | (_) _(_(( (_()_) \n") +
wrapFlames("\\__ \\/ _| / _` || |/ _` | | || ' \\))/ _` \\ \n") +
"|___/\\__| \\__,_||_|\\__,_| |_||_||_| \\__, | \n" +
" |___/ ")
}
Expand All @@ -49,7 +54,17 @@ class ScaldingILoop
*
* @return a prompt string to use for this REPL.
*/
override def prompt: String = "\nscalding> "
override def prompt: String = ScaldingShell.prompt()

/**
* Search for files with the given name in all directories from current directory
* up to root.
*/
private def findAllUpPath(filename: String): List[File] =
Iterator.iterate(System.getProperty("user.dir"))(new File(_).getParent)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you say current directory here, but you are actually getting the user's home dir.

I like the idea of a current directory so I could put some .scalding_repl file in my science directory have that different from my home. Am I missing this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, this property is named kind of confusingly named: "user.dir" is the current working directory (of the user, I guess...)

scala> System.getProperty("user.dir")
res0: java.lang.String = /Users/bholt/dev/hub/scalding

scala> System.getProperty("user.home")
res1: java.lang.String = /Users/bholt

.takeWhile(_ != "/")
.flatMap(new File(_).listFiles.filter(_.toString.endsWith(filename)))
.toList

/**
* Gets the list of commands that this REPL supports.
Expand All @@ -64,6 +79,11 @@ class ScaldingILoop
"com.twitter.scalding._",
"com.twitter.scalding.ReplImplicits._",
"com.twitter.scalding.ReplImplicitContext._")

// interpret all files named ".scalding_repl" from the current directory up to the root
findAllUpPath(".scalding_repl")
.reverse // work down from top level file to more specific ones
.foreach(f => loadCommand(f.toString))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ import com.google.common.io.Files
*/
object ScaldingShell extends MainGenericRunner {

/** Customizable prompt. */
var prompt: () => String = { () => Console.BLUE + "\nscalding> " + Console.RESET }

/**
* An instance of the Scala REPL the user will interact with.
*/
Expand Down
9 changes: 9 additions & 0 deletions scripts/test_repl_tutorial.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ trap onExit ERR
time $SCALD_REPL < tutorial/ReplTutorial1.scala
diff tutorial/data/hello.txt tutorial/data/output1.txt

# Run from inside tutorial directory so we pick up definition
# of 'scaldingReplInitWasLoaded' from 'tutorial/.scalding_repl'
# If it does, then this 'script' exits early with success.
# Otherwise it continues and exits with an error.
cd tutorial; echo "
if (scaldingReplInitWasLoaded) System.exit(0)
System.exit(1)
" | $SCALD_REPL

# restore stty
SCALA_EXIT_STATUS=0
onExit
Expand Down
2 changes: 2 additions & 0 deletions tutorial/.scalding_repl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// for use in testing to verify that '.scalding_repl' files are loaded
val scaldingReplInitWasLoaded = true