Skip to content

Commit

Permalink
Sync NuGet version with CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
dsplaisted committed Apr 11, 2018
1 parent 543b765 commit dcc217c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<MicrosoftExtensionsDependencyModelVersion>2.1.0-preview2-26306-03</MicrosoftExtensionsDependencyModelVersion>
<NETStandardLibraryNETFrameworkVersion>2.0.1-servicing-26011-01</NETStandardLibraryNETFrameworkVersion>
<NewtonsoftJsonVersion>9.0.1</NewtonsoftJsonVersion>
<NuGetBuildTasksPackVersion>4.7.0-preview1.4982</NuGetBuildTasksPackVersion>
<NuGetBuildTasksPackVersion>4.7.0-preview4.5065</NuGetBuildTasksPackVersion>
<NuGetPackagingVersion>$(NuGetBuildTasksPackVersion)</NuGetPackagingVersion>
<NuGetProjectModelVersion>$(NuGetBuildTasksPackVersion)</NuGetProjectModelVersion>
<PlatformAbstractionsVersion>2.0.0</PlatformAbstractionsVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
using NuGet.Common;
using NuGet.Packaging;
using NuGet.Packaging.Core;
using NuGet.Packaging.Signing;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

Expand Down Expand Up @@ -141,19 +144,112 @@ private void ExtractNupkg(string nugetCache, string nupkg)

if (!File.Exists(pathResolver.GetHashPath(identity.Id, identity.Version)))
{
using (var fileStream = File.OpenRead(nupkg))
PackageExtractor.InstallFromSourceAsync(
identity,
new MockPackageDownloader(nupkg, identity),
pathResolver,
new PackageExtractionContext(
PackageSaveMode.Defaultv3,
XmlDocFileSaveMode.None,
NullLogger.Instance,
signedPackageVerifier: null),
CancellationToken.None).Wait();
}
}

// MockPackageDownloader packaged after class from NuGet tests:
// https://github.com/NuGet/NuGet.Client/blob/90524ca5f2313702df2f29b72649c9611aa4b086/test/NuGet.Core.Tests/NuGet.Packaging.Test/NugetPackageUtilTests.cs#L805
private sealed class MockPackageDownloader : IPackageDownloader
{
private bool _isDisposed;
private readonly string _packageFilePath;
private readonly PackageIdentity _packageIdentity;
private Lazy<PackageArchiveReader> _packageReader;
private Lazy<FileStream> _sourceStream;

public IAsyncPackageContentReader ContentReader => _packageReader.Value;
public IAsyncPackageCoreReader CoreReader => _packageReader.Value;

public ISignedPackageReader SignedPackageReader => _packageReader.Value;

public string Source => "MockSource";

internal MockPackageDownloader(
string packageFilePath,
PackageIdentity packageIdentity)
{
_packageFilePath = packageFilePath;
_packageIdentity = packageIdentity;
_packageReader = new Lazy<PackageArchiveReader>(GetPackageReader);
_sourceStream = new Lazy<FileStream>(GetSourceStream);
}

public void Dispose()
{
if (!_isDisposed)
{
if (_packageReader.IsValueCreated)
{
_packageReader.Value.Dispose();
}

if (_sourceStream.IsValueCreated)
{
_sourceStream.Value.Dispose();
}

GC.SuppressFinalize(this);

_isDisposed = true;
}
}

public async Task<bool> CopyNupkgFileToAsync(string destinationFilePath, CancellationToken cancellationToken)
{
using (var source = File.OpenRead(_packageFilePath))
{
PackageExtractor.InstallFromSourceAsync(
identity,
stream => fileStream.CopyToAsync(stream, 4096, CancellationToken.None),
pathResolver,
new PackageExtractionContext(
PackageSaveMode.Defaultv3,
XmlDocFileSaveMode.None,
NullLogger.Instance,
signedPackageVerifier: null),
CancellationToken.None).Wait();
using (var dest = File.OpenWrite(destinationFilePath))
{
await source.CopyToAsync(dest, 4096, cancellationToken);
}
}
return true;
}

public Task<string> GetPackageHashAsync(string hashAlgorithm, CancellationToken cancellationToken)
{
_sourceStream.Value.Seek(0, SeekOrigin.Begin);

var bytes = new CryptoHashProvider(hashAlgorithm).CalculateHash(_sourceStream.Value);
var packageHash = Convert.ToBase64String(bytes);

return Task.FromResult(packageHash);
}

public void SetExceptionHandler(Func<Exception, Task<bool>> handleExceptionAsync)
{
}

public void SetThrottle(SemaphoreSlim throttle)
{
}

private PackageArchiveReader GetPackageReader()
{
_sourceStream.Value.Seek(0, SeekOrigin.Begin);

return new PackageArchiveReader(_sourceStream.Value);
}

private FileStream GetSourceStream()
{
return new FileStream(
_packageFilePath,
FileMode.Open,
FileAccess.Read,
FileShare.Read,
bufferSize: 4096,
useAsync: true);
}
}
}
Expand Down

0 comments on commit dcc217c

Please sign in to comment.