2013-09-01T00:36:08-04:00http://DannyDouglass.com/Octopress2011-02-24T10:02:54-05:00http://DannyDouglass.com/blog/2011/02/24/create-a-recursive-ruby-script-to-replace-file-and-directory-names-and-file-contentsOver this past weekend I began templating a project structure that we use at my day/evening/night job to allow developers to simply execute a script and provide a few parameters to
initialize a new development project (including build script, fxcop analysis, unit testing, visual studio projects, etc.). I didn’t think this would take that long despite being relatively new to the Ruby world.

No matter, I’m a developer after all and should be, better be capable of handling this simple script. I decided to start simple and just use the directory global searching method to recursively get everything within my project template’s directory:

OK, good start. Now I have all the items (files and directories) within my current executing directory. I figured it would be easy to then replace the templated text in the directory and file paths at that point.

Not so fast!

The global directory search – Dir.glob() – actually traverses the directory structure and returns an array of all the file/directory names within the executing directory. Take a look at the ruby documentation on the it here. What ended up happening was that as I changed directory names using a top-down approach, I was
invalidating paths used later in the loop. For example, /project/store is the first directory to change in the example structure below:

Original Directory Structure

/project/store/ – [first to change]

/project/store/hats

/project/store/jackets

When we try to change /project/store/hats ruby throws an error because the root path /project/store would have changed. The fix for this was simple,reverse the array and go from bottom-up:

Better. Time to add the logic to replace the placeholders I put in the file and directory names. At this point I don’t care about whether the item is a file or directory so I treat them the same:

Much better. Now I need to determine if I am dealing with a file or a directory since I will need to replace the contents of files where I find the placeholder text as well. Turns out that was pretty darn simple.

Final Script

I can think of a lot of ways to improve this for more complicated scenarios, but this worked just fine for my needs. Hopefully this helps someone else too. And as I mentioned previously, I’m relatively new to Ruby and welcome and constructive feedback or suggestions for improvement that any Ruby experts have for me.

]]>2010-11-06T13:50:09-04:00http://DannyDouglass.com/blog/2010/11/06/simplify-using-xml-data-with-automapper-and-linqtoxmlI recently ran into a scenario at work that required manually consuming several SOAP web services, which I’m sure you can imagine was rather monotonous. A co-worker (Seth Carney) and I tried a few different approaches, but we finally settled on a solution that simplified consumption of the xml and ultimately made the code more testable. That solution centered around leveraging AutoMapper, an open source object-object mapping tool, to create a link between the XElements returned in the SOAP messages and custom contracts we created – in a reusable manner.

I put together a quick demo that shows how you could use the same approach to consume and display the Twitter Public Timeline (using the API’s Xml response type).

1. Getting the Project Setup

After creating a basic MVC3 (download beta) project and the associated test project, the first step was to get the AutoMapper package installed. I have been using NuGet, Microsoft’s recently announced package management system, to install any open source dependencies. The following command was all that was needed to setup AutoMapper in my MVC3 project (read more about NuGet here and here):

PM> add-package AutoMapper

2. Creating the Mapping

With AutoMapper installed I’m ready to get started creating the components necessary for the xml-to-object mapping. The first step is creating a quick contract used in my application to represent the Tweet object:

Nothing crazy here – just a simple entity. These are all fields that are provided in the response from the Twitter API using a different name for some fields. In simple cases where the source and destination objects have the same name you can setup a map very quickly using this syntax:

Mapper.CreateMap<SourceObj, DestinationObj>();

However, AutoMapper does not support Xml by default I have to specify the fields that I will be mapping. Using the Fluent API in AutoMapper I’m able to chain my field mappings. Take a look at one example field mapped in my example – the tweet’s Body:

It may look complicated at first, but all that is really happening here is that we are providing details to AutoMapper on what value to use in my source object and how to map it to the destination object’s property. There is one particular line I would like to focus on in the above Body field mapping:

The XElementResolver is a custom value resolver that Seth came up with to handle parsing the XmlElement source object to retrieve a strongly-typed value for use in the mapping. I’ll detail that more in a moment, but before we move on take a look at my full mapping:

3. The Generic XElementResolver

This custom value resolver is the real key that allowed these XElement-to-Contract maps to work in the original solution. I’ve reused this resolver in this example as we saw above. This was all that was necessary to create the custom resolver class:

This generic XElementResolver allows use to easily pass the type of the value retrieved in our mapping above. For example, the following syntax is used to strongly type the value retrieved from the XmlElement in the Id field’s .ForMember() declaration above:

ResolveUsing<XElementResolver<ulong>>()

With my mapping completely configured and instantiated, I’m ready to invoke the Twitter API and leverage AutoMapper to display that latest Public Timeline.

4. Putting the Pieces Together

I created a simple class responsible for retrieving the Twitter API response:

And finally, after a little formatting in my View using the new Razor view engine from Microsoft, I have my public timeline displaying!

I know we had a hard time finding a lot of content on how to leverage AutoMapper to map Xml sources, so hopefully this is as helpful to you as it was for us. As I mentioned before, the source is available on my GitHub page (that I am just starting to utilize):

I’d love to hear any other ways people are using AutoMapper with an Xml source. Shoot me an email at me@DannyDouglass.com if you have an alternative solution.

Cheers!

]]>2010-10-27T07:52:08-04:00http://DannyDouglass.com/blog/2010/10/27/console2powershellposhhgMoving from Subversion to Mercurial was more than a change from a centralized version control system to a distributed solution. I also found myself moving away from the explorer integration provided by the Tortoise products to the command line for the execution of my hg commands. This approach is dramatically faster once you get used to the syntax, which really only takes a hot minute.

Certain features in TortoiseHg still provide visual advantages that I find useful, such as the visual repository log. During an Agile.Net Bootcamp with Jimmy Bogard he showed us an collection of tools he utilizes to provide an improved Mercurial command line experience. The final product is a more descriptive prompt that displays branch information, as well as the number and types of changes made since your last commit:

1. Console 2

You may already being using this very useful command prompt shell, but in case you are not go download it now! This tool provides certain advantages over a standard command prompt, such as tabbed prompts, ability to integrate other shells (e.g. PowerShell), modified styles, control over keyboard commands, and much more. Once you have downloaded console 2, just extract it somewhere (I used my c:\Program Files directory).

2. Windows PowerShell

You can read more on Windows PowerShell here and here. It comes with Windows 7, but you also download it for other versions of Windows. If you do not have it, download and install Windows PowerShell.

After installing PowerShell, we want to create a physical PowerShell profile file. The default location on Windows 7 is c:/users/username/document/WindowsPowerShell/ and you can force the creation of your PowerShell profile file by executing the following command in a PowerShell prompt:

New-Item -path $profile -type file -force

After executing that command you can verify the file was created:

3. Posh-Hg Download and PowerShell Integration

Posh-Hg was inspired by the Posh-Git, which provides a “custom prompt and tab expansion when using Mercurial from within a Windows PowerShell command line”. There’s more information on this blog post about Posh-Hg. You will want to download this file and place it somewhere on disk. Important: Before unzipping, make sure to right-click on the posh-hg-1.0 zip file and Unblock it, otherwise you will run into security issues executing the scripts each and every time (very, very annoying):

Once you’ve unblocked the file you need to unzip it. I recommend saving it the same location as your Windows PowerShell profile. In my case that was C:\Users\Danny\Documents\WindowsPowerShell. Now you are set to add posh-hg to you PowerShell profile. Simply open your PowerShell profile file and add the following text to it, making sure to modify the path to the posh-hg profile.example.ps1 file appropriately:

. path\to\posh-hg\profile.example.ps1

You can now open a new PowerShell prompt and verify the installation by navigating to a Hg repository on disk. You may receive an error explaining that your profile “cannot be loaded because the execution of scripts is disabled on this system”. If that happens you will need to adjust the PowerShell script policy by executing the following command (type [Y] when prompted):

Set-ExecutionPolicy Unrestricted

If you needed to set your execution policy, you will need to close your current PowerShell prompt and open a new one for the changes to take affect and allow for your profile to be loaded. Try navigation to a Hg repository on disk and you’ll see a much more friendly prompt:

4. PowerShell Integration with Console2 and Posh-Hg

With everything installed and configured outside of Console2 we are set to add PowerShell to our Console2 installation. You can download the files to update your Console2 installation (the console.xml file). If you have not customized anything previously with Console2, you can just override the existing file without causing any harm. Drop the powershell.ico icon file in the Console2 directory and you are ready to test out the changes in Console2. Open your console, navigate to a Mercurial repository on disk and you should see the changes:

Here is a breakdown of the meaning the symbol/number combinations:

“+” – # of files that have been added (not committed)

“~” – # of files that have been modified (not committed)

“-” – # of files that have been removed (not committed)

“?” – # of untracked files in your repo

“!” – # of missing files

You can edit how the Hg details are display as well. Take a look at the posh-hg codeplex page for those details.

This blog post helped me accomplish the 4th step of my install and provided the zip file (which I slightly modified) used to update the console2 configuration.

Done!

You are now setup with a significantly more useful PowerShell command prompt that can help you visually get a feel for the state of your repository immediately. Of course, I’m positive you check in early and often, which results in this information being substantially more helpful. If I’m always showing changes to 50 – 100+ files, how much does that really help me???

Added Bonus: Take a look at ScottHa’s blog post on adding a “PowerShell Prompt Here” context menu item for Windows Explorer. Very Handy!

Hope you enjoy this as much as I did – thanks to Jimmy Bogard for the exposure to Posh-Hg!

]]>2010-10-06T09:32:25-04:00http://DannyDouglass.com/blog/2010/10/06/nupack-vs-nuproj-vs-openwrapThe ASP.NET MVC 3 Beta was unofficially released yesterday (as far as I can tell) and the release notes include a quote about a package manager named NuPack:

ASP.NET MVC 3 includes NuPack Package Manager, which is an integrated package management tool for adding libraries and tools to Visual Studio projects. For the most part, it automates the steps that developers take todayto get a library into their source tree.

You can work with NuPack as a command line tool, as an integrated console
window inside Visual Studio 2010, from the Visual Studio context menu, and as
set of PowerShell cmdlets.

Unfortunately, the CodePlex site is currently not published and many are waiting to take a look at what’s being released here by Microsoft. There seems to be a good deal of confusion on Twitter revolving around this project so I thought a quick post outlining each of the 3 main package managers in .NET would be helpful. I’ve listed them in alphabetical order:

Update: Thanks to a comment below from Keith Dahlby…it appears that NuPack may be a joint effort between the creators of Nu and Microsoft. You can read more about it on the Outercurve Foundation announcement.

Update #2: Microsoft has announced NuPack was named with the blessing of the Nu (Nubular) team and the projects appears to merging to some extent. Nu will continue moving forward, but may ultimately die-off in favor of NuPack’s native .NET solution.

Nu (Nubular): Nu is a project started by Dru Sellers and Rob Reynolds that I have blogged about here and here. It leverages the Ruby gems platform to bring gems to the .NET world.

Twitter hashtag #NuProj

NuPack: Microsoft’s package management tool included in the ASP.NET MVC 3 Beta. The source is located on CodePlex, but is not published at this time (9:15 EST). Update – source is available! See update #2 above.

Twitter hashtag #NuPack

OpenWrap: A native .NET package management solution by Sebastien Lambla that aims at managing dependencies in a manner that will allow runtime resolution to dependency issues as well.

Twitter hashtag #OpenWrap

Without hearing more on NuPack’s approach to managing .NET packages it is hard to compare the 3. I am sure that an announcement will come in the near future from someone on the ASP.NET team at Microsoft regarding the ASP.NET MVC3 beat and NuPack. In the meantime, I hope this helps clarify the difference between the different .NET Package Managers.

Stay tuned for more…

]]>2010-10-01T09:17:28-04:00http://DannyDouglass.com/blog/2010/10/01/dcaltnet-package-management-presentationBelow I have provided a variety of material/links from my presentation last night – “The Current State of Package Management in .NET”. Please feel free to contact me using any of the communication channels available in the right column of my blog. I would happy to provide any further information regarding Nu or OpenWrap, or just to discuss your thoughts on the topic of package management.

I want to thank everyone that battled the weather last night to make it to the presentation. I really enjoyed my first opportunity to present at an ALT.NET event. Also, thanks to Matthew Podwysocki for allowing me to speak last night. I believe I’ve included everything below that I discussed last night. If you take time to review the slideshow, I’d recommended using one of the two links below the embedded slideshow as they contain the speaker notes with a lot of information.

Gem Creation Example

I showed how to use Nu to download and install packages, as well as how to actually create a Ruby gem that can be utilized with Nu. Below are the links to the PagedList project before refactoring for gem creation and after I modified the project.

]]>2010-08-11T20:39:37-04:00http://DannyDouglass.com/blog/2010/08/11/whats-nu-in-the-dotnet-world-part2-creating-a-gemIf you are not familiar with the Nu project start by reading my first post in this series that gives an introduction to getting started with this promising tool. As a quick reference I’ll post a paragraph from the last post introducing Nu:

“Nu is an open source tool built by Dru Sellers (and several others) that aims at bringing Gems, one of Ruby’s most revered features, to the .NET world. If you are not familiar with Gems I suggest you take a minute to read up on them. I would venture a bet that you are already familiar with at least one gem – Ruby on Rails. The following line of code is all that is required to install the Ruby on Rails gem (after installing the Ruby library of course)…”

This post is focused on sharing my experience in creating my first gem for the Spark View Engine. I would be doing you a great disservice if I did not start by saying how easy it is to create a gem. Actually easy is the wrong phrase – crazy easy is more telling. Since I would never ask you to take my word for it, it must mean it is time to show some code.

Getting Started

After downloading the latest release for the Spark View Engine here, the first step was to create a directory to hold my gem specification anywhere on my machine, c:/Gems/SparkViewEngine was my choice:

Creating the Gem Specification(GemSpec)

Rob Reynolds described this file as “the most important file for this entire process”. The gemspec file will be named after the project – in this case it is named sparkviewengine.gemspec and will be placed inside my sparkviewengine directory. While we are creating files we can also create a VERSION file that, you may have guessed, stores the version of the gem. We now have a directory with 2 files:

Below is the code from my sparkviewengine.gemspec file, you would simply replace the values with those applicable to the gem you are creating:

The VERSION file is much simpler. I found the version below by opening Reflector and locating the version of the Spark.dll. Be sure to check out Rob Reynold’s blog post for the explanation on why you need to use Reflector and cannot trust the windows explorer properties panel.

1.1.0.0

Adding the Library Files

The hard part is finished in creating our basic gem, now we need to include the spark compiled dlls. For this example we are creating a basic gem and do not have to deal with dependencies. My next gem will be more detailed and I will discuss what steps are necessary to handle dependencies.

For the SparkViewEngine gem we can simply copy the libraries from the bin directory of the Spark 1.1.0.0 release:

and place them into a new lib directory in my SparkViewEngine gem directory:

Documentation

Any documentation can be stored in a docs directory. I added the
index.html and zipped up the samples that came with the Spark 1.1.0.0 release:

That’s it for the setup! My gem specification is ready to be built and uploaded to RubyGems.org.

Building the Gem Specification

gem build sparkviewengine.gemspec

That creates my versioned gem:

Pushing the Gem to RubyGems.org

You need to have an account created on RubyGems.org before you can push. Once that is created simply execute the following code, enter your credentials, and watch as your gem is pushed out to the world!

If you have been looking for that opportunity to contribute to the Open Source community, but keep finding yourself saying, ‘I just do not have the time’, then I am happy to say your chance is here. Go out and create your first gem. You will be surprised out just how easy, errrr crazy easy it is to complete.

Here are some of the resources that aided me in creating my first gem:

]]>2010-08-09T09:20:36-04:00http://DannyDouglass.com/blog/2010/08/09/whats-new-in-the-dotnet-worldIf you thought the word “Nu” in the title of this blog post was misspelled, you are in for a treat. And trust me, you are not the only one who made that grammatical assumption.

Nu is an open source tool built by Dru Sellers that aims at bringing Gems, one of Ruby’s most revered features, to the .NET world. If you are not familiar with Gems I suggest you take a minute to read up on them. I would venture a bet that you are already familiar with at least one gem – Ruby on Rails. The following line of code is all that is required to install the Ruby on Rails gem (after installing the Ruby library of course):

gem install rails

Compare that to what is required to install ASP.NET MVC. Despite having ASP.NET installed, I may need to download a separate installer for a specific version of Visual Studio. Semantics aside, it is just plain more difficult. That is not to say installing ASP.NET MVC is overly complex, nor am I attempting to disrespect the ASP.NET MVC framework. I’m actually a big fan of ASP.NET MVC. My intention is to show that the gem installation is just crazy simpleand a piece of the programmatic puzzle missing in .NET development. Many, including myself, are hoping that Nu proves to be the solution to this shortcoming.

This is not the first time someone has attempted to solve the “Missing Gem” (as I have decided to label it) mystery in the .NET world. I recall my friend Troy Goode discussing this exact problem with Stephen Walther just prior to him joining Microsoft way back in early 2008. Many others have tried to replace Ruby Gems in the .NET world, but none have been successful. Dru Sellers decided to take a different approach and leverage the existing Ruby Gems tools and expand upon it to bring gems to .NET. Brilliant.

Enough discussion about Nu – I will let the code speak for itself. In the following example I will create a basic ASP.NET MVC 2 web application (.NET 4.0) and use the Nu package installer to hook in AutoFac, a .NET IoC container. Do not be alarmed if you do not know what AutoFac or IoC are at this point. Just understand that I have an external component that I would like to utilize in my project. For a list of all the currently available Nu packages you can take a look at the Current .NET Nu Packages page on the Nu wiki. Now let’s see the code!

Note: You must install Ruby as a prerequisite to using Nu since it leverages Ruby’s Gem tool.

The first step is to create a new ASP.NET MVC 2 application:

)

Once the application is created I can quickly run it and see the screen that anyone who has read about or used ASP.NET MVC before is very familiar with:

Now that I have my project created I want to download the AutoFac library and bring it into my project. Prior to Nu, I would do this by opening a browser, finding the AutoFac download page, download, possibly unzip the files, etc. Nu makes this much simpler. First thing I want to do install Nu using the Ruby gem tool by simply executing the command gem install nu.

Once I have installed Nu, I can easily download the AutoFac gem by executing the command nu install autofac.

Note that by default Nu will install your gems local (in a new lib directory) to the folder where you execute the nu install #somegem# command. Taking a look at my c:\projects\Nu_AspNetMvc2_Demo directory I can see that two folders were created: .nu and lib. The lib folder will hold all of the gems I install in this directory from here on out, unless I override the default at a later time. I will not go into further details, but my AutoFac dlls are now stored inside of my /lib/autofac package directory.You can take a look at the example’s source to see more about the package directory structure.

Currently Nu is in its early stages and I am still required to add the DLLs to my project manually, but very soon this will not be the case. A Nu for Visual Studio plugin is in the works that will simplify this step – take a look at this video preview to see it in action. I imagine there will be other ways to accomplish this as well. For now, I will simply add the required references as I would with any other library I downloaded to my Visual Studio Project:

Now all I need to do is follow these instructions to hookup AutoFac 2 in my global.asax.cs (code shown below) and I am good to go! Running my project shows me that same familiar MVC homepage, however this time my controller factory is running using the AutoFac controller factory (see line 33 below):

As you can see there is a lot of power in using Nu. There are many other areas I could have gone into further detail, but I wanted this to be a simple introduction to the power of Nu packages. I plan on following up with additional posts to show other features of Nu and maybe even a post on creating your very own Gem. In the meantime I want to include a few resources to help you learn more about Nu:

]]>2010-08-02T19:12:14-04:00http://DannyDouglass.com/blog/2010/08/02/mercurial-create-remote-repositories-using-powershellIn case you are not familiar with Mercurial (Hg), it is a Distributed Version Control System (DVCS) aimed at simplifying revision control. The purpose of this post is not to introduce Mercurial, but you can read more about Mercurial at the following links to get better acquainted with the software:

Now that we are all comfortable with Hg (the chemical symbol for Mercury and nickname for Mercurial), I would like to take present an issue I spent some time working on today. There are several different publishing mechanisms in Mercurial, but for the setup at my company we decided to simply use http being that our provider is only internally available and protected by several firewalls and other security measures. We chose this approach over the more popular SSH publishing mechanism being that we are hosting our Mercurial installation on a Windows 2008 R2 server. Had we installed it on a box running something other than Windows, SSH would have solved the problem I’m about to describe below. Here’s a quick view of our basic setup:

One limitation when using http as a publishing mechanism is that you cannot create a repository remotely; no if, ands, or buts about it. Another twist was that our CM (Configuration Management) procedures dictate that our developers are not permitted to RDP into the Mercurial server. This left us trying to decide how to allow developers to create repositories remotely since our http communication channel does not allow remote repository creation.

Troy Goode, a good friend and colleague, suggested that we leverage PowerShell to solve the problem. After spending a little bit of time with the Configuration Manager a PowerShell solution was in place. Using the following basic PowerShell command, a remote session was initiated without making the developer an administrator on the machine with RDP access:

ENTER-PSSession your-server-name-or-ipaddress

I originally encountered an Access Denied error, which we discovered was caused by my account not have the appropriate permissions on the Mercurial server to create a remote PowerShell session. This is pretty easy to remedy and you can read more about it on the TechNet library page. Simply execute the following cmdlet on the server to manage the PowerShell remote session security configuration:

The last change is to set the appropriate directory permissions on the remote server to the location where developers are permitted to create repositories. That should be straightforward enough without going into any examples since repository architectures can differ drastically.

It can’t be that easy, can it? Well, good news – it is that simple! With those changes you should be able to remote manage (create and delete) your Mercurial (Hg) repositories via PowerShell. Hopefully this helps you save some time in your setup of Mercurial – a very awesome, and my current favorite, Version Control System.