Skip to content

Instantly share code, notes, and snippets.

@robgha01
Created September 17, 2016 16:16
Show Gist options
  • Save robgha01/52067e2ba48e7877ab844dab9fca2a31 to your computer and use it in GitHub Desktop.
Save robgha01/52067e2ba48e7877ab844dab9fca2a31 to your computer and use it in GitHub Desktop.
namespace BlueLeet.Build
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Cake.Core;
using Cake.Core.Annotations;
using Cake.Core.IO;
using JetBrains.Annotations;
using NuGet;
/// <summary>
/// The extended NuGet.
/// </summary>
[UsedImplicitly]
[CakeAliasCategory("BlueLeetNuGet")]
[CakeNamespaceImport("NuGet.Core")]
[CakeNamespaceImport("NuGet.Analysis")]
[CakeNamespaceImport("NuGet.Analysis.Rules")]
[CakeNamespaceImport("NuGet.Frameworks")]
[CakeNamespaceImport("NuGet.Resources")]
[CakeNamespaceImport("NuGet.Runtime")]
[CakeNamespaceImport("NuGet.Shared")]
public static class BlueLeetNuGetAliases
{
/// <summary>
/// NuGet project dependencies.
/// </summary>
/// <param name="context">
/// The <c>context</c>.
/// </param>
/// <param name="path">
/// A relative <see cref="DirectoryPath"/> where packages.config resides.
/// </param>
/// <returns>
/// A <see cref="IEnumerable{PackageReference}"/>.
/// </returns>
[CakeMethodAlias]
[UsedImplicitly]
public static IEnumerable<PackageReference> GetPackageReferences(this ICakeContext context, DirectoryPath path)
{
if (!path.IsRelative)
{
throw new CakeException("DirectoryPath must be relative!");
}
var packagePath = path.CombineWithFilePath(new FilePath("packages.config"));
if (!File.Exists(packagePath.FullPath))
{
throw new CakeException(string.Format("Could not find a packages.config file in '{0}'", path.FullPath));
}
var file = new PackageReferenceFile(packagePath.FullPath);
return file.GetPackageReferences();
}
/// <summary>
/// Get a NuGet project dependency by <paramref name="packageId"/>.
/// </summary>
/// <param name="context">
/// This <see cref="ICakeContext"/>.
/// </param>
/// <param name="path">
/// A relative <see cref="DirectoryPath"/> where packages.config resides.
/// </param>
/// <param name="packageId">
/// The package Name.
/// </param>
/// <returns>
/// A <see cref="IEnumerable{PackageReference}"/>.
/// </returns>
[CakeMethodAlias]
[UsedImplicitly]
public static PackageReference GetPackageReference(this ICakeContext context, DirectoryPath path, string packageId)
{
if (!path.IsRelative)
{
throw new CakeException("DirectoryPath must be relative!");
}
var packagePath = path.CombineWithFilePath(new FilePath("packages.config"));
if (!File.Exists(packagePath.FullPath))
{
throw new CakeException(string.Format("Could not find a packages.config file in '{0}'", path.FullPath));
}
var file = new PackageReferenceFile(packagePath.FullPath);
return file.GetPackageReferences().FirstOrDefault(x => x.Id.Equals(packageId, StringComparison.OrdinalIgnoreCase));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment