7. CONVERT GROOVY CODE
Groovy / java code to javascript code
Groovy / java file to javascript file
Folder to folder or javascript file
List of files / folders to folder or javascript file
@Grab('org.grooscript:grooscript:1.1.1')
import org.grooscript.GrooScript
String result = GrooScript.convert '''
def sayHello = { println "Hello ${it}!" }
['Groovy','JavaScript','GrooScript'].each sayHello
'''
println result
8. CONVERSION OPTIONS
• classPath - List of folders or jar’s to find dependencies. Before groovy code is converted to javascript, that code have to
be successfully compiled. For example: [''src/groovy'']
• customization - to each file to be converted, will be applied a groovy customization. For example you can for example
type check all the groovy classes to be converted with { -> ast(TypeChecked)}
• initialText / finalText - you can add test at the beginning or end of your converted javascript file.You can add comments
or javascript code. For example: ''//This code has been generated with grooscript''
• recursive - if you specify a folder with the source conversion option, by default will not convert files inside folders.To
convert recursive, just set to true
• mainContextScope - you can help the converted, specifying functions and variables that will be available in the main
context of the javascript environment. For example: [''$'', ''window'',''myFunction'']
• addGsLib - you can add a grooscript js lib at the beginning of the converted file. For example you can add grooscript.js
to the result, and then you don’t have to worry about include that dependency in javascript ''grooscript''
10. NOT SUPPORTED
SEE DOCUMENTATION
• Non groovy-core (json, builders, I/O, …)
• Java / GroovyTypes (Thread, LinkedHashMap, Stack, …)
• Super
• Methods with same name (overload), classes with same name
• Java 8 stuff not supported by groovy
• Module extensions, complex metaprogramming, groovy AST’s
after Semantic Phase
11. @GsNotConvert
@GsNative
import org.grooscript.asts.GsNative
class Data {
@GsNative
def saySomething(some) {/*
console.log(some);
*/}
@GsNative
def five() {/*
return 5;
*/ 1 + 1 + 3
}
}
import org.grooscript.asts.GsNotConvert
@GsNotConvert
class ClassNotConvert {
def a
}
class A {
@GsNotConvert
def methodNoConvert() {
‘No!’
}
def hello() {
println ‘Hello!’
}
}
14. given:
def result = HtmlBuilder.build {
body {
ul(class: 'list', id: 'mainList') {
2.times { number ->
li number + 'Hello!'
}
}
}
}
expect:
result == "<body><ul class='list' id=‘mainList'>
<li>0Hello!</li><li>1Hello!</li></ul></body>"
HtmlBuilder
15. import org.grooscript.jquery.GQueryImpl
def result = new Result()
def gQuery = new GQueryImpl()
gQuery.onReady {
gQuery.doRemoteCall(“${JSON_ADDRESS}”, 'GET', null, { res ->
gQuery('.result').html('OK')
result = res
}, {
result = 'FAIL!'
}, Result)
}
GQuery
16. given:
def result = []
Observable.from([1, 5, 9, 12, 3, 8]).
filter { it < 5 }.
map { 'H' * it }.
subscribe { event ->
result << event
}
expect:
result == ['H', 'HHH']
Observable
21. var gs = require('grooscript');
bower install grooscript
22. WHY GROOSCRIPT?
• You have all the java / groovy tools and IDE’s
• Meet javascript! Create your own libraries
• Don’t repeat code in two languages
• Same templates in client and server
• Single development environment
• Don’t learn another “to Javascript” tool
• Static typed if you want to
• but… mainly… because is…