Monday 30 July 2012

Automate Nuget Package Creation using MSBuild

There are lots of blogs on this topic, but none of them helped me achieve my goal 100%. To state my problem, I wanted a Continuous Integration strategy which meant that every class library I built for the other members of the team would be accessible via a Nuget package. Every checkin will 
  • Trigger a new version of the Class Library to be built
  • A nuget package created with the new version number of the class library included in the name of the package.
  • The package will be moved to a network shared location where the other team members can consume the package
In a previous blog post I describe how you can create an Assembly file and share it across all your projects in the solution.

Now we create a MSBuild file and add and call the following targets:

<Target Name="CreatePackage">
       <Exec WorkingDirectory="$(WorkingFolder)"
        Command="$(NuGetApp) spec -f" />      
       <Exec WorkingDirectory="$(WorkingFolder)"
        Command="$(NuGetApp) pack MyProject.csproj -Verbose -Prop Configuration=Release" />
</Target>

<Target Name="MovePackage">    
       <Exec WorkingDirectory="$(WorkingFolder)"
          Command="move /y *.nupkg $(PackageDir)" />
</Target>

The NugetApp variable represents the path to the Nuget.exe command line utility used to create the Nuget package. In my case, I downloaded the utility into a folder on the same level as the working folder and named it "Tools" with subfolder "Nuget", I set the NugetApp property to like so:

<NugetApp>$(WorkingFolder)/../Tools/Nuget/Nuget.exe</NugetApp>

The PackageDir property is whatever network share location of your choice.

Now run your your MSBuild file in TeamCity.

Happy building!!!.

Thursday 19 July 2012

ASP.NET MVC 3 Share App_GlobalResources files in all Class Libraries

I wanted to add all my global resource/.resx files e.g. Error Messages into the  the same folder in the ASP.NET MVC 3 App_GlobalResources folder and shared the settings in all referenced class libraries. Well, the idea is to have only one file to maintain, as opposed to ErrorMessage resource file per class library. So I went through the following steps:


  • Write-Click the ASP.NET MVC 3 project and Add ASP.NET Folder App_GlobalResources.
  • Change the Access Modifier to "Public" from "Internal". If this is greyed out in the VS 2010 UI, open the accompanying .Designer.cs file and change the access level to "Public".
Inside the ASP.NET MVC 3 project you can access this resource using the class generated in the Designer.cs file. Life is a little different from inside a references class library, but not too complicated. If you open the .Designer.cs file you will notice the Strongly-Typed references utilises the ResourceManager class in the System.Resources Namespace.

I will use the same class from the class libraries. Find the code below;

var temp = new ResourceManager("Resources.ErrorMessages", Assembly.Load("App_GlobalResources"));
var resourceNameString = temp.GetString("My_Resource_Name");

Of course in a real application it will be advisable to create a custom strongly-typed and Interfaced class 
with properties that will return the values of your settings, so you can unit test your main application code by mocking the settings in your resource files.

Happy coding!!!!.