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 ) 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.
- publicstaticstring RemoveHtmlTags(thisstring text)
- //add some layout
- text = Regex.Replace(text, "<br />", "\n");
- text = Regex.Replace(text, "</p>", "\n");
- //replace weird characters
- text = Regex.Replace(text, "&", "&");
- text = Regex.Replace(text, " ", " ");
- //remove remaining HTML tags
- returnRegex.Replace(text, @"<[^>]*>", string.Empty);
- publicclassRestClient : IRestClient
- privateHttpClient _client;
- publicasyncTask<T> Get<T>(CancellationToken cancellationToken, string url, bool includeResultProps = true)
- where T : new()
- _client = newHttpClient();
- var result = await _client.GetAsync(url);
- string json = await result.Content.ReadAsStringAsync();
- if (!includeResultProps)
- JObject obj = JObject.Parse(json);
- json = obj["results"].ToString();
- catch (JsonSerializationException e)
- returnnew T();
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
- @echo Off
- set config=%1
- if "%config%" == "" (
- set config=Release
- set version=
- if not "%PackageVersion%" == "" (
- set version=-Version %PackageVersion%
- REM Run package restore
- %nuget% install WindowsPhone\packages.config -OutputDirectory packages -Prerelease -NonInteractive
- REM Build solution
- %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.
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!
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.
A big shout out to the MyGet team for helping me getting started with their awesome service! http://www.myget.org/Home/Team