-
Notifications
You must be signed in to change notification settings - Fork 5
/
Helper.scala
283 lines (217 loc) · 7.88 KB
/
Helper.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import collection.mutable.ListBuffer
import java.io._
import org.fusesource.scalate.{TemplateEngine, DefaultRenderContext, Binding}
import org.fusesource.scalate.support.FileResourceLoader
import org.fusesource.scalate.util.IOUtil._
trait SCM {
def url:String
def web_url:Option[String]
}
class Git(var url:String, var web_url: Option[String] = None, var branches: List[String] = List("master")) extends SCM {
def branches(values:String*): this.type = { branches = List(values: _*); this}
def web_url(value:Option[String]): this.type = { web_url = value; this}
}
class GitHub(var user:String, var project:String) extends Git("git://github.com/"+user+"/"+project+".git", Some("http://github.com/"+user+"/"+project+""))
class Subversion(val url:String, val local:String) extends SCM {
var web_url:Option[String] = Some(url)
def web_url(value:Option[String]): this.type = { web_url = value; this}
}
case class MavenOptions(var goals: List[String] = Nil, var profiles: List[String] = Nil, var name: String = "maven-2.2.1", var rootPom : String = "pom.xml") {
/**
* Returns the command line argument for the given goals and profiles
*/
def goalsArguments = text(goals, " ") + goals.mkString(" ") + text(profiles, " -P") + profiles.mkString(",")
protected def text[T](t: Traversable[T], notEmpty: String, empty: String = "") = if (t.isEmpty) empty else notEmpty
}
case class Parameter(name: String, description:String="", value:String="", kind:String="hudson.model.StringParameterDefinition")
case class IrcNotify(room: String)
case class JUnitPublisher(testResults: String = "**/target/*-reports/*.xml")
case class Build(name: String) {
var template: String = name + ".jade"
def template(value: String): this.type = { template = value; this }
var maven = new MavenOptions()
def maven(value: MavenOptions): this.type = { maven = value; this }
var timeout = 60
def timeout(value: Int): this.type = {timeout = value; this}
var parameters = List[Parameter]()
def parameters(values: Parameter*): this.type = {parameters = List(values: _*); this}
var ircs = List[IrcNotify]()
def ircs(values: IrcNotify*): this.type = {ircs = List(values: _*); this}
var junitPublisher: Option[JUnitPublisher] = None
def junitPublisher(v: JUnitPublisher): this.type = {junitPublisher = Some(v); this}
}
case class Project(val name:String, val scm:SCM) {
var disabled = false
def disable: Unit = {
disabled = true
}
var jdks = List("jdk6")
def jdks(values:String*): this.type = { jdks = List(values: _*); this}
var jvmOpts = "-Xmx1G -XX:MaxPermSize=256m"
var labels = List("ubuntu", "windows")
def labels(values:String*): this.type = { labels = List(values: _*); this}
def timeout(value:Int): this.type = {
checkin.timeout(value)
platform.timeout(value)
perfectus_tests.timeout(value)
this
}
def git(proc: (Git)=>Unit): this.type = { proc(scm.asInstanceOf[Git]); this }
def git = scm.asInstanceOf[Git]
// Helper methods
/**
* Sets the maven name for all the builds
*/
def mavenName(name: String): this.type = {
for (b <- builds) {
b.maven.name = name
}
this
}
/**
* Lets the project be configured using a block which avoids global variables
* needing to be defined for each project, instead its a local scoped parameter
*/
def using(f: Project => Unit): this.type = {
f(this)
this
}
/**
* Adds IRC notifications to the given list of builds
*/
def ircNotify(i: IrcNotify, builds: Build*): Unit = {
for (b <- builds) {
b.ircs(i)
}
}
// builds
val checkin = Build("checkin").junitPublisher(JUnitPublisher())
val platform = Build("platform").junitPublisher(JUnitPublisher())
val deploy = Build("deploy").timeout(30) // we avoid taking the full build timeout value as the default
val dualjdk = Build("dualjdk").junitPublisher(JUnitPublisher())
val perfectus_tests = Build("perfectus-tests").
parameters(Parameter("TAG", "tag or branch to execute against")).
junitPublisher(JUnitPublisher())
var builds: List[Build] = List(checkin, platform, deploy)
def addBuild(b: Build): this.type = {
builds = b :: builds
this
}
def removeBuild(b: Build): this.type = {
builds = builds filterNot(_ == b)
this
}
def removeBuild(f: Project => Build): this.type =
removeBuild(f(this))
def removeBuilds(list: List[Build]): this.type = {
builds = builds filterNot(list contains)
this
}
def removeBuilds(f: Project => List[Build]): this.type =
removeBuilds(f(this))
}
object Helper {
def with_trailing_slash(value:String) = {
if( value.endsWith("/") ) {
value
} else {
value + "/"
}
}
}
abstract class Helper {
var jobs_dir:File = _
val projects = ListBuffer[Project]()
def add(project: Project): Project = {
projects += project
project
}
/**
* Configures the model
*/
protected def configure: Unit
/**
* Runs the command line shell
*/
def main(args: Array[String]): Unit = {
// TODO should we use Karaf annotations for this stuff?
if (args.length < 1) {
println("Expected the jobs directory as an argument")
System.exit(1)
}
jobs_dir = new File(args(0))
configure
generate
}
/////////////////////////////////////////////////////////////////////
// Config Generators
/////////////////////////////////////////////////////////////////////
def subversion(project:String, url:String) = {
add(Project(project, new Subversion(url, project)))
}
def defaultIrcNotify(p: Project) = {
for (b <- List(p.checkin, p.deploy)) {
if (b.ircs.isEmpty) {
b.ircs(IrcNotify("fuseforge"))
}
}
p
}
def github(user:String, project:String) = {
val p = add(Project(project, new GitHub(user, project)))
defaultIrcNotify(p)
}
def forge_git(project:String) = {
val p = add(new Project(project, new Git("ssh://git@forge.fusesource.com/"+project+".git")))
defaultIrcNotify(p)
}
def generate(): Unit = {
for (project <- projects) {
generate(project)
}
}
def generate(project:Project): Unit = {
for (b <- project.builds) {
build(project, b)
}
}
def build(project:Project, build: Build): Unit = {
job(project.name + "-" + build.name, render(build.template, Map("project" -> project, "build" -> build)))
}
def perfectus(name:String, p:Project) = {
var g = p.git
g = new Git(g.url, g.web_url, List("${TAG}"))
val project = new Project(name, g)
project.perfectus_tests.maven = p.perfectus_tests.maven
project.timeout(p.platform.timeout)
project.builds = List(project.perfectus_tests)
add(project)
}
/////////////////////////////////////////////////////////////////////
// IO Helpers
/////////////////////////////////////////////////////////////////////
private def job(name:String, config:String) = {
write(jobs_dir/name/"config.xml", config)
}
private def write(file:File, value:Any) = {
file.getParentFile.mkdirs
writeText(file, value.toString)
}
/////////////////////////////////////////////////////////////////////
// Scalate Helpers
/////////////////////////////////////////////////////////////////////
org.fusesource.scalate.scaml.ScamlOptions.autoclose = null
private val engine = new TemplateEngine
engine.resourceLoader = new FileResourceLoader(Some(new File("./src/main/template")))
engine.workingDirectory = new File("./target/scalate")
engine.bindings ++= List(Binding("val project: Project"), Binding("var build: Build"))
private def render( template:String, attributes: Map[String,Any]) = {
val buffer = new StringWriter()
val context = new DefaultRenderContext(null, engine, new PrintWriter(buffer))
for ((key, value) <- attributes) {
context.attributes(key) = value
}
context.include(template, false)
buffer.toString
}
}