<?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>Gigu's blog &#187; microsoft</title>
	<atom:link href="http://blog.gigoo.org/tag/microsoft/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.gigoo.org</link>
	<description>wEBbLOG</description>
	<lastBuildDate>Thu, 01 Jul 2010 22:59:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Verify JavaScript with JSLint during build using Nant</title>
		<link>http://blog.gigoo.org/2009/03/25/verify-javascript-with-jslint-during-build-using-nant/</link>
		<comments>http://blog.gigoo.org/2009/03/25/verify-javascript-with-jslint-during-build-using-nant/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 13:39:06 +0000</pubDate>
		<dc:creator>Greg Gigon</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jslint]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[nant]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://blog.gigoo.org/2009/03/25/verify-javascript-with-jslint-during-build-using-nant/</guid>
		<description><![CDATA[ Project I’m working on has a lot of JavaScript. We are minimizing JavaScript files during the build and combining them into one file. Problem is when someone checks in some invalid JavaScript into repository that doesn’t minify properly. As a result you can end up with not working JavaScript at all. There is a [...]]]></description>
			<content:encoded><![CDATA[<p><img title="image" style="display: inline; margin: 0px 5px 0px 0px" height="40" alt="image" src="http://blog.gigoo.org/wp-content/uploads/2009/03/image1.png" width="129" align="left" /> Project I’m working on has a lot of JavaScript. We are minimizing JavaScript files during the build and combining them into one file. Problem is when someone checks in some invalid JavaScript into repository that doesn’t minify properly. As a result you can end up with not working JavaScript at all. There is a tool called <a title="JSLint" href="http://www.jslint.com/lint.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.jslint.com');" target="_blank">JSLint</a>. It is created by <strong><em>Douglas Crockford</em></strong> who is also the author of a very good book about JavaScript: “<em><strong><a href="http://www.amazon.com/exec/obidos/ASIN/0596517742/" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.amazon.com');" target="_blank">JavaScript: The Good Pars</a></strong></em>”.</p>
<p><a title="JSLint" href="http://www.jslint.com/lint.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.jslint.com');" target="_blank">JSLint</a> is a very strict JavaScript verifier. JS files corrected to a JSLint specification could be minified with confidence. </p>
<p>We made the decision that build should brake when <a title="JSLint" href="http://www.jslint.com/lint.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.jslint.com');" target="_blank">JSLint</a> detects invalid JavaScript file, so developer will need to fix it before the code checkin.</p>
<h2>Prepare the tool base</h2>
<p>As a first step we need to get all the tools that will execute JSLint validation against our codebase. You can grab <a href="http://www.jslint.com/fulljslint.js" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.jslint.com');" target="_blank"><strong><em>fulljslint.js</em></strong></a> from the JSLint home page. There is no executable binary for JSLint, it is just a JavaScript file with set of rules and function to execute verification. Our working environment is Windows we can execute validation using <strong><em>cscript.exe</em></strong> command line tool that Windows has. There is a very useful script created by <strong><em>Jason Diamond </em></strong>that helps to execute JSLint validation. You can get it from from here: <a href="http://jason.diamond.name/downloads/jslint.wsf" onclick="javascript:pageTracker._trackPageview('/outbound/article/jason.diamond.name');" target="_blank"><strong><em>jslint.wsf</em></strong></a><strong><em>. </em></strong>One last thing is a command line batch file that helps execute this tool. You can name it whatever you would like to. I named it <strong><em>jslint.bat. </em></strong>The content of it looks like this:</p>
<blockquote><pre><strong>@cscript //nologo %~dp0\jslint.wsf %*</strong></pre>
</blockquote>
<p>I put all those three files into build tree, in tools folder. You can check if all the files are valid and working properly by running this command from command line:</p>
<blockquote>
<p><strong>jslint.bat c:\path\to\your\js\file.js</strong></p>
</blockquote>
<h2>Create a build target</h2>
<p>Now that we have all the tools we can create a build target. We are using <strong><em><a title="Nant build tool" href="http://nant.sourceforge.net/" onclick="javascript:pageTracker._trackPageview('/outbound/article/nant.sourceforge.net');" target="_blank">Nant</a></em></strong> as a build tool. I created Nant target and called it jslint. It looks like this.</p>
<blockquote>
<p><strong>&lt;target name=&quot;jslint&quot; description=&quot;validates JS files&quot;&gt;<br />
      <br />&#160;&#160;&#160; &lt;foreach item=&quot;File&quot; property=&quot;jsfile&quot;&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;in&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;items&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;include name=&quot;path\to\scripts\folder\**\*.js&quot; /&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/items&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/in&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;do&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;exec program=&quot;jslint.bat&quot; commandline=&quot;${jsfile}&quot; /&gt; </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/do&gt; </p>
<p>&#160;&#160;&#160; &lt;/foreach&gt; </p>
<p>&lt;/target&gt;</strong></p>
</blockquote>
<p>There is a lot of stuff that could be turned on/off during validation. If you type <strong><em>jslint.bat </em></strong>without any parameters it will give you a list of possible options. Those could as well be tweaked in the <a href="http://jason.diamond.name/downloads/jslint.wsf" onclick="javascript:pageTracker._trackPageview('/outbound/article/jason.diamond.name');" target="_blank"><strong><em>jslint.wsf</em></strong></a> file.</p>
<p>Hope this helps you as it did for us.</p>
<p>Some says that JSLint might hurt your feelings. I say, bring it on <img src='http://blog.gigoo.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Gregster</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gigoo.org/2009/03/25/verify-javascript-with-jslint-during-build-using-nant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom JavaScript KungFu and management for Microsoft MVC.NET</title>
		<link>http://blog.gigoo.org/2009/03/12/custom-javascript-kungfu-and-management-for-microsoft-mvcnet/</link>
		<comments>http://blog.gigoo.org/2009/03/12/custom-javascript-kungfu-and-management-for-microsoft-mvcnet/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 13:09:15 +0000</pubDate>
		<dc:creator>Greg Gigon</dc:creator>
				<category><![CDATA[development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[mvc]]></category>

		<guid isPermaLink="false">http://blog.gigoo.org/2009/03/12/custom-javascript-kungfu-and-management-for-microsoft-mvcnet/</guid>
		<description><![CDATA[ Rails community got a lot things right for web application. One of them is the way they manage JavaScript files. I’m working on a web project where I use a lot of JavaScript and MVC.NET as Web Framework. Team is quite big and JavaScript is getting out of control. Files are everywhere with a [...]]]></description>
			<content:encoded><![CDATA[<p><img style="display: inline; margin: 0px 5px 0px 0px" src="http://blog.gigoo.org/wp-content/uploads/2009/02/image-thumb.png" align="left" /> Rails community got a lot things right for web application. One of them is the way they manage JavaScript files. I’m working on a web project where I use a lot of JavaScript and MVC.NET as Web Framework. Team is quite big and JavaScript is getting out of control. Files are everywhere with a lot of code. And then when it goes into production it is the best to have it as one compressed and minified file. So, here what we did.</p>
<h3>Where am I, ups, production, I better behave</h3>
<p>The easiest possible way of telling the app where it is running will be in <strong>Web.config</strong>. There is already a bunch of custom setting in there, so why not just have one more. When the time comes based on the <strong>Environment </strong>information we can decide if we will still spit out single un-minified files for a development environment or serve one minified file for lightning fast production performance. </p>
<p> <span id="more-4784"></span><br />
<h3>Help me, help me, Mr Helper</h3>
<p>So, the idea with the helper is: we use it to include every single file that we need on our page. When it knows that we are on Production it will replace imports with one, plus potentially minify it and compress. So I created a class called <strong>JSHelper</strong>.</p>
<p>JSHelper will contain two methods, one will kind of register the file for inclusion, and the other will do the actual job. So, the C# code goes somewhat like this:</p>
<blockquote><p><em>public class <strong>JSHelper</strong>         <br />{         <br />&#160;&#160;&#160;&#160; private readonly <strong>IList</strong>&lt;<strong>string</strong>&gt; listOfFilesToInclude = new <strong>List</strong>&lt;<strong>string</strong>&gt;();         <br />&#160;&#160;&#160;&#160; public const <strong>string</strong> KEY = &quot;JSHelper_Key&quot;;</em></p>
<p><em>&#160;&#160;&#160;&#160; public void IncludeJs(<strong>string</strong> fileName)         <br />&#160;&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; if (!listOfFilesToInclude.Contains(fileName))         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; listOfFilesToInclude.Add(fileName);         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }         <br />&#160;&#160;&#160;&#160; }</em></p>
<p><em>&#160;&#160;&#160;&#160; public <strong>string</strong> IncludeAllJs()         <br />&#160;&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; var scriptRoot = <strong>VirtualPathUtility</strong>.ToAbsolute(&quot;~/Scripts&quot;);         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; var scriptFormat = &quot;&lt;script src=\&quot;{0}/{1}\&quot; type=\&quot;text/javascript\&quot;&gt;&lt;/script&gt;\r\n&quot;;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; var importedJavaScripts = &quot;&quot;;         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; foreach (var fileName in listOfFilesToInclude)         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; importedJavaScripts += <strong>string</strong>.Format(scriptFormat, scriptRoot, fileName);         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; return importedJavaScripts;         <br />&#160;&#160;&#160;&#160; }         <br />}</em></p>
</blockquote>
<p>So, as you can see it will also eliminate potential errors of including the same JS file twice.</p>
<p>Now to solve next problem. The <strong>JSHelper </strong>needs to be a single instance per <strong>Web Request</strong>. It needs to be as it holds the state of all the files to be imported. Having a static means that it is shared across all web requests, not good. It has to be an instance for a every server thread. Controllers are save place for our helper to be, plus they can put stuff to a <strong>ViewData. </strong>As you might or not be aware this is accessible in <strong>aspx </strong>View template.</p>
<h3></h3>
<h3>Take over Control</h3>
<p>In MVC.NET there is base class for all controllers that every controller inherits. It is called <strong>Controller. </strong>Because we got some more stuff to setup in all our controller we created our own that extends base framework controller and every controller in our application extends it. This is a great place to put our JSHelper in. Code goes something like that:</p>
<blockquote><p><em>public abstract class <strong>ApplicationController</strong> : </em><em><strong>Controller          <br /></strong>{         <br />&#8230;         <br />&#160;&#160;&#160;&#160; protected override void OnActionExecuting(<strong>ActionExecutingContext</strong> filterContext)         <br />&#160;&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ViewData[<strong>JSHelper</strong>.KEY] = new <strong>JSHelper</strong>();         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <strong>base</strong>.OnActionExecuting(filterContext);         <br />&#160;&#160;&#160;&#160; }         <br /></em><em>…        <br /></em><em>}</em></p>
</blockquote>
<p>This code will create new instance of <strong>JSHelper </strong>and put it into your <strong>ViewData</strong> for usage. Because we also extend MVC.NET base class we can have JSHelper accessible in every View we got in the application.</p>
<h3>My VIEW of the world</h3>
<p>So the code goes something like this:</p>
<blockquote><p><em>public class <strong>BaseViewPage</strong>&lt;<strong>TModel</strong>&gt; : <strong>ViewPage</strong>&lt;<strong>TModel</strong>&gt; where <strong>TModel</strong> : class         <br />{         <br />&#160;&#160;&#160; public <strong>JSHelper</strong> JSHelper         <br />&#160;&#160;&#160; {         <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; get { return (<strong>JSHelper</strong>) ViewData[<strong>JSHelper</strong>.KEY]; }         <br />&#160;&#160;&#160; } </em></p>
<p><em>}</em></p>
</blockquote>
<p>Something similar I done for the <strong>Master Page</strong>. This way we could combine all JavaScripts into one place.</p>
<p>So, the example of a View with JSHelper looks like this.</p>
<h4>Master page</h4>
<blockquote><p><em>…</em></p>
<p><em>&lt;head&gt;        <br />&lt;% JSHelper.IncludeJs(&quot;jquery.js&quot;);%&gt;         <br />&lt;% JSHelper.IncludeJs(&quot;other-awesome-library.js&quot;); %&gt;         <br /></em></p>
<p><em>&lt;asp:ContentPlaceHolder ID=&quot;headContent&quot; runat=&quot;server&quot; /&gt;        <br /></em></p>
<p><em>&lt;%= JSHelper.IncludeAllJs() %&gt;        </p>
<p>&lt;/head&gt;         </p>
<p>&lt;body&gt;</em></p>
<p><em>…</em></p>
</blockquote>
<h4>View</h4>
<blockquote><p>…</p>
<p>&lt;asp:Content ContentPlaceHolderID=”headContent” runat=”server”&gt;      </p>
<p>&lt;% JSHelper.IncludeJs(&quot;blabla.js&quot;);%&gt;      <br />&lt;% JSHelper.IncludeJs(&quot;hm-hm.js&quot;);%&gt;</p>
<p>&lt;/asp:Content&gt;</p>
<p>…</p>
</blockquote>
<h3>Final pieces</h3>
<p>Now we can put into use our <strong>Web.config </strong>file<strong> </strong>and change <strong>IncludeAllJs </strong>method to serve files based on environment we are in. Not posting implementation as it could be achieved in many ways. </p>
<p>So, when we are serving JS on production environment we can serve one single file prepared before as a deployment task or we can dynamically prepare it by pointing web browser to another controller that will combine all the files and minified it on a flight. </p>
<p>You can look at some JavaScript code minification in here: <a title="JavaScript minification" href="http://www.crockford.com/javascript/jsmin.cs" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.crockford.com');" target="_blank">http://www.crockford.com/javascript/jsmin.cs</a></p>
<p>Let me know if you find this ideas useful.</p>
<p>Cheerios, Greg</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.gigoo.org/2009/03/12/custom-javascript-kungfu-and-management-for-microsoft-mvcnet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
