/
Build.scala
119 lines (105 loc) · 3.88 KB
/
Build.scala
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import sbt._
import Keys._
import Load.{ BuildStructure, StructureIndex }
import scala.collection.{ mutable, immutable }
trait SbtCreateConfig {
def name: String
def organization: String
def version: String
def scalaVersion: String
}
object SbtCreateConfig {
private def prop(propName: String, alt: String) = System.getProperty(propName) match {
case null => alt
case value => value
}
implicit def defaultProjectConfig = new SbtCreateConfig {
def name = prop("sbt-create.name", "project-name-here")
def organization = prop("sbt-create.organization", "your.organization.here")
def version = prop("sbt-create.version", "0.1-SNAPSHOT")
def scalaVersion = prop("sbt.scala.version", "2.9.0-1")
}
}
class TemplateBuild(implicit sbtCreateConfig: SbtCreateConfig) extends Build {
// BuildStructure contains:
// units: Map[URI, LoadedBuildUnit]
// root: URI
// settings: Seq[Setting[_]]
// data: Settings[Scope]
// index: StructureIndex
// streams: Streams
// delegates: Scope => Seq[Scope]
// scopeLocal: ScopeLocal
private val cachedExtraction = new collection.mutable.HashMap[State, WState]
private implicit def stateWrapper(state: State): WState =
cachedExtraction.getOrElseUpdate(state, new WState(state))
private implicit def revealStructure(state: State): BuildStructure =
stateWrapper(state).structure
private implicit def revealStructureIndex(state: State): StructureIndex =
revealStructure(state).index
private implicit def revealSession(state: State): SessionSettings =
stateWrapper(state).session
private class WState(state: State) {
val extracted = Project extract state
def projectId = extracted.currentProject.id
def structure = extracted.structure
def session = extracted.session
def currentRef = extracted.currentRef
def rootProject = structure.rootProject
def allProjects = structure.allProjects
def index = structure.index
def taskToKey = index.taskToKey
def keyMap = index.keyMap // Map[String, AttributeKey[_]]
def keyIndex = index.keyIndex
def currentKeys = keyIndex keys Some(currentRef) map index.keyMap
def sortedKeys = currentKeys.toSeq sortBy (_.label)
}
private class Tap[T](target: T) {
def show(): Unit = target match {
case xs: TraversableOnce[_] => xs foreach println
case _ => println(target)
}
def tap[U](f: T => U): T = {
f(target)
target
}
}
private implicit def createTapper[T](target: T): Tap[T] = new Tap(target)
def currentBranch = ("git status -sb".lines_! headOption) getOrElse "-" stripPrefix "## "
val buildShellPrompt = {
(state: State) => "%s:%s>".format(
state.projectId,
currentBranch
)
}
lazy val testSettings = Seq(
libraryDependencies ++= Seq(
"org.scala-tools.testing" % "specs_2.9.0-1" % "1.6.8" % "test"
),
libraryDependencies <+= (scalaVersion)(v => "org.scala-lang" % "scala-compiler" % v)
)
lazy val buildSettings = Seq(
resolvers += ScalaToolsSnapshots,
organization := sbtCreateConfig.organization,
version := sbtCreateConfig.version,
scalaVersion := sbtCreateConfig.scalaVersion,
// retrieveManaged := true,
shellPrompt := buildShellPrompt
// logLevel := Level.Debug,
)
lazy val templateConfig = Project(
id = sbtCreateConfig.name,
base = file("."),
aggregate = Nil,
dependencies = Nil,
delegates = Nil,
settings = Defaults.defaultSettings ++ buildSettings ++ testSettings ++ Seq(
commands += helpNames
)
)
// A sample command definition.
def helpNames = Command.command("help-names") { (state: State) =>
state tap (_.sortedKeys map (_.label) show)
}
}
object TemplateBuild extends TemplateBuild { }