<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ted Naleid &#187; shortcut</title>
	<atom:link href="http://naleid.com/blog/category/shortcut/feed/" rel="self" type="application/rss+xml" />
	<link>http://naleid.com/blog</link>
	<description>Groovy, Grails and OS X tips and tricks</description>
	<lastBuildDate>Wed, 16 Jun 2010 17:38:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Groovy Each Iterator with Peek-ahead at Next Collection Value</title>
		<link>http://naleid.com/blog/2010/06/15/groovy-each-iterator-with-peek-ahead-at-next-collection-value/</link>
		<comments>http://naleid.com/blog/2010/06/15/groovy-each-iterator-with-peek-ahead-at-next-collection-value/#comments</comments>
		<pubDate>Wed, 16 Jun 2010 04:55:10 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[groovy]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[shortcut]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=191</guid>
		<description><![CDATA[Groovy closures combined with iterators make it simple to create our own enhanced iterators that let us process a collection how we want to.
I write my own custom iterators all the time and name them something descriptive.  This makes the code much more readable. Rather than trying to decipher what a for loop is [...]]]></description>
			<content:encoded><![CDATA[<p>Groovy closures combined with iterators make it simple to create our own enhanced iterators that let us process a collection how we want to.</p>
<p>I write my own custom iterators all the time and name them something descriptive.  This makes the code much more readable. Rather than trying to decipher what a for loop is trying to do, we wrap up all of that iteration logic into a meaningful name and we cleanly separate that iteration from the processing that we&#8217;re doing with each element.</p>
<p>This kind of design is a core concept in Uncle Bob&#8217;s <a href="http://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882" onclick="pageTracker._trackPageview('/outgoing/www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882?referer=');">Clean Code</a>, one of my favorite programming books in the last few years.</p>
<p>This example iterates over a collection and calls the passed in closure until we hit a value greater than 5.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> eachUntilGreaterThanFive <span style="color: #669966;">=</span> <span style="color: #669966;">&#123;</span> collection, closure <span style="color: #669966;">-&gt;</span>
    <span style="color: #996600;">for</span> <span style="color: #669966;">&#40;</span> value <span style="color: #996600;">in</span> collection <span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span>
        <span style="color: #996600;">if</span> <span style="color: #669966;">&#40;</span> value  <span style="color: #669966;">&gt;</span> <span style="color: #cc66cc;">5</span> <span style="color: #669966;">&#41;</span> <span style="color: #000000; font-weight: bold;">break</span>
        closure<span style="color: #669966;">&#40;</span>value<span style="color: #669966;">&#41;</span>
    <span style="color: #669966;">&#125;</span>
<span style="color: #669966;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> a <span style="color: #669966;">=</span> <span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">6</span>, <span style="color: #cc66cc;">7</span><span style="color: #669966;">&#93;</span>
&nbsp;
eachUntilGreaterThanFive<span style="color: #669966;">&#40;</span>a<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span>
    <span style="color: #663366;">println</span> it
<span style="color: #669966;">&#125;</span></pre></div></div>

<p>prints:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #cc66cc;">1</span>
<span style="color: #cc66cc;">2</span>
<span style="color: #cc66cc;">3</span>
<span style="color: #cc66cc;">4</span>
<span style="color: #cc66cc;">5</span></pre></div></div>

<p>This code makes it obvious what the iterator is doing (looping till we hit a condition) as well as what will happen with each element iterated over (print it out).</p>
<p>For a real life example, I had a need to iterate over a list of values and where I needed both the current object as well as a peek at the next object in the list.</p>
<p>Doing this is Java is a bit of a pain, but groovy makes it easy to write and (hopefully) to read, we can also add it directly onto the Collection metaClass so that it&#8217;s available for all of our Collection instances:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #aaaadd; font-weight: bold;">Collection</span>.<span style="color: #006600;">metaClass</span>.<span style="color: #006600;">eachWithPeek</span> <span style="color: #669966;">=</span> <span style="color: #669966;">&#123;</span> closure <span style="color: #669966;">-&gt;</span>
    <span style="color: #000000; font-weight: bold;">def</span> last <span style="color: #669966;">=</span> <span style="color: #000000; font-weight: bold;">null</span>
    delegate<span style="color: #669966;">?</span>.<span style="color: #663399;">each</span> <span style="color: #669966;">&#123;</span> current <span style="color: #669966;">-&gt;</span>
        <span style="color: #996600;">if</span> <span style="color: #669966;">&#40;</span>last<span style="color: #669966;">&#41;</span> closure<span style="color: #669966;">&#40;</span>last, current<span style="color: #669966;">&#41;</span>
        last <span style="color: #669966;">=</span> current
    <span style="color: #669966;">&#125;</span>
    <span style="color: #996600;">if</span> <span style="color: #669966;">&#40;</span>last<span style="color: #669966;">&#41;</span> closure<span style="color: #669966;">&#40;</span>last, <span style="color: #000000; font-weight: bold;">null</span><span style="color: #669966;">&#41;</span>
<span style="color: #669966;">&#125;</span></pre></div></div>

<p>These test cases show that as we iterate through the collection, we can see the current item and peek at the next one (if any).  If the collection is empty, we don&#8217;t execute the closure it, and if we&#8217;re at the end of the list there isn&#8217;t anything to peek at:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #669966;">&#91;</span><span style="color: #669966;">&#93;</span>.<span style="color: #006600;">eachWithPeek</span> <span style="color: #669966;">&#123;</span> current, peek <span style="color: #669966;">-&gt;</span>
    <span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #000000; font-weight: bold;">false</span> <span style="color: #808080; font-style: italic;">// shouldn't get here, nothing to iterate through</span>
<span style="color: #669966;">&#125;</span>
&nbsp;
<span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #669966;">&#93;</span>.<span style="color: #006600;">eachWithPeek</span> <span style="color: #669966;">&#123;</span> current, peek <span style="color: #669966;">-&gt;</span>
    <span style="color: #000000; font-weight: bold;">assert</span> current <span style="color: #669966;">==</span> <span style="color: #cc66cc;">1</span>
    <span style="color: #000000; font-weight: bold;">assert</span> peek <span style="color: #669966;">==</span> <span style="color: #000000; font-weight: bold;">null</span>  <span style="color: #808080; font-style: italic;">// only 1 element, nothing to peek at</span>
<span style="color: #669966;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> results <span style="color: #669966;">=</span> <span style="color: #669966;">&#91;</span><span style="color: #669966;">&#93;</span>
<span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span><span style="color: #669966;">&#93;</span>.<span style="color: #006600;">eachWithPeek</span> <span style="color: #669966;">&#123;</span> current, peek <span style="color: #669966;">-&gt;</span>
    results <span style="color: #669966;">&lt;&lt;</span> <span style="color: #669966;">&#91;</span>current, peek<span style="color: #669966;">&#93;</span>
<span style="color: #669966;">&#125;</span>
<span style="color: #000000; font-weight: bold;">assert</span> results <span style="color: #669966;">==</span> <span style="color: #669966;">&#91;</span><span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #669966;">&#93;</span>, <span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">3</span><span style="color: #669966;">&#93;</span>, <span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">3</span>, <span style="color: #cc66cc;">4</span><span style="color: #669966;">&#93;</span>, <span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">5</span><span style="color: #669966;">&#93;</span>, <span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">5</span>, <span style="color: #000000; font-weight: bold;">null</span><span style="color: #669966;">&#93;</span><span style="color: #669966;">&#93;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2010/06/15/groovy-each-iterator-with-peek-ahead-at-next-collection-value/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using a Unique Grails Working Directory for each Mercurial Branch</title>
		<link>http://naleid.com/blog/2010/05/07/using-a-unique-grails-working-directory-for-each-mercurial-branch/</link>
		<comments>http://naleid.com/blog/2010/05/07/using-a-unique-grails-working-directory-for-each-mercurial-branch/#comments</comments>
		<pubDate>Fri, 07 May 2010 18:07:40 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[mercurial]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[shortcut]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=184</guid>
		<description><![CDATA[At work, we&#8217;re using mercurial for our source control.  As we&#8217;ve released code to production we&#8217;ve needed to branch our repository to support what&#8217;s in production as well as ongoing development.  
By default, grails uses ~/.grails as the working directory.  If you&#8217;re doing branchy development, you can run into problems with this [...]]]></description>
			<content:encoded><![CDATA[<p>At work, we&#8217;re using <a href="http://mercurial.selenic.com/" onclick="pageTracker._trackPageview('/outgoing/mercurial.selenic.com/?referer=');">mercurial</a> for our source control.  As we&#8217;ve released code to production we&#8217;ve needed to branch our repository to support what&#8217;s in production as well as ongoing development.  </p>
<p>By default, grails uses <code>~/.grails</code> as the working directory.  If you&#8217;re doing branchy development, you can run into problems with this if you&#8217;ve got plugins installed in one branch that aren&#8217;t in the other.  Having a unique directory per branch prevents you from having to run <code>grails clean</code> all the time.</p>
<p>Here&#8217;s a quick shell script that changes the grails working directory to have the branch name as a suffix if your source is contained in a mercurial repository (ex: the default branch would be <code>~/.grails_default</code> and the 1.0 branch would be <code>~/.grails_1.0</code>).  If your application is not in a repo, it just uses the regular <code>~/.grails directory</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">#!/bin/sh</span>
<span style="color: #007800;">HG_BRANCH</span>=<span style="color: #000000; font-weight: bold;">`</span>hg branch <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null<span style="color: #000000; font-weight: bold;">`</span>
<span style="color: #007800;">GRAILS_SCRIPT</span>=<span style="color: #007800;">$GRAILS_HOME</span><span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>grails
&nbsp;
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$HG_BRANCH</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
	<span style="color: #007800;">GRAILS_WORK_DIR</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> ~<span style="color: #000000; font-weight: bold;">`/</span>.grails_<span style="color: #007800;">$HG_BRANCH</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;** grails working directory: <span style="color: #007800;">$GRAILS_WORK_DIR</span>&quot;</span>
	<span style="color: #007800;">$GRAILS_SCRIPT</span> -Dgrails.work.dir=<span style="color: #007800;">$GRAILS_WORK_DIR</span> $<span style="color: #000000; font-weight: bold;">@</span>
<span style="color: #000000; font-weight: bold;">else</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;** default grails working directory&quot;</span>
	<span style="color: #007800;">$GRAILS_SCRIPT</span> $<span style="color: #000000; font-weight: bold;">@</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>Just save this script as &#8220;grails&#8221; and put it in your PATH before the $GRAILS_HOME/bin directory (also make sure that you&#8217;ve defined $GRAILS_HOME).  I have a ~/bin directory that&#8217;s the first thing in my PATH.</p>
<p>If you use the grails-debug command, you can repeat these steps for that, just change <code>GRAILS_SCRIPT</code> to <code>$GRAILS_HOME/bin/grails-debug</code>.</p>
<p>This same technique could easily be modified to be used for other source control systems such as git or subversion.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2010/05/07/using-a-unique-grails-working-directory-for-each-mercurial-branch/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Grails Testing Alias to Rerun Failed Tests</title>
		<link>http://naleid.com/blog/2009/11/03/grails-testing-alias-to-rerun-failed-tests/</link>
		<comments>http://naleid.com/blog/2009/11/03/grails-testing-alias-to-rerun-failed-tests/#comments</comments>
		<pubDate>Wed, 04 Nov 2009 03:29:58 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[shortcut]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[zshrc]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=99</guid>
		<description><![CDATA[A while ago I blogged about my grails testing aliases and how much time they save me.
I&#8217;ve made some enhancements to them in the interim that have made them even easier to use.  
The most important alias is gtaf, which is short for &#8220;grails test-app&#8221; for failed tests.
It will search through your test output [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I <a href="http://naleid.com/blog/2009/06/14/grails-testing-command-line-aliases/">blogged about my grails testing aliases</a> and how much time they save me.</p>
<p>I&#8217;ve made some enhancements to them in the interim that have made them even easier to use.  </p>
<p>The most important alias is <code>gtaf</code>, which is short for &#8220;grails test-app&#8221; for failed tests.</p>
<p>It will search through your test output directory and look for any tests that failed.  If it finds any, it will rerun only those tests.  Otherwise, it will rerun all tests.  That makes it easy to just use <code>gtaf</code> all the time.  If any tests fail, it will open them up using Console.app.  </p>
<p>If you&#8217;re not on OSX, or would like to use something else to view the failed test logs, just modify the <code>testlog</code> alias to do something different.<br />
<span id="more-99"></span><br />
There are alternative versions of it to only run failed integration tests (<code>gtaif</code> &#8211; Grails Test App Integration Failed), failed unit tests (<code>gtauf</code> &#8211; Grails Test App Unit Failed) or attach a debugger to the tests as they run (<code>gtadf</code> &#8211; Grails Test App Debug Failed).</p>
<p>These aliases, plus most of the rest of my zsh setup, is <a href="http://bitbucket.org/tednaleid/shared-zshrc/src/" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/shared-zshrc/src/?referer=');">available on bitbucket</a>.  If you haven&#8217;t used zsh before and are still using bash, I suggest <a href="http://naleid.com/blog/2009/05/13/shared-zshrc-file/">switching over for the numerous benefits that it gives you</a>.</p>
<p>Here&#8217;s the section of zshrc_general that has the grails testing aliases, just add this to your .zshrc/.bashrc and make sure to uncomment the appropriate GRAILS_TEST_LOG_DIRECTORY export if you&#8217;re not running grails 1.2 yet:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># grails &gt; 1.2</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">GRAILS_TEST_LOG_DIRECTORY</span>=target<span style="color: #000000; font-weight: bold;">/</span>test-reports
&nbsp;
<span style="color: #666666; font-style: italic;"># grails &lt; 1.2</span>
<span style="color: #666666; font-style: italic;"># export GRAILS_TEST_LOG_DIRECTORY=test/reports</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># after grails-test if there were ERROR messages, you can open those logs with the Console.app using this</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">testlog</span>=<span style="color: #ff0000;">'for F in `grep -lE &quot;FAILED|Caused\ an\ ERROR&quot; $GRAILS_TEST_LOG_DIRECTORY/plain/*.txt`; do echo &quot;&gt;&gt;&gt; opening&quot; $F; open -a Console $F; done;'</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># grails-debug-suspend doesn't exist by default, it's a copy of grails-debug with the suspend flag changed to &quot;y&quot; so that</span>
<span style="color: #666666; font-style: italic;"># we can attach a remote debugger before it proceeds past startup</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># aliases where you can optionally pass in a set of tests to run (or no argument to run all tests in that group)</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gta</span>=grailsTestApp
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtad</span>=grailsTestAppDebug
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtau</span>=grailsTestAppUnit
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaud</span>=grailsTestAppUnitDebug
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtai</span>=grailsTestAppIntegration
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaid</span>=grailsTestAppIntegrationDebug
&nbsp;
<span style="color: #666666; font-style: italic;"># aliases that will rerun any failed tests (or all tests if there aren't any failed tests)</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaf</span>=grailsTestAppFailed
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtadf</span>=grailsTestAppDebugFailed
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtauf</span>=grailsTestAppUnitFailed
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaudf</span>=grailsTestAppUnitDebugFailed
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaif</span>=grailsTestAppIntegrationFailed
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">gtaidf</span>=grailsTestAppIntegrationDebugFailed
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTestApp<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails <span style="color: #ff0000;">&quot;&quot;</span> $<span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppFailed<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsFailedTests grails <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppDebug<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails-debug-suspend <span style="color: #ff0000;">&quot;&quot;</span> $<span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppDebugFailed<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsFailedTests grails-debug-suspend <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppUnit<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails <span style="color: #660033;">-unit</span> $<span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppUnitFailed<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsFailedTests grails <span style="color: #660033;">-unit</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppUnitDebug<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails-debug-suspend <span style="color: #660033;">-unit</span> $<span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppUnitDebugFailed<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsFailedTests grails-debug-suspend <span style="color: #660033;">-unit</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppIntegration<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails <span style="color: #660033;">-integration</span> $<span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppIntegrationFailed<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsFailedTests grails <span style="color: #660033;">-integration</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppIntegrationDebug<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsTest grails-debug-suspend <span style="color: #660033;">-integration</span> $<span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #000000; font-weight: bold;">function</span> grailsTestAppIntegrationDebugFailed<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span> grailsFailedTests grails-debug-suspend <span style="color: #660033;">-integration</span> <span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsFailedTests<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #007800;">FAILED_TESTS</span>=<span style="color: #ff0000;">''</span>
	<span style="color: #000000; font-weight: bold;">for</span> F <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-lE</span> <span style="color: #ff0000;">&quot;FAILED|Caused\ an\ ERROR&quot;</span> <span style="color: #007800;">$GRAILS_TEST_LOG_DIRECTORY</span><span style="color: #000000; font-weight: bold;">/</span>plain<span style="color: #000000; font-weight: bold;">/*</span>.txt<span style="color: #000000; font-weight: bold;">`</span>; <span style="color: #000000; font-weight: bold;">do</span>
		<span style="color: #007800;">FAILED_TESTS</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$FAILED_TESTS</span> <span style="color: #780078;">`echo $F | sed -E 's/.*TEST-(.*)Tests.txt/\1/g'`</span>&quot;</span>
	<span style="color: #000000; font-weight: bold;">done</span>;
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;failed tests: <span style="color: #007800;">$FAILED_TESTS</span>&quot;</span>
	grailsTest $<span style="color: #000000;">1</span> $<span style="color: #000000;">2</span> <span style="color: #007800;">$FAILED_TESTS</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> grailsTest<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
	<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Running: $1 test-app $2 $3 || testlog &quot;</span>
    <span style="color: #000000; font-weight: bold;">time</span> $<span style="color: #000000;">1</span> test-app $<span style="color: #000000;">2</span> $<span style="color: #000000;">3</span> <span style="color: #000000; font-weight: bold;">||</span> testlog
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>I believe that they&#8217;ll also work fine as-is in bash, though I haven&#8217;t tested them.</p>
<p><script type="text/javascript">var dzone_style = '1';</script><br />
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2009/11/03/grails-testing-alias-to-rerun-failed-tests/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Poor Man&#8217;s &#8220;top&#8221; for MySQL</title>
		<link>http://naleid.com/blog/2009/06/18/poor-mans-top-for-mysql/</link>
		<comments>http://naleid.com/blog/2009/06/18/poor-mans-top-for-mysql/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 05:39:04 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[shortcut]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=57</guid>
		<description><![CDATA[I&#8217;m currently working at a startup that&#8217;s small enough that we don&#8217;t have a dedicated DBA and I&#8217;ve been doing a lot of mysql maintenance work recently.  I wanted a quick dashboard for which commands were currently running and how long they&#8217;ve been running for.  Sort of like top but for mysql.
Combining the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m currently working at a startup that&#8217;s small enough that we don&#8217;t have a dedicated DBA and I&#8217;ve been doing a lot of mysql maintenance work recently.  I wanted a quick dashboard for which commands were currently running and how long they&#8217;ve been running for.  Sort of like <a href="http://en.wikipedia.org/wiki/Top_(Unix)" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Top_Unix?referer=');">top</a> but for mysql.</p>
<p>Combining the unix <a href="http://en.wikipedia.org/wiki/Watch_(Unix)" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Watch_Unix?referer=');">&#8220;watch&#8221;</a> command with the mysql <a href="http://dev.mysql.com/doc/refman/5.1/en/show-processlist.html" onclick="pageTracker._trackPageview('/outgoing/dev.mysql.com/doc/refman/5.1/en/show-processlist.html?referer=');">&#8220;show processlist&#8221;</a> command gives me what I&#8217;m looking for.  A quick, self-updating status of the current state of the database.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">watch <span style="color: #660033;">-n</span> <span style="color: #000000;">5</span> <span style="color: #660033;">--differences</span> <span style="color: #ff0000;">&quot;mysql -u username -psekrit -e 'show processlist'&quot;</span></pre></div></div>

<p>Shows something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">Every 5.0s: mysql <span style="color: #660033;">-n</span> <span style="color: #000000;">5</span> <span style="color: #660033;">-u</span> username <span style="color: #660033;">-psekrit</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'show processlist'</span>                                   Thu Jun <span style="color: #000000;">18</span> 05:<span style="color: #000000;">25</span>:<span style="color: #000000;">14</span> <span style="color: #000000;">2009</span>
&nbsp;
Id      User    Host       db      Command Time    State         Info
<span style="color: #000000;">3141</span>    admin   localhost  mydb    Query   <span style="color: #000000;">34978</span>   freeing items SELECT <span style="color: #c20cb9; font-weight: bold;">id</span>, <span style="color: #7a0874; font-weight: bold;">type</span>, active, email FROM user WHERE email
<span style="color: #000000;">3146</span>    admin   localhost  mydb    Sleep   <span style="color: #000000;">0</span>                     NULL       
<span style="color: #000000;">24876</span>   root    localhost  NULL    Query   <span style="color: #000000;">0</span>       NULL          show processlist</pre></div></div>

<p>Where the &#8220;time&#8221; column is the number of seconds the query has been running, and Info holds the actual query (you can use &#8220;show full processlist&#8221; to see the full query).</p>
<p>Watch is a nice little linux util that runs a command every &#8220;n&#8221; seconds (it defaults to 2 seconds).  If you use the &#8211;differences switch, it will highlight the differences between one update and another.  I use watch for all kinds of monitoring activities, such as watching a directory to see a file grow in size as it gets transfered.</p>
<p>Here&#8217;s a quick shell function that you can add to your .bashrc/.bash_profile/.zshrc to use on arbitrary hosts:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> mysqltop<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #007800;">MYSQL_OPTS</span>=$<span style="color: #000000; font-weight: bold;">@</span>
    watch <span style="color: #660033;">-n</span> <span style="color: #000000;">5</span> <span style="color: #660033;">--differences</span> <span style="color: #ff0000;">&quot;mysql <span style="color: #007800;">$MYSQL_OPTS</span> -e 'show processlist'&quot;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span></pre></div></div>

<p>Then just pass in any creds/host info you need like a normal mysql command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mysqltop <span style="color: #660033;">-u</span> ted <span style="color: #660033;">-psekrit1</span> <span style="color: #660033;">-h</span> example.com <span style="color: #660033;">-P</span> <span style="color: #000000;">3307</span></pre></div></div>

<p>If you&#8217;re on linux, you probably already have &#8220;watch&#8221; installed.  If you&#8217;re on OSX, you probably don&#8217;t, but you can get it quickly through macports.  Install macports, make sure &#8220;port&#8221; is in your path and run:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">sudo</span> port <span style="color: #c20cb9; font-weight: bold;">install</span> watch</pre></div></div>

<p>There is also a command called <a href="http://jeremy.zawodny.com/mysql/mytop/" onclick="pageTracker._trackPageview('/outgoing/jeremy.zawodny.com/mysql/mytop/?referer=');">mytop</a> that you can get which looks like the same thing, but prints out the processlist details with some nicer formatting and a little extra information.  It&#8217;s in macports, but it has a number of dependencies including mysql so if you didn&#8217;t install mysql through macports, you might want to stick with what I have above or get it another way.</p>
<p>(EDIT: updated with bash function)</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2009/06/18/poor-mans-top-for-mysql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>OSX AppleScript command line util to eject all removable disks</title>
		<link>http://naleid.com/blog/2009/05/26/osx-applescript-command-line-util-to-eject-all-removable-disks/</link>
		<comments>http://naleid.com/blog/2009/05/26/osx-applescript-command-line-util-to-eject-all-removable-disks/#comments</comments>
		<pubDate>Wed, 27 May 2009 03:16:28 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[shortcut]]></category>
		<category><![CDATA[applescript]]></category>
		<category><![CDATA[eject]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=53</guid>
		<description><![CDATA[On my MacBook pro, I&#8217;ve got 4 removable hard drives (2 physical in 2 partitions each) and a JungleDisk mount.
I found it painful to manually eject each individual drive in the finder so I threw together this quick AppleScript to eject all the disks.

tell application &#34;Finder&#34;
	eject &#40;every disk&#41;
end tell

Just open up /Applications/AppleScript/ScriptEditor.app and paste that [...]]]></description>
			<content:encoded><![CDATA[<p>On my MacBook pro, I&#8217;ve got 4 removable hard drives (2 physical in 2 partitions each) and a <a href="http://www.jungledisk.com/" onclick="pageTracker._trackPageview('/outgoing/www.jungledisk.com/?referer=');">JungleDisk mount</a>.</p>
<p>I found it painful to manually eject each individual drive in the finder so I threw together this quick AppleScript to eject all the disks.</p>

<div class="wp_syntax"><div class="code"><pre class="applescript" style="font-family:monospace;"><span style="color: #ff0033; font-weight: bold;">tell</span> <span style="color: #0066ff;">application</span> <span style="color: #009900;">&quot;Finder&quot;</span>
	<span style="color: #0066ff;">eject</span> <span style="color: #000000;">&#40;</span><span style="color: #ff0033;">every</span> <span style="color: #0066ff;">disk</span><span style="color: #000000;">&#41;</span>
<span style="color: #ff0033; font-weight: bold;">end</span> <span style="color: #ff0033; font-weight: bold;">tell</span></pre></div></div>

<p>Just open up /Applications/AppleScript/ScriptEditor.app and paste that in.  Then choose &#8220;Save As&#8221; and pick &#8220;Application&#8221;.  That will compile the script and create a .app file that you can click on to run, or you can put it in your path and execute it there.</p>
<p>I think the same kind of script could be created with the command line &#8220;diskutil eject&#8221; command, but this seemed cleaner as I wasn&#8217;t able to come up with a generic way to figure out which disks were &#8220;ejectable&#8221; and which weren&#8217;t.  AppleScript is able to figure that all out for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2009/05/26/osx-applescript-command-line-util-to-eject-all-removable-disks/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Shared zshrc file</title>
		<link>http://naleid.com/blog/2009/05/13/shared-zshrc-file/</link>
		<comments>http://naleid.com/blog/2009/05/13/shared-zshrc-file/#comments</comments>
		<pubDate>Wed, 13 May 2009 05:20:02 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[shortcut]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[zshrc]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=52</guid>
		<description><![CDATA[Over the years, I&#8217;ve had a number of requests for me to share my zshrc file with friends and coworkers.  In the past, I&#8217;ve normally trimmed out the sensitive parts by hand and then e-mailed the most useful stuff.  I&#8217;ve always intended to make this an easier process and I&#8217;ve finally gotten around [...]]]></description>
			<content:encoded><![CDATA[<p>Over the years, I&#8217;ve had a number of requests for me to share my zshrc file with friends and coworkers.  In the past, I&#8217;ve normally trimmed out the sensitive parts by hand and then e-mailed the most useful stuff.  I&#8217;ve always intended to make this an easier process and I&#8217;ve finally gotten around to it.</p>
<p>I&#8217;ve created a new <a href="http://bitbucket.org/tednaleid/shared-zshrc/" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/shared-zshrc/?referer=');">bitbucket repository to hold my shared zshrc configuration</a>.  You can get it for yourself by cloning the repository:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> ~
hg clone http:<span style="color: #000000; font-weight: bold;">//</span>bitbucket.org<span style="color: #000000; font-weight: bold;">/</span>tednaleid<span style="color: #000000; font-weight: bold;">/</span>shared-zshrc<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p><span id="more-52"></span><br />
If you don&#8217;t have mercurial installed, you can just <a href="http://bitbucket.org/tednaleid/shared-zshrc/downloads/" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/shared-zshrc/downloads/?referer=');">grab the compressed archive of your choice from the downloads page</a>.</p>
<p>I&#8217;d suggest cloning/unzipping into your home directory, but you can put it wherever you want.</p>
<p>There&#8217;s an install.sh script in there that will create a new ~/.zshrc file.   If you have an existing .zshrc file, just move it out of the way and run the install script (it won&#8217;t let you whack an existing file).</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>install.sh</pre></div></div>

<p>That created .zshrc file will load up all of my shared settings and aliases from wherever you have the shared-zshrc directory.  It also will look for a host specific file where any sensitive info, or host dependent info can be added.  I normally put things like my change directory and ssh aliases in here.</p>
<p>I probably created around half of these files myself, but I couldn&#8217;t tell you for the life of me where the majority of the other half came from.  This file has accreted over the years like barnacles on a ship.</p>
<p>Some of these commands originally started out in either a tcsh file (or was it ksh?), I eventually moved to bash, and then a couple of years ago, a coworker showed me the wonders of zsh and I haven&#8217;t looked back.</p>
<p>If you&#8217;re still using bash, give zsh a try.  Changing your shell is as easy as running this command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">chsh</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">zsh</span></pre></div></div>

<h2>ZSH Specific Stuff</h2>
<p>Most of the zsh shell configuration magic is in <a href="http://bitbucket.org/tednaleid/shared-zshrc/src/tip/zshrc_compinstall" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/shared-zshrc/src/tip/zshrc_compinstall?referer=');">zshrc_compinstall</a>.  Some features include:</p>
<ul>
<li>shared history across shells</li>
<li>command spelling correction</li>
<li>vastly improved tab completion of commands (including keyboard navigation of potential matches)</li>
<li>file globbing that makes finding files easy (try <code>ls **/*.txt</code> to recursively search for all txt files, much easier than <code>find . -name "*.txt"</code></li>
<li>automatically using pushd for cd commands, use &#8220;dh&#8221; to see a history of your directories, use cd -N to go back N directories in your history (ex: <code>cd -3</code>)</li>
</ul>
<h2>Useful Aliases</h2>
<p>Some of the most useful aliases in <a href="http://bitbucket.org/tednaleid/shared-zshrc/src/tip/zshrc_general" onclick="pageTracker._trackPageview('/outgoing/bitbucket.org/tednaleid/shared-zshrc/src/tip/zshrc_general?referer=');">zshrc_general</a> include:</p>
<h3>Grep through Aliases (ag)</h3>
<p>I often forget what some of my less often used aliases are, but I know what commands they use.  I&#8217;ve set up &#8220;ag&#8221; (alias grep) to help me search through my aliases and find the right one:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">%</span> ag python
<span style="color: #007800;">webshare</span>=<span style="color: #ff0000;">'python -m SimpleHTTPServer'</span></pre></div></div>

<h3>Grepping History (gh)</h3>
<p>Great for finding commands in your history that you can only remember part of.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">  gh grails
  ...
  <span style="color: #000000;">582</span>  grails create-app testDates
  <span style="color: #000000;">584</span>  grails create-domain-class Foo
  <span style="color: #000000;">585</span>  grails generate-all Foo
  <span style="color: #000000;">587</span>  <span style="color: #7a0874; font-weight: bold;">cd</span> grails-app
  <span style="color: #000000;">591</span>  grails generate-views Foo
  <span style="color: #000000;">592</span>  grails run-app</pre></div></div>

<h3>Global Alias &#8211; GV &#8211; to &#8220;grep -v&#8221;</h3>
<p>Grep is great for finding things, but often you find too much and need to slice stuff out.  If you use &#8220;grep -v&#8221; that finds all lines that <b>don&#8217;t</b> match a pattern.  The global alias &#8220;GV&#8221; that makes this even easier.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">   <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-ri</span> foo</pre></div></div>

<p>If that finds too much stuff (say catalina log files and svn files that you don&#8217;t care about), just slice them out with GV:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">   <span style="color: #c20cb9; font-weight: bold;">grep</span> <span style="color: #660033;">-ri</span> foo GV <span style="color: #c20cb9; font-weight: bold;">svn</span> GV catalina</pre></div></div>

<p>That will remove any lines that have &#8220;svn&#8221; or &#8220;catalina&#8221; anywhere in them (including the file name).</p>
<p>I think that rc files tend to be very personal things, what works for me likely won&#8217;t work for you exactly as it is.  But I also love to look at other people&#8217;s rc files to grab the things that look interesting.  Feel free to poke through mine and grab whatever strikes your fancy.</p>
<p>There&#8217;s a ton of other stuff in there that I use every day, including sections specific to version control (svn and hg) as well as groovy/grails shortcuts.  Feel free to poke around and ask me any questions about what something does.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2009/05/13/shared-zshrc-file/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Groovy: Enhancement to String class to add a regexp find method</title>
		<link>http://naleid.com/blog/2009/03/28/groovy-enhancement-to-string-class-to-add-a-regexp-find-method/</link>
		<comments>http://naleid.com/blog/2009/03/28/groovy-enhancement-to-string-class-to-add-a-regexp-find-method/#comments</comments>
		<pubDate>Sun, 29 Mar 2009 03:47:47 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[groovy]]></category>
		<category><![CDATA[shortcut]]></category>
		<category><![CDATA[regexp]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=49</guid>
		<description><![CDATA[I just submitted a groovy patch that enhances the String class with a &#8220;find&#8221; method that makes working with regular expressions much easier.
One of the most common use cases is to search a string for a regular expression pattern.  If a match is found, then do something with the matched value.

Currently in groovy, the [...]]]></description>
			<content:encoded><![CDATA[<p>I just submitted a <a href="http://jira.codehaus.org/browse/GROOVY-3443" onclick="pageTracker._trackPageview('/outgoing/jira.codehaus.org/browse/GROOVY-3443?referer=');">groovy patch</a> that enhances the String class with a &#8220;find&#8221; method that makes working with regular expressions much easier.</p>
<p>One of the most common use cases is to search a string for a regular expression pattern.  If a match is found, then do something with the matched value.<br />
<span id="more-49"></span><br />
Currently in groovy, the recommended way to do this is to create a matcher and then use indexes to work with any matches that might be found:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #aa0000;">&quot;10292&quot;</span> <span style="color: #669966;">==</span> <span style="color: #669966;">&#40;</span><span style="color: #aa0000;">&quot;New York, NY 10292&quot;</span> <span style="color: #669966;">=</span>~ /\d<span style="color: #669966;">&#123;</span><span style="color: #cc66cc;">5</span><span style="color: #669966;">&#125;</span>/<span style="color: #669966;">&#41;</span><span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #669966;">&#93;</span></pre></div></div>

<p>If you try to do that on a string that doesn&#8217;t actually match the regular expression, you&#8217;ll get an IndexOutOfBoundException.  To be safe, you need to check matcher.find() (not &#8220;matches&#8221; as that requires the entire string to match!) to see if the string is actually in there:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> m <span style="color: #669966;">=</span> <span style="color: #669966;">&#40;</span><span style="color: #aa0000;">&quot;New York, NY&quot;</span> <span style="color: #669966;">=</span>~ /\d<span style="color: #669966;">&#123;</span><span style="color: #cc66cc;">5</span><span style="color: #669966;">&#125;</span>/<span style="color: #669966;">&#41;</span><span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #669966;">&#93;</span>
<span style="color: #000000; font-weight: bold;">def</span> zip
<span style="color: #996600;">if</span> <span style="color: #669966;">&#40;</span>m.<span style="color: #663399;">find</span><span style="color: #669966;">&#40;</span><span style="color: #669966;">&#41;</span><span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span>
   zip <span style="color: #669966;">=</span> m<span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #669966;">&#93;</span>
<span style="color: #669966;">&#125;</span></pre></div></div>

<p>It also has inconsistent behavior if the regular expression happens to have capture groups in it.  Then it returns an array containing the match and the capture groups, forcing you to index into that array to actually get the match you want:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #aa0000;">&quot;c&quot;</span> <span style="color: #669966;">==</span> <span style="color: #669966;">&#40;</span><span style="color: #aa0000;">&quot;foo car baz&quot;</span> <span style="color: #669966;">=</span>~ /<span style="color: #669966;">&#40;</span>.<span style="color: #669966;">&#41;</span>ar/<span style="color: #669966;">&#41;</span><span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #669966;">&#93;</span><span style="color: #669966;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #669966;">&#93;</span></pre></div></div>

<p>Groovy has already added closure aware replace method to the String class.  The patch adds a complimentary find method to string that will return the string matched by the closure without needing to worry about matcher objects and array indexes.</p>
<p>You can either call it without a closure to get the full found match back (even if it has groups in it):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #aa0000;">&quot;10292&quot;</span> <span style="color: #669966;">==</span> <span style="color: #aa0000;">&quot;New York, NY 10292&quot;</span>.<span style="color: #663399;">find</span><span style="color: #669966;">&#40;</span>/\d<span style="color: #669966;">&#123;</span><span style="color: #cc66cc;">5</span><span style="color: #669966;">&#125;</span>/<span style="color: #669966;">&#41;</span></pre></div></div>

<p>It safely returns a null if the match isn&#8217;t found, which can clean up boilerplate safety checks quite a bit.  The user can check for null using groovy truth if they want to:</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">def</span> zip <span style="color: #669966;">=</span> <span style="color: #aa0000;">&quot;New York, NY&quot;</span>.<span style="color: #663399;">find</span><span style="color: #669966;">&#40;</span>/\d<span style="color: #669966;">&#123;</span><span style="color: #cc66cc;">5</span><span style="color: #669966;">&#125;</span>/<span style="color: #669966;">&#41;</span>   <span style="color: #808080; font-style: italic;">// returns null</span>
&nbsp;
<span style="color: #996600;">if</span> <span style="color: #669966;">&#40;</span>zip<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span> ... <span style="color: #669966;">&#125;</span></pre></div></div>

<p>If you want to work with capture groups, or manipulate the value, you can pass a closure to the find method that will be passed the full match as well as any capture groups (just as the collection based regular expression methods work):</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">// no capture groups, only the match is passed to the closure</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #aa0000;">&quot;bar&quot;</span> <span style="color: #669966;">==</span> <span style="color: #aa0000;">&quot;foo bar baz&quot;</span>.<span style="color: #663399;">find</span><span style="color: #669966;">&#40;</span>/.<span style="color: #006600;">ar</span>/<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span> match <span style="color: #669966;">-&gt;</span> <span style="color: #000000; font-weight: bold;">return</span> match <span style="color: #669966;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// one capture group</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #aa0000;">&quot;b&quot;</span> <span style="color: #669966;">==</span> <span style="color: #aa0000;">&quot;foo bar baz&quot;</span>.<span style="color: #663399;">find</span><span style="color: #669966;">&#40;</span>/<span style="color: #669966;">&#40;</span>.<span style="color: #669966;">&#41;</span>ar/<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span> match, firstLetter <span style="color: #669966;">-&gt;</span> <span style="color: #000000; font-weight: bold;">return</span> firstLetter <span style="color: #669966;">&#125;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">// many capture groups, all passed to the closure after the full match</span>
<span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #aa0000;">&quot;2339999&quot;</span> <span style="color: #669966;">==</span> <span style="color: #aa0000;">&quot;adsf 233-9999 adsf&quot;</span>.<span style="color: #663399;">find</span><span style="color: #669966;">&#40;</span>/<span style="color: #669966;">&#40;</span>\d<span style="color: #669966;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #669966;">&#125;</span><span style="color: #669966;">&#41;</span><span style="color: #669966;">?-?</span><span style="color: #669966;">&#40;</span>\d<span style="color: #669966;">&#123;</span><span style="color: #cc66cc;">3</span><span style="color: #669966;">&#125;</span><span style="color: #669966;">&#41;</span><span style="color: #669966;">-</span><span style="color: #669966;">&#40;</span>\d<span style="color: #669966;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #669966;">&#125;</span><span style="color: #669966;">&#41;</span>/<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span> match, areaCode, exchange, stationNumber <span style="color: #669966;">-&gt;</span>
    <span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #aa0000;">&quot;233-9999&quot;</span> <span style="color: #669966;">==</span> match
    <span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #000000; font-weight: bold;">null</span> <span style="color: #669966;">==</span> areaCode
    <span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #aa0000;">&quot;233&quot;</span> <span style="color: #669966;">==</span> exchange
    <span style="color: #000000; font-weight: bold;">assert</span> <span style="color: #aa0000;">&quot;9999&quot;</span> <span style="color: #669966;">==</span> stationNumber
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #aa0000;">&quot;$exchange$stationNumber&quot;</span>
<span style="color: #669966;">&#125;</span></pre></div></div>

<p>If you think this would be a valuable addition to groovy, you can <a href="http://jira.codehaus.org/browse/GROOVY-3443" onclick="pageTracker._trackPageview('/outgoing/jira.codehaus.org/browse/GROOVY-3443?referer=');">vote for it in JIRA</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2009/03/28/groovy-enhancement-to-string-class-to-add-a-regexp-find-method/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Bash/Zsh aliases to switch Groovy and Grails version</title>
		<link>http://naleid.com/blog/2009/03/08/bashzsh-aliases-to-switch-groovy-and-grails-version/</link>
		<comments>http://naleid.com/blog/2009/03/08/bashzsh-aliases-to-switch-groovy-and-grails-version/#comments</comments>
		<pubDate>Mon, 09 Mar 2009 00:58:34 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[shortcut]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=46</guid>
		<description><![CDATA[I work on a couple of different grails projects that use a variety of versions of groovy and grails.  I&#8217;ve thrown together a quick shell script that makes it easy to create a new alias to switch between different versions depending on what project you&#8217;re working with.

function switchGrails&#40;&#41; &#123;
    echo &#34;Switching [...]]]></description>
			<content:encoded><![CDATA[<p>I work on a couple of different grails projects that use a variety of versions of groovy and grails.  I&#8217;ve thrown together a quick shell script that makes it easy to create a new alias to switch between different versions depending on what project you&#8217;re working with.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> switchGrails<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Switching to groovy version: $1&quot;</span>
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Switching to grails version: $2&quot;</span>
    <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>groovy,grails<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
    <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>$<span style="color: #000000;">1</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>groovy
    <span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">ln</span> <span style="color: #660033;">-s</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>$<span style="color: #000000;">2</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>grails
    <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Done!&quot;</span>
    <span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-latr</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span><span style="color: #7a0874; font-weight: bold;">&#123;</span>groovy,grails<span style="color: #7a0874; font-weight: bold;">&#125;</span>
<span style="color: #7a0874; font-weight: bold;">&#125;</span>
&nbsp;
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">g104</span>=<span style="color: #ff0000;">'switchGrails &quot;groovy-1.5.7&quot; &quot;grails-1.0.4&quot;'</span>
<span style="color: #7a0874; font-weight: bold;">alias</span> <span style="color: #007800;">g11rc2</span>=<span style="color: #ff0000;">'switchGrails &quot;groovy-1.6.0&quot; &quot;grails-1.1-RC2&quot;'</span></pre></div></div>

<p>You can create your own aliases like the ones above to switch to the groovy/grails combinations that you happen to be working with.  </p>
<p>Just stick the code above in your .profile/.bashrc/.zshrc file and restart your shell to make the aliases available.</p>
<p>This function assumes that you&#8217;ve got you&#8217;ve got groovy and grails installed in your /usr/local directory and that you use a symlink at /usr/local/groovy that $GROOVY_HOME is pointed to and /usr/local/grails that $GRAILS_HOME is pointed to.  If those assumptions aren&#8217;t correct for you, you&#8217;ll have to tweak the script.</p>
<p>It also uses &#8220;sudo&#8221; as it assumes that /usr/local is owned by root and not by your logged in user.  If you&#8217;ve chowned the directory to yourself, you can remove the sudo (and the need to enter your password).</p>
<p>Now it&#8217;s easy for me to switch between different projects by just typing the appropriate alias:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">pollux<span style="color: #000000; font-weight: bold;">%</span> g104
Switching to groovy version: groovy-1.5.7
Switching to grails version: grails-1.0.4
Done<span style="color: #000000; font-weight: bold;">!</span>
lrwxr-xr-x  <span style="color: #000000;">1</span> root  wheel  <span style="color: #000000;">23</span> Mar  <span style="color: #000000;">8</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">55</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>groovy -<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>groovy-1.5.7
lrwxr-xr-x  <span style="color: #000000;">1</span> root  wheel  <span style="color: #000000;">23</span> Mar  <span style="color: #000000;">8</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">55</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>grails -<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>grails-1.0.4
&nbsp;
&nbsp;
pollux<span style="color: #000000; font-weight: bold;">%</span> g11rc2 
Switching to groovy version: groovy-1.6.0
Switching to grails version: grails-<span style="color: #000000;">1.1</span>-RC2
Done<span style="color: #000000; font-weight: bold;">!</span>
lrwxr-xr-x  <span style="color: #000000;">1</span> root  wheel  <span style="color: #000000;">23</span> Mar  <span style="color: #000000;">8</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">56</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>groovy -<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>groovy-1.6.0
lrwxr-xr-x  <span style="color: #000000;">1</span> root  wheel  <span style="color: #000000;">25</span> Mar  <span style="color: #000000;">8</span> <span style="color: #000000;">19</span>:<span style="color: #000000;">56</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>grails -<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>local<span style="color: #000000; font-weight: bold;">/</span>grails-<span style="color: #000000;">1.1</span>-RC2</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2009/03/08/bashzsh-aliases-to-switch-groovy-and-grails-version/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Using Groovy Regular Expressions to Parse Code From a Markdown File</title>
		<link>http://naleid.com/blog/2009/01/01/using-groovy-regular-expressions-to-parse-code-from-a-markdown-file/</link>
		<comments>http://naleid.com/blog/2009/01/01/using-groovy-regular-expressions-to-parse-code-from-a-markdown-file/#comments</comments>
		<pubDate>Thu, 01 Jan 2009 16:55:37 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[groovy]]></category>
		<category><![CDATA[shortcut]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[groovymag]]></category>
		<category><![CDATA[jig]]></category>
		<category><![CDATA[markdown]]></category>
		<category><![CDATA[regexp]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[shim]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=41</guid>
		<description><![CDATA[The January 2009 issue of GroovyMag was released today.  In it, I&#8217;ve written an article that shows how easy regular expressions are to use in groovy.  It starts with some regular expression basics, shows some common idiomatic groovy usage patterns and wraps up with some of the cool new features that groovy 1.6 [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://www.groovymag.com/main.issues.description/id=5/" onclick="pageTracker._trackPageview('/outgoing/www.groovymag.com/main.issues.description/id=5/?referer=');">January 2009 issue of GroovyMag</a> was released today.  In it, I&#8217;ve written an article that shows how easy regular expressions are to use in groovy.  It starts with some regular expression basics, shows some common idiomatic groovy usage patterns and wraps up with some of the cool new features that groovy 1.6 is adding to regexp handling.</p>
<p>There are over 30 code samples in then article and I wanted to make sure while writing and editing that all of the code samples ran exactly as they appeared in the article text.  Also, when you download an issue of GroovyMag, you get a zip file that has a PDF and a set of code &#8220;listing&#8221; files for each article.  Each listing file contains a snippet of groovy code that appeared in the issue.</p>
<p><a href='http://naleid.com/blog/wp-content/uploads/2009/01/listings.png'><img src="http://naleid.com/blog/wp-content/uploads/2009/01/listings.png" alt="Directory screenshot showing listing files" title="listings" class="alignnone size-full wp-image-43" /></a></p>
<p>I decided to write a couple of simple groovy scripts to keep things <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Don_27t_repeat_yourself?referer=');">DRY</a>, and to ensure that my edits didn&#8217;t break anything.  The first script extracts code listings out of my draft article and saved them to individually numbered listing files.  The second script executes each of the listing files and reported success or failure for each.  Sort of a poor man&#8217;s JUnit for writing articles.</p>
<p><span id="more-41"></span></p>
<p>Both of these scripts leverage regular expressions and the techniques that are outlined in GroovyMag.</p>
<p>I wrote the article using the <a href="http://daringfireball.net/projects/markdown/syntax" onclick="pageTracker._trackPageview('/outgoing/daringfireball.net/projects/markdown/syntax?referer=');">markdown syntax</a>.  If you haven&#8217;t seen markdown before, it&#8217;s a wiki-like syntax that is very easy to read and write, and can also be converted into a variety of formats easily, including HTML.  </p>
<p>In markdown, code blocks are simply lines that are separated from other lines by at least one blank line (or the start of the file) and are indented at least 4 spaces.</p>
<p><a href='http://naleid.com/blog/wp-content/uploads/2009/01/markdown_code.png'><img src="http://naleid.com/blog/wp-content/uploads/2009/01/markdown_code.png" alt="Markdown Screenshot Showing Code Indentation" title="markdown_code" width="700"  class="alignnone size-full wp-image-43" /></a></p>
<p>This is the script that I used to parse through the markdown file.  It uses a regular expression to find all of the code blocks in the markdown and then writes each of them to it&#8217;s own listing file.  Listing files were to be named consecutively from listing_1.txt through listing_n.txt.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/env groovy</span>
INPUT_START_OR_BLANK_LINE <span style="color: #669966;">=</span> /<span style="color: #669966;">&#40;</span><span style="color: #669966;">?</span>:\A<span style="color: #669966;">|</span>\n\n<span style="color: #669966;">&#41;</span>/
FOUR_SPACES_OR_TAB <span style="color: #669966;">=</span> /<span style="color: #669966;">&#40;</span><span style="color: #669966;">?</span>:<span style="color: #669966;">&#91;</span> <span style="color: #669966;">&#93;</span><span style="color: #669966;">&#123;</span><span style="color: #cc66cc;">4</span><span style="color: #669966;">&#125;</span><span style="color: #669966;">|</span>\t<span style="color: #669966;">&#41;</span>/
CODE <span style="color: #669966;">=</span> /.<span style="color: #669966;">*</span>\n<span style="color: #669966;">+</span>/
CODE_LINES <span style="color: #669966;">=</span> /<span style="color: #669966;">&#40;</span><span style="color: #669966;">?</span>:$FOUR_SPACES_OR_TAB$CODE<span style="color: #669966;">&#41;</span>/
LOOKAHEAD_FOR_NON_CODE_LINE <span style="color: #669966;">=</span> /<span style="color: #669966;">&#40;</span><span style="color: #669966;">?</span>:<span style="color: #669966;">&#40;</span><span style="color: #669966;">?=</span>^<span style="color: #669966;">&#91;</span> <span style="color: #669966;">&#93;</span><span style="color: #669966;">&#123;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">4</span><span style="color: #669966;">&#125;</span>\S<span style="color: #669966;">&#41;</span><span style="color: #669966;">|</span>\Z<span style="color: #669966;">&#41;</span>/
&nbsp;
<span style="color: #808080; font-style: italic;">// this regular expression will find all of the consecutive code lines in a markdown file</span>
<span style="color: #808080; font-style: italic;">// in a markdown file, if the line starts with a tab or at least 4 spaces, it's a code line</span>
<span style="color: #808080; font-style: italic;">// slightly modified from one in markdownj</span>
<span style="color: #808080; font-style: italic;">// see: http://github.com/myabc/markdownj/tree/master/src/java/com/petebevin/markdown/MarkdownProcessor.java</span>
MARKDOWN_CODE_BLOCK <span style="color: #669966;">=</span> <span style="color: #aa0000;">&quot;(?m)&quot;</span> <span style="color: #669966;">+</span> 
                      <span style="color: #aa0000;">&quot;$INPUT_START_OR_BLANK_LINE&quot;</span> <span style="color: #669966;">+</span>
                      <span style="color: #aa0000;">&quot;($CODE_LINES+)&quot;</span> <span style="color: #669966;">+</span>
                      <span style="color: #aa0000;">&quot;$LOOKAHEAD_FOR_NON_CODE_LINE&quot;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> removeOldListings<span style="color: #669966;">&#40;</span>dir<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span>
    dir.<span style="color: #006600;">eachFileMatch</span><span style="color: #669966;">&#40;</span>~/.<span style="color: #669966;">*</span>listing_\d<span style="color: #669966;">+</span>\.<span style="color: #006600;">txt</span>/<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span> file <span style="color: #669966;">-&gt;</span>
        <span style="color: #663366;">println</span> <span style="color: #aa0000;">&quot;Removing $file&quot;</span>
        file.<span style="color: #006600;">delete</span><span style="color: #669966;">&#40;</span><span style="color: #669966;">&#41;</span>
    <span style="color: #669966;">&#125;</span>   
<span style="color: #669966;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">def</span> createListings<span style="color: #669966;">&#40;</span>file<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span>
    listingNumber <span style="color: #669966;">=</span> <span style="color: #cc66cc;">1</span>
    <span style="color: #669966;">&#40;</span>file.<span style="color: #006600;">text</span> <span style="color: #669966;">=</span>~ MARKDOWN_CODE_BLOCK<span style="color: #669966;">&#41;</span>.<span style="color: #663399;">each</span> <span style="color: #669966;">&#123;</span> full, codeBlock <span style="color: #669966;">-&gt;</span>
        <span style="color: #000000; font-weight: bold;">def</span> listing <span style="color: #669966;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">File</span><span style="color: #669966;">&#40;</span><span style="color: #aa0000;">&quot;listing_${(listingNumber++).toString().padLeft(3,'0')}.txt&quot;</span><span style="color: #669966;">&#41;</span> 
        <span style="color: #663366;">println</span> <span style="color: #aa0000;">&quot;Creating $listing&quot;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;">// groovy's String.eachLine skips blank lines, but we want these in our source </span>
        <span style="color: #808080; font-style: italic;">// to make things more readable so we'll make our own eachLine</span>
        <span style="color: #669966;">&#40;</span>codeBlock <span style="color: #669966;">=</span>~ /.<span style="color: #669966;">*</span>/<span style="color: #669966;">&#41;</span>.<span style="color: #663399;">each</span> <span style="color: #669966;">&#123;</span> line <span style="color: #669966;">-&gt;</span>          
            <span style="color: #808080; font-style: italic;">// each markdown code block comes back with a tab or 4 spaces at the beginning, strip those off</span>
            <span style="color: #000000; font-weight: bold;">def</span> matcher <span style="color: #669966;">=</span> <span style="color: #669966;">&#40;</span>line <span style="color: #669966;">=</span>~ /$FOUR_SPACES_OR_TAB<span style="color: #669966;">&#40;</span>.<span style="color: #669966;">*</span><span style="color: #669966;">&#41;</span>/<span style="color: #669966;">&#41;</span>
            <span style="color: #996600;">if</span> <span style="color: #669966;">&#40;</span>matcher.<span style="color: #006600;">matches</span><span style="color: #669966;">&#40;</span><span style="color: #669966;">&#41;</span><span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span>
                matcher.<span style="color: #663399;">find</span> <span style="color: #669966;">&#123;</span> fullLine, code <span style="color: #669966;">-&gt;</span>  listing <span style="color: #669966;">&lt;&lt;</span> <span style="color: #aa0000;">&quot;$code&quot;</span> <span style="color: #669966;">&#125;</span>
            <span style="color: #669966;">&#125;</span> <span style="color: #996600;">else</span> <span style="color: #669966;">&#123;</span>
                listing <span style="color: #669966;">&lt;&lt;</span> <span style="color: #aa0000;">&quot;$line<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>
            <span style="color: #669966;">&#125;</span>
        <span style="color: #669966;">&#125;</span>
    <span style="color: #669966;">&#125;</span>
<span style="color: #669966;">&#125;</span>
&nbsp;
removeOldListings<span style="color: #669966;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">File</span><span style="color: #669966;">&#40;</span><span style="color: #aa0000;">&quot;.&quot;</span><span style="color: #669966;">&#41;</span><span style="color: #669966;">&#41;</span>
createListings<span style="color: #669966;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">File</span><span style="color: #669966;">&#40;</span><span style="color: #aa0000;">&quot;article.markdown&quot;</span><span style="color: #669966;">&#41;</span><span style="color: #669966;">&#41;</span></pre></div></div>

<p>Once I had all of those listing file, I used this script to execute each of the listing files and report whether any problems had occurred during the execution.  It uses the &#8220;eachFileMatch&#8221; method groovy adds to the File object, which you can give a regular expression pattern so that you can iterate over a targeted subset of files to process.</p>

<div class="wp_syntax"><div class="code"><pre class="groovy" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#! /usr/bin/env groovy</span>
<span style="color: #000000; font-weight: bold;">def</span> executeListings<span style="color: #669966;">&#40;</span>listingDir<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span>
    listingDir.<span style="color: #006600;">eachFileMatch</span><span style="color: #669966;">&#40;</span>~/.<span style="color: #669966;">*</span>listing_\d<span style="color: #669966;">+</span>\.<span style="color: #006600;">txt</span>/<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span> listing <span style="color: #669966;">-&gt;</span>
        <span style="color: #663366;">print</span> <span style="color: #aa0000;">&quot;Executing $listing...&quot;</span>       
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #669966;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">new</span> GroovyShell<span style="color: #669966;">&#40;</span><span style="color: #669966;">&#41;</span>.<span style="color: #006600;">evaluate</span><span style="color: #669966;">&#40;</span>listing<span style="color: #669966;">&#41;</span>
            <span style="color: #663366;">println</span> <span style="color: #aa0000;">&quot;Success!&quot;</span>
        <span style="color: #669966;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #669966;">&#40;</span>java.<span style="color: #006600;">lang</span>.<span style="color: #006600;">AssertionError</span> e<span style="color: #669966;">&#41;</span> <span style="color: #669966;">&#123;</span>
            <span style="color: #663366;">println</span> <span style="color: #aa0000;">&quot;ERROR!&quot;</span>
            e.<span style="color: #006600;">printStackTrace</span><span style="color: #669966;">&#40;</span><span style="color: #669966;">&#41;</span>
        <span style="color: #669966;">&#125;</span>
    <span style="color: #669966;">&#125;</span>   
<span style="color: #669966;">&#125;</span>
&nbsp;
executeListings<span style="color: #669966;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #aaaadd; font-weight: bold;">File</span><span style="color: #669966;">&#40;</span><span style="color: #aa0000;">&quot;.&quot;</span><span style="color: #669966;">&#41;</span><span style="color: #669966;">&#41;</span></pre></div></div>

<p>Just like a good set of unit tests, these scripts gave me the courage to make edits to my article, without needing to worry if I was breaking something or forgetting to make a change.  It&#8217;s a different kind of meta-programming when I can use regular expressions to help me write about using regular expressions :)</p>
<p><script type="text/javascript">var dzone_url = 'http://naleid.com/blog/2009/01/01/using-groovy-regular-expressions-to-parse-code-from-a-markdown-file/';</script><br />
<script type="text/javascript">var dzone_style = '1';</script><br />
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2009/01/01/using-groovy-regular-expressions-to-parse-code-from-a-markdown-file/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Getting Grails New Uber Generate-All to Proceed without Prompting</title>
		<link>http://naleid.com/blog/2008/12/09/getting-grails-new-uber-generate-all-to-proceed-without-prompting/</link>
		<comments>http://naleid.com/blog/2008/12/09/getting-grails-new-uber-generate-all-to-proceed-without-prompting/#comments</comments>
		<pubDate>Tue, 09 Dec 2008 22:20:43 +0000</pubDate>
		<dc:creator>tednaleid</dc:creator>
				<category><![CDATA[command line]]></category>
		<category><![CDATA[grails]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[osx]]></category>
		<category><![CDATA[shortcut]]></category>
		<category><![CDATA[generate all]]></category>
		<category><![CDATA[scaffolding]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[template]]></category>

		<guid isPermaLink="false">http://naleid.com/blog/?p=39</guid>
		<description><![CDATA[In my current project, I&#8217;ve been doing a lot of tweaking of the default grails scaffolding templates.  Because of this, I need to run the new uber generate-all command quite a bit to recreate things.
The one problem with this script is that if the files already exist, a prompt will come up after ~10 [...]]]></description>
			<content:encoded><![CDATA[<p>In my current project, I&#8217;ve been doing a lot of tweaking of the default grails scaffolding templates.  Because of this, I need to run the new uber generate-all command quite a bit to recreate things.</p>
<p>The one problem with this script is that if the files already exist, a prompt will come up after ~10 seconds or so (after the grails environment bootstraps) asking you if you want to Overwrite everything:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">grails generate-all <span style="color: #ff0000;">&quot;*&quot;</span> 
...
~<span style="color: #000000;">10</span> seconds pass
...
Generating views <span style="color: #000000; font-weight: bold;">for</span> domain class Baz ...
File <span style="color: #000000; font-weight: bold;">/</span>foobar<span style="color: #000000; font-weight: bold;">/</span>grails-app<span style="color: #000000; font-weight: bold;">/</span>views<span style="color: #000000; font-weight: bold;">/</span>baz<span style="color: #000000; font-weight: bold;">/</span>list.gsp already exists. Overwrite?y,n,a</pre></div></div>

<p>This was a bit of a pain as I&#8217;d often kick the script off, get distracted and then come back to the shell with that prompt still waiting for me to tell it what to do.  I&#8217;d rather just start working with my shiny new scaffolding.</p>
<p>The easy solution to this is simply to pipe the answer you want into the grails command:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;a&quot;</span> <span style="color: #000000; font-weight: bold;">|</span> grails generate-all <span style="color: #ff0000;">&quot;*&quot;</span></pre></div></div>

<p>Doing that will pipe the &#8220;a&#8221; into the grails command so that when the prompt finally comes up, it knows that it can continue regenerating all of my scaffolding.</p>
<p>It seems simple enough after I figured it out, but I thought it could save some other people time when they&#8217;re hacking around with templates and know they want to regenerate all of them.<br />
<span id="more-39"></span><br />
<script type="text/javascript">var dzone_style = '1';</script><br />
<script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script> </p>
]]></content:encoded>
			<wfw:commentRss>http://naleid.com/blog/2008/12/09/getting-grails-new-uber-generate-all-to-proceed-without-prompting/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
