Created
December 20, 2012 05:40
-
-
Save plecong/4343176 to your computer and use it in GitHub Desktop.
Groovy script to watch a directory for LESS files and compile them. Requires Java 7.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @Grab('com.asual.lesscss:lesscss-engine:1.3.0') | |
| import java.nio.file.FileSystems | |
| import java.nio.file.StandardWatchEventKinds | |
| import com.asual.lesscss.LessEngine | |
| import com.asual.lesscss.LessOptions | |
| CliBuilder cli = new CliBuilder() | |
| cli.with { | |
| w longOpt: 'watch', args: 1, argName: 'less file/directory', 'LESS file or directory of LESS files to watch, default: <current directory>' | |
| o longOpt: 'output', args: 1, argName: 'output directory', 'Output directory for compiled CSS files, default: <current directory>' | |
| c longOpt: 'compress', 'Compress the compiled CSS' | |
| l longOpt: 'less', args: 1, argName: 'less.js', 'Path to custom less.js. Defaults to using version packaged with Asual LessEngine' | |
| h longOpt: 'help', 'Show usage information' | |
| } | |
| def options = cli.parse(args) | |
| if (!options) { return } | |
| if (options.h) { cli.usage(); return } | |
| def lessOptions = new LessOptions() | |
| if (options.c) { | |
| lessOptions.compress = true | |
| } | |
| if (options.l) { | |
| def lessJsFile = new File(options.l) | |
| if (lessJsFile.exists()) { | |
| lessOptions.less = lessJsFile.toURI().toURL() | |
| } else { | |
| System.err.println("Warning: Couldn't find custom less.js file: ${lessJsFile.canonicalPath}") | |
| } | |
| } | |
| def watchDir = options.w ? new File(options.w) : new File('.') | |
| if (!watchDir.exists()) { | |
| System.err.println("Warning: Couldn't find directory to watch: ${watchDir.canonicalPath}, using current") | |
| watchDir = new File('.') | |
| } | |
| def outputDir = options.o ? new File(options.o) : new File('.') | |
| if (!outputDir.exists()) { | |
| outputDir.mkdirs() | |
| } | |
| def engine = new LessEngine(lessOptions) | |
| def watchService = FileSystems.getDefault().newWatchService() | |
| watchDir.toPath().register(watchService, | |
| StandardWatchEventKinds.ENTRY_MODIFY, | |
| StandardWatchEventKinds.ENTRY_CREATE) | |
| println "Now watching for LESS files at: ${watchDir.canonicalPath}" | |
| while (true) { | |
| def key = watchService.take() | |
| if (key) { | |
| key.pollEvents() | |
| .collect { new File(watchDir, it.context().toString()) } | |
| .findAll { it.isFile() && it.name.endsWith('.less') } | |
| .each { | |
| def name = it.name.substring(0, it.name.lastIndexOf('.')) + '.css' | |
| def output = new File(outputDir, name) | |
| println "Compiling ${it.canonicalPath} -> ${output.canonicalPath}" | |
| engine.compile(it, output) | |
| } | |
| key.reset() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment