Skip to content

Commit

Permalink
Upgrade NuGetV3LibDownloader
Browse files Browse the repository at this point in the history
  • Loading branch information
zentron committed Jan 15, 2024
1 parent ebe6884 commit 38cbe4c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 39 deletions.
4 changes: 2 additions & 2 deletions source/Calamari.Common/Calamari.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
<ItemGroup Condition="'$(TargetFramework)' == 'net462' ">
<Reference Include="System.Security" />
<Reference Include="System.Net" />
<PackageReference Include="NuGet.CommandLine" Version="2.8.6" />
<PackageReference Include="NuGet.Core" Version="2.14.0" />
<PackageReference Include="Microsoft.Net.Http" Version="2.2.29" />
<PackageReference Include="AlphaFS" Version="2.1.3-octopus0006" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Autofac" Version="4.8.0" />
<PackageReference Include="NuGet.Commands" Version="3.5.0" />
<PackageReference Include="NuGet.CommandLine" Version="4.9.6" />
<PackageReference Include="NuGet.Commands" Version="4.9.6" />
<PackageReference Include="Globfish" Version="1.0.4" />
<PackageReference Include="JavaPropertiesParser" Version="0.2.1" />
<PackageReference Include="Microsoft.Web.Xdt" Version="3.1.0" />
Expand Down
2 changes: 1 addition & 1 deletion source/Calamari.Shared/Calamari.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<PackageReference Include="Microsoft.Web.Xdt" Version="3.1.0" />
<PackageReference Include="System.Diagnostics.Tracing" Version="4.3.0" />
<PackageReference Include="Polly" Version="5.4.0" />
<PackageReference Include="NuGet.Commands" Version="3.5.0" />
<PackageReference Include="NuGet.Commands" Version="4.9.6" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.1'">
<PackageReference Include="Markdown" Version="2.1.0" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using System.IO;
using Calamari.Common.Plumbing.Logging;
using System.Threading.Tasks;
using Calamari.Common.Plumbing.Extensions;
using NuGet.DependencyResolver;
using NuGet.LibraryModel;
using NuGet.Commands;
using NuGet.Packaging.Core;
using Octopus.Versioning;

namespace Calamari.Integration.Packages.NuGet
{
public class NuGetV3LibDownloader
{
public static void DownloadPackage(string packageId, IVersion version, Uri feedUri, ICredentials feedCredentials, string targetFilePath)
public static void DownloadPackage(string packageId,
IVersion version,
Uri feedUri,
ICredentials feedCredentials,
string targetFilePath)
{
ILogger logger = new NugetLogger();
var sourceRepository = Repository.Factory.GetCoreV3(feedUri.AbsoluteUri);
Expand All @@ -26,45 +30,90 @@ public static void DownloadPackage(string packageId, IVersion version, Uri feedU
sourceRepository.PackageSource.Credentials = new PackageSourceCredential("octopus", cred.UserName, cred.Password, true);
}

using (var sourceCacheContext = new SourceCacheContext() { NoCache = true })
var targetPath = Directory.GetParent(targetFilePath).FullName;
if (!Directory.Exists(targetPath))
{
var providers = new SourceRepositoryDependencyProvider(sourceRepository, logger, sourceCacheContext);
var libraryIdentity = new LibraryIdentity(packageId, version.ToNuGetVersion(), LibraryType.Package);
Directory.CreateDirectory(targetPath);
}

var targetPath = Directory.GetParent(targetFilePath).FullName;
if (!Directory.Exists(targetPath))
{
Directory.CreateDirectory(targetPath);
}
var targetTempNupkg = Path.Combine(targetPath, Path.GetRandomFileName());
var packageIdentity = new PackageIdentity(packageId, version.ToNuGetVersion());
PerformNuGetDownload(packageIdentity, targetTempNupkg, sourceRepository, logger)
.GetAwaiter()
.GetResult();

string targetTempNupkg = Path.Combine(targetPath, Path.GetRandomFileName());
using (var nupkgStream = new FileStream(targetTempNupkg,
FileMode.Create,
FileAccess.ReadWrite,
FileShare.ReadWrite | FileShare.Delete,
4096,
true))
{
providers.CopyToAsync(libraryIdentity, nupkgStream, CancellationToken.None)
.GetAwaiter()
.GetResult();
}
File.Move(targetTempNupkg, targetFilePath);

File.Move(targetTempNupkg, targetFilePath);
}
}

static async Task PerformNuGetDownload(PackageIdentity packageIdentity,
string targetFilePath,
SourceRepository sourceRepository,
ILogger logger)
{
using var sourceCacheContext = new SourceCacheContext() { NoCache = true };
var providers = new SourceRepositoryDependencyProvider(sourceRepository,
logger,
sourceCacheContext,
false,
true);
using var downloader = await providers.GetPackageDownloaderAsync(packageIdentity, sourceCacheContext, logger, CancellationToken.None);
await downloader.CopyNupkgFileToAsync(targetFilePath, default);
}

public class NugetLogger : ILogger
{
public void LogDebug(string data) => Log.Verbose(data);
public void LogVerbose(string data) => Log.Verbose(data);
public void LogInformation(string data) => Log.Info(data);
public void LogMinimal(string data) => Log.Verbose(data);
public void LogWarning(string data) => Log.Warn(data);
public void LogError(string data) => Log.Error(data);
public void LogSummary(string data) => Log.Info(data);
public void LogInformationSummary(string data) => Log.Info(data);
public void LogErrorSummary(string data) => Log.Error(data);
public void LogDebug(string data) => Common.Plumbing.Logging.Log.Verbose(data);
public void LogVerbose(string data) => Common.Plumbing.Logging.Log.Verbose(data);
public void LogInformation(string data) => Common.Plumbing.Logging.Log.Info(data);
public void LogMinimal(string data) => Common.Plumbing.Logging.Log.Verbose(data);
public void LogWarning(string data) => Common.Plumbing.Logging.Log.Warn(data);
public void LogError(string data) => Common.Plumbing.Logging.Log.Error(data);
public void LogInformationSummary(string data) => Common.Plumbing.Logging.Log.Info(data);

public void Log(LogLevel level, string data)
{
switch (level)
{
case LogLevel.Debug:
LogDebug(data);
break;
case LogLevel.Verbose:
LogVerbose(data);
break;
case LogLevel.Information:
LogInformation(data);
break;
case LogLevel.Minimal:
LogMinimal(data);
break;
case LogLevel.Warning:
LogWarning(data);
break;
case LogLevel.Error:
LogError(data);
break;
default:
throw new ArgumentOutOfRangeException(nameof(level), level, null);
}
}

public Task LogAsync(LogLevel level, string data)
{
Log(level, data);
return Task.CompletedTask;
}

public void Log(ILogMessage message)
{
Log(message.Level, message.Message);
}

public Task LogAsync(ILogMessage message)
{
Log(message);
return Task.CompletedTask;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public void ShouldSupportFeedzNuGetVersion3Feeds(string versionString)
[Test]
[TestCaseSource(nameof(ArtifactoryNuGet3SupportedVersionStrings))]
[TestCaseSource(nameof(ArtifactoryNuGet2SupportedVersionStrings))]
[Platform("Net-4.5")]
public void ArtifactoryShouldSupportNuGetVersion3Feeds(string versionString)
{
var calamariResult = DownloadPackage(TestNuGetPackageId, versionString, "nuget-local", ArtifactoryNuGetV3FeedUrl);
Expand Down

0 comments on commit 38cbe4c

Please sign in to comment.