This project contains Ant tasks to run
CoffeeScript
from Ant, and compile CoffeeScript files to JavaScript.
It uses Rhino as the underlying JavaScript engine.
Grab latest files from here Downloads
The example below writes a line to stdout and a line to stderr.
<CoffeeScript>
stdout.println "stdout: hello, world!"
stderr.println "stderr: hello, world!"
</CoffeeScript>
The example below will set the property theResult
to the
string "the result"
<CoffeeScript result="theResult">
"the result"
</CoffeeScript>
When run after the previous command, this example will print the value of previous output.
<CoffeeScript>
<arg value="${theResult}"/>
stdout.println "the previous result was '${argv[0]}'"
</CoffeeScript>
Here's an example that runs CoffeeScript stored in a file.
<CoffeeScript src="test/sample.coffee" result="theResult2">
<arg value="arg #1"/>
<arg value="the second arg"/>
<arg value="finally, the third arg"/>
</CoffeeScript>
Here's some example CoffeeScript that could be stored in that file:
result: []
stdout.println "in $__FILE__"
stdout.println "argv.length: ${argv.length}"
result: for i in [0...argv.length]
stdout.println "argv[${i}]: ${argv[i]}"
argv[i]
result: result.join ", "
stdout.println "the result from running in the script is '${result}'"
result
This example compiles CoffeeScript into JavaScript that puts all files at the test/out directory with no .call() wrapper.
<CoffeeScriptC nowrap="true" destDir="test/out" verbose="true" nesting="false">
<fileset dir="test/src" includes="**/*" />
</CoffeeScriptC>
This example compiles CoffeeScript into JavaScript that puts all files under their child directories test/out/** with no .call() wrapper.
<CoffeeScriptC nowrap="true" destDir="test/out" verbose="true" nesting="true">
<fileset dir="test/src" includes="**/*" />
</CoffeeScriptC>
Three Ant tasks are included:
Runs JavaScript code.
The JavaScript
task supports the following attributes:
-
src
- the name of a file that contains the JavaScript code to runInstead of running a file of JavaScript code, you can place the JavaScript code in the task as text instead. JavaScript code embedded directly in the task will NOT have typical Ant property substitution done on it.
-
result
- the name of a property to place the result of running the JavaScript code in. The result of the JavaScript code is converted to a string and then placed in the property.
The JavaScript
task supports the following nested elements:
-
<arg>
- contains an argument to pass to the JavaScript code. May be used multiple times. Must contain an attribute namedvalue
which is the value to pass to the JavaScript code. Note that this is similar to theExec
task.An
<arg>
element may specify an attribute namedtype
whose value isfile
, in which case the argument is passed to JavaScript as ajava.io.File
object. Thevalue
attribute should contain a file name relative to the Project'sbasedir
attribute.
When the JavaScript code runs, the following variables will be set:
argv
- an array of the values set by the<arg>
elementsstdout
- set toSystem.out
stderr
- set toSystem.err
__FILE__
- set to the name of the scripttask
- set to the Ant task that is being runutil
- set to an object with some useful methods
When your script runs with the JavaScript
or CoffeeScript
tasks,
an additional object is available to your code in the global property
name util
. This object has the following functions:
-
readFile(fileName)
This function will read the specified file and return the contents as a string. The file name is relative to the project's
basedir
. -
writeFile(fileName, contents)
This function will write the specified contents to the specified file. The file name is relative to the project's
basedir
.
Exactly the same as the JavaScript
task, only the source is
treated as CoffeeScript instead of JavaScript.
Compiles CoffeeScript source to JavaScript.
The CoffeeScript
task supports the following attributes:
-
destDir
- the output directory for the JavaScript files. If not specified, uses the "basedir" of the Ant project. -
noWrap
- sets the "noWrap" compile option of the CoffeeScript compiler. Setting totrue
will cause the function wrapper around the output to not be generated. The default isfalse
, which will cause the function wrapper to be generated. (yeah, I hate negative option names too, prolly will change this) -
verbose
- prints a message to the console for every file processed. (defaults to true) -
nesting
- will inherit and maintin directory nested directory structure in the fileset base directories. If set to false will put all files at top level destDir. (defaults to false) -
overwriteNewer
- boolean to force overwrite if js is newer than coffee. (defaults to false)
The CoffeeScript
task supports the typical "fileSet" sort of
nested elements, as near as I can tell. This is how you specify
input files. To generate the name of the output file, the path
of the input file is completely stripped off, and a ".js"
suffix
is added to create the base name. Those files are all written
to the destDir
directory.
You can run the jar file from the command line to print some installation help and version information.
java -jar csat.jar
-
Download a csat-{version}.jar file from https://github.com/ctoestreich/CoffeeScriptAntTasks/tree/master/downloads
or from the original source at
-
Add it to a project in your Eclipse workspace
-
For any Ant scripts you would like to use the tasks in, add the following lines:
<path id="cp"><pathelement path="lib/csat.jar"/></path> <taskdef name="JavaScript" classname="csat.JavaScriptTask" classpathref="cp"/> <taskdef name="CoffeeScript" classname="csat.CoffeeScriptTask" classpathref="cp"/> <taskdef name="CoffeeScriptC" classname="csat.CoffeeScriptCTask" classpathref="cp"/>
Pretty much same as the Eclipse instructions, just don't use Eclipse.
- Make sure jar is added to project/global library and also to your module dependency
To rebuild this jar:
-
checkout the project at github as an Eclipse project
-
run the build/get-libs.xml Ant script to load the required external code
-
refresh the project
-
update src/csat/versions.properties to give CSAT-VERSION a new version label
-
run the build/build-jar.xml Ant script to build the jar
http://github.com/pmuellr/CoffeeScriptAntTasks
MIT license: http://www.opensource.org/licenses/mit-license.php
- Updated to CoffeeScript 1.6.3
- Adding flag for
overwriteNewer
- boolean to force overwrite if js is newer than coffee. (defaults to false) - Adding ant tests for overwrite
- Updated to CoffeeScript 1.4.0
- Changed Task for
CoffeeScriptC
to inherit Task instead of MatchingTask so we can add a boolean flag to inherit directory nesting. The new flag is callednesting
. - Changed
noWrap
to the newbare
param for coffee-script compiler. - Added download directory containing versioned jars.
util.readFile()
was returning an empty string for non-existant files, now throws exceptionutil.readFile()
andwriteFile()
no longer take into account the task's basedir- upgrade to CoffeeScript 0.9.0
- add
type
attribute to the<arg>
element forJavaScript
andCoffeeScript
to support passing Project basedir-resolved files into scripts.
- add a JSON object, since this version of Rhino doesn't have one
- make the util object available to scripts with some i/o functions
- fix the location of the versions.properties file in the ant scripts
- added Rhino's toolsrc to the rhino-src.zip file built
- README fixes
- change the old
out
attribute of theJavaScript
andCoffeeScript
taks toresult
- initial buildable version