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

<channel>
	<title>Cory O&#039;Daniel - These are just words &#187; rake</title>
	<atom:link href="http://coryodaniel.com/index.php/tag/rake/feed/" rel="self" type="application/rss+xml" />
	<link>http://coryodaniel.com</link>
	<description>Software development, thoughts, and randomness</description>
	<lastBuildDate>Wed, 01 Sep 2010 18:49:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Rails rake file for compressing your Javascript with YUI</title>
		<link>http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/</link>
		<comments>http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/#comments</comments>
		<pubDate>Wed, 10 Feb 2010 18:35:29 +0000</pubDate>
		<dc:creator>Cory O'Daniel</dc:creator>
				<category><![CDATA[RubyDevelopment]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rake]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://coryodaniel.com/?p=462</guid>
		<description><![CDATA[Here is a quick rake file I threw together to use with my Ruby YUI 2.0 (I-refuse-to-make-a-gem-
edition).
This of course should be used with the Ruby YUI Compressor I posted about the other day.
This script will tar/gzip all of your javascripts just incase something stupid happens. So you have a little safety net.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'fileutils'
require 'pathname'
&#160;
namespace :js [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a quick rake file I threw together to use with my Ruby YUI 2.0 (I-refuse-to-make-a-gem-<br />
edition).</p>
<p>This of course should be used with the <a href="http://coryodaniel.com/index.php/2010/02/08/ruby-yui-compressor-by-cory-v-2-0-simpler-shorter/">Ruby YUI Compressor</a> I posted about the other day.</p>
<p>This script will tar/gzip all of your javascripts just incase something stupid happens. So you have a little safety net.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'fileutils'</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'pathname'</span>
&nbsp;
namespace <span style="color:#ff3333; font-weight:bold;">:js</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  desc <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>INFO
Output Compressed JavaScript to STDOUT; 
  COMPRESS=ALL to compress all Javascript files that DONT contain <span style="color:#996600;">&quot;.min.&quot;</span> 
INFO
&nbsp;
  task <span style="color:#ff3333; font-weight:bold;">:yui</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:environment</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    targz<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> ENV<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'COMPRESS'</span><span style="color:#006600; font-weight:bold;">&#93;</span> == <span style="color:#996600;">'ALL'</span>
      <span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span>Rails.<span style="color:#9900CC;">root</span>.<span style="color:#9900CC;">to_s</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#ff3333; font-weight:bold;">:public</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#ff3333; font-weight:bold;">:javascripts</span> <span style="color:#006600; font-weight:bold;">/</span> <span style="color:#996600;">'**/*.js'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>javascript<span style="color:#006600; font-weight:bold;">|</span>
        <span style="color:#9966CC; font-weight:bold;">next</span> <span style="color:#9966CC; font-weight:bold;">if</span> javascript =~ <span style="color:#006600; font-weight:bold;">/</span>\.<span style="color:#9900CC;">min</span>\.<span style="color:#006600; font-weight:bold;">/</span>
&nbsp;
        compress<span style="color:#006600; font-weight:bold;">&#40;</span>javascript<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">exist</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>Rails.<span style="color:#9900CC;">root</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;config/yui.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        javascripts = <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#CC0066; font-weight:bold;">load</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;config/yui.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">read</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;javascripts&quot;</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">if</span> !javascripts.<span style="color:#9900CC;">blank</span>?
          javascripts.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>script<span style="color:#006600; font-weight:bold;">|</span> compress<span style="color:#006600; font-weight:bold;">&#40;</span>Rails.<span style="color:#9900CC;">root</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;public/javascripts&quot;</span> <span style="color:#006600; font-weight:bold;">+</span> script<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#125;</span>
        <span style="color:#9966CC; font-weight:bold;">else</span>
          <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span>, <span style="color:#996600;">&quot;No javascript files in config/yui.yml&quot;</span> 
        <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        <span style="color:#CC0066; font-weight:bold;">raise</span> <span style="color:#CC00FF; font-weight:bold;">Exception</span>, <span style="color:#996600;">&quot;config/yui.yml Not Found; Do rake js:generate to create one or COMPRESS=ALL to run without a config file&quot;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  desc <span style="color:#996600;">&quot;Generate YUI Compressor Config file&quot;</span>
  task <span style="color:#ff3333; font-weight:bold;">:generate</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:environment</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    config_path = Rails.<span style="color:#9900CC;">root</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">&quot;config/yui.yml&quot;</span>
    <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>config_path, <span style="color:#996600;">&quot;w+&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span>
      f.<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>CONFIG
<span style="color:#006600; font-weight:bold;">---</span>
javascripts:
  <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#996600;">&quot;application.js&quot;</span>
  <span style="color:#006600; font-weight:bold;">-</span> <span style="color:#996600;">&quot;jquery.js&quot;</span>
CONFIG
      <span style="color:#CC0066; font-weight:bold;">puts</span> config_path
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> targz<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    tgz_file = <span style="color:#996600;">&quot;#{Time.now.to_i}.javascripts.tgz&quot;</span>
    <span style="color:#996600;">`cd #{Rails.root + &quot;public&quot;}; tar -zcf #{tgz_file} javascripts/`</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Backed up assets to: #{tgz_file}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> compress<span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Compressing #{path}&quot;</span>
    file_handle = <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>path<span style="color:#006600; font-weight:bold;">&#41;</span>
    compressed_output = YUI.<span style="color:#9900CC;">compress_safe</span> file_handle
    file_handle.<span style="color:#9900CC;">close</span>
&nbsp;
    <span style="color:#008000; font-style:italic;">#overwrite the file</span>
    <span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#CC0066; font-weight:bold;">open</span><span style="color:#006600; font-weight:bold;">&#40;</span>path, <span style="color:#996600;">&quot;w+&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>file<span style="color:#006600; font-weight:bold;">|</span> file.<span style="color:#CC0066; font-weight:bold;">puts</span> compressed_output <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Wanna use it?<br />
1. Drop it in your lib/tasks folder or wherever your Rakefile looks</p>
<p>You can compress 'everything' or specific files. </p>
<p>If you want to only compress specific Javascript files do:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">rake js:generate 
<span style="color: #666666; font-style: italic;"># Edit your config/yui.yml file</span>
rake js:yui</pre></td></tr></table></div>

<p>If you want to compress "everything" (It actually won't compress files that contain ".min.". You can remove the regexp's if you don't like it.)</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="bash" style="font-family:monospace;">rake js:yui <span style="color: #007800;">COMPRESS</span>=ALL</pre></td></tr></table></div>

<p>Yay, now your raking it in (Pun harhar) w/ your web2.0 yui compressed rails app. Woot woot.</p>
<p align="left"><a class="tt" href="http://twitter.com/home/?status=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI+http://bit.ly/c5ZCBM" title="Post to Twitter"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-twitter-big2.png" alt="Post to Twitter" /></a> <a class="tt" href="http://digg.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/&amp;title=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI" title="Post to Digg"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-digg-big2.png" alt="Post to Digg" /></a> <a class="tt" href="http://www.facebook.com/share.php?u=http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/&amp;t=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI" title="Post to Facebook"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-facebook-big2.png" alt="Post to Facebook" /></a> <a class="tt" href="http://reddit.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/&amp;title=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI" title="Post to Reddit"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-reddit-big2.png" alt="Post to Reddit" /></a> <a class="tt" href="http://stumbleupon.com/submit?url=http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/&amp;title=A+Rails+rake+file+for+compressing+your+Javascript+with+YUI" title="Post to StumbleUpon"><img class="nothumb" src="http://coryodaniel.com/wp-content/plugins/tweet-this/icons/tt-su-big2.png" alt="Post to StumbleUpon" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://coryodaniel.com/index.php/2010/02/10/a-rails-rake-file-for-compressing-your-javascript-with-yui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
