Real World Nuget

Posted by JoshReuben on Geeks with Blogs See other posts from Geeks with Blogs or by JoshReuben
Published on Sun, 23 Sep 2012 09:39:38 GMT Indexed on 2012/09/23 15:38 UTC
Read the original article Hit count: 327

Filed under:

Why Nuget

A higher level of granularity for managing references

When you have solutions of many projects that depend on solutions of many projects etc à escape from Solution Hell.

Links

· Using A GUI (Package Explorer) to build packages - http://docs.nuget.org/docs/creating-packages/using-a-gui-to-build-packages

· Creating a Nuspec File - http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvcnuget_topic2.aspx

· consuming a Nuget Package - http://msdn.microsoft.com/en-us/vs2010trainingcourse_aspnetmvcnuget_topic3

· Nuspec reference - http://docs.nuget.org/docs/reference/nuspec-reference

· updating packages - http://nuget.codeplex.com/wikipage?title=Updating%20All%20Packages

· versioning - http://docs.nuget.org/docs/reference/versioning

POC Folder Structure

clip_image002

POC Setup Steps

· Install package explorer

· Source

o Create a source solution – configure output directory for projects (Project > Properties > Build > Output Path)

· Package

o Add assemblies to package from output directory (D&D)- add net folder

clip_image004

o File > Export – save .nuspec files and lib contents

<?xml version="1.0" encoding="utf-16"?>
<package >
<metadata>
<id>MyPackage</id>
<version>1.0.0.3</version>
<title />
<authors>josh-r</authors>
<owners />
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>My package description.</description>
<summary />
</metadata>
</package>

o File > Save – saves .nupkg file

· Create Target Solution

o In Tools > Options: Configure package source & Add package

clip_image006

clip_image008

Select projects:

clip_image010

Output from package manager (powershell console)

------- Installing...MyPackage 1.0.0 -------

Added file 'NugetSource.AssemblyA.dll' to folder 'MyPackage.1.0.0\lib'.

Added file 'NugetSource.AssemblyA.pdb' to folder 'MyPackage.1.0.0\lib'.

Added file 'NugetSource.AssemblyB.dll' to folder 'MyPackage.1.0.0\lib'.

Added file 'NugetSource.AssemblyB.pdb' to folder 'MyPackage.1.0.0\lib'.

Added file 'MyPackage.1.0.0.nupkg' to folder 'MyPackage.1.0.0'.

Successfully installed 'MyPackage 1.0.0'.

Added reference 'NugetSource.AssemblyA' to project 'AssemblyX'

Added reference 'NugetSource.AssemblyB' to project 'AssemblyX'

Added file 'packages.config'.

Added file 'packages.config' to project 'AssemblyX'

Added file 'repositories.config'.

Successfully added 'MyPackage 1.0.0' to AssemblyX.

==============================

o Packages folder created at solution level

o Packages.config file generated in each project:

<?xml version="1.0" encoding="utf-8"?>

<packages>

  <package id="MyPackage" version="1.0.0" targetFramework="net40" />

</packages>

A local Packages folder is created for package versions installed:

clip_image012

Each folder contains the downloaded .nupkg file and its unpacked contents – eg of dlls that the project references

Note: this folder is not checked in

UpdatePackages

o Configure Package Manager to automatically check for updates

clip_image014

o Browse packages - It automatically picked up the updates

clip_image016

Update Procedure

· Modify source

· Change source version in assembly info

· Build source

· Open last package in package explorer

· Increment package version number and re-add assemblies

· Save package with new version number and export its definition

· In target solution – Tools > Manage Nuget Packages – click on All to trigger refresh , then click on recent packages to see updates

· If problematic, delete packages folder

Versioning

uninstall-package mypackage

install-package mypackage –version 1.0.0.3

uninstall-package mypackage

install-package mypackage –version 1.0.0.4

Dependencies

· clip_image018

<?xml version="1.0" encoding="utf-16"?>
<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">
<metadata>
<id>MyDependentPackage</id>
<version>1.0.0</version>
<title />
<authors>josh-r</authors>
<owners />
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>My package description.</description>
<dependencies>
<group targetFramework=".NETFramework4.0">
<dependency id="MyPackage" version="1.0.0.4" />
</group>
</dependencies>
</metadata>
</package>

Using NuGet without committing packages to source control

http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages

Right click on the Solution node in Solution Explorer and select Enable NuGet Package Restore.

clip_image020

— Recall that packages folder is not part of solution

clip_image022

If you get downloading package ‘Nuget.build’ failed, config proxy to support certificate for https://nuget.org/api/v2/ & allow unrestricted access to packages.nuget.org

To test connectivity: get-package –listavailable

To test Nuget Package Restore – delete packages folder and open vs as admin.

In nugget msbuild:

<Import Project="$(SolutionDir)\.nuget\nuget.targets" />

TFSBuild Integration

Modify Nuget.Targets file

<RestorePackages Condition="  '$(RestorePackages)' == '' ">

True

</RestorePackages>

<PackageSource Include="\\IL-CV-004-W7D\Packages" />

Add System Environment variable EnableNuGetPackageRestore=true & restart the “visual studio team foundation build service host” service.

clip_image024

Important: Ensure Network Service has access to Packages folder

Nugetter TFS Build integration

Add Nugetter build process templates to TFS source control

For Build Controller - Specify location of custom assemblies

clip_image026

Generate .nuspec file from Package Explorer: File > Export

clip_image028

Edit the file elements – remove path info from src and target attributes

<?xml version="1.0" encoding="utf-16"?>

<package xmlns="http://schemas.microsoft.com/packaging/2012/06/nuspec.xsd">

    <metadata>

        <id>Common</id>

        <version>1.0.0</version>

        <title />

        <authors>josh-r</authors>

        <owners />

        <requireLicenseAcceptance>false</requireLicenseAcceptance>

        <description>My package description.</description>

        <dependencies>

            <group targetFramework=".NETFramework3.5" />

        </dependencies>

    </metadata>

    <files>

        <file src="CommonTypes.dll" target="CommonTypes.dll" />

        <file src="CommonTypes.pdb" target="CommonTypes.pdb" />

Add .nuspec file to solution so that it is available for build:

Dev\NovaNuget\Common\NuSpec\common.1.0.0.nuspec

clip_image030

Add a Build Process Definition based on the Nugetter build process template:

clip_image032

Configure the build process – specify:

· .sln to build

· Base path (output directory)

· Nuget.exe file path

· .nuspec file path

clip_image034

clip_image036

Copy DLLs to a binary folder

1) Set copy local for an assembly reference to false

2)  MSBuild Copy Task – modify .csproj file: http://msdn.microsoft.com/en-us/library/3e54c37h.aspx

 <ItemGroup>
    <MySourceFiles Include="$(MSBuildProjectDirectory)\..\SourceAssemblies\**\*.*" />
  </ItemGroup>
 
 
  <Target Name="BeforeBuild">
    <Copy SourceFiles="@(MySourceFiles)" DestinationFolder="bin\debug\SourceAssemblies" />
  </Target>

3) Set Probing assembly search path from app.config - http://msdn.microsoft.com/en-us/library/823z9h8w(v=vs.80).aspx -                

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <probing privatePath="SourceAssemblies"/>
    </assemblyBinding>
  </runtime>
</configuration>

Forcing 'copy local = false'

The following generic powershell script was added to the packages install.ps1:

param($installPath, $toolsPath, $package, $project)
if( $project.Object.Project.Name -ne "CopyPackages")
{
$asms = $package.AssemblyReferences | %{$_.Name}
foreach ($reference in $project.Object.References)
{
if ($asms -contains $reference.Name + ".dll")
{
$reference.CopyLocal = $false;
}
}
}

An empty project named "CopyPackages" was added to the solution - it references all the packages and is the only one set to CopyLocal="true". No MSBuild knowledge required.

clip_image038

© Geeks with Blogs or respective owner