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
GH1743: Implements functionality for downloading NuGet packages #1768
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,113 @@ | ||
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; | ||
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 IFileSystem _fileSystem; | ||
private readonly INuGetContentResolver _contentResolver; | ||
private readonly ICakeConfiguration _config; | ||
private readonly ICakeLog _log; | ||
private readonly PackagePathResolver _pathResolver; | ||
|
||
private static readonly ISet<string> _blackListedPackages = new HashSet<string>(new[] | ||
{ | ||
"Cake.Common", | ||
"Cake.Core" | ||
}, StringComparer.OrdinalIgnoreCase); | ||
|
||
public NugetFolderProject( | ||
IFileSystem fileSystem, | ||
INuGetContentResolver contentResolver, | ||
ICakeConfiguration config, | ||
ICakeLog log, | ||
PackagePathResolver pathResolver, | ||
string root) : base(root, pathResolver) | ||
{ | ||
_fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem)); | ||
_contentResolver = contentResolver ?? throw new ArgumentNullException(nameof(contentResolver)); | ||
_config = config ?? throw new ArgumentNullException(nameof(config)); | ||
_log = log ?? throw new ArgumentNullException(nameof(log)); | ||
_pathResolver = pathResolver ?? throw new ArgumentNullException(nameof(pathResolver)); | ||
_installedPackages = new HashSet<PackageIdentity>(); | ||
} | ||
|
||
public override Task<bool> InstallPackageAsync(PackageIdentity packageIdentity, DownloadResourceResult downloadResourceResult, | ||
INuGetProjectContext nuGetProjectContext, CancellationToken token) | ||
{ | ||
_installedPackages.Add(packageIdentity); | ||
|
||
if (_fileSystem.Exist(new DirectoryPath(_pathResolver.GetInstallPath(packageIdentity)))) | ||
{ | ||
_log.Debug("Package {0} has already been installed.", packageIdentity.ToString()); | ||
return Task.FromResult(true); | ||
} | ||
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)); | ||
var installPath = new DirectoryPath(_pathResolver.GetInstallPath(package)); | ||
|
||
if (!_fileSystem.Exist(installPath)) | ||
{ | ||
_log.Warning("Package {0} is not installed.", packageReference.Package); | ||
return Array.Empty<IFile>(); | ||
} | ||
|
||
files.AddRange(_contentResolver.GetFiles(installPath, 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; | ||
} | ||
|
||
var dependencyInstallPath = new DirectoryPath(_pathResolver.GetInstallPath(package)); | ||
|
||
if (!_fileSystem.Exist(dependencyInstallPath)) | ||
{ | ||
_log.Warning("Package {0} is not installed.", dependency.Id); | ||
continue; | ||
} | ||
|
||
files.AddRange(_contentResolver.GetFiles(dependencyInstallPath, packageReference, type)); | ||
} | ||
} | ||
|
||
return files; | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cake-build/cake-team can anyone tell me why this test is failing here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see above comment