Simplicity is the ultimate sophistication. - Leonardo da Vinci

Twitter Bootstrap in Visual Studio 2010 with Chirpy and Dotless

For my latest side project, I decided to play around with ASP.NET MVC 4 and Twitter Bootstrap. Bootstrap is a  front-end toolkit open sourced by Twitter engineers, and comes chock-full of goodies like a responsive grid framework, LESS, jQuery plugins and CSS3 styling.

I’ve heard a lot about CSS compilers like Sass and LESS, but thought I would start with LESS first simply because I didn’t have to learn any new syntax. When using LESS in Visual Studio you essentially have three options for compiling:

  • Client-side (JavaScript) – Include the LESS.js script in your site and link directly to your .less files. The script will compile your LESS into CSS at runtime. Unfortunately, this means any errors will cause your CSS files to vomit on your live site.
  • Server-side (dotless) – Install via NuGet or GitHub. Compiles dynamically via ASP.NET HttpHandler or compiler .EXE. The compiler will convert your .less files into .css files.
  • Server-side (Chirpy) – Install via Codeplex (NuGet packages are out of date). This VS add-in not only compiles LESS files it also minimizes JS, CSS, LESS files. You name your LESS files with a .chirp.less extension and Chirpy will compile them in realtime. You can also use a .less.css extension so you can retain Intellisense for CSS.

Installing Twitter Bootstrap
I installed two NuGet packages, the Bootstrap, from Twitter package for the base files and then the Bootstrap, from Twitter Less Source package since I wanted to use LESS. It created /less and /images subfolders under the standard /Content folder in MVC, and loaded the appropriate files from Bootstrap. It added bootstrap.js and less.min.js to the /Scripts folder.

Installing Chirpy
Chirpy’s minify capabilities seemed like a good deal in addition to the LESS compilation so I installed the NuGet package. According to the Chirpy documentation and this blog post  from John Hoff (The Brain Donor) on using Chirpy, I set up file groups to “mash” the files together into single .css files which I could then reference from my site. I created a file called

mash.less.chirp.config

and put it in the /Content folder. Here’s what the file contained:

<root>
  <FileGroup Name="style.css">
    <File Path="less/bootstrap.less" />
    <File Path="less/responsive.less" />
  </FileGroup>
</root>

Bootstrap.less and response.less import and load all of the individual .less files included in Bootstrap, so they were the only ones I needed to include.

I also created a file to mash together my JavaScript files. I named the file

mash.js.chirp.config

and put it in the /Scripts folder. Here is what it contained:

<root>
  <FileGroup Name="site.js">
    <File Path="AjaxLogin.js" />
    <File Path="bootstrap.min.js" Minify="false" />
    <File Path="jquery-1.6.2-vsdoc.js" />
    <File Path="jquery-ui-1.8.11.min.js" Minify="false"/>
    <File Path="jquery.validate.min.js" Minify="false"/>
    <File Path="knockout-2.0.0.js" />
    <File Path="less.min.js" Minify="false"/>
  </FileGroup>
</root>

I then saved, and nothing happened. No magical .css or .js file appeared underneath my special chirp.config files. No errors. Even after I rebuilt the solution, it was eerily Chirpy silent. Something was wrong. So, since Chirpy was technically an Add-In, I went to the Add-Ins menu in Visual Studio and Chirpy was not there! I then poked around on the interwebs and realized the NuGet packages were NOT up to date. So I went ahead and downloaded the .vsi file from Codeplex and installed Chirpy the old-fashioned way.

Almost immediately, my compiled .css file appeared, along with a whole host of CSS errors from the Bootstrap LESS files. I turned to Google, and it led me to lo and behold, another post from John Huff about Twitter Bootstrap woes with Chirpy. Apparently the Chirpy compiler does not like the Bootstrap LESS. His recommended solution was to use the dotless library to compile the LESS files and then use Chirpy to mash and minimize the files.

Installing dotless
So I installed dotless via NuGet. Following the instructions in Huff’s blog post, I added two calls to the dotless .EXE compiler in my MVC project’s Pre-build events (Project -> Properties -> Build Events tab):

"$(SolutionDir)packages\dotless.1.3.0.0\tool\dotless.compiler.exe" 
       "$(ProjectDir)Content\less\bootstrap.less" 
       "$(ProjectDir)Content\css\bootstrap.css"

"$(SolutionDir)packages\dotless.1.3.0.0\tool\dotless.compiler.exe" 
       "$(ProjectDir)Content\less\responsive.less" 
       "$(ProjectDir)Content\css\responsive.css"

I included quotations around both the .exe and the source and target file paths because my solution and project directories both contain spaces in the path folder names. I created a /css subfolder in my /Content directory to hold the generated .css files. After doing an initial build, I navigated to the /css folder, saw the two new files (with shiny CSS culled from all those Bootstrap LESS files), and added them to my VS project.

Updating Chirpy
I then updated my mash.less.chirp.config file to mash together the newly compiled .css files instead:

<root>
  <FileGroup Name="style.css">
    <File Path="css/bootstrap.css" />
    <File Path="css/responsive.css" />
  </FileGroup>
</root>

And voila, my style.min.css file appeared within a few seconds after saving and building the project, and it included the minified CSS from both .css files! I then updated my site template to use “style.min.css” as my stylesheet.

Sure, it would have been nice to only have to use Chirpy, but since I really wanted to use Bootstrap for this project, and for some reason Bootstrap and Chirpy just don’t get along at the moment, this is a perfectly fine workaround for the time being.

DevConnections 2012: Day One

Today was the first full day of the DevConnections 2012 conference, held in Las Vegas. Since I tend to get overwhelmed by all my conference notes and materials when I return home, I thought I would post the interesting bits from the sessions I attended on Day One. This is by no means a complete summary of all the session material, just things I found noteworthy and want to follow-up on later.

Morning Keynote: Connected Devices, Continual Services – Jason Zander, Corporate Vice President, Visual Studio (Microsoft)

  • 90% of Visual Studio 11 development teams use Scrum. Approximately 1,000 developers and they have set half the milestones as the last version, with no loss in productivity.
  • New “Page Inspector” feature  - brings IE “Developer Tools”-like functionality  into the Visual Studio environment. Lets you hover over sections of page in Design view and see highlighted sections in code, CSS, JS, etc. all on one screen. I might actually use it instead of Firebug. Maybe. :)
  • Windows 8 emulator was pretty cool and sexy.
  • Expression Blend 11 will now have development environment for HTML, previously missing.
  • PowerPoint storyboarding, bringing Balsamiq-like mockup functionality to PowerPoint.
  • Brian Keller demo’ed the new feedback functionality baked into VS11: can request/give feedback, take screenshots, edit screenshots, add comments, etc.
  • Intellitrace diagnostics added to Systems Center Essentials. Our team has been moving toward greater logging/monitoring capacity, starting with the third-party tool Gibraltar. Having Intellitrace data on production apps would be very useful.
  • Visual Studio Ultimate Feature Packs

What’s New in  SQL Server 2012 Reporting Services – Chuck Heinzelman, Microsoft

  • PowerView – new UI design experience and data visualization tool. Self-service data exploration. Requires Tabular BI Semantic Model and SharePoint integrated mode. Uses Silverlight, requires IE or Firefox. PowerView reports can be embedded into PowerPoint; if connected to server/data source, can interact with report within PowerPoint slide.
  • Data Alerts – can send email to users upon report conditions. Report feed is simply an Atom feed, so you can subscribe to it. Schedule it to be checked down to the minute and/or when results change. Runs as background service on SharePoint. Requires stored credentials. Email will include link to launch report. Only available if report is running in SharePoint integrated mode.
  • SharePoint integrated mode should run faster, though still not quite on par with Native mode.
  • Designer for VS2010 can support reports from 2008 and up.
  • SQL Azure – SSRS is now available as a service on Azure. Operational reporting against SQL Azure data. Renders reports in ReportViewer controls. Can develop within Visual Studio using SQL Server Data Tools.
  • When Chuck asked the audience who was using SQL Azure, no one raised their hands. He seemed surprised.
  • SQL Server 2012 Early Adoption Cookbook

 Async: New Feature for Responsive Programming in Visual Studio 11 and .NET 4.5 – Lucian Wischik, Microsoft

  •  Born from Microsoft Research, team has spent two years bringing it to developer fruition.
  • The UI message pump is key to async.
  • With async, the message pump spins continuously like usual, but is not blocked by long-running processes.
  • The Await keyword is like a placeholder for tasks. It helps the program remember where it left off, before it returns to the caller. When the task process finishes, it notifies the message pump, and continues execution at the placeholder.
  • Repercussions: you will need to disable UI elements/methods to prevent multiple invokes. It is easy to deadlock when you mix sync and async code, so don’t do it!
  • Tasks are a promise of future results.
  • Async functions are required to return tasks. As a result, only top level callers can have async subs; otherwise, you must use functions.
  • Console apps: since there is no message pump, calls are passed to the thread pool. Returned tasks are posted to another thread. This is not parallelism, just different threads. As a result, you cannot use ThreadLocal storage.
  • CancellationTokenSource – cancel requests no longer needed.
  • The UI thread is like a single waiter (thread) serving multiple  customers (tasks).
  • Asynchrony and concurrency don’t need multiple threads.
  • No multiple threads = no race conditions!

Using ASP.NET MVC 4, Web API, jQuery Mobile to Create an Engaging Web App Targeting Modern Tablets and Mobiles – Scott Hanselman, Microsoft

  • Users accessing web via mobile only: US-UK: 25%; China: 30%; India: 59%; Egypt: 70%.
  • If you do nothing else to make your site mobile friendly, add the viewport meta tag.
  • To fix your site on the client: custom CSS, media queries, adaptive rendering (responsive design).
    • Scott demo’ed his own blog and how it uses adaptive rendering, including the “opt group” syntax to handle his main navigation menu. Also has a post on how to make embedded YouTube videos fluid and responsive.
    • Mediaqueri.es - Gallery of cool responsive designs, with four different breakpoints for each site.
    • 51degrees.mobi - open source database of devices along with dpi, width, etc. You can query the database for your custom CSS.
    • ImageResizer – NuGet package provides API for manipulating images. Works server side.
  • To fix your site on the server:
    • Custom mobile views using display modes.
    • Mobile UI frameworks: jQuery Mobile comes included with MVC, but Sencha Touch, Kendo UI are others.
    • Electric Plum – mobile device simulator.
    • MobileFirst movement – design for mobile first, desktop second.
    • GetSkeleton – Boilerplate HTML/CSS template for responsive design.
    • 320AndUp – Another boilerplate template.
    • In the global.asax, use the DisplayModeProvider to add new “displayModes” for different devices.
Fundamentals of Windows 8 XAML Metro Style Apps – John Papa
  • Unlike desktop apps, Metro apps use the same WinRT APIs whether they are XAML or HTML/CSS based.
  • Use the skillset you have, whether it’s XAML or HTML. Both have same tooling, same WinRT.
  • If you’re a XAML developer, you’re already a Win8 developer.
  • Windows 8 is a content-centric UX: it’s about what you don’t see, what’s left out: extra chrome, extraneous navigation, etc.
  • File types/templates are all about consuming and displaying data: grid groupings, split applications, etc.
  • Windows 8 has new namespace, new controls.
  • Handling events is about handling the Touch APIs: pointers, gestures, and manipulations.
  • WinRT consolidates mouse, stylus, and touch inputs; handle via Case statements?
  • Styling  is like painting a wall from blue to red.
  • Templates are like changing a wall to a window.
  • Three ways to use animations: use baked in animations, roll your own custom animations, and use the Animations library.
Evening Keynote: One ASP.NET – Scott Hanselman, Principal Program Manager, Community Architect for Web Platform & Tools (Microsoft)
  • One ASP.NET – feedback has been that the New Project dialog in Visual Studio is like a frightening fork in the road, and once you make a decision you are stuck with it forever. Team wants to change that perception.
  • MVC 4 comes with Modernizr, jQuery, jQuery Mobile. HTML5 requires polyfilling for browsers that don’t support those elements yet.
  • jQuery Intellisense in Visual Studio 11
  • Scott Hanselman was joined by Scott Hunter and Scott Guthrie to announce the “open-sourcing” of ASP.NET MVC, Razor, and Web API on Codeplex. http://aspnetwebstack.codeplex.com
  • Miguel de Icaza was the first “third-party” to request a pull on the project. Watched it live during presentation. :)
  • Contributors will have to submit an agreement, stating that the code is theirs (and not their employer’s, etc.)
  • All code will go through same review, unit testing, security testing, etc. as code written by Microsoft team. Same standards applied to other open source project incorporated into ASP.NET MVC: jQuery, Modernizr, JSON.NET.
  • Subscribe to project’s RSS feed to follow check-ins.

3 Scotts, 3 red polos [pic]

Scott Guthrie, Scott Hunter, Scott Hanselman announcing open sourced ASP.NET web stack at DevConnections.

Open sourced! [Pic]

ASP.NET web stack open sourced on Codeplex!