<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Thorsten Lorenz</title>
	<atom:link href="http://thorstenlorenz.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://thorstenlorenz.wordpress.com</link>
	<description>The coding cycle diaries</description>
	<lastBuildDate>Fri, 27 Jan 2012 19:16:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='thorstenlorenz.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Thorsten Lorenz</title>
		<link>http://thorstenlorenz.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://thorstenlorenz.wordpress.com/osd.xml" title="Thorsten Lorenz" />
	<atom:link rel='hub' href='http://thorstenlorenz.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Bookmarklet to turn off jQuery animations</title>
		<link>http://thorstenlorenz.wordpress.com/2012/01/08/bookmarklet-to-turn-off-jquery-animations/</link>
		<comments>http://thorstenlorenz.wordpress.com/2012/01/08/bookmarklet-to-turn-off-jquery-animations/#comments</comments>
		<pubDate>Sun, 08 Jan 2012 18:09:53 +0000</pubDate>
		<dc:creator>Thorsten Lorenz</dc:creator>
				<category><![CDATA[Bookmarklets]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://thorstenlorenz.wordpress.com/?p=427</guid>
		<description><![CDATA[Animations on websites are usually nice, but not always. One example, where an animation gets annoying is github. They sport a slide animation when going back and forward while browsing the code. I found this annoying in general, but it became unacceptable once I started using Safari on Lion OS when browsing github repos. As [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=427&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Animations on websites are usually nice, but not always.</p>
<p>One example, where an animation gets annoying is <a href="http://github.com">github</a>. They sport a slide animation when going back and forward while browsing the code.</p>
<p>I found this annoying in general, but it became unacceptable once I started using Safari on Lion OS when browsing github repos.</p>
<p>As you know Safari already has a sliding animation when going backward or forward in the browser using the two finger gesture on the trackpad.</p>
<p>With the added github animation, you see two sliding animations right after each other.</p>
<p>Turns out a simple bookmarklet can help.</p>
<p>Since github is using jQuery, turning off all jQuery animations does the trick.</p>
<p>Unfortunately WordPress keeps expanding bookmarklet urls to, so you&#8217;ll have to follow these steps in order to install it:</p>
<ul>
<li>Copy the code snippet below</li>
<li>create a new bookmark, name it something like StopFx</li>
<li>edit the bookmark and paste the copied text into the URL/Address field.</li>
</ul>
<pre>javascript:(function(){jQuery.fx.off=true;})();</pre>
<p>Once you arrive at a github repo invoke the bookmarklet and enjoy distraction free source code browsing.</p>
<p>Of course this will work  for any website with annoying jQuery animations,</p>
<p>In case you are interested in creating your own Bookmarklets, you can find more information <a href="http://thorstenlorenz.wordpress.com/2011/05/21/having-fun-with-javascript-bookmarklets-to-determine-loaded-libraries/">here</a>.</p>
<p>As a final note, in order to edit bookmarks in Safari, you have to select &#8220;Show All Bookmarks&#8221; from the &#8220;Bookmarks&#8221; folder. I am adding this, because that was not at all obvious to me.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thorstenlorenz.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thorstenlorenz.wordpress.com/427/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thorstenlorenz.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thorstenlorenz.wordpress.com/427/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thorstenlorenz.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thorstenlorenz.wordpress.com/427/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thorstenlorenz.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thorstenlorenz.wordpress.com/427/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thorstenlorenz.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thorstenlorenz.wordpress.com/427/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thorstenlorenz.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thorstenlorenz.wordpress.com/427/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thorstenlorenz.wordpress.com/427/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thorstenlorenz.wordpress.com/427/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=427&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thorstenlorenz.wordpress.com/2012/01/08/bookmarklet-to-turn-off-jquery-animations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/44f22a4a1324372978a8781dd5748878?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tlorenz10</media:title>
		</media:content>
	</item>
		<item>
		<title>Vim, CoffeeScript and the Node Repl</title>
		<link>http://thorstenlorenz.wordpress.com/2011/10/22/vim-coffeescript-and-the-node-repl/</link>
		<comments>http://thorstenlorenz.wordpress.com/2011/10/22/vim-coffeescript-and-the-node-repl/#comments</comments>
		<pubDate>Sat, 22 Oct 2011 23:43:32 +0000</pubDate>
		<dc:creator>Thorsten Lorenz</dc:creator>
				<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://thorstenlorenz.wordpress.com/?p=416</guid>
		<description><![CDATA[I was looking for a way to get intellisense like features for JavaScript/CoffeScript in Vim, but was disappointed by the &#8220;tags&#8221; plugins. These plugins just give you some keywords about the language in question, but cannot really give you information about the object you are currently dealing with because they never evluate the code that [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=416&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I was looking for a way to get intellisense like features for JavaScript/CoffeScript in Vim, but was disappointed by the &#8220;tags&#8221; plugins.</p>
<p>These plugins just give you some keywords about the language in question, but cannot really give you information about the object you are currently dealing with because they never evluate the code that created it.</p>
<p>Realistically since JavaScript is a dynamic language, it would be hard for any tool to give proper support without actually running parts of your code. Fortunately we have repls for that. It&#8217;s the same idea as Small talk (except in that case the repl is built into the IDE).</p>
<p>Usually when trying to figure things out, run some code in the repl and now it can tell you all kinds of information about objects that were created up to that point. This also includes all exports of modules &#8211; which is very useful.</p>
<p>Unfortunately up until now it was a multi step process to source my code in the repl in order to play with it.</p>
<p>I read <a href="http://technotales.wordpress.com/2007/10/03/like-slime-for-vim/">this post</a>, which explains, how to set up a screen in some unix terminal and then send some text over the created socket. The author also implemented a Vim plugin called <a href="https://github.com/jpalardy/vim-slime">slime.vim</a>. I <a href="https://github.com/thlorenz/vim-slime">forked i</a>t and added some CoffeeScript specific feature which I will explain.</p>
<p>Finally we can send code snippets from Vim directly to the repl without any cut and pasting overhead.</p>
<p>It is actually quite simple to set these things up.</p>
<ul>
<li>Install slime.vim into your vim plugins folder</li>
<li>Open a terminal window and start a named screen e.g. &#8220;screen -S coffee&#8221;</li>
<li>Start a repl inside that screen e.g. coffee for a CoffeeScript repl</li>
<li>Open vim, select some code snippet in visual mode and press Ctrl-c twice</li>
<li>At the prompt give the screen name e.g. coffee and accept the given window name</li>
<li>The selected code should have been sent to the repl and you can now inspect the created objects</li>
</ul>
<p>That&#8217;s it!</p>
<p>Unfortunately the coffee repl is not quite as nice as the node repl (especially when it comes to inspect functions and properties of an object), which is why I <a href="https://github.com/thlorenz/vim-slime">extended the plugin a bit</a>.</p>
<p>If you press Ctrl-C Ctrl-S after selecting a CoffeeScript code snippet, it will be compiled into JavaScript before it is sent to the screen. This means you can directly send your CoffeeScript code to a node repl and inspect it things in there.</p>
<p>I have yet to figure out how to send a &lt;Tab&gt; signal over in order to trigger completion from inside Vim, so anyone with an idea, please comment!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thorstenlorenz.wordpress.com/416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thorstenlorenz.wordpress.com/416/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thorstenlorenz.wordpress.com/416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thorstenlorenz.wordpress.com/416/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thorstenlorenz.wordpress.com/416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thorstenlorenz.wordpress.com/416/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thorstenlorenz.wordpress.com/416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thorstenlorenz.wordpress.com/416/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thorstenlorenz.wordpress.com/416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thorstenlorenz.wordpress.com/416/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thorstenlorenz.wordpress.com/416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thorstenlorenz.wordpress.com/416/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thorstenlorenz.wordpress.com/416/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thorstenlorenz.wordpress.com/416/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=416&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thorstenlorenz.wordpress.com/2011/10/22/vim-coffeescript-and-the-node-repl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/44f22a4a1324372978a8781dd5748878?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tlorenz10</media:title>
		</media:content>
	</item>
		<item>
		<title>Dependency Injection is dead, long live Verbs!</title>
		<link>http://thorstenlorenz.wordpress.com/2011/07/23/dependency-injection-is-dead-long-live-verbs/</link>
		<comments>http://thorstenlorenz.wordpress.com/2011/07/23/dependency-injection-is-dead-long-live-verbs/#comments</comments>
		<pubDate>Sat, 23 Jul 2011 16:38:02 +0000</pubDate>
		<dc:creator>Thorsten Lorenz</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[CoffeeScript]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[MEF]]></category>

		<guid isPermaLink="false">http://thorstenlorenz.wordpress.com/?p=322</guid>
		<description><![CDATA[Disclaimer This post evaluates a new approach of how to wire our code together while staying decoupled. The reader should keep in mind, that the author has not tried this approach in a larger size project and as with any design pattern there may be problems that only become apparent when used in a real world scenario. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=322&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h3>Disclaimer</h3>
<p>This post evaluates a new approach of how to wire our code together while staying decoupled. The reader should keep in mind, that the author has not tried this approach in a larger size project and as with any design pattern there may be problems that only become apparent when used in a real world scenario.</p>
<h3>Why another pattern?</h3>
<p>There has been a recent interest in writing code in a more functional style &#8211; not only because it is more elegant and fun. Yet most common patterns that are used to wire the code together lend themselves very well to a pure object oriented paradigm.</p>
<p>After reading <a href="http://steve-yegge.blogspot.com/2006/03/execution-in-kingdom-of-nouns.html">this post</a>, I was convinced that it is time to re-evaluate how we are doing things when it comes to managing our dependencies, which currently is very Noun/object centric.</p>
<p>It is on us to leverage the opportunities that the current languages and libraries offer in order to improve the architecture of our applications.</p>
<h3>What is the pattern all about?</h3>
<p>I will focus on the possibilities that open up once we use a language that allows passing functions around the same way that we are used to with objects. As a result we can decrease coupling even more than is possible with traditional Dependency Injection.</p>
<p>I also want to point the reader to a <a href="http://northhorizon.net/2011/patterns-with-mef-and-rx/">post by my colleague Daniel Moore</a>, who explored and implemented a similar pattern with streams using <a href="http://mef.codeplex.com/">MEF</a> and <a href="http://www.microsoft.com/download/en/details.aspx?id=26649">Rx</a>.</p>
<p>I will give two examples on how to implement this verb centric pattern. One example will be using <a href="//jashkenas.github.com/coffee-script/">CoffeScript</a>. The other one will be in C# paired with <a href="http://mef.codeplex.com/">MEF</a>.</p>
<p>From here on after I will refer to objects and dependencies as nouns and to functions as verbs.</p>
<h3>I don&#8217;t want to call anyone</h3>
<p>Let&#8217;s say I have some controller that needs to send a message entered by the user. There will be someone in my arsenal of nouns that knows how to do that, so I will inject the Emailer or IEmailer or something along those lines.</p>
<p>Then I can tell the emailer to send messages via:</p>
<pre><code>emailer.Send(msg)</code></pre>
<p>Now let&#8217;s assume that I need to receive a confirmation message. Well, I just tell the emailer to give it to me:</p>
<pre>reply = emailer.Receive()</pre>
<p>All good right? But do I really need to know who is taking care of things? Not really.<br />
All I would have to know is what verb to use in order to get a certain thing done:</p>
<pre>sendMessage(msg)
reply = receiveMessage()</pre>
<h3>What&#8217;s wrong with Dependency Injection?</h3>
<p>What if we change responsibilities later. Let&#8217;s say we still send messages via email, but now receive them directly from the server. At this point I have to change all the code that tells the emailer to receive the message. I would probably inject me a server and change the message receiving calls to something like:</p>
<pre>reply = server.Receive()</pre>
<p>Not a big deal you think? What if I have to change it in 10 places and don&#8217;t forget, since I follow good practices and am writing tests for my code, I now also need to change who does what in my test setup.<br />
Lets remind ourselves of a principle that the authors of <a href="http://www.amazon.com/Code-Complete-Practical-Handbook-Construction/dp/0735619670/ref=sr_1_1?s=books&amp;ie=UTF8&amp;qid=1310916830&amp;sr=1-1">Code Complete</a> elaborate on in chapter 7.5. It goes something like this:</p>
<blockquote><p>If I have a record with 20 fields on it and a method, that only uses 3 of them, I should pass the 3 fields in as parameters instead of passing the entire record.</p></blockquote>
<p>It makes sense. The less anyone knows about anyone else, the least coupled they are, so we shouldn&#8217;t spread more knowledge about how things are arranged than necessary.</p>
<p>Facebook is the best example of what happens when we don&#8217;t follow this advice. Since they so very successfully spread the knowledge about who is related to whom, we are now somehow coupled to almost anyone in the world.</p>
<p>This is not desirable for software systems though.</p>
<p>To summarize:</p>
<blockquote><p>Knowledge containment makes for better decoupling and this applies to everything needed by a part of a system.</p></blockquote>
<p>Dependency Injection does not adhere to this though when it comes to telling a system how to do things. Instead we inject the entire dependency that happens to have the verb we need. All the system wanted was to know <strong>how to do things</strong>, but instead we told it <strong>who knows how to</strong> do it as well.</p>
<p>As a result<strong> dependency injection spreads more information than necessary and that is what&#8217;s wrong with it</strong>.</p>
<h3>What do I need to know?</h3>
<p>In the above example, I needed to know what verb to use when sending a message and that I have to give it a message to send. If I wanted to receive a message, I had to know the appropriate verb for that and that I&#8217;m expecting a message to be returned to me. This boils down to the following generalization which maps perfectly to our examples:</p>
<pre><strong>output(s)     verb   input(s)</strong>
              sendMessage(msg)
reply     =   receiveMessage()</pre>
<h2>The CoffeeScript Example</h2>
<p>As explained in a <a href="http://thorstenlorenz.wordpress.com/2011/07/11/how-to-make-node-js-coffeescript-and-jasmine-play-nice-with-vim/">previous post</a>, I decided to re-implement the node-chat example in CoffeeScript in a BDD manner.</p>
<p>Once I had it all working using dependency injection, I decided to use verbs instead and see how it works out.</p>
<h3>Using Dependency Injection</h3>
<p>Originally the chat server init function looked like this:</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;"><span style="color:#98fb98;">export.init </span><span style="color:#ffdead;">=</span> (<span style="color:#98fb98;">fake </span><span style="color:#ffdead;">=</span> { }) <span style="color:#98fb98;">-&gt;</span>

 <span style="color:#98fb98;">sys </span><span style="color:#ffdead;">=</span> fake.sys <span style="color:#f0e68c;font-weight:bold;">or</span> require <span style="color:#ffa0a0;">"sys"</span>
 <span style="color:#98fb98;">router </span><span style="color:#ffdead;">=</span> fake.router <span style="color:#f0e68c;font-weight:bold;">or</span> require(<span style="color:#ffa0a0;">"./lib/node-router"</span>)
 <span style="color:#98fb98;">server </span><span style="color:#ffdead;">=</span> router.getServer()

 <span style="color:#98fb98;">qs </span><span style="color:#ffdead;">=</span> fake.qs <span style="color:#f0e68c;font-weight:bold;">or</span> require <span style="color:#ffa0a0;">"querystring"</span>
 <span style="color:#98fb98;">url </span><span style="color:#ffdead;">=</span> fake.url <span style="color:#f0e68c;font-weight:bold;">or</span> require <span style="color:#ffa0a0;">"url"</span>

 <span style="color:#98fb98;">process </span><span style="color:#ffdead;">=</span> fake.process <span style="color:#f0e68c;font-weight:bold;">or</span> global.process</pre>
<p>As you can see I allow the tests to fake certain dependencies. If a faked dependency  was not present &#8211; e.g. when used in production, the appropriate node module was used.<br />
In my tests, I injected the fakes like this:</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;">  <span style="color:#98fb98;">server_stub </span><span style="color:#ffdead;">=</span>
    <span style="color:#98fb98;">gets</span><span style="color:#ffdead;">:</span> {}
    <span style="color:#98fb98;">get</span><span style="color:#ffdead;">:</span> (id, callback) <span style="color:#98fb98;">-&gt;</span> <span style="color:#bdb76b;font-weight:bold;">@gets</span>[id]<span style="color:#ffdead;">=</span> callback; <span style="color:#bdb76b;font-weight:bold;">undefined</span>
    <span style="color:#98fb98;">listens_on</span><span style="color:#ffdead;">:</span> {}
    <span style="color:#98fb98;">listen</span><span style="color:#ffdead;">:</span> (port, host) <span style="color:#98fb98;">-&gt;</span> <span style="color:#bdb76b;font-weight:bold;">@listens_on</span><span style="color:#ffdead;">=</span> { <span style="color:#98fb98;">port</span><span style="color:#ffdead;">:</span> port, <span style="color:#98fb98;">host</span><span style="color:#ffdead;">:</span> host }

  <span style="color:#bdb76b;font-weight:bold;">@server_stub</span><span style="color:#ffdead;">=</span> server_stub
  <span style="color:#bdb76b;font-weight:bold;">@router_stub</span><span style="color:#ffdead;">=</span>
    <span style="color:#98fb98;">getServer</span><span style="color:#ffdead;">:</span> <span style="color:#98fb98;">-&gt;</span> server_stub
    <span style="color:#98fb98;">staticHandler</span><span style="color:#ffdead;">:</span> (file) <span style="color:#98fb98;">-&gt;</span>

  <span style="color:#98fb98;">process_stub </span><span style="color:#ffdead;">=</span>
    <span style="color:#98fb98;">memoryUsage</span><span style="color:#ffdead;">:</span> <span style="color:#98fb98;">-&gt;</span> <span style="color:#98fb98;">rss</span><span style="color:#ffdead;">:</span> mem_rss_stub
    <span style="color:#98fb98;">env</span><span style="color:#ffdead;">:</span>
      <span style="color:#ffa0a0;">PORT</span><span style="color:#ffdead;">:</span> port_stub

  <span style="color:#bdb76b;font-weight:bold;">@sut</span><span style="color:#ffdead;">=</span> require(<span style="color:#ffa0a0;">"./../server"</span>)
  <span style="color:#bdb76b;font-weight:bold;">@sut</span>.init
    <span style="color:#98fb98;">router</span><span style="color:#ffdead;">:</span> <span style="color:#bdb76b;font-weight:bold;">@router_stub</span>
    <span style="color:#98fb98;">sys</span><span style="color:#ffdead;">:</span> { <span style="color:#98fb98;">puts</span><span style="color:#ffdead;">:</span> (msg) <span style="color:#98fb98;">-&gt;</span>  } <span style="color:#87ceeb;"># stop sys.puts from cluttering up the test output</span>
    <span style="color:#98fb98;">process</span><span style="color:#ffdead;">:</span> process_stub</pre>
<p>The bootstrapper was rather simple, as it had to inject nothing, since the real modules where to be used in this case.</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;"><span style="color:#98fb98;">chat_server </span><span style="color:#ffdead;">=</span> require <span style="color:#ffa0a0;">"./server"</span>
chat_server.init()</pre>
<p>Routing calls like</p>
<pre>server.get req, res { ... }</pre>
<p>were spread throughout the chat server.</p>
<h3>Using Verbs</h3>
<p>As a proof of concept I refactored the chat server to use verbs where possible and thus have it know the least about where anything is coming from and how the world is arranged.<br />
The chat server now learns from the injected <code>config</code> how to do things, but is not made aware of who is responsible for doing them.</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;">
<span style="color:#98fb98;">exports.init </span><span style="color:#ffdead;">=</span> (config) <span style="color:#98fb98;">-&gt;</span>

  <span style="color:#f0e68c;font-weight:bold;">throw</span> <span style="color:#ffa0a0;">&quot;Need to pass in config object that has required verbs like log, route, etc.&quot;</span> <span style="color:#f0e68c;font-weight:bold;">unless</span> config?

  <span style="color:#87ceeb;"># States </span>
  <span style="color:#98fb98;">env          </span><span style="color:#ffdead;">=</span>  config.env

  <span style="color:#87ceeb;"># Verbs</span>
  <span style="color:#98fb98;">memoryUsage  </span><span style="color:#ffdead;">=</span>  ()            <span style="color:#98fb98;">-&gt;</span> config.memoryUsage()

  <span style="color:#98fb98;">log          </span><span style="color:#ffdead;">=</span>  (msg)         <span style="color:#98fb98;">-&gt;</span> config.log msg
  <span style="color:#98fb98;">route_static </span><span style="color:#ffdead;">=</span>  (file)        <span style="color:#98fb98;">-&gt;</span> config.route_static file
  <span style="color:#98fb98;">route        </span><span style="color:#ffdead;">=</span>  (req, res)    <span style="color:#98fb98;">-&gt;</span> config.route req, res
  <span style="color:#98fb98;">listen       </span><span style="color:#ffdead;">=</span>  (port, host)  <span style="color:#98fb98;">-&gt;</span> config.listen port, host

  <span style="color:#87ceeb;"># Modules</span>
  <span style="color:#98fb98;">qs           </span><span style="color:#ffdead;">=</span>  require <span style="color:#ffa0a0;">&quot;querystring&quot;</span>
  <span style="color:#98fb98;">url          </span><span style="color:#ffdead;">=</span>  require <span style="color:#ffa0a0;">&quot;url&quot;</span>
</pre>
<p>As you can see, there is still one noun that is passed (<code>env</code>), but this is only used in order to query the state of the environment.<br />
I formatted the code in the Verbs section in a way that clearly shows what verbs are used and what their inputs are. Anyone who needs to initialize the chat server can thus easily see what it needs to know. You can think of it as a config section.</p>
<p>In my tests I can easily set things up since now I just have to pass in certain functions instead of having to build up fakes.</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;">  <span style="color:#bdb76b;font-weight:bold;">@listens_on</span><span style="color:#ffdead;">=</span> {}
  <span style="color:#98fb98;">routes </span><span style="color:#ffdead;">=</span> {}

  <span style="color:#bdb76b;font-weight:bold;">@server_get</span><span style="color:#ffdead;">=</span> (method, <span style="color:#98fb98;">req </span><span style="color:#ffdead;">=</span> { }) <span style="color:#98fb98;">-&gt;</span>
    <span style="color:#98fb98;">res </span><span style="color:#ffdead;">=</span> <span style="color:#bdb76b;font-weight:bold;">@res_stub</span>
    routes[<span style="color:#ffa0a0;">"/</span><span style="color:#ffdead;">#{</span>method<span style="color:#ffdead;">}</span><span style="color:#ffa0a0;">"</span>] req, res
    res

  <span style="color:#bdb76b;font-weight:bold;">@sut</span><span style="color:#ffdead;">=</span> require(<span style="color:#ffa0a0;">"./../server"</span>)
  <span style="color:#bdb76b;font-weight:bold;">@sut</span>.init
    <span style="color:#98fb98;">route_static</span><span style="color:#ffdead;">:</span> (file) <span style="color:#98fb98;">-&gt;</span>
    <span style="color:#98fb98;">route</span><span style="color:#ffdead;">:</span> (id, callback) <span style="color:#98fb98;">-&gt;</span> <span style="color:#98fb98;">routes</span>[id]<span style="color:#ffdead;">=</span> callback; <span style="color:#bdb76b;font-weight:bold;">undefined</span>
    <span style="color:#98fb98;">listen</span><span style="color:#ffdead;">:</span> (port, host) <span style="color:#98fb98;">=&gt;</span> <span style="color:#bdb76b;font-weight:bold;">@listens_on</span><span style="color:#ffdead;">=</span> <span style="color:#98fb98;">port</span><span style="color:#ffdead;">:</span> port, <span style="color:#98fb98;">host</span><span style="color:#ffdead;">:</span> host
    <span style="color:#98fb98;">log</span><span style="color:#ffdead;">:</span>  (msg) <span style="color:#98fb98;">-&gt;</span> <span style="color:#87ceeb;"># don't clutter up the test output</span>
    <span style="color:#98fb98;">memoryUsage</span><span style="color:#ffdead;">:</span> <span style="color:#98fb98;">-&gt;</span> <span style="color:#98fb98;">rss</span><span style="color:#ffdead;">:</span> mem_rss_stub
    <span style="color:#98fb98;">env</span><span style="color:#ffdead;">:</span> <span style="color:#ffa0a0;">PORT</span><span style="color:#ffdead;">:</span> port_stub</pre>
<p>The calls to <code>server.get req, res { ... }</code> were replaced with <code>route req, res { ... }</code> calls and thus no knowledge of there even being a server is spread throughout the code anymore.<br />
The bootstrapper takes on the responsibility of figuring out who does what in order to properly initialize the chat server.</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;"><span style="color:#98fb98;">sys </span><span style="color:#ffdead;">=</span> require <span style="color:#ffa0a0;">"sys"</span>
<span style="color:#98fb98;">router </span><span style="color:#ffdead;">=</span> require(<span style="color:#ffa0a0;">"./lib/node-router"</span>)
<span style="color:#98fb98;">server </span><span style="color:#ffdead;">=</span> router.getServer()

<span style="color:#98fb98;">chat_server </span><span style="color:#ffdead;">=</span> require <span style="color:#ffa0a0;">"./server"</span>

chat_server.init

  <span style="color:#87ceeb;"># Nouns</span>
  <span style="color:#98fb98;">env</span><span style="color:#ffdead;">:</span>            process.env

  <span style="color:#87ceeb;"># Verbs</span>
  <span style="color:#98fb98;">memoryUsage</span><span style="color:#ffdead;">:</span>    process.memoryUsage
  <span style="color:#98fb98;">log</span><span style="color:#ffdead;">:</span>            sys.puts
  <span style="color:#98fb98;">route_static</span><span style="color:#ffdead;">:</span>   router.staticHandler
  <span style="color:#98fb98;">route</span><span style="color:#ffdead;">:</span>          server.get
  <span style="color:#98fb98;">listen</span><span style="color:#ffdead;">:</span>         server.listen</pre>
<p>I also formatted it in a way that makes it read like a config section.<br />
This is the only place where the knowledge of how things are arranged lives and thus changing things around later becomes easy.</p>
<h3>Verbs are Aspect Oriented Programming friendly</h3>
<p>When injecting verbs as described I have much more control about how things are getting done, which allows me to quickly add/remove aspects concerning certain actions.<br />
Lets say I want to log what routing calls the chat server registers, but I don&#8217;t want to touch my router code.<br />
All I have to do is introduce a more verbose routing function, which logs information and then calls the original one and then pass that into init. Here is an abbreviated example:</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;"><span style="color:#98fb98;">verbose_route </span><span style="color:#ffdead;">=</span> (req, res) <span style="color:#98fb98;">-&gt;</span>
  console.log <span style="color:#ffa0a0;">"Routing request: "</span>, req
  server.get req, res

chat_server.init
   [...]
  <span style="color:#87ceeb;"># Verbs</span>
  <span style="color:#98fb98;">route</span><span style="color:#ffdead;">:</span>          verbose_route
   [...]</pre>
<p>Although this is a very simple example, it should give you an idea of how powerful this approach is and how much it simplifies extending your application.</p>
<h3>Verbs and C# using MEF</h3>
<p>I created a <a href="https://github.com/thlorenz/VerbsWithMEF">sample application</a> that shows how to implement the same pattern in C# using <a href="http://mef.codeplex.com/">MEF</a>.</p>
<p>The main worker in the application is the runner:</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;">[Export]
<span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">class</span> Runner
{
    <span style="color:#bdb76b;font-weight:bold;">private</span> <span style="color:#bdb76b;font-weight:bold;">readonly</span> Action&lt;<span style="color:#bdb76b;font-weight:bold;">string</span>&gt; _print;
    <span style="color:#bdb76b;font-weight:bold;">private</span> <span style="color:#bdb76b;font-weight:bold;">readonly</span> Func&lt;<span style="color:#bdb76b;font-weight:bold;">string</span>&gt; _read;
    <span style="color:#bdb76b;font-weight:bold;">private</span> <span style="color:#bdb76b;font-weight:bold;">readonly</span> Action&lt;<span style="color:#bdb76b;font-weight:bold;">int</span>&gt; _shutDown;

    [ImportingConstructor]
    <span style="color:#bdb76b;font-weight:bold;">public</span> Runner(
        [Import(Verbs.Print)] Action&lt;<span style="color:#bdb76b;font-weight:bold;">string</span>&gt; print,
        [Import(Verbs.Read)] Func&lt;<span style="color:#bdb76b;font-weight:bold;">string</span>&gt; read,
        [Import(Verbs.Shutdown)] Action&lt;<span style="color:#bdb76b;font-weight:bold;">int</span>&gt; shutDown)
    {
        _print = print;
        _read = read;
        _shutDown = shutDown;
    }

    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">void</span> Run()
    {
        PrintStartupInfo();

        InteractWithUser();

        _shutDown(<span style="color:#ffa0a0;">0</span>);
    }

    <span style="color:#bdb76b;font-weight:bold;">private</span> <span style="color:#bdb76b;font-weight:bold;">void</span> InteractWithUser()
    {
        _print(<span style="color:#ffa0a0;">"Please enter your name: "</span>);

        var name = _read();

        _print(<span style="color:#ffa0a0;">"Hello "</span> + name);
        _print(<span style="color:#ffa0a0;">"Please press enter to shut down"</span>);

         _read();
    }

    <span style="color:#bdb76b;font-weight:bold;">private</span> <span style="color:#bdb76b;font-weight:bold;">void</span> PrintStartupInfo()
    {
        _print(<span style="color:#ffa0a0;">"The super verbs application has started."</span>);
    }
}</pre>
<p>As you can see it gets the verbs injected as single parameters instead of via a config.The Import statements are telling MEF how to resolve the injected verbs (in case you need to read up on how this works head on over <a href="http://mef.codeplex.com/documentation">here</a>).</p>
<p>But where are these verbs actually coming from? Well the Runner doesn&#8217;t know nor is he supposed to. To tell you the truth, it doesn&#8217;t matter. We just need to make sure, that someone is exporting them, so that MEF can resolve them.</p>
<p>It so happens, that we have a UserInterface that knows how to read and write:</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;"><span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">class</span> UserInterface
{
    [Export(Verbs.Print)]
    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">void</span> Print(<span style="color:#bdb76b;font-weight:bold;">string</span> message)
    {
        Console.WriteLine(message);
    }

    [Export(Verbs.Read)]
    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">string</span> Read()
    {
        <span style="color:#f0e68c;font-weight:bold;">return</span> Console.ReadLine();
    }
}</pre>
<p>and the ApplicationManager knows how to shut down the application:</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;"><span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">class</span> ApplicationManager
{
    <span style="color:#bdb76b;font-weight:bold;">private</span> <span style="color:#bdb76b;font-weight:bold;">readonly</span> Action&lt;<span style="color:#bdb76b;font-weight:bold;">string</span>&gt; _print;

    [ImportingConstructor]
    <span style="color:#bdb76b;font-weight:bold;">public</span> ApplicationManager([Import(Verbs.Print)] Action&lt;<span style="color:#bdb76b;font-weight:bold;">string</span>&gt; print)
    {
        _print = print;
    }

    [Export(Verbs.Shutdown)]
    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">void</span> ShutDownApplication(<span style="color:#bdb76b;font-weight:bold;">int</span> code)
    {
        _print(<span style="color:#ffa0a0;">"Shutting down ...."</span>);
        Environment.Exit(code);
    }
}</pre>
<p>The Export statements use the same identifiers as the Import statements of the Runner. This allows MEF to hook everything together.<br />
I could simply inline the strings for these identifiers, but want to avoid introducing bugs due to typos.<br />
Therefore I created constant identifiers in a Verbs class.</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;"><span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">static</span> <span style="color:#bdb76b;font-weight:bold;">class</span> Verbs
{
    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">const</span> <span style="color:#bdb76b;font-weight:bold;">string</span> Print = <span style="color:#ffa0a0;">"Verbs.Print"</span>;
    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">const</span> <span style="color:#bdb76b;font-weight:bold;">string</span> Read = <span style="color:#ffa0a0;">"Verbs.Read"</span>;
    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">const</span> <span style="color:#bdb76b;font-weight:bold;">string</span> Shutdown = <span style="color:#ffa0a0;">"Verbs.Shutdown"</span>;
}</pre>
<p>Finally we need to tell MEF to wire things up. We do this in the Program.</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;"><span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">class</span> Program
{
    <span style="color:#bdb76b;font-weight:bold;">private</span> CompositionContainer _container;

    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">static</span> <span style="color:#bdb76b;font-weight:bold;">void</span> Main(<span style="color:#bdb76b;font-weight:bold;">string</span>[] args)
    {
        var p = <span style="color:#f0e68c;font-weight:bold;">new</span> Program();
        p.Run();
    }

    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">void</span> Run()
    {
        Compose();

        var runner = _container.GetExport&lt;Runner&gt;().Value;
        runner.Run();
    }

    <span style="color:#bdb76b;font-weight:bold;">private</span> <span style="color:#bdb76b;font-weight:bold;">void</span> Compose()
    {
        var catalog = <span style="color:#f0e68c;font-weight:bold;">new</span> AssemblyCatalog(Assembly.GetExecutingAssembly());
        _container = <span style="color:#f0e68c;font-weight:bold;">new</span> CompositionContainer(catalog);
        _container.ComposeParts(<span style="color:#f0e68c;font-weight:bold;">this</span>);
    }
}</pre>
<p>We create a container in the <code> Compose() </code> method in order to make MEF lookup all of the exports in our assembly. Then we resolve the <code> Runner </code> at which point MEF injects all of our exported Verbs.<br />
The full example is available <a href="https://github.com/thlorenz/VerbsWithMEF">here</a> on the master branch.</p>
<h3>Adding another Printer</h3>
<p>In order to demonstrate how extensible our little application is, lets assume we want to add a Debugger that sends a  timestamped version of the message to the output window whenever someone prints it.<br />
At the same time we want to keep printing to the console.<br />
We can do this without changing a single line of code inside our Runner &#8211; I promise!</p>
<p>We need to introduce a method that when called, will aggregate and then invoke every method that claims to know how to print (via the appropriate Export). We will use MEF&#8217;s ImportMany feature to accomplish this task.<br />
For simplicity lets just slap this method onto the ApplicationManager &#8211; we can always move it later since no one will be aware of where it lives.</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;"><span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">class</span> ApplicationManager
{
    <span style="color:#bdb76b;font-weight:bold;">private</span> <span style="color:#bdb76b;font-weight:bold;">readonly</span> IEnumerable&lt;Action&lt;<span style="color:#bdb76b;font-weight:bold;">string</span>&gt;&gt; _printToMany;

    [ImportingConstructor]
    <span style="color:#bdb76b;font-weight:bold;">public</span> ApplicationManager(
        [ImportMany(Verbs.CompositePrint, AllowRecomposition = <span style="color:#ffa0a0;">true</span>)]
        IEnumerable&lt;Action&lt;<span style="color:#bdb76b;font-weight:bold;">string</span>&gt;&gt; printToMany)
    {
        _printToMany = printToMany;
    }

    [Export(Verbs.Shutdown)]
    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">void</span> ShutDownApplication(<span style="color:#bdb76b;font-weight:bold;">int</span> code)
    {
        ApplicationPrint(<span style="color:#ffa0a0;">"Shutting down ...."</span>);
        Environment.Exit(code);
    }

    [Export(Verbs.Print)]
    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">void</span> ApplicationPrint(<span style="color:#bdb76b;font-weight:bold;">string</span> msg)
    {
        <span style="color:#f0e68c;font-weight:bold;">foreach</span> (var print <span style="color:#f0e68c;font-weight:bold;">in</span> _printToMany)
        {
            print(msg);
        }
    }
}</pre>
<p>It now exports its <code> ApplicationPrint </code> method under the <code> Verbs.Print </code> identifier that the Runner knows about. When invoked, it finds all print methods that where exported under the new <code> Verbs.CompositePrint </code> identifier and invokes them one after the other.<br />
Since it exports itself under the same identifier that the UserInterface used to export its <code> Print</code> method previously, it ends up replacing it.</p>
<p>There are two things left to do:</p>
<p>First we need to update the print method in our UserInterface to export it self as the Verbs.CompositePrint (this is an extra verb we add to our <code> Verbs</code> class).</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;"><span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">class</span> UserInterface
{
    [Export(Verbs.CompositePrint)]
    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">void</span> Print(<span style="color:#bdb76b;font-weight:bold;">string</span> message)
    {
        Console.WriteLine(message);
    }

    [Export(Verbs.Read)]
    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">string</span> Read()
    {
        <span style="color:#f0e68c;font-weight:bold;">return</span> Console.ReadLine();
    }
}</pre>
<p>Secondly we now introduce the Debugger that will export a print method with the same identifier.</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;"><span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">class</span> Debugger
{
    [Export(Verbs.CompositePrint)]
    <span style="color:#bdb76b;font-weight:bold;">public</span> <span style="color:#bdb76b;font-weight:bold;">void</span> Print(<span style="color:#bdb76b;font-weight:bold;">string</span> message)
    {
        Debug.WriteLine(DateTime.Now + <span style="color:#ffa0a0;">" - "</span> + message);
    }
}</pre>
<p>As a result whenever the Runner prints a message it will end up calling the <code> ApplicationPrint</code> method which in turn calls the print methods on the <code> UserInterface </code> and the <code> Debugger </code> with the passed message. As promised, the Runner didn&#8217;t change and is totally oblivious to the new way that things are done now.</p>
<p>This version of the application is available <a href="https://github.com/thlorenz/VerbsWithMEF">here</a> on the multiprint branch</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thorstenlorenz.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thorstenlorenz.wordpress.com/322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thorstenlorenz.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thorstenlorenz.wordpress.com/322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thorstenlorenz.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thorstenlorenz.wordpress.com/322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thorstenlorenz.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thorstenlorenz.wordpress.com/322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thorstenlorenz.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thorstenlorenz.wordpress.com/322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thorstenlorenz.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thorstenlorenz.wordpress.com/322/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thorstenlorenz.wordpress.com/322/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thorstenlorenz.wordpress.com/322/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=322&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thorstenlorenz.wordpress.com/2011/07/23/dependency-injection-is-dead-long-live-verbs/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/44f22a4a1324372978a8781dd5748878?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tlorenz10</media:title>
		</media:content>
	</item>
		<item>
		<title>How to make node.js, CoffeeScript and Jasmine play nice with Vim</title>
		<link>http://thorstenlorenz.wordpress.com/2011/07/11/how-to-make-node-js-coffeescript-and-jasmine-play-nice-with-vim/</link>
		<comments>http://thorstenlorenz.wordpress.com/2011/07/11/how-to-make-node-js-coffeescript-and-jasmine-play-nice-with-vim/#comments</comments>
		<pubDate>Mon, 11 Jul 2011 02:20:07 +0000</pubDate>
		<dc:creator>Thorsten Lorenz</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Vim]]></category>
		<category><![CDATA[BDD]]></category>
		<category><![CDATA[CoffeeScript]]></category>

		<guid isPermaLink="false">http://thorstenlorenz.wordpress.com/?p=262</guid>
		<description><![CDATA[In order to see how much fun it could be to develop a web application BDD style, I decided to re-implement the node-chat sample application. I started by implementing the chat server which runs on node.js, so making my chat server run on node.js as well was an easy decision. Why I used CoffeeScript over just [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=262&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In order to see how much fun it could be to develop a web application BDD style, I decided to re-implement the <a href="http://github.com/ry/node_chat/tree/master">node-chat sample application</a>.</p>
<p>I started by implementing the chat server which runs on <a href="http://nodejs.org/">node.js</a>, so making my chat server run on node.js as well was an easy decision. Why I used CoffeeScript over just Javascript  and Jasmine for testing will be explained below.</p>
<p>Although I will blog about the resulting implementation and specs separately, the impatient can go <a href="https://github.com/thlorenz/bdd_nodechat">here</a> to have look.</p>
<h2>CoffeeScript</h2>
<p>Everyone loves nice and quiet and for a lot of people that extends to their work environment; ergo we don&#8217;t like noisy code.</p>
<p>I believe in the idea of cleanly formatted code to make it more readable and that these clues are enough for a parser to deduce what I am trying to say. Curly braces make code less readable IMO and are not really necessary.</p>
<p><a href="http://jashkenas.github.com/coffee-script/">CoffeeScript</a> takes this to heart.</p>
<p>It is a language that generates Javascript code. The generated code is as readable as Javascript allows, but at least I don&#8217;t have to look at it all day.</p>
<p>It borrows ideas from a number of languages and combines the best of each into a super succinct and expressive language. Ruby developers will feel right at home and some things like list comprehensions are reminiscent of Haskell.</p>
<p>Like Haskell and Python, CoffeeScript is whitespace sensitive and thus doesn&#8217;t need any extra information about the code structure aka noise.</p>
<h2>CoffeeScript and Vim</h2>
<p>In order to quickly compile and then run my coffee scripts with node, I looked for nice Vim integration and found it <a href="https://github.com/kchmck/vim-coffee-script">here</a>.</p>
<p>Although the site explains how to use it, I will point out the usage scenarios that I found most important.</p>
<h4>Auto-Compilation</h4>
<p>Setup Vim to auto-compile coffee files when they are saved via:</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;font-size:12px;">  <span style="color:#f0e68c;font-weight:bold;">autocmd</span> <span style="color:#bdb76b;font-weight:bold;">BufWritePost</span> *.coffee <span style="color:#f0e68c;font-weight:bold;">silent</span> CoffeeMake! <span style="color:#f0e68c;font-weight:bold;">-</span><span style="color:#f0e68c;font-weight:bold;">b</span> | <span style="color:#f0e68c;font-weight:bold;">cwindow</span></pre>
<p>This is super useful as now I can save my file and run it right after via node.</p>
<p>If there are any compilation errors, they will be shown in a separate window, which disappears only after the errors were resolved and the file is saved again.</p>
<p>Leave off the &#8220;!&#8221; in order to have the cursor jump to the line of the error automatically.</p>
<h4>Coffee-Compile</h4>
<p>This feature is probably most important, if you are like me new to both CoffeScript and Javascript.</p>
<p>It allows compiling the entire Vim buffer or the selected text only and shows the resulting Javascript right inside Vim.</p>
<p>I used it a lot when I wasn&#8217;t sure if the CoffeeScript would do what I expected and to just get an idea of what it would look like in Javascript.</p>
<p>It is so easy to select a few lines of code, run CoffeCompile and watch and learn as the screenshot shows.</p>
<h2><a href="http://thorstenlorenz.files.wordpress.com/2011/07/coffecompile.png"><img class="size-full wp-image-266 alignnone" title="CoffeCompile" src="http://thorstenlorenz.files.wordpress.com/2011/07/screen-shot-2011-07-10-at-6-24-11-pm.png?w=620" alt=""   /></a></h2>
<h2>Jasmine</h2>
<p><a href="http://pivotal.github.com/jasmine">Jasmine</a> is a <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">BDD</a> framework for Javascript, which means you can author your tests in CoffeeScript as well of course. In order to test code running in node.js, you will need <a href="https://github.com/mhevery/jasmine-node">jasmine-node</a>. Install it via</p>
<pre><code>npm install -g jasmine-node</code></pre>
<p>For my project I placed my specification files inside the /spec folder and could immediately run them via</p>
<pre><code>jasmine-node spec</code></pre>
<p>Well almost at least. The site warns:</p>
<blockquote><p>your specifications must have &#8216;spec&#8217; in the filename or jasmine-node won&#8217;t find them!</p></blockquote>
<p>My file was called server_specs.js (after the CoffeScript compile step) and Jasmine still didn&#8217;t find it. Turns out that renaming it to server_spec.js did the trick.</p>
<p><strong>So be aware:</strong></p>
<blockquote><p><strong>In order for Jasmine to find your specification files they need to end with spec.js.</strong></p></blockquote>
<h4>Why I didn&#8217;t use vows</h4>
<p>This is a good time to mention that I gave <a href="http://vowsjs.org/">vows</a> a try first because it seemed to be so very much in vo(w)gue.</p>
<p>It&#8217;s strong point for some and weak point for others &#8211; like me, is, that it runs all tests in parallel.</p>
<p>This is surely a good idea, but makes re-using things like stubs very hard, especially if you use a lot of child contexts like me. Tests start to affect each other in weird ways and it is very hard to keep them separate. As far as I understand, anything that is to be truly isolated needs to be returned by the &#8220;topic&#8221; and this becomes a nuisance once you have a lot of things that are affected by setting up a context.</p>
<p>I dabbled with it for a few hours until I decided the hoops I had to jump through weren&#8217;t worth it &#8211; after all if I wanted to jump through hoops in order to test my code, I could just stick with static languages, right?</p>
<p>I found that I wasn&#8217;t the only one who feels that way. When I looked around for alternatives, I found this on the nodeunit site:</p>
<blockquote><p>While running tests in parallel seems like a good idea for speeding up your test suite, in practice I&#8217;ve found it means writing much more complicated tests. Because of node&#8217;s module cache, running tests in parallel means mocking and stubbing is pretty much impossible.</p></blockquote>
<p>I also think that running tests in parallel is more important if you are dealing with long running integration tests. IMO in that case it is better to separate these from the faster running unit tests and run them only once a day or so.</p>
<h2>Jasmine and Vim</h2>
<p>Although it could be perfectly sufficient to just save you file after you made some change and then switch to the terminal in order to run the tests, it definitely gets in the way of the red &#8211; green &#8211; refactor workflow.</p>
<p>I prefer to just have to hit one shortcut key in order to save/compile the code and run my tests. This allows me to keep my focus on what I am trying to accomplish.</p>
<p>You can certainly run jasmine-node from inside Vim like with a simple command, but with mixed results:</p>
<p><a href="http://thorstenlorenz.files.wordpress.com/2011/07/jasminenodewithcolors.png"><img class="alignnone size-full wp-image-278" title="JasmineNodeWithColors" src="http://thorstenlorenz.files.wordpress.com/2011/07/jasminenodewithcolors.png?w=620" alt=""   /></a></p>
<p>The weird looking numbers are color codes sent to the terminal which the simple Vim terminal interprets in its own ways.</p>
<p>Turns out that jasmine-node, although it sports a <code>--colors</code> option, turns colors on for you even if you don&#8217;t specify it. Fortunately there is a way to turn the colors off explicitly:</p>
<p><a href="http://thorstenlorenz.files.wordpress.com/2011/07/jasminenodenocolors.png"><img class="alignnone size-full wp-image-279" title="JasmineNodeNoColors" src="http://thorstenlorenz.files.wordpress.com/2011/07/jasminenodenocolors.png?w=620" alt=""   /></a></p>
<p>That&#8217;s better!</p>
<p>The only thing left to do is to hook saving all files and running tests up  to a shortcut by adding the following to our .vimrc: (first line ensures our leader key is a comma)</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;font-size:12px;"><span style="color:#f0e68c;font-weight:bold;"> let</span> mapleader<span style="color:#f0e68c;font-weight:bold;">=</span><span style="color:#ffa0a0;">","</span>
  <span style="color:#f0e68c;font-weight:bold;">map</span> <span style="color:#ffdead;">&lt;</span><span style="color:#ffdead;">leader</span><span style="color:#ffdead;">&gt;</span>m :wa \|! jasmine-node spec --noColor <span style="color:#ffdead;">&lt;</span><span style="color:#ffdead;">CR</span><span style="color:#ffdead;">&gt;</span></pre>
<p>In this case hitting <strong><em>,m </em></strong>will save all my open files and then run my tests.</p>
<h4>Clean Test Output</h4>
<p>In order to keep my test result output from being cluttered up with log messages and such, I replaces node&#8217;s <code>sys</code> that is normally obtained via <code>sys = require "sys"</code>, with a stub that does nothing when <code>sys.puts</code> is called.</p>
<p>Just to give an idea of how simple this is here is a quick example:</p>
<p>1. When creating the system under test, we inject the dependencies we want to stub &#8211; including sys</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;font-size:12px;">  <span style="color:#bdb76b;font-weight:bold;">@sut</span>.init
    <span style="color:#98fb98;">router</span><span style="color:#ffdead;">:</span> <span style="color:#bdb76b;font-weight:bold;">@router_stub</span>
    <span style="color:#98fb98;">sys</span><span style="color:#ffdead;">:</span> { <span style="color:#98fb98;">puts</span><span style="color:#ffdead;">:</span> (msg) <span style="color:#98fb98;">-&gt;</span>  } <span style="color:#87ceeb;"># stop sys.puts from cluttering up the test output</span>
    <span style="color:#98fb98;">process</span><span style="color:#ffdead;">:</span> process_stub</pre>
<p>2. The system under test then uses the injected dependencies if present or the real ones otherwise</p>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;font-size:12px;"><span style="color:#98fb98;">server.init </span><span style="color:#ffdead;">=</span> (<span style="color:#98fb98;">fake </span><span style="color:#ffdead;">=</span> { }) <span style="color:#98fb98;">-&gt;</span>

  <span style="color:#98fb98;">sys </span><span style="color:#ffdead;">=</span> fake.sys <span style="color:#f0e68c;font-weight:bold;">or</span> require <span style="color:#ffa0a0;">"sys"</span>
  <span style="color:#98fb98;">router </span><span style="color:#ffdead;">=</span> fake.router <span style="color:#f0e68c;font-weight:bold;">or</span> require(<span style="color:#ffa0a0;">"./lib/node-router"</span>)
  <span style="color:#98fb98;">server </span><span style="color:#ffdead;">=</span> router.getServer()

  <span style="color:#98fb98;">process </span><span style="color:#ffdead;">=</span> fake.process <span style="color:#f0e68c;font-weight:bold;">or</span>global.process</pre>
<p>More details will be explained in a later post.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thorstenlorenz.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thorstenlorenz.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thorstenlorenz.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thorstenlorenz.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thorstenlorenz.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thorstenlorenz.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thorstenlorenz.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thorstenlorenz.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thorstenlorenz.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thorstenlorenz.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thorstenlorenz.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thorstenlorenz.wordpress.com/262/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thorstenlorenz.wordpress.com/262/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thorstenlorenz.wordpress.com/262/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=262&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thorstenlorenz.wordpress.com/2011/07/11/how-to-make-node-js-coffeescript-and-jasmine-play-nice-with-vim/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/44f22a4a1324372978a8781dd5748878?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tlorenz10</media:title>
		</media:content>

		<media:content url="http://thorstenlorenz.files.wordpress.com/2011/07/screen-shot-2011-07-10-at-6-24-11-pm.png" medium="image">
			<media:title type="html">CoffeCompile</media:title>
		</media:content>

		<media:content url="http://thorstenlorenz.files.wordpress.com/2011/07/jasminenodewithcolors.png" medium="image">
			<media:title type="html">JasmineNodeWithColors</media:title>
		</media:content>

		<media:content url="http://thorstenlorenz.files.wordpress.com/2011/07/jasminenodenocolors.png" medium="image">
			<media:title type="html">JasmineNodeNoColors</media:title>
		</media:content>
	</item>
		<item>
		<title>Reading Source Code on iPad with Vim syntax highlighting</title>
		<link>http://thorstenlorenz.wordpress.com/2011/06/05/reading-source-code-on-ipad-with-syntax-highlighting/</link>
		<comments>http://thorstenlorenz.wordpress.com/2011/06/05/reading-source-code-on-ipad-with-syntax-highlighting/#comments</comments>
		<pubDate>Sun, 05 Jun 2011 20:21:51 +0000</pubDate>
		<dc:creator>Thorsten Lorenz</dc:creator>
				<category><![CDATA[iPad]]></category>
		<category><![CDATA[Vim]]></category>

		<guid isPermaLink="false">http://thorstenlorenz.wordpress.com/?p=251</guid>
		<description><![CDATA[The best way to learn new languages/technologies is to read some good sample source code. Incidentally there is an abundance of source code, but only so little time. I thought it would be nice to read some on my iPad while I&#8217;m on the train. Of course it would be even better if I could [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=251&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The best way to learn new languages/technologies is to read some good sample source code.</p>
<p>Incidentally there is an abundance of source code, but only so little time. I thought it would be nice to read some on my iPad while I&#8217;m on the train. Of course it would be even better if I could have it syntax colored.</p>
<p>Fortunately as I mentioned in my <a href="http://thorstenlorenz.wordpress.com/2011/05/29/how-to-make-your-code-samples-in-your-blogs-look-like-they-look-in-vim/">previous post</a>, there is an easy command in Vim to convert any text to html, including the colors.</p>
<pre>:TOhtml</pre>
<p>does the trick.</p>
<p>So here is what you need to do:</p>
<ol>
<li> Get yourself an iPad reader that supports html &#8211; I use <a href="http://itunes.apple.com/app/id363448914?mt=8">GoodReader</a></li>
<li> Convert the source code you would like to read into html using your Vim editor and save it to a file</li>
<li> Upload the saved html file to your iPad reader (ideally using something quick and convenient like GoodReader&#8217;s WiFi transfer)</li>
</ol>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thorstenlorenz.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thorstenlorenz.wordpress.com/251/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thorstenlorenz.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thorstenlorenz.wordpress.com/251/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thorstenlorenz.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thorstenlorenz.wordpress.com/251/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thorstenlorenz.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thorstenlorenz.wordpress.com/251/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thorstenlorenz.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thorstenlorenz.wordpress.com/251/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thorstenlorenz.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thorstenlorenz.wordpress.com/251/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thorstenlorenz.wordpress.com/251/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thorstenlorenz.wordpress.com/251/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=251&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thorstenlorenz.wordpress.com/2011/06/05/reading-source-code-on-ipad-with-syntax-highlighting/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/44f22a4a1324372978a8781dd5748878?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tlorenz10</media:title>
		</media:content>
	</item>
		<item>
		<title>How to make your code samples in your blogs look like they look in Vim</title>
		<link>http://thorstenlorenz.wordpress.com/2011/05/29/how-to-make-your-code-samples-in-your-blogs-look-like-they-look-in-vim/</link>
		<comments>http://thorstenlorenz.wordpress.com/2011/05/29/how-to-make-your-code-samples-in-your-blogs-look-like-they-look-in-vim/#comments</comments>
		<pubDate>Sun, 29 May 2011 15:22:34 +0000</pubDate>
		<dc:creator>Thorsten Lorenz</dc:creator>
				<category><![CDATA[Blogging]]></category>
		<category><![CDATA[Vim]]></category>

		<guid isPermaLink="false">http://thorstenlorenz.wordpress.com/?p=239</guid>
		<description><![CDATA[Four simple Steps Open the code sample in your favorite editor (that would be vim) Execute :TOhtml (this generates a complete html file including a &#60;style&#62; section that contains the needed CSS and a &#60;body&#62; section that contains the HTMLversion of your code) Copy the generated CSS into the CSS file of your blog (in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=239&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h2>Four simple Steps</h2>
<ol>
<li>Open the code sample in your favorite editor (that would be vim)</li>
<li>Execute<strong> :TOhtml</strong> (this generates a complete html file including a<strong> &lt;style&gt; section that contains the needed CSS</strong> and a <strong>&lt;body&gt; section that contains the HTML</strong>version of your code)</li>
<li>Copy the generated <strong>CSS into the CSS file of your blog</strong> (in wordpress you&#8217;d get there via Appearance/Edit CSS)</li>
<li>Include the generated <strong>HTML body in your blog</strong></li>
</ol>
<p>I haven&#8217;t tried this, but alternatively you could just copy the entire html (including the &lt;style&gt;) into your blog and thus combine steps 3 and 4.</p>
<p>The disadvantage of that approach is that in this case you&#8217;d have to include the CSS part in every blog that has a code sample.</p>
<h2>If you don&#8217;t want to upgrade</h2>
<p>In case you don&#8217;t want to pay the $15/year for the Custom CSS upgrade, scrap steps 3 and 4 and instead do the following in order to inline the CSS.</p>
<ol>
<li>Copy the code generated in step 2 from above to the clipboard</li>
<li>Go to <a href="http://premailer.dialect.ca/">http://premailer.dialect.ca/</a></li>
<li>Select <strong>Paste HTML as the source</strong></li>
<li>Paste your code into the text field</li>
<li>In the Options check everything <strong>except</strong>  <em>Don’t remove &lt;style&gt; and &lt;link&gt; elements</em></li>
<li>Hit <strong>Submit</strong> and copy the <strong>HTML results</strong></li>
<li>Include the copied HTML in your blog</li>
</ol>
<div><span class="Apple-style-span" style="color:#3c3c46;font-family:'Univers LT W01 57 Condensed', sans-serif;font-size:20px;line-height:20px;"><br />
</span></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thorstenlorenz.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thorstenlorenz.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thorstenlorenz.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thorstenlorenz.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thorstenlorenz.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thorstenlorenz.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thorstenlorenz.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thorstenlorenz.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thorstenlorenz.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thorstenlorenz.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thorstenlorenz.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thorstenlorenz.wordpress.com/239/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thorstenlorenz.wordpress.com/239/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thorstenlorenz.wordpress.com/239/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=239&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thorstenlorenz.wordpress.com/2011/05/29/how-to-make-your-code-samples-in-your-blogs-look-like-they-look-in-vim/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/44f22a4a1324372978a8781dd5748878?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tlorenz10</media:title>
		</media:content>
	</item>
		<item>
		<title>Having fun with JavaScript bookmarklets to determine loaded libraries</title>
		<link>http://thorstenlorenz.wordpress.com/2011/05/21/having-fun-with-javascript-bookmarklets-to-determine-loaded-libraries/</link>
		<comments>http://thorstenlorenz.wordpress.com/2011/05/21/having-fun-with-javascript-bookmarklets-to-determine-loaded-libraries/#comments</comments>
		<pubDate>Sat, 21 May 2011 23:50:19 +0000</pubDate>
		<dc:creator>Thorsten Lorenz</dc:creator>
				<category><![CDATA[Bookmarklets]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://thorstenlorenz.wordpress.com/?p=214</guid>
		<description><![CDATA[Lately I have been looking at ways to make writing JavaScript a little more fun . Actually the first step was to write CoffeScript instead and letting it compile the JavaScript for me. I&#8217;m also very interested in the libraries that make a lot of things like cross browser issues easier. So a lot of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=214&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Lately I have been looking at ways to make writing JavaScript a little more fun .<br />
Actually the first step was to write <a href="http://jashkenas.github.com/coffee-script/">CoffeScript</a> instead and letting it compile the JavaScript for me.</p>
<p>I&#8217;m also very interested in the libraries that make a lot of things like cross browser issues easier. So a lot of times when I see a site I like, I&#8217;m wondering, what libraries it is using under the hood. I wanted a faster way to determine this than &#8216;view page source&#8217;.<br />
So this became my first little JavaScript task.</p>
<p>I went to <a href="http://http://jsfiddle.net/">jsFiddle</a> to spike how it could be done (unfortunately I couldn&#8217;t use <a href="http://jquery.com/">jQuery</a> here, since then I would have needed to reference it and it would obviously have obscured the results).</p>
<p>After a few attempts I had my little JavaScript function ready. Since I couldn&#8217;t rely on any map function, it is using a lot of for loops and is not too pretty, but some people may find it useful:</p>
<pre>
<pre style="font-family:monospace;color:#ffffff;background-color:#333333;">
<span style="color:#ffff00;"> 1 </span>(<span style="color:#98fb98;">function</span> showLoadedLibs() <span style="color:#98fb98;">{</span>
<span style="color:#ffff00;"> 2 </span>
<span style="color:#ffff00;"> 3 </span>    <span style="color:#98fb98;">var</span> names = <span style="color:#98fb98;">[</span><span style="color:#ffa0a0;">'jquery'</span>, <span style="color:#ffa0a0;">'mootools'</span>, <span style="color:#ffa0a0;">'backbone'</span>, <span style="color:#ffa0a0;">'prototype'</span>, <span style="color:#ffa0a0;">'yui'</span>, <span style="color:#ffa0a0;">'simpleyui'</span>, <span style="color:#ffa0a0;">'glow'</span>, <span style="color:#ffa0a0;">'dojo'</span>, <span style="color:#ffa0a0;">'modernizr'</span>, <span style="color:#ffa0a0;">'processing'</span>, <span style="color:#ffa0a0;">'ext-core'</span>, <span style="color:#ffa0a0;">'raphael'</span>, <span style="color:#ffa0a0;">'right'</span><span style="color:#98fb98;">]</span>;
<span style="color:#ffff00;"> 4 </span>    <span style="color:#98fb98;">var</span> scripts = <span style="color:#f0e68c;font-weight:bold;">document</span>.getElementsByTagName(<span style="color:#ffa0a0;">&quot;script&quot;</span>);
<span style="color:#ffff00;"> 5 </span>    <span style="color:#98fb98;">var</span> loadedScriptNames = <span style="color:#ffa0a0;">&quot;&quot;</span>;
<span style="color:#ffff00;"> 6 </span>    <span style="color:#f0e68c;font-weight:bold;">for</span> (<span style="color:#98fb98;">var</span> i = 0; i &lt; scripts.length; i++) <span style="color:#98fb98;">{</span>
<span style="color:#ffff00;"> 7 </span>        <span style="color:#98fb98;">var</span> src = scripts<span style="color:#98fb98;">[</span>i<span style="color:#98fb98;">]</span>.src;
<span style="color:#ffff00;"> 8 </span>        <span style="color:#f0e68c;font-weight:bold;">for</span> (<span style="color:#98fb98;">var</span> n = 0; n &lt; names.length; n++) <span style="color:#98fb98;">{</span>
<span style="color:#ffff00;"> 9 </span>            <span style="color:#98fb98;">var</span> lib = <span style="color:#f0e68c;font-weight:bold;">new</span> <span style="color:#bdb76b;font-weight:bold;">RegExp</span>(names<span style="color:#98fb98;">[</span>n<span style="color:#98fb98;">]</span> + <span style="color:#ffa0a0;">'[^/]*[.]js'</span>).exec(src);
<span style="color:#ffff00;">10 </span>            <span style="color:#f0e68c;font-weight:bold;">if</span> (lib) <span style="color:#98fb98;">{</span>
<span style="color:#ffff00;">11 </span>                loadedScriptNames += <span style="color:#ffa0a0;">&quot; &quot;</span> + lib + <span style="color:#ffa0a0;">&quot;,&quot;</span>;
<span style="color:#ffff00;">12 </span>            <span style="color:#98fb98;">}</span>
<span style="color:#ffff00;">13 </span>        <span style="color:#98fb98;">}</span>
<span style="color:#ffff00;">14 </span>    <span style="color:#98fb98;">}</span>
<span style="color:#ffff00;">15 </span>    <span style="color:#f0e68c;font-weight:bold;">alert</span>(<span style="color:#ffa0a0;">&quot;</span><span style="color:#ffdead;">\n</span><span style="color:#ffa0a0;">Loaded libraries: &quot;</span> + loadedScriptNames.substr(0, loadedScriptNames.length - 1) + <span style="color:#ffa0a0;">&quot;</span><span style="color:#ffdead;">\n\n</span><span style="color:#ffa0a0;">Looked for: &quot;</span> + names);
<span style="color:#ffff00;">16 </span>
<span style="color:#ffff00;">17 </span><span style="color:#98fb98;">}</span>());
</pre>
</pre>
<p>In order to use it as a bookmarklet, I went to the <a href="http://ted.mielczarek.org/code/mozilla/bookmarklet.html">Bookmarklet Crunchinator</a> to &#8216; crunch&#8217; the above code.</p>
<p>The result looks like this:</p>
<p><code>javascript:(function(){(function showLoadedLibs(){var names=['jquery','mootools','backbone','prototype','yui','simpleyui','glow','dojo','modernizr','processing','ext-core','raphael','right'];var scripts=document.getElementsByTagName("script");var loadedScriptNames="";for(var i=0;i&lt;scripts.length;i++){src=scripts[i].src;for(var n=0;n&lt;names.length;n++){var lib=new RegExp(names[n]+'[^/]*[.]js').exec(src);if(lib){loadedScriptNames+=" "+lib+",";}}}alert("\nLoaded libraries: "+loadedScriptNames.substr(0,loadedScriptNames.length-1)+"\n\nLooked for: "+names);}());})();</code></p>
<p>Finally, I added it to my bookmark bar &#8211; the above &#8216;crunched&#8217; code becomes the Url of the bookmark.</p>
<p>All I have to do now is click this bookmark and am informed about known libraries that are loaded on the page I am currently on.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thorstenlorenz.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thorstenlorenz.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thorstenlorenz.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thorstenlorenz.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thorstenlorenz.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thorstenlorenz.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thorstenlorenz.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thorstenlorenz.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thorstenlorenz.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thorstenlorenz.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thorstenlorenz.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thorstenlorenz.wordpress.com/214/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thorstenlorenz.wordpress.com/214/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thorstenlorenz.wordpress.com/214/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=214&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thorstenlorenz.wordpress.com/2011/05/21/having-fun-with-javascript-bookmarklets-to-determine-loaded-libraries/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/44f22a4a1324372978a8781dd5748878?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tlorenz10</media:title>
		</media:content>
	</item>
		<item>
		<title>Automatically loading Vim files from Dropbox folder</title>
		<link>http://thorstenlorenz.wordpress.com/2011/02/20/automatically-loading-vim-files-from-dropbox-folder/</link>
		<comments>http://thorstenlorenz.wordpress.com/2011/02/20/automatically-loading-vim-files-from-dropbox-folder/#comments</comments>
		<pubDate>Sun, 20 Feb 2011 03:20:31 +0000</pubDate>
		<dc:creator>Thorsten Lorenz</dc:creator>
				<category><![CDATA[Dropbox]]></category>
		<category><![CDATA[Vim]]></category>

		<guid isPermaLink="false">http://thorstenlorenz.wordpress.com/?p=199</guid>
		<description><![CDATA[I have been a happy Dropbox user for a while now. I try to link whatever possible to it like: Visual Studio settings PowerShell modules Resharper mounted live template file Until today I was still copying my vimfiles back and forth though. So I finally took the time to investigate how to point the vim [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=199&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I have been a happy <a href="http://www.dropbox.com/">Dropbox</a> user for a while now. I try to link whatever possible to it like:</p>
<ul>
<li>Visual Studio settings</li>
<li>PowerShell modules</li>
<li>Resharper mounted live template file</li>
</ul>
<p>Until today I was still copying my vimfiles back and forth though.</p>
<p>So I finally took the time to investigate how to point the vim runtimepath to a folder of my choice.</p>
<p>Turns out there are two ways to do this:</p>
<ol>
<li>Point the vim runtime path to a folder in your dropbox</li>
<li>Create a link from your local vimfiles folder to the vimfiles folder in your dropbox</li>
</ol>
<h3>Point the vim runtime path to a folder in your dropbox</h3>
<p>In this case vim itself (&#8216;:help runtimepath&#8217;) was more helpful than stackoverflow and google.</p>
<p>This <a href="http://stackoverflow.com/questions/3377298/how-can-i-override-vim-and-vimrc-paths-but-no-others-in-vim/3384476#3384476">stackoverflow answer</a> maybe useful to others, but in my case I never could get the printf() statemtent to work and I also would like to start vim without specifying the path every time.</p>
<p>As I said, the help helped (npi) and after a bit of experimentation I came up with this solution.</p>
<p>I added</p>
<p><code>set runtimepath=~/Dropbox/Settings/vimfiles,$VIM,$VIMRUNTIME</code></p>
<p>to my .vimrc file.</p>
<p>Note that for Windows the dropbox folder is called &#8216;My Dropbox&#8217; and therefore you need to escape the space like so:</p>
<p><code>set runtimepath=~/Dropboxes/Gmail/My\ Dropbox/Settings/vimfiles,$VIM,$VIMRUNTIME</code></p>
<p>Now I could remove my .vim folder (vimfiles on Windows ) from my HOME directory and am picking up all my plugins, syntax highlighters, etc.  directly from the given vimfiles folder inside my Dropbox.</p>
<p>Only the .vimrc file has to stay were it is and will have to be synchronized among my computers since something has to do the pointing.</p>
<p><span style="font-size:15px;font-weight:bold;">Create a link from your usual vimfiles folder to the vimfiles folder in your dropbox</span></p>
<p>I found a <a href="http://lifehacker.com/#!5154698/sync-files-and-folders-outside-your-my-dropbox-folder">great explanation</a> on how to do this on different operating systems.</p>
<p>Here is an example on how to do this on Windows:</p>
<ol>
<li>Make sure that all your vimfiles have been copied to your Dropbox folder.</li>
<li>Remove the vimfiles in you HOME directory.</li>
<li>Launch cmd.exe and use mklink to create a link from ~/vimfiles to the vimfiles in your dropbox e.g.:<br />
<code>mklink /D "c:\Users\yourusername\vimfiles" "C:\Users\yourusername\My Dropbox\Settings\vimfiles"</code></li>
<li>Optionally do the same for your .vimrc file (I actually link it to a window specific one, because I use a different font for each OS):<br />
<code>mklink "c:\Users\yourusername\.vimrc" "C:\Users\yourusername\My Dropbox\Settings\.vimrc_windows"</code></p>
<p>In this case you need to make sure to not directly edit the local .vimrc as this will overwrite the link you created. Instead edit the .vimrc inside your Dropbox folder.</li>
</ol>
<p>Both solutions allow me to extend my vim, for all my machines in one shot.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thorstenlorenz.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thorstenlorenz.wordpress.com/199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thorstenlorenz.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thorstenlorenz.wordpress.com/199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thorstenlorenz.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thorstenlorenz.wordpress.com/199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thorstenlorenz.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thorstenlorenz.wordpress.com/199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thorstenlorenz.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thorstenlorenz.wordpress.com/199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thorstenlorenz.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thorstenlorenz.wordpress.com/199/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thorstenlorenz.wordpress.com/199/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thorstenlorenz.wordpress.com/199/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=199&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thorstenlorenz.wordpress.com/2011/02/20/automatically-loading-vim-files-from-dropbox-folder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/44f22a4a1324372978a8781dd5748878?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tlorenz10</media:title>
		</media:content>
	</item>
		<item>
		<title>Go to specific parent directory with PowerShell</title>
		<link>http://thorstenlorenz.wordpress.com/2011/01/01/go-to-specific-parent-directory-with-powershell/</link>
		<comments>http://thorstenlorenz.wordpress.com/2011/01/01/go-to-specific-parent-directory-with-powershell/#comments</comments>
		<pubDate>Sat, 01 Jan 2011 19:59:03 +0000</pubDate>
		<dc:creator>Thorsten Lorenz</dc:creator>
				<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://thorstenlorenz.wordpress.com/?p=157</guid>
		<description><![CDATA[After typing commands similar to  &#8217;cd ..\..\..\&#8217; a few too many times, I needed a better solution. My first idea was to have something like cd&#8230; and depending on the number of dots go up to the nth parent. Still, I&#8217;d have to count how many directories up my target is. A colleague of mine [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=157&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>After typing commands similar to  &#8217;cd ..\..\..\&#8217; a few too many times, I needed a better solution.</p>
<p>My first idea was to have something like cd&#8230; and depending on the number of dots go up to the nth parent. Still, I&#8217;d have to count how many directories up my target is.</p>
<p>A colleague of mine had a better idea:</p>
<address>Lets say you are in</address>
<address>C:\Users\JoeShmoe\Documents\Books\</address>
<address>and you would like to go to</address>
<address>c:\Users.</address>
<address>Ideally you just type something like &#8216;<span style="color:#0000ff;">cdto users</span>&#8216; or even &#8216;<span style="color:#0000ff;">cdto u</span>&#8216;</address>
<p>&nbsp;</p>
<p>It would indeed be dandy to have such a function, so I invested some time and came up with the below implementation.</p>
<pre class='PowerShellColorizedScript' style='background-color:#000000;font-size:13px;font-family:Consolas;'>

<span style='color:#aeaeae;'>&lt;#
    .Synopsis
        Sets location to the first parent directory that whose name starts with the given target dir.
    .Description
        Searches upwards to the root drive.
        If target dir is not found, a message is emitted to the console
    .Example
        cd c:\user\joeshmoe\Documents
        C:\user\joeshmoe\Documents&gt;
        Set-LocationTo us
        C:\user&gt;
#&gt;</span>            

<span style='color:#ffde00;'>function</span> <span style='color:#ffffff;'>Set-LocationTo</span> <span style='color:#ffffff;'>{</span>
    <span style='color:#ffde00;'>param</span><span style='color:#ffffff;'>(</span>  <span style='color:#a9a9a9;'>[</span><span style='color:#84a7c1;'>parameter</span><span style='color:#ffffff;'>(</span><span style='color:#ffffff;'>Mandatory</span> <span style='color:#a9a9a9;'>=</span> <span style='color:#00d42d;'>$true</span><span style='color:#ffffff;'>)</span><span style='color:#a9a9a9;'>]</span>
         <span style='color:#a9a9a9;'>[</span><span style='color:#84a7c1;'>ValidateNotNullOrEmpty</span><span style='color:#ffffff;'>(</span><span style='color:#ffffff;'>)</span><span style='color:#a9a9a9;'>]</span>
            <span style='color:#84a7c1;'>[string]</span> <span style='color:#00d42d;'>$targetDir</span><span style='color:#ffffff;'>)</span>            

    <span style='color:#00d42d;'>$dirs</span> <span style='color:#a9a9a9;'>=</span> <span style='color:#ffffff;'>(</span><span style='color:#ffff60;'>pwd</span><span style='color:#ffffff;'>)</span><span style='color:#a9a9a9;'>.</span><span style='color:#ffffff;'>Path</span><span style='color:#a9a9a9;'>.</span><span style='color:#ffffff;'>Split</span><span style='color:#ffffff;'>(</span><span style='color:#00d42d;'>'\'</span><span style='color:#ffffff;'>)</span>
    <span style='color:#ffde00;'>for</span><span style='color:#ffffff;'>(</span><span style='color:#00d42d;'>$i</span> <span style='color:#a9a9a9;'>=</span> <span style='color:#00d42d;'>$dirs</span><span style='color:#a9a9a9;'>.</span><span style='color:#ffffff;'>Length</span> <span style='color:#a9a9a9;'>-</span> <span style='color:#98fe1e;'>1</span><span style='color:#ffffff;'>;</span> <span style='color:#00d42d;'>$i</span> <span style='color:#a9a9a9;'>-ge</span> <span style='color:#98fe1e;'>0</span><span style='color:#ffffff;'>;</span> <span style='color:#00d42d;'>$i</span><span style='color:#a9a9a9;'>--</span><span style='color:#ffffff;'>)</span> <span style='color:#ffffff;'>{</span>
        <span style='color:#ffde00;'>if</span> <span style='color:#ffffff;'>(</span><span style='color:#00d42d;'>$dirs</span><span style='color:#a9a9a9;'>[</span><span style='color:#00d42d;'>$i</span><span style='color:#a9a9a9;'>]</span><span style='color:#a9a9a9;'>.</span><span style='color:#ffffff;'>ToLower</span><span style='color:#ffffff;'>(</span><span style='color:#ffffff;'>)</span><span style='color:#a9a9a9;'>.</span><span style='color:#ffffff;'>StartsWith</span><span style='color:#ffffff;'>(</span><span style='color:#00d42d;'>$targetDir</span><span style='color:#a9a9a9;'>.</span><span style='color:#ffffff;'>ToLower</span><span style='color:#ffffff;'>(</span><span style='color:#ffffff;'>)</span><span style='color:#ffffff;'>)</span><span style='color:#ffffff;'>)</span> <span style='color:#ffffff;'>{</span>
            <span style='color:#00d42d;'>$targetIndex</span> <span style='color:#a9a9a9;'>=</span> <span style='color:#00d42d;'>$i</span>
            <span style='color:#ffde00;'>break</span>
        <span style='color:#ffffff;'>}</span>
    <span style='color:#ffffff;'>}</span>
    <span style='color:#ffde00;'>if</span><span style='color:#ffffff;'>(</span><span style='color:#00d42d;'>$targetIndex</span> <span style='color:#a9a9a9;'>-eq</span> <span style='color:#98fe1e;'>0</span><span style='color:#ffffff;'>)</span> <span style='color:#ffffff;'>{</span>
        <span style='color:#ffff60;'>Write-Host</span> <span style='color:#00d42d;'>"Unable to resolve $targetDir"</span>
        <span style='color:#ffde00;'>return</span>
    <span style='color:#ffffff;'>}</span>            

    <span style='color:#00d42d;'>$targetPath</span> <span style='color:#a9a9a9;'>=</span> <span style='color:#00d42d;'>''</span>
    <span style='color:#ffde00;'>for</span><span style='color:#ffffff;'>(</span><span style='color:#00d42d;'>$i</span> <span style='color:#a9a9a9;'>=</span> <span style='color:#98fe1e;'>0</span><span style='color:#ffffff;'>;</span> <span style='color:#00d42d;'>$i</span> <span style='color:#a9a9a9;'>-le</span> <span style='color:#00d42d;'>$targetIndex</span><span style='color:#ffffff;'>;</span> <span style='color:#00d42d;'>$i</span><span style='color:#a9a9a9;'>++</span><span style='color:#ffffff;'>)</span> <span style='color:#ffffff;'>{</span>
       <span style='color:#00d42d;'>$targetPath</span> <span style='color:#a9a9a9;'>+=</span> <span style='color:#00d42d;'>$dirs</span><span style='color:#a9a9a9;'>[</span><span style='color:#00d42d;'>$i</span><span style='color:#a9a9a9;'>]</span> <span style='color:#a9a9a9;'>+</span> <span style='color:#00d42d;'>'\'</span>
    <span style='color:#ffffff;'>}</span>            

    <span style='color:#ffff60;'>Set-Location</span> <span style='color:#00d42d;'>$targetPath</span>
<span style='color:#ffffff;'>}</span>            

<span style='color:#ffff60;'>Set-Alias</span> <span style='color:#ffde00;'>-Name</span> <span style='color:#ffffff;'>cdto</span> <span style='color:#ffde00;'>-Value</span> <span style='color:#ffffff;'>Set-LocationTo
</span>
</pre>
</blockquote>
<p>If you are sourcing this Set-Location.ps1 from within a module, make sure to export aliases as well e.g.:</p>
<pre class='PowerShellColorizedScript' style='background-color:#000000;font-size:13px;font-family:Consolas;'>

<span style='color:#ffff60;'>Export-ModuleMember</span> <span style='color:#ffde00;'>-Function</span> <span style='color:#ffffff;'>*</span> <span style='color:#ffde00;'>-Alias</span> <span style='color:#ffffff;'>*
</span>
</pre>
<p>otherwise &#8216;cdto&#8217; won&#8217;t be available.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thorstenlorenz.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thorstenlorenz.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thorstenlorenz.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thorstenlorenz.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thorstenlorenz.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thorstenlorenz.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thorstenlorenz.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thorstenlorenz.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thorstenlorenz.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thorstenlorenz.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thorstenlorenz.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thorstenlorenz.wordpress.com/157/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thorstenlorenz.wordpress.com/157/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thorstenlorenz.wordpress.com/157/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=157&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thorstenlorenz.wordpress.com/2011/01/01/go-to-specific-parent-directory-with-powershell/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/44f22a4a1324372978a8781dd5748878?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tlorenz10</media:title>
		</media:content>
	</item>
		<item>
		<title>PowerShell and .svnignore</title>
		<link>http://thorstenlorenz.wordpress.com/2010/09/04/powershell-and-svnignore/</link>
		<comments>http://thorstenlorenz.wordpress.com/2010/09/04/powershell-and-svnignore/#comments</comments>
		<pubDate>Sat, 04 Sep 2010 16:17:44 +0000</pubDate>
		<dc:creator>Thorsten Lorenz</dc:creator>
				<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Svn]]></category>

		<guid isPermaLink="false">http://thorstenlorenz.wordpress.com/?p=143</guid>
		<description><![CDATA[I got very tired of manually ignoring &#8220;bin&#8221;, &#8220;obj&#8221; folders and various files (e.g. _Resharper*) when using Svn. So I looked for a better solution &#8211;  something similar to Git&#8217;s .gitignore. Reading up on some svn documentation resulted in the below function that will look for ignore definitions (similar to .gitignore) in a given .svnignore [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=143&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div>
<p>I got very tired of manually ignoring &#8220;bin&#8221;, &#8220;obj&#8221; folders and various files (e.g. _Resharper*) when using Svn.</p>
<p>So I looked for a better solution &#8211;  something similar to Git&#8217;s .gitignore.</p>
<p>Reading up on some svn documentation resulted in the below function that will look for ignore definitions (similar to .gitignore) in a given .svnignore file and apply these to all folders recursively.</p>
<pre class="PowerShellColorizedScript" style="background-color:#000000;font-size:13px;font-family:Consolas;">

<span style="color:#00d42d;">$psd</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ffff60;">Split-Path</span> <span style="color:#ffffff;">(</span><span style="color:#ffff60;">Get-Variable</span> <span style="color:#ffffff;">MyInvocation</span><span style="color:#ffffff;">)</span><span style="color:#a9a9a9;">.</span><span style="color:#ffffff;">Value</span><span style="color:#a9a9a9;">.</span><span style="color:#ffffff;">MyCommand</span><span style="color:#a9a9a9;">.</span><span style="color:#ffffff;">Path</span>            

<span style="color:#ffde00;">function</span> <span style="color:#ffffff;">Ignore-Svn</span> <span style="color:#ffffff;">{</span>
    <span style="color:#ffde00;">param</span><span style="color:#ffffff;">(</span>  <span style="color:#a9a9a9;">[</span><span style="color:#84a7c1;">parameter</span><span style="color:#ffffff;">(</span><span style="color:#ffffff;">Mandatory</span> <span style="color:#a9a9a9;">=</span> <span style="color:#00d42d;">$true</span><span style="color:#ffffff;">)</span><span style="color:#a9a9a9;">]</span>
      <span style="color:#a9a9a9;">[</span><span style="color:#84a7c1;">ValidateNotNullOrEmpty</span><span style="color:#ffffff;">(</span><span style="color:#ffffff;">)</span><span style="color:#a9a9a9;">]</span>
            <span style="color:#00d42d;">$path</span><span style="color:#a9a9a9;">,</span>
            <span style="color:#00d42d;">$svnIgnoreFile</span> <span style="color:#a9a9a9;">=</span> <span style="color:#00d42d;">"$psd\.svnignore"</span><span style="color:#a9a9a9;">,</span>
            <span style="color:#84a7c1;">[switch]</span> <span style="color:#00d42d;">$recurse</span><span style="color:#ffffff;">)</span>            

    <span style="color:#00d42d;">$fullPath</span> <span style="color:#a9a9a9;">=</span> <span style="color:#ffffff;">(</span><span style="color:#ffff60;">Resolve-Path</span> <span style="color:#00d42d;">$path</span><span style="color:#ffffff;">)</span>
    <span style="color:#ffde00;">if</span><span style="color:#ffffff;">(</span><span style="color:#00d42d;">$recurse</span><span style="color:#ffffff;">)</span> <span style="color:#ffffff;">{</span> <span style="color:#00d42d;">$recFlag</span> <span style="color:#a9a9a9;">=</span> <span style="color:#00d42d;">"-R"</span> <span style="color:#ffffff;">}</span>
 <span style="color:#ffff60;">svn</span> <span style="color:#ffffff;">propset</span> <span style="color:#ffffff;">svn:ignore</span> <span style="color:#00d42d;">$fullPath</span> <span style="color:#ffde00;">-F</span> <span style="color:#00d42d;">$svnIgnoreFile</span> <span style="color:#00d42d;">$recFlag</span>
<span style="color:#ffffff;">}
</span>
</pre>
<div>Here is my .svnignore file:</div>
<blockquote>
<div>
<div>*.obj</div>
<div>*.exe</div>
<div>*.html</div>
<div>*.exp</div>
<div>*.pdb</div>
<div>*.user</div>
<div>*.aps</div>
<div>*.pch</div>
<div>*.vspscc</div>
<div>*_i.c</div>
<div>*_p.c</div>
<div>*.ncb</div>
<div>*.suo</div>
<div>*.tlb</div>
<div>*.tlh</div>
<div>*.bak</div>
<div>*.cache</div>
<div>*.ilk</div>
<div>*.log</div>
<div>*.htm</div>
<div>*.zip</div>
<div>*.sdf</div>
<div>*.opensdf</div>
<div>*.lib</div>
<div>*.sbr</div>
<div>*.msi</div>
<div>*.exe</div>
<div>*.orig</div>
<div>*.db</div>
<div>_ReSharper.*</div>
<div>Release</div>
<div>Debug</div>
<div>bin</div>
<div>obj</div>
<div>_UpgradeReport_Files</div>
</div>
</blockquote>
<div>Finally in order to add all new files that are not ignored, we can use the following function:</div>
<pre class="PowerShellColorizedScript" style="background-color:#000000;font-size:13px;font-family:Consolas;">

<span style="color:#ffde00;">function</span> <span style="color:#ffffff;">Add-Svn</span> <span style="color:#ffffff;">{</span>
 <span style="color:#ffff60;">svn</span> <span style="color:#ffffff;">st</span> <span style="color:#a9a9a9;">|</span> <span style="color:#ffff60;">%</span><span style="color:#ffffff;">{</span> <span style="color:#ffde00;">if</span> <span style="color:#ffffff;">(</span><span style="color:#00d42d;">$_</span> <span style="color:#a9a9a9;">-match</span> <span style="color:#00d42d;">"\?\s+(.*)"</span><span style="color:#ffffff;">)</span> <span style="color:#ffffff;">{</span> <span style="color:#ffff60;">svn</span> <span style="color:#ffffff;">add</span> <span style="color:#00d42d;">$matches</span><span style="color:#a9a9a9;">[</span><span style="color:#98fe1e;">1</span><span style="color:#a9a9a9;">]</span> <span style="color:#ffffff;">}</span> <span style="color:#ffffff;">}</span>
<span style="color:#ffffff;">}
</span>
</pre>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/thorstenlorenz.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/thorstenlorenz.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/thorstenlorenz.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/thorstenlorenz.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/thorstenlorenz.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/thorstenlorenz.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/thorstenlorenz.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/thorstenlorenz.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/thorstenlorenz.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/thorstenlorenz.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/thorstenlorenz.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/thorstenlorenz.wordpress.com/143/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/thorstenlorenz.wordpress.com/143/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/thorstenlorenz.wordpress.com/143/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=thorstenlorenz.wordpress.com&amp;blog=13778800&amp;post=143&amp;subd=thorstenlorenz&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://thorstenlorenz.wordpress.com/2010/09/04/powershell-and-svnignore/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/44f22a4a1324372978a8781dd5748878?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">tlorenz10</media:title>
		</media:content>
	</item>
	</channel>
</rss>
