Skip to content

Commit

Permalink
Code cleanup
Browse files Browse the repository at this point in the history
Remove unnecessary code
  • Loading branch information
ocalles committed Oct 12, 2022
1 parent 8786ee8 commit 3ea4ca2
Showing 1 changed file with 37 additions and 62 deletions.
99 changes: 37 additions & 62 deletions src/Tasks/Microsoft.NET.Build.Tasks/ResolvePackageAssets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Linq;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
Expand Down Expand Up @@ -680,7 +681,6 @@ internal sealed class CacheWriter : IDisposable
{
private const int InitialStringTableCapacity = 32;

private Dictionary<string, string> _targetNameToAliasMap;
private ResolvePackageAssets _task;
private BinaryWriter _writer;
private LockFile _lockFile;
Expand Down Expand Up @@ -711,7 +711,6 @@ public CacheWriter(ResolvePackageAssets task)
_task = task;
_lockFile = new LockFileCache(task).GetLockFile(task.ProjectAssetsFile);
_packageResolver = NuGetPackageResolver.CreateResolver(_lockFile);
_targetNameToAliasMap = CreateTargetNameToAliasMap();

// If we are doing a design-time build, we do not want to fail the build if we can't find the
// target framework and/or runtime identifier in the assets file. This is because the design-time
Expand Down Expand Up @@ -753,19 +752,6 @@ public CacheWriter(ResolvePackageAssets task)
{
ComputePackageExclusions();
}

Dictionary<string, string> CreateTargetNameToAliasMap() => _lockFile.Targets.ToDictionary(t => t.Name, t =>
{
var alias = _lockFile.GetLockFileTargetAlias(t);
if (string.IsNullOrEmpty(t.RuntimeIdentifier))
{
return alias;
}
else
{
return alias + "/" + t.RuntimeIdentifier;
}
});
}

public void WriteToCacheFile()
Expand Down Expand Up @@ -1120,6 +1106,16 @@ private static bool TryParseVersion(string value, int startIndex, int length, ou
}
}

private struct PackageDefinitionItem
{
public string Name;
public string Type;
public string Version;
public string ResolvedPath;
public string Path;
public string DiagnosticLevel;
}

private void WriteItemGroup(Action writeItems)
{
var placeholder = WritePlaceholder();
Expand Down Expand Up @@ -1448,9 +1444,9 @@ private void WritePackageDependenciesDesignTime()
{
HashSet<string> projectFileDependencies = _lockFile.GetProjectFileDependencySet(_compileTimeTarget.Name);

List<ITaskItem> packageDependencies = GetPackageDependencies();
List<string> packageDependencies = GetPackageDependencies();

List<ITaskItem> packageDefinitions = GetPackageDefinitions();
List<PackageDefinitionItem> packageDefinitions = GetPackageDefinitions();

var implicitPackageReferences = GetImplicitPackageReferences(_task.DefaultImplicitPackages);

Expand All @@ -1462,61 +1458,41 @@ private void WritePackageDependenciesDesignTime()
// First, we scan PackageDependencies to build the set of packages in our target.

var allowItemSpecs = new HashSet<string>(StringComparer.OrdinalIgnoreCase);

foreach (var dependency in packageDependencies)
{
if (dependency.HasMetadataValue(MetadataKeys.ParentPackage))
{
// ignore non-top-level packages (those with ParentPackage)
continue;
}

var target = dependency.GetMetadata(MetadataKeys.ParentTarget);

if (!StringComparer.OrdinalIgnoreCase.Equals(target, _task.TargetFramework))
{
// skip dependencies for other targets
continue;
}

allowItemSpecs.Add(dependency.ItemSpec);
}
allowItemSpecs = packageDependencies.ToHashSet();

// Second, find PackageDefinitions that match our allowed item specs

var outputItems = new List<ITaskItem>(allowItemSpecs.Count);

foreach (var packageDef in packageDefinitions)
{
if (!allowItemSpecs.Contains(packageDef.ItemSpec))
if (!allowItemSpecs.Contains(packageDef.Name))
{
// We are not interested in this definition (not top-level, or wrong target)
continue;
}

var dependencyType = GetDependencyType(packageDef.GetMetadata(MetadataKeys.Type));
var dependencyType = GetDependencyType(packageDef.Type);

if (dependencyType == DependencyType.Package ||
dependencyType == DependencyType.Unresolved)
{
var name = packageDef.GetMetadata(MetadataKeys.Name);
var name = packageDef.Name;

if (string.IsNullOrEmpty(name))
{
// Name is required
continue;
}

var version = packageDef.GetMetadata(MetadataKeys.Version) ?? string.Empty;
var resolvedPath = packageDef.GetMetadata(MetadataKeys.ResolvedPath);
var version = packageDef.Version ?? string.Empty;
var resolvedPath = packageDef.ResolvedPath;
var resolved = !string.IsNullOrEmpty(resolvedPath);
var path = (resolved
? resolvedPath
: packageDef.GetMetadata(MetadataKeys.Path)) ?? string.Empty;
: packageDef.Path) ?? string.Empty;
var isImplicitlyDefined = implicitPackageReferences.Contains(name);
var diagnosticLevel = packageDef.GetMetadata(MetadataKeys.DiagnosticLevel) ?? string.Empty;
var diagnosticLevel = packageDef.DiagnosticLevel ?? string.Empty;

WriteItem(packageDef.ItemSpec);
WriteItem(packageDef.Name);
WriteMetadata(MetadataKeys.Name, name);
WriteMetadata(MetadataKeys.Version, version);
WriteMetadata(MetadataKeys.Path, path);
Expand All @@ -1526,14 +1502,14 @@ private void WritePackageDependenciesDesignTime()
}
}

List<ITaskItem> GetPackageDependencies()
List<string> GetPackageDependencies()
{
List <ITaskItem> dependencies = new();
List <string> dependencies = new();

var resolvedPackageVersions = _compileTimeTarget.Libraries
.ToDictionary(pkg => pkg.Name, pkg => pkg.Version.ToNormalizedString(), StringComparer.OrdinalIgnoreCase);

string frameworkAlias = _targetNameToAliasMap[_compileTimeTarget.Name];
string frameworkAlias = _lockFile.GetLockFileTargetAlias(_compileTimeTarget);

var transitiveProjectRefs = new HashSet<string>(
_compileTimeTarget.Libraries
Expand All @@ -1546,11 +1522,9 @@ List<ITaskItem> GetPackageDependencies()
if (projectFileDependencies.Contains(package.Name))
{
string packageId = GetPackageId(package);
TaskItem item = new TaskItem(packageId);
item.SetMetadata(MetadataKeys.ParentTarget, frameworkAlias); // Foreign Key
item.SetMetadata(MetadataKeys.ParentPackage, string.Empty); // Foreign Key

dependencies.Add(item);
// itemSpec
dependencies.Add(packageId);
}
}

Expand All @@ -1560,26 +1534,27 @@ List<ITaskItem> GetPackageDependencies()
static string GetPackageId(LockFileTargetLibrary package) => $"{package.Name}/{package.Version.ToNormalizedString()}";

// get library and file definitions
List<ITaskItem> GetPackageDefinitions()
List<PackageDefinitionItem> GetPackageDefinitions()
{
List<ITaskItem> packageDefinitions = new();
List<PackageDefinitionItem> packageDefinitions = new();

foreach (var package in _lockFile.Libraries)
{
PackageDefinitionItem item = new PackageDefinitionItem();

var packageName = package.Name;
var packageVersion = package.Version.ToNormalizedString();
string packageId = $"{packageName}/{packageVersion}";
var item = new TaskItem(packageId);
item.SetMetadata(MetadataKeys.Name, packageName);
item.SetMetadata(MetadataKeys.Type, package.Type);
item.SetMetadata(MetadataKeys.Version, packageVersion);

item.SetMetadata(MetadataKeys.Path, package.Path ?? string.Empty);
item.Name = packageName;
item.Type = package.Type;
item.Version = packageVersion;
item.Path = package.Path ?? string.Empty;

string resolvedPackagePath = ResolvePackagePath(package);
item.SetMetadata(MetadataKeys.ResolvedPath, resolvedPackagePath ?? string.Empty);
item.ResolvedPath = resolvedPackagePath ?? string.Empty;

item.SetMetadata(MetadataKeys.DiagnosticLevel, GetPackageDiagnosticLevel(package));
item.DiagnosticLevel = GetPackageDiagnosticLevel(package);

packageDefinitions.Add(item);
}
Expand Down

0 comments on commit 3ea4ca2

Please sign in to comment.