Permalink
Browse files

Move metadata sorting logic to the AssemblyMetadata class

  • Loading branch information...
zvirja committed Oct 27, 2017
1 parent 57c212d commit 646c941012fdf765ad9ca994e42090abce1b5792
@@ -1,45 +1,99 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace NuGetPe.AssemblyMetadata
{
/// <summary>
/// Meta data of the assembly,
/// </summary>
public class AssemblyMetaData : Dictionary<string, string>
public class AssemblyMetaData
{
public const string ReferencedAssembliesKey = "Referenced assemblies";
private Dictionary<string, string> MetadataEntries { get; } = new Dictionary<string, string>();
private string FullName { get; set; }
private string StrongName { get; set; }
private IEnumerable<AssemblyName> ReferencedAsseblies { get; set; }
/// <summary>
/// Set Fullname of the assembly and determine strong name.
/// </summary>
/// <remarks>Helper</remarks>
public void SetFullName(AssemblyName assemblyName)
{
this[FullNameLabel] = assemblyName.FullName;
if (assemblyName == null) throw new ArgumentNullException(nameof(assemblyName));
FullName = assemblyName.FullName;
try
{
var publicKey = assemblyName.GetPublicKeyToken();
var isStrongNamed = publicKey != null && publicKey.Length > 0;
if (isStrongNamed)
{
this[StrongNamedLabel] = $"Yes, version {assemblyName.Version}";
}
else
{
this[StrongNamedLabel] = "No";
}
StrongName = isStrongNamed
? $"Yes, version {assemblyName.Version}"
: "No";
}
catch (Exception)
{
//ignore
}
}
private const string FullNameLabel = "Full Name";
private const string StrongNamedLabel = "Strong Name";
/// <summary>
/// Set list of referenced assembly names.
/// </summary>
public void SetReferencedAssemblyNames(IEnumerable<AssemblyName> referencedAssemblyNames)
{
if (referencedAssemblyNames == null) throw new ArgumentNullException(nameof(referencedAssemblyNames));
ReferencedAsseblies = referencedAssemblyNames;
}
/// <summary>
/// Add arbitrary metadata information.
/// </summary>
public void AddMetadata(string displayName, string value)
{
if (string.IsNullOrEmpty(displayName)) throw new ArgumentNullException(nameof(displayName));
MetadataEntries[displayName] = value;
}
/// <summary>
/// Gets all the metadata entries sorted by importance
/// </summary>
public IEnumerable<KeyValuePair<string, string>> GetMetadataEntriesOrderedByImportance()
{
if (FullName != null)
{
yield return MakePair("Full Name", FullName);
}
if (StrongName != null)
{
yield return MakePair("Strong Name", StrongName);
}
foreach (var entry in MetadataEntries.OrderBy(kv => kv.Key))
{
yield return entry;
}
if (ReferencedAsseblies != null)
{
var assemblyNamesDelimitedByLineBreak = string.Join(
Environment.NewLine,
ReferencedAsseblies
.OrderBy(assName => assName.Name)
.Select(assName => assName.FullName));
yield return MakePair("Referenced assemblies", assemblyNamesDelimitedByLineBreak);
}
}
private static KeyValuePair<string, string> MakePair(string displayName, string value)
{
return new KeyValuePair<string, string>(displayName, value);
}
}
}
@@ -47,8 +47,6 @@ private static void AddAssemblyAttributes(AssemblyMetadataParser parser, Assembl
{
try
{
var attributesToAdd = new Dictionary<string, string>();
foreach (var attribute in parser.GetAssemblyAttributes())
{
var value = TryReadAttributeValue(attribute);
@@ -58,12 +56,8 @@ private static void AddAssemblyAttributes(AssemblyMetadataParser parser, Assembl
}
string displayName = ReadAttributeDisplayName(attribute);
attributesToAdd[displayName] = value;
result.AddMetadata(displayName, value);
}
// Add all info or nothing if failed in the middle.
// That is to not confuse clients with partial output.
result.AddRange(attributesToAdd);
}
catch (Exception)
{
@@ -111,12 +105,9 @@ private static void AddReferencedAssemblyInfo(AssemblyMetadataParser parser, Ass
{
try
{
result[AssemblyMetaData.ReferencedAssembliesKey] = string.Join(
Environment.NewLine,
parser.GetReferencedAssemblyNames()
.OrderBy(assName => assName.Name)
.Select(assName => assName.FullName)
);
// ToArray is required, otherwise sequence might be enumerated when context is already closed.
var referencedAssemblyNames = parser.GetReferencedAssemblyNames().ToArray();
result.SetReferencedAssemblyNames(referencedAssemblyNames);
}
catch
{
@@ -26,18 +26,15 @@ public object GetView(string extension, Stream stream)
try
{
IDictionary<string, string> assemblyData = AssemblyMetadataReader.ReadMetaData(tempFile);
var assemblyMetadata = AssemblyMetadataReader.ReadMetaData(tempFile);
var grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Auto) });
if (assemblyData != null)
if (assemblyMetadata != null)
{
var orderedAssemblyDataEntries = assemblyData
// Put information about referenced assemblies at the end.
.OrderBy(d => d.Key.Equals(AssemblyMetaData.ReferencedAssembliesKey, StringComparison.Ordinal))
.ThenBy(d => d.Key);
var orderedAssemblyDataEntries = assemblyMetadata.GetMetadataEntriesOrderedByImportance();
foreach (var data in orderedAssemblyDataEntries)
{

0 comments on commit 646c941

Please sign in to comment.