I just started using Nu to manage the dependencies in my Fluency project. I had used package managers for ruby and php in the past, and loved them, so I couldn’t pass up the opportunity to try out Nu. I am glad I did.
What the heck is Nu?
Nu is a package manager for .NET (finally!). It allows you to install code libraries and all their dependencies automatically, and keep them updated. If you have ever heard of RubyGems, this is the same thing. In fact, its built on top of ruby gems!
Installing Nu
To use Nu, you first need to install ruby. You can get it here. Then upgrade RubyGems by dropping to a command prompt and typing
gem update --system
Then to install Nu you type
gem install nu
And that’s it…Nu is installed and ready to go.
Using Nu
Using Nu is dirt simple. Wanna install NUnit?
C:\project>nu install nunit
Gem nunit is not installed locally - I am now going to try and install it
Successfully installed nunit-2.5.7.10213.20100801
Installed package nunit.
Done! How about NHibernate?
C:\project>nu install nhibernate
Gem nhibernate is not installed locally - I am now going to try and install it
Successfully installed nhibernate-2.1.2.4000
Loading dependency: castle.core = 1.1.0.0
Loading dependency: castle.dynamicproxy2 = 2.1.0.0
Loading dependency: castle.core = 1.1.0.0
Loading dependency: log4net = 1.2.10.0
Installed package nhibernate.
Sure beats the pants off navigating out to the website to download the latest version, doesn’t it? And it even installed all the dependencies.
Creating a gem to be installed using Nu
So what does it take to make a project installable using Nu? Not much. I was surprised at how easy it was.
When I converted Fluency to install all its dependencies using Nu, I found one library that that wasn’t yet packaged as a gem and installable through Nu. This was the SharpTestsEx library. So I did what any self-respecting supporter of open-source software would do, I decided to add it myself so anyone else could use it.
1. Prepare the gemspec
To do this, I created a gems folder and in it, a gemspec file “sharptestsex.gemspec†with the following contents
version = File.read(File.expand_path("../VERSION",__FILE__)).strip Gem::Specification.new do |spec| spec.platform = Gem::Platform::RUBY spec.name = 'sharptestsex' spec.version = version spec.files = Dir['lib/**/*'] spec.summary = 'Sharp Tests Ex - .NET Fluent Assertions for Your Tests' spec.description = '#TestsEx (Sharp Tests Extensions) is a set of extensible extensions. The main target is write short assertions where the Visual Studio IDE intellisense is your guide. #TestsEx can be used with NUnit, MsTests, xUnit, MbUnit.' spec.authors = ['Fabio Maulo','JDiamond'] spec.email = '' spec.homepage = 'http://sharptestex.codeplex.com/' spec.rubyforge_project = 'sharptestsex' end
and a “VERSION†file that simply contained the version number
1.0.0.0
I then created a gems/lib/ folder and placed the binaries in it. The spec.files attribute indicates that I want to include all the files in this folder in the gem.
2. Build and test the gem locally.
Then I built the gem at the command prompt with
gem build sharptestsex.gemspec
This will product the gem file names sharptestsex-1.0.0.0.gem. I tested it with nu locally by installing the gem directly (without going to the rubygems server) by specifying the filename of the gem.
gem install sharptestsex-1.0.0.0.gem
This installed it from my local drive into the gem cache. The gem cache is where nu gets the libraries. If nu does not find the requested library in the gemcache, it asks rubygems to get it from the server. Installing it like I did above ensured its in the cache so nu could find it. Then I tested that nu could install it.
nu install sharptestsex
Once I saw it all worked, I uninstalled the gem so that I would be able to test that it pulls from the server. Uninstalling the gem deletes it from the local gemcache. I had to do this because if it existed in the cache, it wouldn’t even try to download if from the server the next time I test it. I uninstalled it with…
gem uninstall sharptestsex
3. Push the gem to RubyGems.org
Now I was ready to push the gem out to RubyGems.org. To do that, I needed to sign up for a free account, which was quick and painless. The I pushed the gem up.
gem push sharptestsex-1.0.0.0.gem
I was prompted for my RubyGems.org login and password, and then the gem was uploaded.
4. The final test
Then my new gem is ready to install with nu! I tested it with…
nu install sharptestsex
I checked, and sure enough, I saw a lib folder that contains a sharptestsex folder with all the binaries. It was way easier than I thought.
Resources
I drew heavily from the following resources while getting this to work.
- How To: Gems and .NET – Rob Reynolds, “The Fervent Coderâ€
- Creating a "New" Gem for "Nu" – From 0 to 100 in 24 Hours – Bill Simser