grails - Groovy - How to reflect code from another groovy app to find Classes/Properties/Types within -
first, came .net background please excuse lack of groovy lingo. when in .net shop, using typescript c# build web apps. in our controllers, receive/respond dtos (data xfer objects). got quite headache every time create/modify dto had update typescript interface (the d.ts file) corresponded it.
so created little app (a simple exe) loaded dll webapp it, reflected on find dtos (filtering specific namespaces), , parse through them find each class name within, properties, , properties' data types, generate information string, , saved d.ts file.
this app configured run on every build of website. way, when go run/debug/build website, update d.ts files automatically - made working typescript easier.
long story short, how achieve grails website if write simple groovy app generate d.ts want?
-- or --
how ide (ex intellij) run groovy file (that part of app) generation post-build?
i did find still need way run on compile:
class foo { def feck = "fe" def arse = "ar" def drink = "dr" } class foo2 { def feck = "fe2" def arse = "ar2" def drink = "dr2" } def f = new foo() def f2 = new foo2() f2.properties.each { prop, val -> if(prop in ["metaclass","class"]) return if(f.hasproperty(prop)) f[prop] = val } assert f.feck == "fe2" assert f.arse == "ar2" assert f.drink == "dr2"
i've been able extract domain objects , persistent fields via following gant script:
in scripts/props.groovy:
import static groovy.json.jsonoutput.* includetargets << grailsscript("_grailsbootstrap") target(props: "lists persistent properties each domain class") { depends(loadapp) def propmap = [:].withdefault { [] } grailsapp.domainclasses.each { it?.persistentproperties?.each { prop -> if (prop.hasproperty('name') && prop.name) { propmap[it.clazz.name] << ["${prop.name}": "${prop.gettype()?.name}"] } } } // necessary file i/o here (just printing example) println prettyprint(tojson(propmap)) } setdefaulttarget(props)
this can run via command line so:
grails props
which produces output following:
{ "com.mycompany.user": [ { "type": "java.lang.string" }, { "username": "java.lang.string" }, { "password": "java.lang.string" } ], "com.mycompany.person": [ { "name": "java.lang.string" }, { "alive": "java.lang.boolean" } ] }
a couple of drawbacks approach don't transient properties , i'm not sure how hook _events.groovy eventcompileend event.
Comments
Post a Comment