sbt/launch/Create.scala

55 lines
1.7 KiB
Scala
Raw Normal View History

2009-10-15 02:53:15 +02:00
package xsbt.boot
2009-10-18 04:40:02 +02:00
import Pre._
2009-10-15 02:53:15 +02:00
import java.io.{File, FileInputStream, FileOutputStream}
import java.util.Properties
object Initialize
{
2009-10-18 04:40:02 +02:00
def create(file: File, promptCreate: String, enableQuick: Boolean, spec: List[AppProperty])
2009-10-15 02:53:15 +02:00
{
SimpleReader.readLine(promptCreate + " (y/N" + (if(enableQuick) "/s" else "") + ") ") match
{
2009-10-18 04:40:02 +02:00
case None => error("")
2009-10-15 02:53:15 +02:00
case Some(line) =>
line.toLowerCase match
{
case "y" | "yes" => process(file, spec, _.create)
2009-10-18 04:40:02 +02:00
case "n" | "no" | "" => error("")
2009-10-15 02:53:15 +02:00
case "s" => process(file, spec, _.quick)
}
}
}
2009-10-18 04:40:02 +02:00
def fill(file: File, spec: List[AppProperty]): Unit = process(file, spec, _.fill)
def process(file: File, appProperties: List[AppProperty], select: AppProperty => Option[PropertyInit])
2009-10-15 02:53:15 +02:00
{
val properties = new Properties
if(file.exists)
Using(new FileInputStream(file))( properties.load )
val uninitialized =
for(property <- appProperties; init <- select(property) if properties.getProperty(property.name) == null) yield
initialize(properties, property.name, init)
if(!uninitialized.isEmpty)
{
file.getParentFile.mkdirs()
Using(new FileOutputStream(file))( out => properties.save(out, "") )
}
2009-10-15 02:53:15 +02:00
}
def initialize(properties: Properties, name: String, init: PropertyInit)
{
init match
{
2009-10-18 04:40:02 +02:00
case set: SetProperty => properties.setProperty(name, set.value)
case prompt: PromptProperty =>
def noValue = error("No value provided for " + prompt.label)
SimpleReader.readLine(prompt.label + prompt.default.toList.map(" [" + _ + "]").mkString + ": ") match
2009-10-15 02:53:15 +02:00
{
case None => noValue
case Some(line) =>
2009-10-18 04:40:02 +02:00
val value = if(isEmpty(line)) prompt.default.getOrElse(noValue) else line
2009-10-15 02:53:15 +02:00
properties.setProperty(name, value)
}
}
}
}