Nico's digital footprint

I grew up in the nineties, that makes me awesome by default

Using MyGet for those common classes

by Nico

Every .net developer, be it a mobile, web or windows developer, uses (or should use) NuGet. A big, and fast growing, gallery of useful (and less useful but fun) libraries. The problem I was having with NuGet is that for every project I needed to go search in that big gallery for the packages I wanted. Luckily someone had the same problem and decided to do something about it. And so MyGet was born.

The MyGet team sells MyGet as Nuget-as-a-service. You can create your own feed, drop in the NuGet packages you tend to use (even mirror them) and add the feed into your package manager. This way you get your own personal gallery, no more searching for the ones you need, those feeds are public by the way, everyone can use them. If you want a private feed you’ll need to look at the MyGet price plans.

Getting started with MyGet is really easy, just go to http://www.myget.org log in with your Microsoft, Google, Yahoo, Facebook, OpenID, GitHub or StackOverflow account. After you login you can add feeds to your account.

Creating a feed is as easy as giving it a name and a description and selecting the feed type (public, community or private). Once your feed is created you can start adding packages to it. To add a package enter the search term just like you would do when you use NuGet in Visual Studio, click the package you want and click Add.

If you want, in the Add package dialog, you can select to mirror the package. This will copy the entire package from the NuGet feed into MyGet, meaning that you’ll still be able to install the package when NuGet goes down. To give you an idea of how a feed could look like, here’s some of the packages in my Windows Phone feed.

To use your freshly created feed you’ll need to add it as a package source in NuGet. Go to the Package Manager settings from the Visual Studio options menu (or open the Manage NuGet packages dialog in a project and click Settings). Here you can add a new package source.

Once added, the new package source will show up in NuGet and you’ll be able to install the packages from you own feed.

Managing my own package feed has really reduced the time I used to spent searching and adding packages. But the MyGet team has been working hard on a really cool feature.

Do the Wonka

The MyGet team (all Belgian developers by the way – hooray for Belgium Smile) developed a feature they codenamed “Wonka”. It’s currently in Beta but it’s working great so far. This is what Wonka is all about

They’ve setup their own Build server, including support for building Windows Phone 8 libraries (thanks to the command line compiler in the WP8 SDK). Hearing about such a cool feature and hearing that it’s in beta made me want to try it. And I started thinking, maybe I could put a bunch of classes that I tend to use in some form in multiple projects in one library and have that library in my feed.

The project itself is still very small but it will grow when I need extra functionality, it currently holds a Regex class to get rid of HTML tags in strings and a RestClient class that uses the portable HttpClient to do a Get operation. For reference, here are the classes.

Code Snippet
  1. publicstaticclassRegexHelper
  2. {
  3.     publicstaticstring RemoveHtmlTags(thisstring text)
  4.     {
  5.         //add some layout
  6.         text = Regex.Replace(text, "<br />", "\n");
  7.         text = Regex.Replace(text, "</p>", "\n");
  8.  
  9.         //replace weird characters
  10.         text = Regex.Replace(text, "&amp;", "&");
  11.         text = Regex.Replace(text, "&nbsp;", " ");
  12.  
  13.         //remove remaining HTML tags
  14.         returnRegex.Replace(text, @"<[^>]*>", string.Empty);
  15.     }
  16. }
Code Snippet
  1. publicclassRestClient : IRestClient
  2. {
  3.     privateHttpClient _client;
  4.  
  5.     publicasyncTask<T> Get<T>(CancellationToken cancellationToken, string url, bool includeResultProps = true)
  6.         where T : new()
  7.     {
  8.         try
  9.         {
  10.             _client = newHttpClient();
  11.  
  12.             var result = await _client.GetAsync(url);
  13.             string json = await result.Content.ReadAsStringAsync();
  14.  
  15.             if (!includeResultProps)
  16.             {
  17.                 JObject obj = JObject.Parse(json);
  18.                 json = obj["results"].ToString();
  19.             }
  20.  
  21.             returnJsonConvert.DeserializeObject<T>(json);
  22.         }
  23.         catch (JsonSerializationException e)
  24.         {
  25.             returnnew T();
  26.         }
  27.     }
  28. }

The project contains Json.net and the portable HttpClient, both referenced through NuGet. Now we need a way for the build server to get a hold of these packages, we can accomplish this by enabling the NuGet package restore. Right-click the solution and you’ll see the option right there.

This will add a .nuget folder to your solution that you’ll need to check-in into your source control.

So we have a working project with some classes that we would like to get into a NuGet package by using the MyGet build server. What we need to do is get this package into a repository. I used BitBucket for this because it has free private repositories. You can also add a GitHub or Codeplex repository.

If you use the from BitBucket button it will ask you for permission to access your account on BitBucket and will show you a list of all your repositories hosted there. Select the one you need and click Add. Do take notice of the warning.

Add the repository and click the edit button next to it. Enter your BitBucket credentials here so MyGet can access your private repository to fetch the sources. And that’s it, you can now hit the Build button to fetch the sources and try to build them. In my case this immediately resulted in a big fat red Failed message. The problem here is something of a chicken-egg problem (a big thanks here to the MyGet team for helping me figuring this one out!). So here’s the problem:

  • The project file needs the package to run
  • The project file is needed for package restore
  • package needs package restore

The solution here is to setup a build.bat in the solution folder. The MyGet build server will search for a build.bat to execute, if it doesn’t find one it will just use the sln available.

With the build.bat file we first fetch the necessary packages and then build the solution

Code Snippet
  1. @echo Off
  2. set config=%1
  3. if "%config%" == "" (
  4.    set config=Release
  5. )
  6.  
  7. set version=
  8. if not "%PackageVersion%" == "" (
  9.    set version=-Version %PackageVersion%
  10. )
  11.  
  12. REM Run package restore
  13. %nuget% install WindowsPhone\packages.config -OutputDirectory packages -Prerelease -NonInteractive
  14.  
  15. REM Build solution
  16. %WINDIR%\Microsoft.NET\Framework\v4.0.30319\msbuild WindowsPhone.sln /p:Configuration="%config%" /m /v:M /fl /flp:LogFile=msbuild.log;Verbosity=Normal /nr:false

Add this to your solution in Visual Studio, make sure it’s called build.bat and check it into your source control. Now it should build.

Automating builds

Next step to make everything even more awesome is to add a hook to BitBucket so that MyGet will fetch and build the sources everytime a commit is pushed into the repository. First you’ll need the HTTP POST link from MyGet, you’ll find it underneath your build source.

Once you’ve got that link, go to the BitBucket repository and click the Settings icon, in the Settings go to Services and add a POST service.

Past the URL and save the service. Now everytime you push a commit to BitBucket a new build should get queued in MyGet (the page refreshes automatically to show the build process).

And just like that we’ve created a NuGet package in a MyGet feed with continuous delivery, how cool is this right? If I check my feed from Visual Studio my package shows up!

Conclusion

In this post I’ve explained how I use MyGet, a NuGet-as-a-service, to create a package feed with only the packages that are relevant for me. I’ve also explained how I’ve added my own library of classes that I use in multiple projects in that feed and setup continuous delivery so that the package gets updated with every commit that gets pushed to the repository.

Resources:

A big shout out to the MyGet team for helping me getting started with their awesome service! http://www.myget.org/Home/Team


Tags:

.Net | continuous delivery | myget | NuGet | PCL

blog comments powered by Disqus
  Log in

About the author

Hi,

My name is Nico, I’m an MVP Windows Platform Development living in Belgium.
I’m currently employed as a .NET consultant at RealDolmen, one of Belgium’s leading IT single source providers.

I'm also founding member and board member of the Belgian Metro App Developer Network, a user group focussed on Windows 8 and Windows Phone development. If you're in Belgium feel free to drop by if we're doing an event. http://www.madn.be

Since June 2012 I'm a proud member of Microsoft's Extended Experts Team Belgium. And in February 2013 I became a member of DZone's Most Valuable Bloggers family.

In 2013 I became a book author and wrote "Windows 8 app projects, XAML & C# edition".

In 2014 I received the MVP award for the very first time.

I hope to get feedback from my readers either through comments, mail (nico_vermeir@hotmail.com), twitter, facebook, …

 

mvp

 

 

MeetLogo

 

MVBLogo

mybook

 

Month List