Monday, 15 April 2013

How to: Use NuGet in Source Control with Nuget Package Manager Restore

When enjoying the convenience NuGet brings us, it is easy to forget the fact that the references will come back to bite you the hardest, specially when working with version control. 

This time, I hit a jackpot checkout my project from TFS in a new device, and realise all my dependencies all gong like they never existed, only leaving the NuGet config file proving I wasn't day dreaming before.

Therefore, I would highly recommend the following practices for the people who'd like to keep their project clean and tidy: no unnecessary package or exes checked in, but still be able to use the dependencies when the project is checked out.

1. Enabling package restore during build in Visual Studio options
Enable Package Restore in Options

2. Enabling package restore in solution
Enable Package Restore in Solution

And this is what you get
Files Generated by Enabling Package Restore

But it is not the end of the story, simply because you don't want to break the rules to check in any .dll or .exe. And this will end up with a loose end as next time you check out, you still find something missing, which is the NuGet.exe file. Therefore, to complete the operation, follow the next step.

3. Force NuGet.exe to download

Open up 'NuGet.targets', find 'DownloadNuGetExe', and set the value to true.

<!-- Download NuGet.exe if it does not already exist -->
<DownloadNuGetExe Condition=" '$(DownloadNuGetExe)' == '' ">true</DownloadNuGetExe>

4. Configure proxy to allow download

If you are working within an enterprise network, the auto-download of 'nuget.exe' might not work. In which case, you will have to configure a proxy for it.

Open up 'NuGet.targets', find task 'DownloadNuGet', and configure the snippet with your proxy and credentials.

<Code Type="Fragment" Language="cs">
    <![CDATA[
    try {
        WebProxy proxy = new WebProxy(<proxy address>, <port>);
        proxy.Credentials = new NetworkCredential(<user name>, <password>, <domain>);
        OutputFilename = Path.GetFullPath(OutputFilename);

        Log.LogMessage("Downloading latest version of NuGet.exe...");
        WebClient webClient = new WebClient();
        webClient.Proxy = proxy;
        webClient.DownloadFile("https://nuget.org/nuget.exe", OutputFilename);

        return true;
    }
    catch (Exception ex) {
        Log.LogErrorFromException(ex);
        return false;
    }
]]>
</Code>

* Some useful tips

To keep things neat and tidy, you can now delete the 'packages' folder, because that is the whole point of doing this, right? To remove the redundant dependency files, which saves not just space but also time checking out or cloning depends on what version control mechanism you use, instead allowing build event to retrieve them back automatically.

You can now check in the changes to TFS, and you will find your TFS build server and your check out are working again!

References:
http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages
http://stackoverflow.com/questions/12022154/prevent-needing-to-add-nuget-exe-to-source-control

No comments:

Post a Comment