Skip to content

Commit

Permalink
refactor: Move Mono.Cecil extension methods not specific to either ap…
Browse files Browse the repository at this point in the history
…ireference or commandlinehelp to Common assembly
  • Loading branch information
ap0llo committed Jun 6, 2020
1 parent a577cce commit aeae588
Show file tree
Hide file tree
Showing 31 changed files with 131 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using Grynwald.MdDocs.ApiReference.Model.XmlDocs;
using Grynwald.MdDocs.Common;
using Mono.Cecil;

namespace Grynwald.MdDocs.ApiReference.Model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using Grynwald.MdDocs.ApiReference.Model.XmlDocs;
using Grynwald.MdDocs.Common;
using Mono.Cecil;

namespace Grynwald.MdDocs.ApiReference.Model
Expand Down
1 change: 1 addition & 0 deletions src/MdDocs.ApiReference/Model/SimpleMemberDocumentation.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using Grynwald.MdDocs.ApiReference.Model.XmlDocs;
using Grynwald.MdDocs.Common;
using Grynwald.Utilities.Collections;
using Mono.Cecil;

Expand Down
1 change: 1 addition & 0 deletions src/MdDocs.ApiReference/Model/TypeDocumentation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using Grynwald.MdDocs.ApiReference.Model.XmlDocs;
using Grynwald.MdDocs.Common;
using Grynwald.Utilities.Collections;
using Microsoft.Extensions.Logging;
using Mono.Cecil;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Grynwald.MdDocs.Common;
using Grynwald.Utilities.Collections;
using Mono.Cecil;

Expand Down Expand Up @@ -98,13 +99,13 @@ public static IEnumerable<CustomAttribute> GetCustomAttributes(this TypeDefiniti
return type.CustomAttributes
.Where(attribute =>
{
if (typeKind == TypeKind.Class && attribute.AttributeType.FullName == Constants.DefaultMemberAttributeFullName)
if (typeKind == TypeKind.Class && attribute.AttributeType.FullName == SystemTypeNames.DefaultMemberAttributeFullName)
return false;

if (typeKind == TypeKind.Class && attribute.AttributeType.FullName == Constants.ExtensionAttributeFullName)
if (typeKind == TypeKind.Class && attribute.AttributeType.FullName == SystemTypeNames.ExtensionAttributeFullName)
return false;

if (typeKind == TypeKind.Struct && attribute.AttributeType.FullName == Constants.IsReadOnlyAttributeFullName)
if (typeKind == TypeKind.Struct && attribute.AttributeType.FullName == SystemTypeNames.IsReadOnlyAttributeFullName)
return false;

if (!attribute.AttributeType.Resolve().IsPublic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ private static void AppendTypeModifiers(StringBuilder definitionBuilder, TypeDef
break;

case TypeKind.Struct:
if (type.CustomAttributes.Any(a => a.AttributeType.FullName == Constants.IsReadOnlyAttributeFullName))
if (type.CustomAttributes.Any(a => a.AttributeType.FullName == SystemTypeNames.IsReadOnlyAttributeFullName))
{
definitionBuilder.Append("readonly ");
}
Expand All @@ -477,7 +477,7 @@ private static void AppendBaseTypes(TypeDefinition type, TypeKind typeKind, Stri
if (typeKind == TypeKind.Enum)
{
var underlyingType = type.Fields.Single(f => f.Name == "value__").FieldType;
if (underlyingType.FullName != Constants.Int32FullName)
if (underlyingType.FullName != SystemTypeNames.Int32FullName)
{
definitionBuilder.Append(" : ");
definitionBuilder.Append(GetDisplayName(underlyingType));
Expand All @@ -488,8 +488,8 @@ private static void AppendBaseTypes(TypeDefinition type, TypeKind typeKind, Stri
// get the default (implicit) base type: "object" for classes, "System.ValueType" for structs
// if the base type is the default type, the base type will not be explicitly included in the definition
var defaultBaseType = typeKind == TypeKind.Struct
? Constants.ValueTypeFullName
: (typeKind == TypeKind.Class ? Constants.ObjectFullName : "");
? SystemTypeNames.ValueTypeFullName
: (typeKind == TypeKind.Class ? SystemTypeNames.ObjectFullName : "");

if (type.HasInterfaces)
{
Expand Down Expand Up @@ -594,7 +594,7 @@ private static string GetLiteral(TypeReference typeReference, object value)
var typeDefinition = typeReference.Resolve();

// string => put in quotation marks
if (typeReference.FullName == Constants.StringFullName)
if (typeReference.FullName == SystemTypeNames.StringFullName)
{
if (value is null)
{
Expand Down Expand Up @@ -694,7 +694,7 @@ private static string GetLiteral(TypeReference typeReference, object value)
}
}

private static bool IsFlagsEnum(TypeDefinition type) => type.CustomAttributes.Any(a => a.AttributeType.FullName == Constants.FlagsAttributeFullName);
private static bool IsFlagsEnum(TypeDefinition type) => type.CustomAttributes.Any(a => a.AttributeType.FullName == SystemTypeNames.FlagsAttributeFullName);

private static string GetDisplayName(TypeReference typeReference)
{
Expand All @@ -718,7 +718,7 @@ private static string GetDefinition(ParameterDefinition parameter)
AppendCustomAttributes(definitionBuilder, parameter.GetCustomAttributes(), singleLine: true);

// add "params" prefix if method allows multiple values
if (parameter.CustomAttributes.Any(a => a.AttributeType.FullName == Constants.ParamArrayAttributeFullName))
if (parameter.CustomAttributes.Any(a => a.AttributeType.FullName == SystemTypeNames.ParamArrayAttributeFullName))
{
definitionBuilder.Append("params ");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
#pragma warning disable IDE1006 // Naming Styles: Ignore hint that const strings should be prefixed with 's_' as all these fields are public
namespace Grynwald.MdDocs.CommandLineHelp
{
internal static class Constants
/// <summary>
/// Defines constants for names of type names defined by the "CommandLineParser" library
/// </summary>
internal static class CommandLineParserTypeNames
{
public const string VerbAttributeFullName = "CommandLine.VerbAttribute";
public const string OptionAttributeFullName = "CommandLine.OptionAttribute";
public const string ValueAttributeFullName = "CommandLine.ValueAttribute";
public const string AssemblyUsageAttributeFullName = "CommandLine.Text.AssemblyUsageAttribute";

public const string AssemblyTitleAttributeFullName = "System.Reflection.AssemblyTitleAttribute";
public const string AssemblyInformationalVersionAttribute = "System.Reflection.AssemblyInformationalVersionAttribute";

public const string BooleanFullName = "System.Boolean";
}
}
#pragma warning restore IDE1006 // Naming Styles
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,10 @@
<ProjectReference Include="..\MdDocs.Common\Grynwald.MdDocs.Common.csproj" />
</ItemGroup>



<ItemGroup>
<Compile Include="../shared/Nullable.cs" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Grynwald.MdDocs.Common;
using Grynwald.MdDocs.Common.Model;
using Microsoft.Extensions.Logging;
using Mono.Cecil;
Expand Down Expand Up @@ -36,14 +37,14 @@ public static ApplicationDocumentation FromAssemblyFile(string filePath, ILogger
{
var types = definition.MainModule.Types.Where(x => !x.IsAbstract);

if (types.Any(x => x.HasAttribute(Constants.VerbAttributeFullName)))
if (types.Any(x => x.HasAttribute(CommandLineParserTypeNames.VerbAttributeFullName)))
{
logger.LogInformation($"Found a class attributed with '{Constants.VerbAttributeFullName}'. Assuming application has sub-commands");
logger.LogInformation($"Found a class attributed with '{CommandLineParserTypeNames.VerbAttributeFullName}'. Assuming application has sub-commands");
return MultiCommandApplicationDocumentation.FromAssemblyDefinition(definition, logger);
}
else
{
logger.LogInformation($"Found *no* class attributed with '{Constants.VerbAttributeFullName}'. Assuming application without sub-commands");
logger.LogInformation($"Found *no* class attributed with '{CommandLineParserTypeNames.VerbAttributeFullName}'. Assuming application without sub-commands");
return SingleCommandApplicationDocumentation.FromAssemblyDefinition(definition, logger);
}
}
Expand All @@ -53,7 +54,7 @@ public static ApplicationDocumentation FromAssemblyFile(string filePath, ILogger
protected static string LoadApplicationName(AssemblyDefinition definition)
{
var name = definition
.GetAttributeOrDefault(Constants.AssemblyTitleAttributeFullName)
.GetAttributeOrDefault(SystemTypeNames.AssemblyTitleAttributeFullName)
?.ConstructorArguments?.Single().Value as string;

if (name == null || String.IsNullOrEmpty(name))
Expand All @@ -68,7 +69,7 @@ protected static string LoadApplicationName(AssemblyDefinition definition)
protected static string? LoadApplicationVersion(AssemblyDefinition definition)
{
var version = definition
.GetAttributeOrDefault(Constants.AssemblyInformationalVersionAttribute)
.GetAttributeOrDefault(SystemTypeNames.AssemblyInformationalVersionAttribute)
?.ConstructorArguments.Single().Value as string;

if (String.IsNullOrEmpty(version))
Expand All @@ -82,7 +83,7 @@ protected static string LoadApplicationName(AssemblyDefinition definition)

protected static IReadOnlyList<string> LoadAssemblyUsage(AssemblyDefinition definition)
{
var assemblyUsageAttribute = definition.GetAttributeOrDefault(Constants.AssemblyUsageAttributeFullName);
var assemblyUsageAttribute = definition.GetAttributeOrDefault(CommandLineParserTypeNames.AssemblyUsageAttributeFullName);

return assemblyUsageAttribute == null
? Array.Empty<string>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Grynwald.MdDocs.Common;
using Microsoft.Extensions.Logging;
using Mono.Cecil;

Expand Down Expand Up @@ -42,8 +43,8 @@ private IReadOnlyList<CommandDocumentation> LoadCommands(AssemblyDefinition defi
{
return definition.MainModule.Types
.Where(x => !x.IsAbstract)
.WithAttribute(Constants.VerbAttributeFullName)
.Where(x => !x.GetAttribute(Constants.VerbAttributeFullName).GetPropertyValueOrDefault<bool>("Hidden"))
.WithAttribute(CommandLineParserTypeNames.VerbAttributeFullName)
.Where(x => !x.GetAttribute(CommandLineParserTypeNames.VerbAttributeFullName).GetPropertyValueOrDefault<bool>("Hidden"))
.Select(type => CommandDocumentation.FromTypeDefinition(this, type, logger))
.ToArray();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Grynwald.MdDocs.Common;
using Microsoft.Extensions.Logging;
using Mono.Cecil;

Expand Down Expand Up @@ -38,7 +39,7 @@ private UnnamedCommandDocumentation LoadCommand(AssemblyDefinition definition, I
{
bool IsCommandLineParameter(PropertyDefinition property)
{
return property.HasAttribute(Constants.OptionAttributeFullName) || property.HasAttribute(Constants.ValueAttributeFullName);
return property.HasAttribute(CommandLineParserTypeNames.OptionAttributeFullName) || property.HasAttribute(CommandLineParserTypeNames.ValueAttributeFullName);
}

// get all types with at least one property attributed as either [Option] or [Value]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Grynwald.MdDocs.Common;
using Microsoft.Extensions.Logging;
using Mono.Cecil;

Expand Down Expand Up @@ -43,7 +44,7 @@ public static CommandDocumentation FromTypeDefinition(MultiCommandApplicationDoc
if (definition is null)
throw new ArgumentNullException(nameof(definition));

var verbAttribute = definition.GetAttribute(Constants.VerbAttributeFullName);
var verbAttribute = definition.GetAttribute(CommandLineParserTypeNames.VerbAttributeFullName);

var name = (string)verbAttribute.ConstructorArguments.First(x => x.Type.FullName == "System.String").Value;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Grynwald.MdDocs.Common;
using Microsoft.Extensions.Logging;
using Mono.Cecil;

Expand Down Expand Up @@ -53,7 +54,7 @@ protected internal CommandDocumentationBase(
protected static IReadOnlyList<OptionDocumentation> LoadOptions(TypeDefinition definition, ILogger logger)
{
return definition.GetAllProperties()
.WithAttribute(Constants.OptionAttributeFullName)
.WithAttribute(CommandLineParserTypeNames.OptionAttributeFullName)
.Select(property => OptionDocumentation.FromPropertyDefinition(property, logger))
.Where(option => !option.Hidden)
.ToArray();
Expand All @@ -62,7 +63,7 @@ protected static IReadOnlyList<OptionDocumentation> LoadOptions(TypeDefinition d
protected static IReadOnlyList<ValueDocumentation> LoadValues(TypeDefinition definition, ILogger logger)
{
return definition.GetAllProperties()
.WithAttribute(Constants.ValueAttributeFullName)
.WithAttribute(CommandLineParserTypeNames.ValueAttributeFullName)
.Select(property => ValueDocumentation.FromPropertyDefinition(property, logger))
.Where(value => !value.Hidden)
.ToArray();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Grynwald.MdDocs.Common;
using Microsoft.Extensions.Logging;
using Mono.Cecil;

Expand All @@ -26,7 +27,7 @@ public static UnnamedCommandDocumentation FromTypeDefinition(ApplicationDocument
throw new ArgumentNullException(nameof(definition));

// unnamed commands must not have a "verb" attribute
if (definition.HasAttribute(Constants.VerbAttributeFullName))
if (definition.HasAttribute(CommandLineParserTypeNames.VerbAttributeFullName))
throw new ArgumentException("Cannot create unnamed command from type definition annotated with a Verb attribute", nameof(definition));

return new UnnamedCommandDocumentation(application, LoadOptions(definition, logger), LoadValues(definition, logger));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Grynwald.MdDocs.Common;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Mono.Cecil;
Expand Down Expand Up @@ -46,7 +47,7 @@ internal OptionDocumentation(
}

private OptionDocumentation(PropertyDefinition definition, ILogger logger)
: base(definition, definition.GetAttribute(Constants.OptionAttributeFullName))
: base(definition, definition.GetAttribute(CommandLineParserTypeNames.OptionAttributeFullName))
{
m_Logger = logger ?? throw new ArgumentNullException(nameof(logger));

Expand All @@ -55,7 +56,7 @@ private OptionDocumentation(PropertyDefinition definition, ILogger logger)
// special handling for bool options:
// bool options are treated as "switch" parameters that do not require a value to be passed in,
// e.g. the option is set using "--option" not "--option true"
if (definition.PropertyType.FullName == Constants.BooleanFullName)
if (definition.PropertyType.FullName == SystemTypeNames.BooleanFullName)
{
IsSwitchParameter = true;

Expand Down Expand Up @@ -92,7 +93,7 @@ public static OptionDocumentation FromPropertyDefinition(PropertyDefinition defi
{
string? name = default;
char? shortName = default;
foreach (var arg in definition.GetAttribute(Constants.OptionAttributeFullName).ConstructorArguments)
foreach (var arg in definition.GetAttribute(CommandLineParserTypeNames.OptionAttributeFullName).ConstructorArguments)
{
if (arg.Type.FullName == typeof(string).FullName)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Grynwald.MdDocs.Common;
using Microsoft.Extensions.Logging;
using Mono.Cecil;

Expand Down Expand Up @@ -28,9 +29,9 @@ internal ValueDocumentation(
Name = name;
}

private ValueDocumentation(PropertyDefinition property) : base(property, property.GetAttribute(Constants.ValueAttributeFullName))
private ValueDocumentation(PropertyDefinition property) : base(property, property.GetAttribute(CommandLineParserTypeNames.ValueAttributeFullName))
{
var valueAttribute = property.GetAttribute(Constants.ValueAttributeFullName);
var valueAttribute = property.GetAttribute(CommandLineParserTypeNames.ValueAttributeFullName);

Index = (int)valueAttribute.ConstructorArguments.Single().Value;
Name = valueAttribute.GetPropertyValueOrDefault<string>("MetaName");
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions src/MdDocs.Common.Test/Grynwald.MdDocs.Common.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

<ItemGroup>
<ProjectReference Include="..\MdDocs.Common\Grynwald.MdDocs.Common.csproj" />
<ProjectReference Include="..\MdDocs.TestHelpers\Grynwald.MdDocs.TestHelpers.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit aeae588

Please sign in to comment.