This document introduces GrooScript, an open source library that converts Groovy code to JavaScript. It provides examples of Groovy code and the corresponding JavaScript output. Key points covered include:
- GrooScript allows continued development in Groovy while outputting JavaScript that can be used in browsers.
- It supports common Groovy features like closures, meta programming and types while making the code more JavaScript friendly.
- The library can be used directly, or with Gradle and Grails plugins to facilitate conversion of Groovy code to JavaScript.
- While not a perfect translation, GrooScript enables continued use of the Groovy development experience for client-side code. The author provides several
2. About me
Developer, I love it
Lazy in english at school
Living in Madrid
Working at Osoco
Grooscript developer
@jfrancoleza
jorge.franco.leza@gmail.com
9. Groovy to Javascript converter
Groovy 2 to Javascript ECMAScript 5
Open source project, Apache 2 license
Library with Groovy and GPars dependencies
No special Javascript objects or functions
Converted code requires grooscript.js to run
Different conversion options
Grails plugin, npm package, gradle plugin
What is grooscript?
14. Using converted code in javascript
Create classes
NameClass();
Default constructor with js object
NameClass({property: value, data: 1});
Call methods, basic parameters
myObject.method(params);
Access properties
myObject.property = 4;
Use functions as closures
list.each(function () { ā¦ });
Traits
Metaprogramming
Mixins, Categories
Groovy types
gs.map({});
gs.set();
gs.range(1, 5);
gs.date();
gs.regExp();
gs.stringBuffer();
ā¦
Groovy list functions added to js lists
Added some functions to numbers and strings
(1).times(function () {});
āGroovyā.startsWith();
āHelloā.replaceAll();
Added curry to functions
Convert objects āto groovyā or āto javascriptā
gs.toGroovy(javaScriptList, NameClass);
gs.toJavascript(myGroovyObject);
Operators
Groovy truth
equals
getters and setters
Conversions
gs.toGroovy(obj, Type);
gs.toJavascript();
15. Limitations
Groovy / Java not fully supported
Working in groovy-core
No good support for inheritance (super)
No methods with same name
Basic support in metaClass
No packages, no classes with same name
No metainfo, expandoMetaClass, ā¦
Only AST transformations applied in semantic phase
No complex Java / Groovy types (LinkedList,ā¦)
ā¦ see documentation for more info on grooscript.org
16. Directly convert code
@Grab(āorg.grooscript:grooscript:0.5ā)
!
import org.grooscript.GrooScript
!
//Convert a fragment code
String jsResult = GrooScript.convert(āprintln āHello!āā)
!
//Convert some file or folder, generate .js files with same name
GrooScript.convert(āpath/to/file.groovyā, ādestination/folderā)
GrooScript.convert(āpath/to/folderā, ādestination/folderā)
GrooScript.convert(listOfFilesAndOrFolders, ādestination/folderā)
19. Main context scope
Customization
ā¢ Requires Groovy 2.1, the closure is passed to withConļ¬g(conf)
ā¢ More info in http://docs.codehaus.org/display/GROOVY/Advanced+compiler+conļ¬guration
ā¢ Sometimes you donāt know where to ļ¬nd a variable
ā¢ When conversion is done, some variables come from other contexts
ā¢ grooscript.js sometimes tries to ļ¬nd missed variables with eval
ā¢ Node.js eval not working same way that browsers do
ā¢ You can deļ¬ne variables or function names with this option
ā¢ Ex. [ā$ā, āmyAwesomeFunctionā, āmyMissedVariableā]
23. Mandatory Screen capture Before start test
More console info
Accept basic parameters
Works ļ¬ne in GroovyTestCase
Not so well in Spock
Have to
deļ¬ne
Phantom.js
path
Feature: PhantomJsTest
24. Example
@GrabConfig(systemClassLoader = true)
@Grab(āorg.grooscript:grooscript:0.5ā)
!
import org.grooscript.asts.PhantomJsTest
!
//Need phantoms installed
System.setProperty(āPHANTOMJS_HOMEā, āpath/to/phantomjsā)
!
@PhantomJsTest(url = āhttp://www.grails.orgā)
void grailsSiteChecks() {
assert $(āaā).size() > 50, āNot enough links ${$(āaā).size()}ā
def title = $(ātitleā)
assert title[0].text == āGrails - The search is over.ā
$(āaā).each {
println it
}
}
!
//Run phantomjs test
grailsSiteChecks()
25. package org.grooscript.builder
!
class HtmlBuilder {
!
String html
!
static String build(@DelegatesTo(HtmlBuilder) Closure closure) {
def mc = new ExpandoMetaClass(HtmlBuilder, false, true)
mc.initialize()
def builder = new HtmlBuilder()
builder.metaClass = mc
closure.delegate = builder
closure()
!
builder.html
}
!
def yield(String text) {
html += text
}
!
def methodMissing(String name, args) { ā¦ }
}
Feature: html builder
def result = HtmlBuilder.build {
body {
ul(class: 'list', id: 'mainList') {
2.times { number ->
li number + 'Hello!'
}
}
}
}
!
assert result == ā<body><ul class='list'
id='mainList'><li>0Hello!</li><li>1Hello!</
li></ul></body>ā
grooscript-builder.js
31. Grooscript Vert.x Plugin
Convert Groovy code to Javascript
Run conversion daemon
Websockets
Eventbus bridge
New port open
Events in the client
Auto reload pages
Both are optional
http://grooscript.org/pluginManual/
v 1.3.1
Requires Java 1.7
34. More eventsHtml Builder
Where
Listen events Render on load
Send event message
= println
Execute on event message
Donāt use ${} in grooscript tags
Strong dependency Resources plugin
Can put code in a .groovy ļ¬le
35. Domain classes in the client*
*Experimental, it requires Groovy 2.1 (grails 2.3)
ā¢ validate, clientValidations **
ā¢ hasErrors
ā¢ count
ā¢ list * without params
ā¢ get
ā¢ save * without params
ā¢ delete
36. Domain classes connected with the server*
*Experimental, it requires Groovy 2.1 (grails 2.3)
ā¢ list
ā¢ get
ā¢ save
ā¢ delete
37. PhantomJs Tests*
*Not working in Grails 2.3, need improvements
New test phase phantomjs
Tests in test/phantomjs
More features
38. features, featuresā¦
next release - new plugin - remove vert.x
From resources plugin to Require.js or Asset pipeline plugin
Improve domain options
PhantomJs tests improvements
Move to websockets/sockJs/stomp with Spring 4 in grails 2.4
and many moreā¦
Remote domain class to grails REST support
40. Gradle plugin
Add in your build.gradle
http://grooscript.wordpress.com/2014/02/22/starting-with-grooscript/
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'org.grooscript:grooscript-gradle-plugin:0.3'
}
}
!
apply plugin: 'grooscript'
!
//If you need to change any conversion option, can do this way, optional
grooscript {
source = [āsrc/main/groovy/presenters']
}
41. Create more conversion tasks: http://grooscript.wordpress.com/2014/01/31/61/
Converted ļ¬les destination
Require.js setup ļ¬le
Files to be converted
44. Example using node.js colors npm module
Node.js is very fast!
http://grooscript.wordpress.com/2014/01/10/impressive-node-js-v8-speed/
https://github.com/chiquitinxx/colors
46. Why would I use grooscript?
ā¢ Create small modules to use in your views
ā¢ You can continue developing in Groovy
ā¢ Can use dslās, typeCheck, ASTās,ā¦ in the browser
ā¢ You have all the java tools and IDEās
ā¢ You can work with new Javascript tools from Groovy
ā¢ Donāt repeat code in two languages
ā¢ Single development environment
ā¢ Create your own architecture in Groovy
ā¢ Donāt learn another āto Javascriptā tool
47. Community will decide
Future
Version 0.5, you can improve
I will continue improving grooscript
Never will support full Groovy / Java
Iām learning a lot, very fun
New grails plugin in summer
Focus next version: more and better java and groovy support