Naos.Packaging.NuGet – Wrapper to easily download packages from NuGet

Summary

NuGet is just plain hard to work with.  I know you shouldn’t throw stones in glass houses and all that.  I’m sure many people think my contributions to Naos are terribly written code.  All that said, I personally think the entire NuGet stack is terribly written code.  Its got far too many layers, zero to terrible documentation, hard to read, hard to follow, counter-intuitive, etc.  Since I needed to be able to simply download packages for use in the Naos.Deployment work I created this library to hide and document all the scariness necessary to download packages from either a private repository or the public one using .NET.

Project

Naos is an opensource project dedicated to providing infrastructure tools for free to aid in small companies having solid platforms to launch on, learn more at http://naosproject.com.

Source

The entire project is open sourced under the MIT license and available at: https://github.com/NaosFramework/Naos.Packaging

Referencing

It’s best to reference the NuGet package: http://www.nuget.org/packages/Naos.Packaging.NuGet which will provide a more feature rich interaction with packages…

The specific NuGet operations using the prototcol are included in a single file which can be copied into your project without a Naos depedency in your project (will still REQUIRE package NuGet.PackageManagement):https://raw.githubusercontent.com/NaosProject/Naos.Packaging/master/Naos.Packaging.NuGet/NuGetPackageManager.cs.

Use

Below is an example of usage in C#.

// this is how you would use the full retriever
[Fact(Skip = "Meant for local debugging and to show usage.")]
public void DownloadPrivate()
{
        var repoConfig = new PackageRepositoryConfiguration
                            {
                                Source = "https://ci.appveyor.com/nuget/XXX",
                                ClearTextPassword = "ThisIsPassword",
                                Username = "ThisIsUser",
                                SourceName = "ThisIsGalleryName",
                                ProtocolVersion = 2,
                            };

    var defaultWorkingDirectory = @"D:\Temp\NewNuGet";
    var pm = new PackageRetriever(repoConfig, defaultWorkingDirectory);
    var bundleAllDependencies = false;
    var package = pm.GetPackage(new PackageDescription { Id = "ThisIsPackage" }, bundleAllDependencies);
    Assert.NotNull(package.PackageFileBytes);
}

[Fact(Skip = "Meant for local debugging and to show usage.")]
public void DownloadPublic()
{
    var defaultWorkingDirectory = @"D:\Temp\NewNuGet";
    var pm = new PackageRetriever(defaultWorkingDirectory);
    var bundleAllDependencies = false;
    var package = pm.GetPackage(new PackageDescription { Id = "Newtonsoft.Json" }, bundleAllDependencies);
    Assert.NotNull(package.PackageFileBytes);
}

// this is how you would use the NuGet only file
[Fact(Skip = "Meant for local debugging and to show usage.")]
public void DownloadPrivate()
{
    var defaultWorkingDirectory = @"D:\Temp\NewNuGet";
    var downloadDirectory = Path.Combine(defaultWorkingDirectory, Guid.NewGuid() + ".tmp");

    var pm = new NuGetPackageManager(2, "ThisIsGalleryName", "https://ci.appveyor.com/nuget/XXX", "ThisIsUser", "ThisIsPassword");

    var includeUnlisted = true;
    var includePreRelease = true;
    var latestVersionTask = pm.GetLatestVersionAsync("ThisIsPackageId", includeUnlisted, includePreRelease);
    latestVersionTask.Wait();
    var version = latestVersionTask.Result;

    var includeDependencies = true;

    pm.DownloadPackageToPathAsync(
        "ThisIsPackageid",
        version,
        downloadDirectory,
        includeDependencies,
        includeUnlisted,
        includePreRelease).Wait();
}

[Fact]
public void DownloadPublic()
{
    var defaultWorkingDirectory = @"D:\Temp\NewNuGet";
    var downloadDirectory = Path.Combine(defaultWorkingDirectory, Guid.NewGuid() + ".tmp");

    var pm = new NuGetPackageManager();

    var includeUnlisted = true;
    var includePreRelease = true;
    var latestVersionTask = pm.GetLatestVersionAsync("Newtonsoft.Json", includeUnlisted, includePreRelease);
    latestVersionTask.Wait();
    var version = latestVersionTask.Result;

    var includeDependencies = true;

    pm.DownloadPackageToPathAsync(
        "Newtonsoft.Json",
        version,
        downloadDirectory,
        includeDependencies,
        includeUnlisted,
        includePreRelease).Wait();
}

Leave a Reply

Your email address will not be published.