-
-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(GH-1743) Implements functionality for downloading NuGet packages
- Opt-in via NuGet_UseInProcessClient - Opt-in load dependencies via NuGet_LoadDependencies or add LoadDependencies as parameter in directive - Fixes #1743
- Loading branch information
1 parent
2093f5e
commit a8e315b
Showing
13 changed files
with
557 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Cake.Core.Configuration; | ||
using Cake.Core.Diagnostics; | ||
using Cake.Core.IO; | ||
using NuGet.Packaging.Core; | ||
using NuGet.ProjectManagement; | ||
using NuGet.Protocol.Core.Types; | ||
using PackageReference = Cake.Core.Packaging.PackageReference; | ||
using PackageType = Cake.Core.Packaging.PackageType; | ||
|
||
namespace Cake.NuGet.Install | ||
{ | ||
internal sealed class NugetFolderProject : FolderNuGetProject | ||
{ | ||
private readonly ISet<PackageIdentity> _installedPackages; | ||
private readonly INuGetContentResolver _contentResolver; | ||
private readonly ICakeConfiguration _config; | ||
private readonly ICakeLog _log; | ||
private static readonly ISet<string> _blackListedPackages = new HashSet<string>(new[] | ||
{ | ||
"Cake.Common", | ||
"Cake.Core" | ||
}, StringComparer.OrdinalIgnoreCase); | ||
|
||
public NugetFolderProject(INuGetContentResolver contentResolver, ICakeConfiguration config, ICakeLog log, string root) : base(root) | ||
{ | ||
_contentResolver = contentResolver ?? throw new ArgumentNullException(nameof(contentResolver)); | ||
_config = config ?? throw new ArgumentNullException(nameof(config)); | ||
_log = log ?? throw new ArgumentNullException(nameof(log)); | ||
_installedPackages = new HashSet<PackageIdentity>(); | ||
} | ||
|
||
public override Task<bool> InstallPackageAsync(PackageIdentity packageIdentity, DownloadResourceResult downloadResourceResult, | ||
INuGetProjectContext nuGetProjectContext, CancellationToken token) | ||
{ | ||
_installedPackages.Add(packageIdentity); | ||
return base.InstallPackageAsync(packageIdentity, downloadResourceResult, nuGetProjectContext, token); | ||
} | ||
|
||
public IReadOnlyCollection<IFile> GetFiles(DirectoryPath directoryPath, PackageReference packageReference, PackageType type) | ||
{ | ||
bool loadDependencies; | ||
if (packageReference.Parameters.ContainsKey("LoadDependencies")) | ||
{ | ||
bool.TryParse(packageReference.Parameters["LoadDependencies"].FirstOrDefault() ?? bool.TrueString, out loadDependencies); | ||
} | ||
else | ||
{ | ||
bool.TryParse(_config.GetValue(Constants.NuGet.LoadDependencies) ?? bool.FalseString, out loadDependencies); | ||
} | ||
|
||
var files = new List<IFile>(); | ||
var package = _installedPackages.First(p => p.Id.Equals(packageReference.Package, StringComparison.OrdinalIgnoreCase)); | ||
files.AddRange(_contentResolver.GetFiles(new FilePath(GetInstalledManifestFilePath(package)).GetDirectory(), packageReference, type)); | ||
|
||
if (loadDependencies) | ||
{ | ||
foreach (var dependency in _installedPackages | ||
.Where(p => !p.Id.Equals(packageReference.Package, StringComparison.OrdinalIgnoreCase))) | ||
{ | ||
if (_blackListedPackages.Contains(dependency.Id)) | ||
{ | ||
_log.Warning("Package {0} depends on package {1}. Will not load this dependency...", | ||
packageReference.Package, dependency.ToString()); | ||
continue; | ||
} | ||
files.AddRange(_contentResolver.GetFiles(new FilePath(GetInstalledManifestFilePath(dependency)).GetDirectory(), packageReference, type)); | ||
} | ||
} | ||
|
||
return files; | ||
} | ||
} | ||
} |
Oops, something went wrong.