summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/cmd/Property.scala
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2010-04-11 00:32:00 +0000
committerPaul Phillips <paulp@improving.org>2010-04-11 00:32:00 +0000
commite93c1a93a2c8a40265b34bb9f1dd61b9470c908d (patch)
treeb9f13ffbe1d1ef59380e32f74380f9797b7fd2cb /src/compiler/scala/tools/cmd/Property.scala
parent71b6aca681ab697304590a96b13847b9bba141dc (diff)
downloadscala-e93c1a93a2c8a40265b34bb9f1dd61b9470c908d.tar.gz
scala-e93c1a93a2c8a40265b34bb9f1dd61b9470c908d.tar.bz2
scala-e93c1a93a2c8a40265b34bb9f1dd61b9470c908d.zip
Introduces scala.tools.cmd providing command li...
Introduces scala.tools.cmd providing command line tool infrastructure. For a quick look at what can be done, see scala.tools.cmd.Demo For a more involved, potentially eye-straining look, see scala.tools.partest.PartestSpec To experience it through the eyes of Joe Partest User, run test/partest Review by community.
Diffstat (limited to 'src/compiler/scala/tools/cmd/Property.scala')
-rw-r--r--src/compiler/scala/tools/cmd/Property.scala71
1 files changed, 71 insertions, 0 deletions
diff --git a/src/compiler/scala/tools/cmd/Property.scala b/src/compiler/scala/tools/cmd/Property.scala
new file mode 100644
index 0000000000..009e7e6142
--- /dev/null
+++ b/src/compiler/scala/tools/cmd/Property.scala
@@ -0,0 +1,71 @@
+/* NSC -- new Scala compiler
+ * Copyright 2005-2010 LAMP/EPFL
+ * @author Paul Phillips
+ */
+
+package scala.tools
+package cmd
+
+import nsc.io._
+import java.util.Properties
+import java.io.FileInputStream
+
+/** Contains logic for translating a property key/value pair into
+ * equivalent command line arguments. The default settings will
+ * translate, given programInfo.runner == "foo" :
+ *
+ * foo.bar=true to --bar // if --bar is unary
+ * foo.bar=quux to --bar quux // if --bar is binary
+ */
+class PropertyMapper(reference: Reference) extends (((String, String)) => List[String]) {
+ import reference._
+ lazy val RunnerName = programInfo.runner
+
+ // e.g. "partest.shootout" -> "--shootout"
+ def propNameToOptionName(key: String): Option[String] = (key split '.').toList match {
+ case List(RunnerName, name) => Some(name)
+ case _ => None
+ }
+
+ def isPassThrough(key: String): Boolean = false // e.g. "partest.options"
+ def onError(key: String, value: String): Unit = () // called when translate fails
+
+ def translate(key: String, value: String): List[String] = {
+ val opt = toOpt(key)
+
+ if (isUnaryOption(key) && isTrue(value)) List(opt)
+ else if (isBinaryOption(key)) List(opt, value)
+ else returning(Nil)(_ => onError(key, value))
+ }
+ def isTrue(value: String) = List("yes", "on", "true") contains value.toLowerCase
+
+ def apply(kv: (String, String)): List[String] = {
+ val (k, v) = kv
+
+ if (isPassThrough(k)) toArgs(v)
+ else propNameToOptionName(k) match {
+ case Some(optName) => translate(optName, v)
+ case _ => Nil
+ }
+ }
+}
+
+trait Property extends Reference {
+ def propMapper: PropertyMapper
+ override def propertyArgs: List[String] = systemPropertiesToOptions
+
+ def loadProperties(file: File): Properties =
+ returning(new Properties)(_ load new FileInputStream(file.path))
+
+ def systemPropertiesToOptions: List[String] =
+ propertiesToOptions(System.getProperties)
+
+ def propertiesToOptions(file: File): List[String] =
+ propertiesToOptions(loadProperties(file))
+
+ def propertiesToOptions(props: java.util.Properties): List[String] = {
+ import collection.JavaConversions._
+ propertiesToOptions(props.toList)
+ }
+ def propertiesToOptions(props: List[(String, String)]) = props flatMap propMapper
+}