Remove unused tasks and make inconsistent package versions a warning not error

This commit is contained in:
Nate McMaster 2017-11-09 11:30:43 -08:00
parent 3d88b10b73
commit 3d4d724b6f
5 changed files with 5 additions and 325 deletions

View File

@ -49,7 +49,7 @@
<_NoTimestampPackages Include="Microsoft.AspNetCore.All" Version="$(PackageVersionNoTimestamp)" />
</ItemGroup>
<RepoTasks.GeneratePackageVersionPropsFile
<GeneratePackageVersionPropsFile
Packages="@(_NoTimestampPackages)"
OutputPath="$(GeneratedNoTimestampPackageVersionPropsPath)" />

View File

@ -171,7 +171,10 @@ namespace RepoTasks
}
sb.AppendLine();
Log.LogMessage(MessageImportance.High, sb.ToString());
Log.LogError("Package versions are inconsistent. See build log for details.");
Log.LogWarning("Package versions are inconsistent. See build log for details.");
// reduced to warning for now.
// TODO: address the complexity of LKG dependencies
// Log.LogError("Package versions are inconsistent. See build log for details.");
}
foreach (var repo in reposThatShouldPatch)

View File

@ -1,196 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using NuGet.Build;
using NuGet.Commands;
using NuGet.Configuration;
using NuGet.DependencyResolver;
using NuGet.Packaging.Core;
using NuGet.Protocol;
using NuGet.Protocol.Core.Types;
using NuGet.Versioning;
using Task = System.Threading.Tasks.Task;
namespace RepoTasks
{
public class DownloadNuGetPackages : Microsoft.Build.Utilities.Task, ICancelableTask
{
private static readonly Task<bool> FalseTask = Task.FromResult(false);
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
[Required]
public ITaskItem[] Packages { get; set; }
[Required]
public string DestinationFolder { get; set; }
[Output]
public ITaskItem[] Files { get; set; }
public void Cancel() => _cts.Cancel();
public override bool Execute()
{
return ExecuteAsync().GetAwaiter().GetResult();
}
public async Task<bool> ExecuteAsync()
{
DestinationFolder = DestinationFolder.Replace('\\', '/');
var requests = new Dictionary<string, List<PackageIdentity>>(StringComparer.OrdinalIgnoreCase);
var files = new List<ITaskItem>();
var downloadCount = 0;
foreach (var item in Packages)
{
var id = item.ItemSpec;
var rawVersion = item.GetMetadata("Version");
if (!NuGetVersion.TryParse(rawVersion, out var version))
{
Log.LogError($"Package '{id}' has an invalid 'Version' metadata value: '{rawVersion}'.");
return false;
}
var source = item.GetMetadata("Source");
if (string.IsNullOrEmpty(source))
{
Log.LogError($"Package '{id}' is missing the 'Source' metadata value.");
return false;
}
if (!requests.TryGetValue(source, out var packages))
{
packages = requests[source] = new List<PackageIdentity>();
}
var request = new PackageIdentity(id, version);
var dest = GetExpectedOutputPath(request);
files.Add(new TaskItem(dest));
if (File.Exists(dest))
{
Log.LogMessage($"Skipping {request.Id} {request.Version}. Already exists in '{dest}'");
continue;
}
else
{
downloadCount++;
packages.Add(request);
}
}
Files = files.ToArray();
if (downloadCount == 0)
{
Log.LogMessage("All packages are downloaded.");
return true;
}
Directory.CreateDirectory(DestinationFolder);
var logger = new MSBuildLogger(Log);
var timer = Stopwatch.StartNew();
logger.LogMinimal($"Downloading {downloadCount} package(s)");
using (var cacheContext = new SourceCacheContext())
{
var defaultSettings = Settings.LoadDefaultSettings(root: null, configFileName: null, machineWideSettings: null);
var sourceProvider = new CachingSourceProvider(new PackageSourceProvider(defaultSettings));
var tasks = new List<Task<bool>>();
foreach (var feed in requests)
{
var repo = sourceProvider.CreateRepository(new PackageSource(feed.Key));
tasks.Add(DownloadPackagesAsync(repo, feed.Value, cacheContext, logger, _cts.Token));
}
var all = Task.WhenAll(tasks);
var wait = TimeSpan.FromSeconds(Math.Max(downloadCount * 5, 120));
var delay = Task.Delay(wait);
var finished = await Task.WhenAny(all, delay);
if (ReferenceEquals(delay, finished))
{
Log.LogError($"Timed out after {wait.TotalSeconds}s");
Cancel();
return false;
}
if (!tasks.All(a => a.Result))
{
Log.LogError("Failed to download all packages");
return false;
}
timer.Stop();
logger.LogMinimal($"Finished downloading {downloadCount} package(s) in {timer.ElapsedMilliseconds}ms");
return true;
}
}
private async Task<bool> DownloadPackagesAsync(
SourceRepository repo,
IEnumerable<PackageIdentity> requests,
SourceCacheContext cacheContext,
NuGet.Common.ILogger logger,
CancellationToken cancellationToken)
{
var remoteLibraryProvider = new SourceRepositoryDependencyProvider(repo, logger, cacheContext, ignoreFailedSources: false, ignoreWarning: false);
var downloads = new List<Task<bool>>();
var metadataResource = await repo.GetResourceAsync<MetadataResource>();
foreach (var request in requests)
{
cancellationToken.ThrowIfCancellationRequested();
if (metadataResource != null && !await metadataResource.Exists(request, logger, cancellationToken))
{
logger.LogError($"Package {request.Id} {request.Version} is not available on '{repo}'");
downloads.Add(FalseTask);
continue;
}
var download = DownloadPackageAsync(cacheContext, logger, remoteLibraryProvider, request, cancellationToken);
downloads.Add(download);
}
await Task.WhenAll(downloads);
return downloads.All(d => d.Result);
}
private async Task<bool> DownloadPackageAsync(SourceCacheContext cacheContext,
NuGet.Common.ILogger logger,
SourceRepositoryDependencyProvider remoteLibraryProvider,
PackageIdentity request,
CancellationToken cancellationToken)
{
var dest = GetExpectedOutputPath(request);
logger.LogInformation($"Downloading {request.Id} {request.Version} to '{dest}'");
using (var packageDependency = await remoteLibraryProvider.GetPackageDownloaderAsync(request, cacheContext, logger, cancellationToken))
{
if (!await packageDependency.CopyNupkgFileToAsync(dest, cancellationToken))
{
logger.LogError($"Could not download {request.Id} {request.Version} from {remoteLibraryProvider.Source}");
return false;
}
}
return true;
}
private string GetExpectedOutputPath(PackageIdentity request)
{
return Path.Combine(DestinationFolder, $"{request.Id.ToLowerInvariant()}.{request.Version.ToNormalizedString()}.nupkg");
}
}
}

View File

@ -1,125 +0,0 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using RepoTasks.ProjectModel;
using RepoTasks.Utilities;
using System.Text;
namespace RepoTasks
{
public class GeneratePackageVersionPropsFile : Task
{
[Required]
public ITaskItem[] Packages { get; set; }
[Required]
public string OutputPath { get; set; }
public override bool Execute()
{
OutputPath = OutputPath.Replace('\\', '/');
Directory.CreateDirectory(Path.GetDirectoryName(OutputPath));
var props = new XElement("PropertyGroup");
var root = new XElement("Project", props);
var doc = new XDocument(root);
props.Add(new XElement("MSBuildAllProjects", "$(MSBuildAllProjects);$(MSBuildThisFileFullPath)"));
var varNames = new HashSet<string>();
var versionElements = new List<XElement>();
foreach (var pkg in Packages)
{
var packageVersion = pkg.GetMetadata("Version");
if (string.IsNullOrEmpty(packageVersion))
{
Log.LogError("Package {0} is missing the Version metadata", pkg.ItemSpec);
continue;
}
string packageVarName;
if (!string.IsNullOrEmpty(pkg.GetMetadata("VariableName")))
{
packageVarName = pkg.GetMetadata("VariableName");
if (!packageVarName.EndsWith("Version", StringComparison.Ordinal))
{
Log.LogError("VariableName for {0} must end in 'Version'", pkg.ItemSpec);
continue;
}
}
else
{
packageVarName = GetVariableName(pkg.ItemSpec);
}
var packageTfm = pkg.GetMetadata("TargetFramework");
var key = $"{packageVarName}/{packageTfm}";
if (varNames.Contains(key))
{
Log.LogError("Multiple packages would produce {0} in the generated dependencies.props file. Set VariableName to differentiate the packages manually", key);
continue;
}
varNames.Add(key);
var elem = new XElement(packageVarName, packageVersion);
if (!string.IsNullOrEmpty(packageTfm))
{
elem.Add(new XAttribute("Condition", $" '$(TargetFramework)' == '{packageTfm}' "));
}
versionElements.Add(elem);
}
foreach (var item in versionElements.OrderBy(p => p.Name.ToString()))
{
props.Add(item);
}
var settings = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Indent = true,
};
using (var writer = XmlWriter.Create(OutputPath, settings))
{
Log.LogMessage(MessageImportance.Normal, $"Generate {OutputPath}");
doc.Save(writer);
}
return !Log.HasLoggedErrors;
}
private string GetVariableName(string packageId)
{
var sb = new StringBuilder();
var first = true;
foreach (var ch in packageId)
{
if (ch == '.')
{
first = true;
continue;
}
if (first)
{
first = false;
sb.Append(char.ToUpperInvariant(ch));
}
else
{
sb.Append(ch);
}
}
sb.Append("PackageVersion");
return sb.ToString();
}
}
}

View File

@ -5,8 +5,6 @@
<UsingTask TaskName="RepoTasks.AnalyzeBuildGraph" AssemblyFile="$(_RepoTaskAssembly)" />
<UsingTask TaskName="RepoTasks.CopyPackagesToSplitFolders" AssemblyFile="$(_RepoTaskAssembly)" />
<UsingTask TaskName="RepoTasks.DownloadNuGetPackages" AssemblyFile="$(_RepoTaskAssembly)" />
<UsingTask TaskName="RepoTasks.GeneratePackageVersionPropsFile" AssemblyFile="$(_RepoTaskAssembly)" />
<UsingTask TaskName="RepoTasks.GenerateRestoreSourcesPropsFile" AssemblyFile="$(_RepoTaskAssembly)" />
<UsingTask TaskName="RepoTasks.VerifyCoherentVersions" AssemblyFile="$(_RepoTaskAssembly)" />
<UsingTask TaskName="RepoTasks.AddMetapackageReferences" AssemblyFile="$(_RepoTaskAssembly)" />