Search code examples
visual-studiomsbuildnugetcsproj

UnPack NuGet package that gets created on build


I am trying to unpack the nuget package that gets created during the build.

My Directory.Build.props file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<Project>
    <PropertyGroup>
        <RestorePackagesPath>C:\packages</RestorePackagesPath>
    </PropertyGroup>
    <PropertyGroup>
        <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
        <PackageOutputPath>C:\LocalNuGetPackages</PackageOutputPath>
    </PropertyGroup>
    <PropertyGroup>
        <Version>1.0.0.1</Version>
    </PropertyGroup>
    <Target Name="UnPack" AfterTargets="Pack">
        <Exec Command="nuget install $(PackageId) -Version $(PackageVersion) -Source C:\LocalNuGetPackages -OutputDirectory C:\packages" />
    </Target>
</Project>

But this gives a different directory structure as Visual Studio is doing it.

Visual Studio produces the following directories

C:\packages\$(PackageId)\$(PackageVersion)\

But the used command (nuget install) produces

C:\packages\$(PackageId).$(PackageVersion)\

Is there a way to call the Visual Studio "internal" nuget to get the same directory structure, or am I missing an argument that enables this structure? Currently I am using the nuget.exe from here https://www.nuget.org/downloads which I have added to PATH in my system variables.

Side note, I am not trying to install the NuGet package to any project, I am just looking to unpack it like Visual Studio does.


Solution

  • As was pointed out in the github issue created by @Perry the command I was actually looking for was

    nuget add "C:\LocalNuGetPackages\castle.core.4.4.0.nupkg" -Source C:\packages -expand
    

    Instead of

    nuget install castle.core -Version 4.4.0 -Source C:\LocalNuGetPackages -OutputDirectory C:\packages
    

    The add command lacks the ability to fetch the nuget package from a "source" (eg. NuGet.org or C:\LocalNuGetPackages) and you already need the *.nupkg downloaded and ready to unpack.

    But this lack of ability is irrelevant in my situation, since upon build I am creating the *.nupkg and they are ther for me to unpack.

    Don't get confused with the argument being called -Source in the add it is actually the equivalent of -OutputDirectory for the install command.

    So for other users/cases it would be desirable to have the install command do the different directory structure, but for me add was what I wanted.