This plugin convert groovy files to javascript, convert groovy templates to a .js file, generate a little web static project to start with grooscript, and listen for changes in files. To do this tasks, uses grooscript, a library that converts groovy code to javascript. In folders or files options, you can’t use wildcards as * or **, just put relative path from folder where build is.
Convert files
There are 2 tasks:
-
convert (org.grooscript.gradle.ConvertTask) to convert groovy files to javascript
-
convertThread (org.grooscript.gradle.ConvertThreadTask) listen changes in groovy files and convert them to javascript
There is an extension to configure this tasks
package org.grooscript.gradle
class ConversionExtension {
def source = ['src/main/groovy']
String destination = 'src/main/webapp/js/app'
List<String> classpath = ['src/main/groovy']
Closure customization
String initialText
String finalText
boolean recursive = false
List<String> mainContextScope
String addGsLib
boolean includeDependencies = false
boolean consoleInfo = false
boolean nashornConsole = false
}
//Example:
grooscript {
source = ['src/main/groovy']
destination = 'js'
}
If you execute >gradle convert
, will convert groovy and java files in src/main/groovy
to .js files, and will
be saved in ./js
folder.
You will use convertThread task when other task keeps gradle up, like a web application server.
If you execute convertThread task with blockExecution=true option, gradle will be running forever doing conversions if files in src/main/groovy
change.
Apart from source
and destination
, mandatory properties, you can define conversion options:
grooscript {
source = ['src/main/groovy/presenters'] //Sources to be converted
destination = 'js' //Target directory or js file
classpath = ['src/groovy'] //Needed classpath to compile source files
customization = null //Customization in files, for example { -> ast(groovy.transform.TypeChecked) }
initialText = '//Grooscript converted file'
initialText = '//End converted file'
recursive = true
mainContextScope = ['$'] //Variables available in main scope
addGsLib = 'grooscript, grooscript-tools' //Include libs (comma separated) from grooscript ('grooscript', 'grooscript.min', 'grooscript-tools' or 'jquery.min')
nashornConsole = true //Use 'print' and not 'console.log'
}
Also you can create distinct conversion tasks to use in your gradle build:
task convertCountries(type: org.grooscript.gradle.ConvertTask) {
source = ['src/main/groovy/countries']
mainContextScope = ['Snap']
destination = 'src/main/webapp/js'
}
task convertChatServer(type: org.grooscript.gradle.ConvertTask) {
source = ['src/main/groovy/chat/NodeServer.groovy', 'src/main/groovy/startServer.groovy']
destination = 'js/allTogether.js'
}
build.dependsOn convertCountries, convertChatServer
Create static project
There is 1 tasks:
-
initStaticWeb that will create files and folder to generate a little static web project to start with grooscript.
Read this guide
Convert groovy templates
There are 2 tasks:
-
templatesJs (org.grooscript.gradle.TemplatesTask) to convert groovy templates to a .js file
-
templatesThread (org.grooscript.gradle.TemplatesThreadTask) listen changes in groovy templates, and generate .js file if some file changes
There is an extension to configure this task:
package org.grooscript.gradle
class TemplatesExtension {
String templatesPath = 'src/main/webapp/templates'
List<String> templates
String destinationFile = 'src/main/webapp/js/lib/Templates.js'
List<String> classpath = ['src/main/groovy']
String customTypeChecker
}
//Example:
templates {
templatesPath = 'templates'
templates = ['main.gtpl', 'little/small.tpl'] //Files from templatesPath
destinationFile = 'js/Templates.js'
classpath = ['src/groovy']
customTypeChecker = 'aCustomTypeCheckerFile.groovy'
}
You can type check your templates with custom type checker, more info here. Example:
import org.codehaus.groovy.ast.expr.MethodCallExpression
beforeMethodCall { call ->
if (call instanceof MethodCallExpression) {
//Not allowing use p tag in templates
handled = (call.method.text != 'p')
}
}
The support in convert templates is a bit limited, you can’t use code out of the template’s engine. You can’t use imports, or insert data from other files. The destination js is the next class, converted, with templates map filled with all converted templates:
package org.grooscript.templates
class Templates {
static Map<String, Closure> templates
static String applyTemplate(String name, model = [:]) {
Closure cl = templates[name]
cl.delegate = model
cl(model)
}
}
The templatesThread task, has a parameter blockExecution
, by default false. If you set to true, the task will
continue forever, blocking execution.
Spy file changes
There is 1 tasks:
-
spyChanges (org.grooscript.gradle.ChangesTask) listen to changes on files
There is an extension to configure this task:
package org.grooscript.gradle
class ChangesExtension {
List<String> files
Closure onChanges
}
//Example:
spy {
files = ['src/main/webapp/static/css/app.css', 'src/main/webapp/static/js/app']
onChanges = { list ->
println 'Changes!! ' + list
}
}
I have introduced spring websockets support in onChanges closure. Now you can send a groovy object to a spring websocket server that uses Stomp protocol. So you can notify by websockets changes on files, in this example I want to send to server a reload message.
spy {
files = ['Templates.js']
onChanges = { list ->
springWebsocketTo 'http://localhost:8080/demo' data 'templates' onChannel '/app/reload'
}
}
Sync grooscript libraries
There is 1 tasks:
-
syncGsLibs (org.grooscript.gradle.SyncGrooscriptLibsTask) sync grooscript js libs with actual version
With this tasks, all your grooscript libraries in the project ('grooscript.js', 'grooscript.min.js', 'grooscript-tools.js') will be replaced by the libs that comes in the grooscript version of the current plugin.
Require.js tasks
There are 2 tasks:
-
requireJs (org.grooscript.gradle.RequireJsTask) to generate require.js modules from a groovy file
-
requireJsThread (org.grooscript.gradle.RequireJsThreadTask) to listen changes in groovy files to generate require.js modules
There is an extension to configure this task:
package org.grooscript.gradle
class RequireJsExtension {
String sourceFile
List<String> classpath = ['src/main/groovy']
String destinationFolder
Closure customization
String initialText
String finalText
List<String> mainContextScope
boolean nashornConsole = false
}
Example:
requireJs {
sourceFile = 'src/main/groovy/MyApp.groovy'
// classpath by default is ['src/main/groovy']
destinationFolder = 'src/main/resources/web/js/app'
}
The requireJsThread task, has a parameter blockExecution
, by default false. If you set to true, the task will
continue forever, blocking execution.