grooscript greach 2015

27
grooscript in action Jorge Franco - @jfrancoleza v1.0.1

Upload: jorge-franco-leza

Post on 17-Jul-2015

817 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Grooscript greach 2015

grooscript in actionJorge Franco - @jfrancoleza

v1.0.1

Page 2: Grooscript greach 2015
Page 3: Grooscript greach 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 4: Grooscript greach 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 5: Grooscript greach 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.0.1')

import org.grooscript.GrooScript

String result = GrooScript.convert ''' def sayHello = { println "Hello ${it}!" } ['Groovy','JavaScript','GrooScript'].each sayHello'''

println result

Page 6: Grooscript greach 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 availables 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 7: Grooscript greach 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 8: Grooscript greach 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 9: Grooscript greach 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 10: Grooscript greach 2015

@GrabConfig(systemClassLoader = true)@Grab(‘org.grooscript:grooscript:1.0.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 11: Grooscript greach 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 12: Grooscript greach 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 13: Grooscript greach 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 14: Grooscript greach 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 15: Grooscript greach 2015

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

http://grooscript.org/starting_gradle.html

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

Page 16: Grooscript greach 2015

DEMO

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

Page 17: Grooscript greach 2015

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

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

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

Page 18: Grooscript greach 2015

var gs = require('grooscript');

bower install grooscript

Page 19: Grooscript greach 2015

WHY GROOSCRIPT?

• You have all the java tools and IDE’s • Don’t repeat code in two languages • Single development environment • Don’t learn another “to Javascript” tool • Static typed if you want to • but… mainly… because is…

Page 20: Grooscript greach 2015

ConciseReadable

Expressive

Page 21: Grooscript greach 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...' } } } }

Page 22: Grooscript greach 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()}

Page 23: Grooscript greach 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()

Page 24: Grooscript greach 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

Page 25: Grooscript greach 2015

NEXT

3

Page 26: Grooscript greach 2015

Q & A

Page 27: Grooscript greach 2015

Thank you!