Skip to content

Instantly share code, notes, and snippets.

@hlship
Created June 29, 2012 16:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hlship/3019081 to your computer and use it in GitHub Desktop.
Save hlship/3019081 to your computer and use it in GitHub Desktop.
CompileCoffeeScript task for Gradle
import ro.isdc.wro.model.resource.*
import ro.isdc.wro.extensions.processor.js.*
buildscript {
repositories { mavenCentral() }
dependencies {
classpath "ro.isdc.wro4j:wro4j-extensions:${versions.wro4j}"
}
}
class CompileCoffeeScript extends DefaultTask {
def srcDir = "src/main/coffeescript"
def outputDir = "${project.buildDir}/compiled-coffeescript"
@InputDirectory
File getSrcDir() { project.file(srcDir) }
@OutputDirectory
File getOutputDir() { project.file(outputDir) }
@TaskAction
void doCompile() {
logger.info "Compiling CoffeeScript sources from $srcDir into $outputDir"
def outputDirFile = getOutputDir()
// Recursively delete output directory if it exists
outputDirFile.deleteDir()
def tree = project.fileTree srcDir, {
include '**/*.coffee'
}
tree.visit { visit ->
if (visit.directory) return
def inputFile = visit.file
def inputPath = visit.path
def outputPath = inputPath.replaceAll(/\.coffee$/, '.js')
def outputFile = new File(outputDirFile, outputPath)
logger.info "Compiling ${inputPath}"
outputFile.parentFile.mkdirs()
def resource = Resource.create(inputFile.absolutePath, ResourceType.JS)
new CoffeeScriptProcessor().process(resource, inputFile.newReader(), outputFile.newWriter())
}
}
}
project.ext.CompileCoffeeScript = CompileCoffeeScript
@alexo
Copy link

alexo commented Jun 29, 2012

You can improve the script performance by reusing CoffeeScriptProcessor instance across all resource processing. I've tried to add a comment to tapestry central, but posting a comment there is not quite user-friendly.

@hlship
Copy link
Author

hlship commented Jun 29, 2012

I'll give that a try ... I don't have enough .coffee files to compile that I'll be able to tell the difference, I suspect.

@alexo
Copy link

alexo commented Jun 29, 2012

The processor itself uses a pool to reuse rhino instance. The idea was to make rhino perform faster. Here is a wiki page describing it: http://code.google.com/p/wro4j/wiki/RhinoPerformanceImprovement

@hlship
Copy link
Author

hlship commented Jun 29, 2012 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment