Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 64 lines (62 sloc) 2.145 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/* sbt -- Simple Build Tool
* Copyright 2009, 2010 Mark Harrah
*/
package xsbt.boot

import Pre._
import java.io.{File, FileInputStream, FileOutputStream}
import java.util.Properties
import scala.collection.immutable.List

object Initialize
{
lazy val selectCreate = (_: AppProperty).create
lazy val selectQuick = (_: AppProperty).quick
lazy val selectFill = (_: AppProperty).fill
def create(file: File, promptCreate: String, enableQuick: Boolean, spec: List[AppProperty])
{
SimpleReader.readLine(promptCreate + " (y/N" + (if(enableQuick) "/s" else "") + ") ") match
{
case None => declined("")
case Some(line) =>
line.toLowerCase match
{
case "y" | "yes" => process(file, spec, selectCreate)
case "s" => process(file, spec, selectQuick)
case "n" | "no" | "" => declined("")
case x =>
System.out.println(" '" + x + "' not understood.")
create(file, promptCreate, enableQuick, spec)
}
}
}
def fill(file: File, spec: List[AppProperty]): Unit = process(file, spec, selectFill)
def process(file: File, appProperties: List[AppProperty], select: AppProperty => Option[PropertyInit])
{
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.store(out, "") )
}
}
def initialize(properties: Properties, name: String, init: PropertyInit)
{
init match
{
case set: SetProperty => properties.setProperty(name, set.value)
case prompt: PromptProperty =>
def noValue = declined("No value provided for " + prompt.label)
SimpleReader.readLine(prompt.label + prompt.default.toList.map(" [" + _ + "]").mkString + ": ") match
{
case None => noValue
case Some(line) =>
val value = if(isEmpty(line)) orElse(prompt.default, noValue) else line
properties.setProperty(name, value)
}
}
}
}
Something went wrong with that request. Please try again.