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
284
285
286
287
288
289
290
|
/* __ *\
** ________ ___ / / ___ Scala Ant Tasks **
** / __/ __// _ | / / / _ | (c) 2005-2009, LAMP/EPFL **
** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ **
** /____/\___/_/ |_/____/_/ | | **
** |/ **
\* */
// $Id$
package scala.tools.ant
import java.io.{File, InputStream, FileWriter}
import org.apache.tools.ant.BuildException
import org.apache.tools.ant.taskdefs.MatchingTask
import org.apache.tools.ant.types.{Path, Reference}
/** <p>
* An Ant task that generates a shell or batch script to execute a
* Scala program.
* This task can take the following parameters as attributes:
* </p><ul>
* <li>file (mandatory),</li>
* <li>class (mandatory),</li>
* <li>platforms,</li>
* <li>classpath,</li>
* <li>properties,</li>
* <li>javaflags,</li>
* <li>toolflags.</li></ul>
*
* @author Gilles Dubochet
* @version 1.1
*/
class ScalaTool extends MatchingTask {
private def emptyPath = new Path(getProject)
/*============================================================================*\
** Ant user-properties **
\*============================================================================*/
abstract class PermissibleValue {
val values: List[String]
def isPermissible(value: String): Boolean =
(value == "") || values.exists(_.startsWith(value))
}
/** Defines valid values for the platforms property. */
object Platforms extends PermissibleValue {
val values = List("unix", "windows")
}
/** The path to the exec script file. ".bat" will be appended for the
* Windows BAT file, if generated. */
private var file: Option[File] = None
/** The main class to run. */
private var mainClass: Option[String] = None
/** Supported platforms for the script. Either "unix" or "windows".
* Defaults to both. */
private var platforms: List[String] = List("unix", "windows")
/** An (optional) path to all JARs that this script depend on. Paths must be
* relative to the scala home directory. If not set, all JAR archives and
* folders in "lib/" are automatically added. */
private var classpath: List[String] = Nil
/** An (optional) path to JARs that this script depends on relative to the
* ant project's basedir. */
private var classpathPath: Path = emptyPath
/** Comma-separated Java system properties to pass to the JRE. Properties
* are formated as name=value. Properties scala.home, scala.tool.name and
* scala.tool.version are always set. */
private var properties: List[(String, String)] = Nil
/** Additional flags passed to the JRE ("java [javaFlags] class"). */
private var javaFlags: String = ""
/** Additional flags passed to the tool ("java class [toolFlags]"). Can only
* be set when a main class is defined. */
private var toolFlags: String = ""
/*============================================================================*\
** Properties setters **
\*============================================================================*/
/** Sets the file attribute. */
def setFile(input: File) =
file = Some(input)
/** Sets the main class attribute. */
def setClass(input: String) =
mainClass = Some(input)
/** Sets the platforms attribute. */
def setPlatforms(input: String) = {
platforms = input.split(",").toList.flatMap { s: String =>
val st = s.trim
if (Platforms.isPermissible(st))
(if (input != "") List(st) else Nil)
else {
error("Platform " + st + " does not exist.")
Nil
}
}
}
/**
* Sets the classpath with which to run the tool.
* Note that this mechanism of setting the classpath is generally preferred
* for general purpose scripts, as this does not assume all elements are
* relative to the ant basedir. Additionally, the platform specific demarcation
* of any script variables (e.g. ${SCALA_HOME} or %SCALA_HOME%) can be specified
* in a platform independant way (e.g. @SCALA_HOME@) and automatically translated
* for you.
*/
def setClassPath(input: String): Unit = {
classpath = classpath ::: input.split(",").toList
}
/**
* A special method that allows ant classpath path definitions to be nested
* within this ant task.
*/
def createClassPath: Path = classpathPath.createPath()
/**
* Adds an Ant Path reference to the tool's classpath.
* Note that all entries in the path must exist either relative to the project
* basedir or with an absolute path to a file in the filesystem. As a result,
* this is not a mechanism for setting the classpath for more general use scripts,
* such as those distributed within sbaz distribution packages.
*/
def setClassPathRef(input: Reference): Unit = {
val tmpPath = emptyPath
tmpPath.setRefid(input)
classpath = classpath ::: tmpPath.list.toList
}
/** Sets JVM properties that will be set whilst running the tool. */
def setProperties(input: String) = {
properties = input.split(",").toList.flatMap { s: String =>
val st = s.trim
val stArray = st.split("=", 2)
if (stArray.length == 2) {
if (input != "") List(Pair(stArray(0), stArray(1))) else Nil
}
else
error("Property " + st + " is not formatted properly.")
}
}
/** Sets flags to be passed to the Java interpreter. */
def setJavaflags(input: String) =
javaFlags = input.trim
/** Sets flags to be passed to the tool. */
def setToolflags(input: String) =
toolFlags = input.trim
/*============================================================================*\
** Properties getters **
\*============================================================================*/
/** Gets the value of the classpath attribute in a Scala-friendly form.
* @returns The class path as a list of files. */
private def getUnixclasspath: String =
transposeVariableMarkup(classpath.mkString("", ":", "").replace('\\', '/'), "${", "}")
/** Gets the value of the classpath attribute in a Scala-friendly form.
* @returns The class path as a list of files. */
private def getWinclasspath: String =
transposeVariableMarkup(classpath.mkString("", ";", "").replace('/', '\\'), "%", "%")
private def getProperties: String =
properties.map({
case Pair(name,value) => "-D" + name + "=\"" + value + "\""
}).mkString("", " ", "")
/*============================================================================*\
** Compilation and support methods **
\*============================================================================*/
/** Generates a build error. Error location will be the current task in the
* ant file.
* @param message A message describing the error.
* @throws BuildException A build error exception thrown in every case. */
private def error(message: String): Nothing =
throw new BuildException(message, getLocation())
// XXX encoding and generalize
private def getResourceAsCharStream(clazz: Class[_], resource: String): Stream[Char] = {
val stream = clazz.getClassLoader() getResourceAsStream resource
if (stream == null) Stream.empty
else Stream continually stream.read() takeWhile (_ != -1) map (_.asInstanceOf[Char])
}
// Converts a variable like @SCALA_HOME@ to ${SCALA_HOME} when pre = "${" and post = "}"
private def transposeVariableMarkup(text: String, pre: String, post: String) : String = {
val chars = scala.io.Source.fromString(text)
val builder = new StringBuilder()
while (chars.hasNext) {
val char = chars.next
if (char == '@') {
var char = chars.next
val token = new StringBuilder()
while (chars.hasNext && char != '@') {
token.append(char)
char = chars.next
}
if (token.toString == "")
builder.append('@')
else
builder.append(pre + token.toString + post)
} else builder.append(char)
}
builder.toString
}
private def readAndPatchResource(resource: String, tokens: Map[String, String]): String = {
val chars = getResourceAsCharStream(this.getClass, resource).iterator
val builder = new StringBuilder()
while (chars.hasNext) {
val char = chars.next
if (char == '@') {
var char = chars.next
val token = new StringBuilder()
while (chars.hasNext && char != '@') {
token.append(char)
char = chars.next
}
if (tokens.contains(token.toString))
builder.append(tokens(token.toString))
else if (token.toString == "")
builder.append('@')
else
builder.append("@" + token.toString + "@")
} else builder.append(char)
}
builder.toString
}
private def writeFile(file: File, content: String) =
if (file.exists() && !file.canWrite())
error("File " + file + " is not writable")
else {
val writer = new FileWriter(file, false)
writer.write(content)
writer.close()
}
/*============================================================================*\
** The big execute method **
\*============================================================================*/
/** Performs the tool creation. */
override def execute() = {
// Tests if all mandatory attributes are set and valid.
if (file.isEmpty) error("Attribute 'file' is not set.")
if (mainClass.isEmpty) error("Main class must be set.")
val resourceRoot = "scala/tools/ant/templates/"
val patches = Map (
("class", mainClass.get),
("properties", getProperties),
("javaflags", javaFlags),
("toolflags", toolFlags)
)
// Consolidate Paths into classpath
classpath = classpath ::: classpathPath.list.toList
// Generate the scripts
if (platforms.contains("unix")) {
val unixPatches = patches + (("classpath", getUnixclasspath))
val unixTemplateResource = resourceRoot + "tool-unix.tmpl"
val unixTemplate = readAndPatchResource(unixTemplateResource, unixPatches)
writeFile(file.get, unixTemplate)
}
if (platforms.contains("windows")) {
val winPatches = patches + (("classpath", getWinclasspath))
val winTemplateResource = resourceRoot + "tool-windows.tmpl"
val winTemplate = readAndPatchResource(winTemplateResource, winPatches)
writeFile(new File(file.get.getAbsolutePath() + ".bat"), winTemplate)
}
}
}
|