Grooscript gr8conf 2015

31
June 2nd - 4th 2015 Copenhagen, Denmark Grooscript in Action

Transcript of Grooscript gr8conf 2015

Page 1: Grooscript gr8conf 2015

June 2nd - 4th 2015Copenhagen, Denmark

Grooscript in Action

Page 2: Grooscript gr8conf 2015

Jorge Franco@jfrancoleza

[email protected]

Page 3: Grooscript gr8conf 2015
Page 4: Grooscript gr8conf 2015

NEWS

v1.1.1

Page 5: Grooscript gr8conf 2015

INFO

• Library (JAR)

• Groovy 2.0+ to javascript ECMA 5

• Apache 2 License

• Converted code needs grooscript.js

• Tools: gradle, grails, npm/bower

Page 6: Grooscript gr8conf 2015

LINKS• Website http://grooscript.org

• Documentation http://grooscript.org/doc.html

• Github https://github.com/chiquitinxx/grooscript

• Demos https://github.com/chiquitinxx/grooscript-demos

• Twitter @grooscript

• Try live! http://grooscript.org/conversions.html

Page 7: Grooscript gr8conf 2015

CONVERT GROOVY CODE

Groovy / java code to javascript codeGroovy / java file to javascript fileFolder 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

Page 8: Grooscript gr8conf 2015

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''

Page 9: Grooscript gr8conf 2015

SUPPORTSEE DOCUMENTATION

• groovy-core

• classes, numbers, lists, maps, sets, date, ranges, closures, AST’s, (g)strings, operators, groovy truth, Expando, categories, traits, beans, switch, metaprogramming, enum, equals, multiple assignment, optional params, default constructor, pointer methods, operator overload, regular expressions, @Delegate, @Category, …

Page 10: Grooscript gr8conf 2015

NOT SUPPORTEDSEE DOCUMENTATION

• Non groovy-core (json, builders, I/O, …)

• Java / Groovy Types (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

Page 11: Grooscript gr8conf 2015

@GsNotConvert@GsNative

import org.grooscript.asts.GsNative

class Data {@GsNativedef saySomething(some) {/*console.log(some);*/}

@GsNativedef five() {/*return 5;*/ 1 + 1 + 3}}

import org.grooscript.asts.GsNotConvert

@GsNotConvertclass ClassNotConvert { def a}

class A {@GsNotConvertdef methodNoConvert() {‘No!’}

def hello() {println ‘Hello!’}}

Page 12: Grooscript gr8conf 2015

@GrabConfig(systemClassLoader = true)@Grab(‘org.grooscript:grooscript:1.1.1’)

import org.grooscript.asts.PhantomJsTest

//Need phantomjs installedSystem.setProperty(‘PHANTOMJS_HOME’, ‘path/to/phantomjs’)

@PhantomJsTest(url = 'http://beta.groovy-lang.org/') void testCountLinks() { def links = $('a') assert links.size() > 40, "Number of links: ${links.size()}" links.toArray().collect { it.toString() }.each { link -> println link }}

testCountLinks()

PhantomJs Tests

Page 13: Grooscript gr8conf 2015

JAVASCRIPT LIBS

• grooscript.js (86 kb) (8 kb minified and gziped)

• grooscript.min.js (33 kb)

• grooscript-tools.js (14 kb) (GQuery, HtmlBuilder, Observable)

Page 14: Grooscript gr8conf 2015

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

Page 15: Grooscript gr8conf 2015

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

Page 16: Grooscript gr8conf 2015

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

Page 17: Grooscript gr8conf 2015

https://github.com/chiquitinxx/grooscript-gradle-plugin

http://grooscript.org/starting_gradle.html

http://grooscript.org/gradle/tasks.html

Page 18: Grooscript gr8conf 2015

DEMO

https://github.com/chiquitinxx/books-demo

Page 19: Grooscript gr8conf 2015

Server Browser

GTPLGTPLGTPL

BookPresenter.groovy

Book.groovy

SpringbootWebsockets

Mongo

SockJsstompjQuery

grooscriptgrooscript-tools

sweet-alertchartist

timenewBook

listOfBooksbookAdded

groovy traits Counter.groovy

StompClient.groovy

reload

Convertedjs files

Page 20: Grooscript gr8conf 2015

http://grooscript.org/grails-plugin/index.html

https://github.com/chiquitinxx/grails-grooscript

https://github.com/chiquitinxx/grooscript-grails3-plugin

Page 21: Grooscript gr8conf 2015

var gs = require('grooscript');

bower install grooscript

Page 22: Grooscript gr8conf 2015

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…

Page 23: Grooscript gr8conf 2015

ConciseReadable

Expressive

Page 24: Grooscript gr8conf 2015

@Component class Counter implements Colorable { Integer number void init() { number = null } void render() { div(class: "widget bg-${randomColor()}") { if (number) { p 'Number of books' em number.toString() a(href:"#", class:"button", onclick: 'bookPresenter.showBooks()') { yield 'Show' } } else { p 'Counting books...' } } } }

AST’sGetter / Setters

Page 25: Grooscript gr8conf 2015

def start() { def countries = loadCountries(). findAll { it.population > 100000}. unique { it.alpha3Code } countries.each { country -> customSigma.addNode id: country.alpha3Code,

x: country.latlng[1], color: ‘purple’ } countries.each { country -> country.borders?.each { border -> this.&hello(border) } } updateNumberCountries countries.size() customSigma.refresh()}

Groovy Lists and Maps

Page 26: Grooscript gr8conf 2015

server { get('/') { render Templates.applyTemplate('index.gtpl') } on('login') { data, socket -> if (data.name && !socket.login) { socket.login = data.name socket.emit 'loginok', [name: data.name] socket.broadcast.emit 'loginok', [name: data.name] } } on('msg') { data, socket -> if (data.msg && socket.login) { socket.broadcast.emit 'msg', [msg: data.msg] } } on('disconnect') { socket -> if (socket.login) { socket.broadcast.emit 'off', [name: socket.login] } }}.start()

Dsl’s

Page 27: Grooscript gr8conf 2015

class Hello { def methodMissing(String name, args) { println "Hello ${name}!" }}

def hello = new Hello()hello.Groovy()hello.Javascript()hello.grooscript()

Metaprogramming

Page 28: Grooscript gr8conf 2015

def plus2 = { it + 2 }def times3 = { it * 3 }

def times3plus2 = plus2 << times3

assert times3plus2(3) == 11

def plus2times3 = times3 << plus2

assert plus2times3.curry(5)() == 21

Functional

Page 29: Grooscript gr8conf 2015

NEXT

3 Continuous Mode

Page 30: Grooscript gr8conf 2015

Q & A

Page 31: Grooscript gr8conf 2015

Thank you!