Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] GH2028: adding credential support for inprocess nuget client (please don't merge) #2090

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Cake.NuGet/Cake.NuGet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
</ItemGroup>
<!-- Global packages -->
<ItemGroup>
<PackageReference Include="NuGet.Credentials" Version="4.6.0" />
<PackageReference Include="NuGet.Frameworks" Version="4.6.0" />
<PackageReference Include="NuGet.Versioning" Version="4.6.0" />
<PackageReference Include="NuGet.ProjectModel" Version="4.6.0" />
Expand Down
120 changes: 120 additions & 0 deletions src/Cake.NuGet/Install/NuGetExtensionLocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using Cake.Core;
using Cake.Core.Configuration;

using NuGet.Configuration;

namespace Cake.NuGet.Install
{
/// <summary>
/// Provides a common facility for locating extensions
/// </summary>
internal sealed class NuGetExtensionLocator : IExtensionLocator // shamelessly copied from nuget source repo
{
private readonly ICakeConfiguration _config;
private readonly ICakeEnvironment _environment;

private static readonly string _extensionsDirectoryRoot =
Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.LocalApplicationData),
"NuGet",
"Commands");

private static readonly string _credentialProvidersDirectoryRoot =
Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.LocalApplicationData),
"NuGet",
"CredentialProviders");

private static readonly string CredentialProviderPattern = "CredentialProvider*.exe";

/// <summary>
/// PATH Enviroment variable name for path to nuget extensions
/// </summary>
public static readonly string ExtensionsEnvar = "NUGET_EXTENSIONS_PATH";

/// <summary>
/// PATH Enviroment variable name for path to nuget credentials providers
/// </summary>
public static readonly string CredentialProvidersEnvar = "NUGET_CREDENTIALPROVIDERS_PATH";

public NuGetExtensionLocator(ICakeEnvironment environment, ICakeConfiguration config)
{
_environment = environment ?? throw new ArgumentNullException(nameof(environment));
_config = config ?? throw new ArgumentNullException(nameof(config));
}

/// <inheritdoc/>
public IEnumerable<string> FindCredentialProviders()
{
var customPaths = ReadPathsFromEnvar(CredentialProvidersEnvar);
return FindAll(
_credentialProvidersDirectoryRoot,
customPaths,
CredentialProviderPattern,
CredentialProviderPattern
);
}

/// <inheritdoc/>
public IEnumerable<string> FindExtensions()
{
var customPaths = ReadPathsFromEnvar(ExtensionsEnvar);
return FindAll(
_extensionsDirectoryRoot,
customPaths,
"*.dll",
"*Extensions.dll"
);
}

private IEnumerable<string> FindAll(
string globalRootDirectory,
IEnumerable<string> customPaths,
string assemblyPattern,
string nugetDirectoryAssemblyPattern)
{
var directories = new List<string>();

// Add all directories from the environment variable if available.
directories.AddRange(customPaths);

// add the global root
directories.Add(globalRootDirectory);

var paths = new List<string>();
foreach (var directory in directories.Where(Directory.Exists))
{
paths.AddRange(Directory.EnumerateFiles(directory, assemblyPattern, SearchOption.AllDirectories));
}

// not sure if the working directory is an appropriate fallback for locating the tools/folder nuget/folder
var nugetDirectory = Path.GetDirectoryName(_config.GetToolPath(_environment.WorkingDirectory, _environment).FullPath); // directory for locating extensions and credentials providers, such as VSTS CredentialProvider
if (nugetDirectory == null)
{
return paths;
}

paths.AddRange(Directory.EnumerateFiles(nugetDirectory, nugetDirectoryAssemblyPattern));

return paths;
}

private static IEnumerable<string> ReadPathsFromEnvar(string key)
{
var result = new List<string>();
var paths = Environment.GetEnvironmentVariable(key);
if (!string.IsNullOrEmpty(paths))
{
result.AddRange(
paths.Split(new[] { ';' },
StringSplitOptions.RemoveEmptyEntries));
}
return result;
}
}
}
9 changes: 8 additions & 1 deletion src/Cake.NuGet/Install/NuGetPackageInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

using Cake.Core;
using Cake.Core.Configuration;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.NuGet.Install.Extensions;

using NuGet.Common;
using NuGet.Configuration;
using NuGet.Credentials;
using NuGet.Frameworks;
using NuGet.PackageManagement;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.Protocol.Core.Types;
using NuGet.Resolver;
using NuGet.Versioning;

using PackageReference = Cake.Core.Packaging.PackageReference;
using PackageType = Cake.Core.Packaging.PackageType;

Expand Down Expand Up @@ -131,6 +134,10 @@ public IReadOnlyCollection<IFile> Install(PackageReference package, PackageType

var resolutionContext = new ResolutionContext(dependencyBehavior, includePrerelease, false, VersionConstraints.None, _gatherCache, _sourceCacheContext);
var downloadContext = new PackageDownloadContext(_sourceCacheContext);
// WIP
var extensionLocator = new NuGetExtensionLocator(_environment, _config);
var pluginCredentialProviderBuilder = new PluginCredentialProviderBuilder(extensionLocator, _nugetSettings, _nugetLogger);
var credentialProviders = pluginCredentialProviderBuilder.BuildAll("verbose");

// First get the install actions.
// This will give us the list of packages to install, and which feed should be used.
Expand Down