Gradle Groovy Shellãã©ã°ã¤ã³ã使ã£ã¦ä¾åã©ã¤ãã©ãªè¾¼ã¿ã®REPLãèµ·åãã #gadvent
ãã®ã¨ã³ããªã¯ G*Advent Calendar(Groovy,Grails,Gradle,Spock...) Advent Calendar 2014 - Qiita ã®12/8æ å½åã§ãã
Gradle Groovy Shellãã©ã°ã¤ã³ã¨ã¯?
Groovyã«ã¯"groovysh"(Groovy Shell)ã¨ããæ©è½ãããã¾ããããã¯Groovyã®ã¤ã³ã¿ã©ã¯ãã£ãã·ã§ã«ãèµ·åãããã®ã§ãããããREPLç¸å½ã®æ©è½ã§ãã
ãããªãã«ä¾¿å©ãªæ©è½ãªã®ã§ãããæ®å¿µãªããGroovyã®æ¨æºã©ã¤ãã©ãªä»¥å¤ãèªã¿è¾¼ããã¨ãã§ãããã©ã¤ãã©ãªã追å ããå ´åã«ã¯èªåã§ã¯ã©ã¹ãã¹ãéãå¿
è¦ãããã¾ãã
Gradle Groovy Shellãã©ã°ã¤ã³ãå©ç¨ããã¨ãGradleãå©ç¨ãã¦ä¾åé¢ä¿ã解決ããç¶æ ã§groovyshãèµ·åãããã¨ãã§ãã¾ãã
Gradle Groovy Shellãã©ã°ã¤ã³ã®å©ç¨æ¹æ³
Gradleã®ãã«ãã¹ã¯ãªããã§gradle-groovysh-pluginã追å ããã ãã§ãã
詳細ã¯ãã¡ããåç
§ããã ãã¨ãããã¨æãã¾ãã
tkruse/gradle-groovysh-plugin · GitHub
V1.0.2ã§ã®æ³¨æç¹
2014/12/8æç¹ã§ã®ææ°ãã¼ã¸ã§ã³(1.0.2)ã§ã¯ãGradle Groovy Shellãã©ã°ã¤ã³ã®ã¿ãé©ç¨ããã¨ãã«ãã¨ã©ã¼ã«ãªãã¾ãã
Javaãã©ã°ã¤ã³ã®ããããã£ãåç
§ãã¦ãããã¨ãåå ã®ããã§ããJavaãã©ã°ã¤ã³ããããã¦é©ç¨ããããã«ãã¦ãã ããã
ä¾) Twitter4Jã使ã£ã¦ã¿ã
ããã§ã¯ä¾ã¨ã㦠Twitter4J - A Java library for the Twitter API ã使ã£ã¦ã¿ã¾ãããã
äºåã« https://apps.twitter.com/ ã§Consumer KeyãAccess Tokenãåå¾ãã¦ããã¦ãã ããã
é©å½ãªãã£ã¬ã¯ããªã§ build.gradle ä½æãã¾ãã
apply plugin: 'com.github.tkruse.groovysh' apply plugin: 'java' buildscript { repositories { jcenter() } dependencies { classpath 'com.tkruse.gradle:gradle-groovysh-plugin:1.0.2' } } repositories { jcenter() } dependencies { compile 'org.twitter4j:twitter4j-core:4.0+' }
ããã¦ã次ã®ã³ãã³ãã§Groovy Shellãèµ·åãã¾ãã
$ gradle -q shell This is a gradle Application Shell. You can import your application classes and act on them. Groovy Shell (2.3.6, JVM: 1.7.0_72) Type ':help' or ':h' for help. ------------------------------------------------------------------------------- groovy:000>
"groovy:000>"ã¨ããããã³ããã表示ãããããããããã«Groovyã³ã¼ããã¿ã¤ããã¦ããã¾ãããã(å®å
¨ã§ã¯ããã¾ããããTABãã¼ã«ããè£å®ããããªãã«å¹ãã¾ãã)
ãªããgroovyshã§ã¯"def"ã§å®ç¾©ããå¤æ°ã¯åç
§ã§ããªãã®ã§ãä½ãã¤ããã«å®ç¾©ããããã«ãã¦ãã ããã
groovy:000> import twitter4j.* ===> twitter4j.* groovy:000> import twitter4j.conf.* ===> twitter4j.*, twitter4j.conf.* groovy:000> cb = new ConfigurationBuilder() ===> twitter4j.conf.ConfigurationBuilder@6c01e903 groovy:000> cb.setOAuthConsumerKey("<YOUR_CONSUMER_KEY>") ===> twitter4j.conf.ConfigurationBuilder@6c01e903 groovy:000> cb.setOAuthConsumerSecret("<YOUR_CONSUMER_SECRET>") ===> twitter4j.conf.ConfigurationBuilder@6c01e903 groovy:000> cb.setOAuthAccessToken("<YOUR_OAUTH_TOKEN>") ===> twitter4j.conf.ConfigurationBuilder@6c01e903 groovy:000> cb.setOAuthAccessTokenSecret("<YOUR_OAUTH_SECRET>") ===> twitter4j.conf.ConfigurationBuilder@5a8fddea groovy:000> tf = new TwitterFactory(cb.build()) ===> twitter4j.TwitterFactory@6bffa4b9 groovy:000> twitter = tf.getInstance() ===> TwitterImpl{INCLUDE_MY_RETWEET=PostParameter{name='include_my_retweet', value='true', file=null, fileBody=null}} groovy:000> query = new Query("gradle") ===> Query{query='gradle', lang='null', locale='null', maxId=-1, count=-1, since='null', sinceId=-1, geocode='null', until='null', resultType='null', nextPageQuery='null'} groovy:000> result = twitter.search(query) [Mon Dec 08 02:35:38 JST 2014]Request: [Mon Dec 08 02:35:38 JST 2014]GET https://api.twitter.com/1.1/search/tweets.json?q=gradle&with_twitter_user_id=true&include_entities=true ã»ã»ã» groovy:000> result.tweets.each{ println "@${it.user.screenName}: ${it.text}" } @csterwa: RT @danveloper: What if bootstrapping a cloud full of @NetflixOSS was as easy as typing "initCloud"? Now it is. https://t.co/44OVUgfKG4 @IndieGameDevBot: RT @lastpoke: Way to download Utility classes with Gradle http://t.co/suUwF0xSyj #Android #AndroidDev #lastpoke ã»ã»ã»
Ctrl+Dã§çµäºãã¾ãã
2015/1/29追è¨
Groovy2.4ã§groovyshã®interpreterModeã追å ããã¾ããã
http://jira.codehaus.org/browse/GROOVY-6623
groovyshèµ·åå¾ã«
:set interpreterMode true
ãå®è¡ããã°ãgroovyshä¸ã§ãæ®éã«ãdef x=3ãã¨ãã§å¤æ°å®ç¾©ã§ããããã«ãªãã¾ããããã§ããã
ã¾ã¨ã
ç°¡åã§ãããGradle Groovy Shellãã©ã°ã¤ã³ã«ã¤ãã¦ç´¹ä»ãã¾ããã
Groovy Shellãã©ã°ã¤ã³ã¯ä¸»ã«Gradleã®ãã«ãã¹ã¯ãªããéçºãæ¯æ´ããããã«ä½¿ããããã¨ãå¤ãããã§ãããããå°ç¨ã§ã¯ãªãããå°ãæ±ç¨çã«ä½ããã¦ããã¨ãããã¨ããããã¾ããä»ã®è¨èªå¦çç³»ã«ãããREPLã«æ¯ã¹ãã¨ããæ©è½ä¸è¶³ãªæã¯å¦ãã¾ããããJavaã®REPLãç»å ´ããã¾ã§ã¯ããã§åãã¨ããã®ãã¢ãªãã¨æãã¾ãã
ã¾ããGroovyã«ã¯è¨èªæ©è½ã¨ãã¦Grape(@Grab)ãç¨æããã¦ããã®ã§ãããGrapeã¯ã¹ã¯ãªããå®è¡ä»¥å¤ã®ä½¿ãæ¹ã§ã¯ãã¾ãåããªãå ´åãããã¾ãã®ã§ããã¾ã使ãåããã¨ããã®ã§ã¯ãªãã§ããããã