Skip to content

Commit

Permalink
Apply all code fixes; no functional changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdeago committed Apr 1, 2022
1 parent 754d1ce commit abafcad
Show file tree
Hide file tree
Showing 13 changed files with 762 additions and 775 deletions.
109 changes: 54 additions & 55 deletions src/ExportAnnotations/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// -----------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------
// Copyright (C) Riccardo De Agostini and Tenacom. All rights reserved.
// Licensed under the MIT license.
// See LICENSE file in the project root for full license information.
// See the LICENSE file in the project root for full license information.
//
// Part of this file may be third-party code, distributed under a compatible license.
// See THIRD-PARTY-NOTICES file in the project root for third-party copyright notices.
// -----------------------------------------------------------------------------------
// See the THIRD-PARTY-NOTICES file in the project root for third-party copyright notices.
// ---------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
Expand All @@ -16,67 +16,66 @@
using McMaster.Extensions.CommandLineUtils;
using ReSharper.ExportAnnotations;

namespace ExportAnnotations
namespace ExportAnnotations;

public sealed class Program
{
public sealed class Program
{
[Argument(0, "assemblyPath", Description = "Full path of the compiled assembly")]
[Required]
[FileExists]
[UsedImplicitly]
public string AssemblyPath { get; } = string.Empty;
[Argument(0, "assemblyPath", Description = "Full path of the compiled assembly")]
[Required]
[FileExists]
[UsedImplicitly]
public string AssemblyPath { get; } = string.Empty;

[Option(
Description = "Do not export annotations (but strip them if --strip is specified)",
LongName = "no-export",
ShortName = "n")]
[UsedImplicitly]
public bool NoExport { get; }
[Option(
Description = "Do not export annotations (but strip them if --strip is specified)",
LongName = "no-export",
ShortName = "n")]
[UsedImplicitly]
public bool NoExport { get; }

[Argument(1, "xmlPath", Description = "Full path of the generated XML file (defaults to a .ExternalAnnotations.xml file side by side with the assembly)")]
[LegalFilePath]
[UsedImplicitly]
public string? XmlPath { get; }
[Argument(1, "xmlPath", Description = "Full path of the generated XML file (defaults to a .ExternalAnnotations.xml file side by side with the assembly)")]
[LegalFilePath]
[UsedImplicitly]
public string? XmlPath { get; }

[Option(
CommandOptionType.MultipleValue,
Description = "Full path of a referenced library (will be actually referenced only when rewriting the assembly)",
LongName = "lib",
ShortName = "l")]
[LegalFilePath]
[UsedImplicitly]
public IEnumerable<string>? LibraryPaths { get; }
[Option(
CommandOptionType.MultipleValue,
Description = "Full path of a referenced library (will be actually referenced only when rewriting the assembly)",
LongName = "lib",
ShortName = "l")]
[LegalFilePath]
[UsedImplicitly]
public IEnumerable<string>? LibraryPaths { get; }

[Option(
Description = "Full path of a text file containing a referenced library path on each line",
LongName = "liblist",
ShortName = "ll")]
[LegalFilePath]
[UsedImplicitly]
public string? LibraryListPath { get; }
[Option(
Description = "Full path of a text file containing a referenced library path on each line",
LongName = "liblist",
ShortName = "ll")]
[LegalFilePath]
[UsedImplicitly]
public string? LibraryListPath { get; }

[Option(Description = "Strip annotations from assembly (CAUTION: will also strip debug symbols on non-Windows systems)")]
[UsedImplicitly]
public bool Strip { get; }
[Option(Description = "Strip annotations from assembly (CAUTION: will also strip debug symbols on non-Windows systems)")]
[UsedImplicitly]
public bool Strip { get; }

public static int Main(string[] args) => CommandLineApplication.Execute<Program>(args);
public static int Main(string[] args) => CommandLineApplication.Execute<Program>(args);

[UsedImplicitly]
private int OnExecute()
{
var listedLibraries = LibraryListPath != null
? File.ReadAllLines(LibraryListPath).Where(p => !string.IsNullOrWhiteSpace(p)).Where(File.Exists)
: Array.Empty<string>();
[UsedImplicitly]
private int OnExecute()
{
var listedLibraries = LibraryListPath != null
? File.ReadAllLines(LibraryListPath).Where(p => !string.IsNullOrWhiteSpace(p)).Where(File.Exists)
: Array.Empty<string>();

var parameters = new AnnotationsExporterParameters()
.WithExportAnnotations(!NoExport)
.WithXmlPath(XmlPath)
.WithLibraries((LibraryPaths ?? Enumerable.Empty<string>()).Concat(listedLibraries))
.WithStripAnnotations(Strip);
var parameters = new AnnotationsExporterParameters()
.WithExportAnnotations(!NoExport)
.WithXmlPath(XmlPath)
.WithLibraries((LibraryPaths ?? Enumerable.Empty<string>()).Concat(listedLibraries))
.WithStripAnnotations(Strip);

AnnotationsExporter.Run(AssemblyPath, parameters);
AnnotationsExporter.Run(AssemblyPath, parameters);

return 0;
}
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
// -----------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------
// Copyright (C) Riccardo De Agostini and Tenacom. All rights reserved.
// Licensed under the MIT license.
// See LICENSE file in the project root for full license information.
// See the LICENSE file in the project root for full license information.
//
// Part of this file may be third-party code, distributed under a compatible license.
// See THIRD-PARTY-NOTICES file in the project root for third-party copyright notices.
// -----------------------------------------------------------------------------------
// See the THIRD-PARTY-NOTICES file in the project root for third-party copyright notices.
// ---------------------------------------------------------------------------------------

using System.Xml.Linq;
using JetBrains.Annotations;
using Mono.Cecil;

namespace ReSharper.ExportAnnotations
namespace ReSharper.ExportAnnotations;

/// <content />
public partial class AnnotationsExporter
{
/// <content />
public partial class AnnotationsExporter
[PublicAPI]
private static void ExportAnnotations(AssemblyDefinition assembly, string xmlPath)
{
[PublicAPI]
private static void ExportAnnotations(AssemblyDefinition assembly, string xmlPath)
{
var xml = new XDocument(AssemblyToXml(assembly));
xml.Save(xmlPath, SaveOptions.None);
}
var xml = new XDocument(AssemblyToXml(assembly));
xml.Save(xmlPath, SaveOptions.None);
}
}
Original file line number Diff line number Diff line change
@@ -1,51 +1,50 @@
// -----------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------
// Copyright (C) Riccardo De Agostini and Tenacom. All rights reserved.
// Licensed under the MIT license.
// See LICENSE file in the project root for full license information.
// See the LICENSE file in the project root for full license information.
//
// Part of this file may be third-party code, distributed under a compatible license.
// See THIRD-PARTY-NOTICES file in the project root for third-party copyright notices.
// -----------------------------------------------------------------------------------
// See the THIRD-PARTY-NOTICES file in the project root for third-party copyright notices.
// ---------------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using Mono.Cecil;
using ReSharper.ExportAnnotations.Internal;

namespace ReSharper.ExportAnnotations
namespace ReSharper.ExportAnnotations;

/// <content />
public partial class AnnotationsExporter
{
/// <content />
public partial class AnnotationsExporter
{
private static IEnumerable<XElement> GetCustomAttributeArgumentsXml(ICustomAttribute customAttribute)
=> customAttribute.ConstructorArguments.Select(CustomAttributeArgumentToXml);
private static IEnumerable<XElement> GetCustomAttributeArgumentsXml(ICustomAttribute customAttribute)
=> customAttribute.ConstructorArguments.Select(CustomAttributeArgumentToXml);

private static IEnumerable<XElement> GetAnnotationsXml(ICustomAttributeProvider member)
=> member.CustomAttributes.Where(IsExportableJetBrainsAnnotation).Select(CustomAttributeToXml);
private static IEnumerable<XElement> GetAnnotationsXml(ICustomAttributeProvider member)
=> member.CustomAttributes.Where(IsExportableJetBrainsAnnotation).Select(CustomAttributeToXml);

private static IEnumerable<XElement> GetGenericParametersXml(IGenericParameterProvider provider)
=> provider.GenericParameters
.Where(p => p.HasCustomAttributes)
.Select(GenericParameterToXml)
.WhereNotNull();
private static IEnumerable<XElement> GetGenericParametersXml(IGenericParameterProvider provider)
=> provider.GenericParameters
.Where(p => p.HasCustomAttributes)
.Select(GenericParameterToXml)
.WhereNotNull();

private static IEnumerable<XElement> GetParametersXml(IEnumerable<ParameterDefinition> parameters)
=> parameters.Where(p => p.HasCustomAttributes).Select(ParameterToXml).WhereNotNull();
private static IEnumerable<XElement> GetParametersXml(IEnumerable<ParameterDefinition> parameters)
=> parameters.Where(p => p.HasCustomAttributes).Select(ParameterToXml).WhereNotNull();

private static IEnumerable<XElement> GetMethodsXml(TypeDefinition type)
=> type.Methods.Select(MethodToXml).WhereNotNull();
private static IEnumerable<XElement> GetMethodsXml(TypeDefinition type)
=> type.Methods.Select(MethodToXml).WhereNotNull();

private static IEnumerable<XElement> GetPropertiesXml(TypeDefinition type)
=> type.Properties.Select(PropertyToXml).WhereNotNull();
private static IEnumerable<XElement> GetPropertiesXml(TypeDefinition type)
=> type.Properties.Select(PropertyToXml).WhereNotNull();

private static IEnumerable<XElement> GetFieldsXml(TypeDefinition type)
=> type.Fields.Select(FieldToXml).WhereNotNull();
private static IEnumerable<XElement> GetFieldsXml(TypeDefinition type)
=> type.Fields.Select(FieldToXml).WhereNotNull();

private static IEnumerable<XElement> GetEventsXml(TypeDefinition type)
=> type.Events.Select(EventToXml).WhereNotNull();
private static IEnumerable<XElement> GetEventsXml(TypeDefinition type)
=> type.Events.Select(EventToXml).WhereNotNull();

private static IEnumerable<XElement> GetTypesXml(AssemblyDefinition assembly)
=> assembly.Modules.SelectMany(m => m.GetTypes()).Where(IsExportedType).SelectMany(TypeToXml);
}
private static IEnumerable<XElement> GetTypesXml(AssemblyDefinition assembly)
=> assembly.Modules.SelectMany(m => m.GetTypes()).Where(IsExportedType).SelectMany(TypeToXml);
}
61 changes: 30 additions & 31 deletions src/ReSharper.ExportAnnotations.Core/AnnotationsExporter-Filters.cs
Original file line number Diff line number Diff line change
@@ -1,49 +1,48 @@
// -----------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------
// Copyright (C) Riccardo De Agostini and Tenacom. All rights reserved.
// Licensed under the MIT license.
// See LICENSE file in the project root for full license information.
// See the LICENSE file in the project root for full license information.
//
// Part of this file may be third-party code, distributed under a compatible license.
// See THIRD-PARTY-NOTICES file in the project root for third-party copyright notices.
// -----------------------------------------------------------------------------------
// See the THIRD-PARTY-NOTICES file in the project root for third-party copyright notices.
// ---------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Mono.Cecil;

namespace ReSharper.ExportAnnotations
namespace ReSharper.ExportAnnotations;

/// <content />
public partial class AnnotationsExporter
{
/// <content />
public partial class AnnotationsExporter
private static readonly IReadOnlyList<string> NonExportableAttributeNames = new[]
{
private static readonly IReadOnlyList<string> NonExportableAttributeNames = new[]
{
"AspMvcSuppressViewErrorAttribute",
"LocalizationRequiredAttribute",
"MeansImplicitUseAttribute",
"NoReorderAttribute",
"PublicAPIAttribute",
"UsedImplicitlyAttribute",
};
"AspMvcSuppressViewErrorAttribute",
"LocalizationRequiredAttribute",
"MeansImplicitUseAttribute",
"NoReorderAttribute",
"PublicAPIAttribute",
"UsedImplicitlyAttribute",
};

private static bool IsExportableJetBrainsAnnotation(CustomAttribute attribute)
=> attribute.AttributeType.Namespace == "JetBrains.Annotations"
&& !NonExportableAttributeNames.Contains(attribute.AttributeType.Name, StringComparer.Ordinal);
private static bool IsExportableJetBrainsAnnotation(CustomAttribute attribute)
=> attribute.AttributeType.Namespace == "JetBrains.Annotations"
&& !NonExportableAttributeNames.Contains(attribute.AttributeType.Name, StringComparer.Ordinal);

private static bool IsExportedType(TypeDefinition type)
private static bool IsExportedType(TypeDefinition type)
{
// ReSharper disable once SwitchStatementMissingSomeCases
switch (type.Attributes & TypeAttributes.VisibilityMask)
{
// ReSharper disable once SwitchStatementMissingSomeCases
switch (type.Attributes & TypeAttributes.VisibilityMask)
{
case TypeAttributes.Public:
case TypeAttributes.NestedPublic:
case TypeAttributes.NestedFamily:
case TypeAttributes.NestedFamORAssem:
return true;
default:
return false;
}
case TypeAttributes.Public:
case TypeAttributes.NestedPublic:
case TypeAttributes.NestedFamily:
case TypeAttributes.NestedFamORAssem:
return true;
default:
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
// -----------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------
// Copyright (C) Riccardo De Agostini and Tenacom. All rights reserved.
// Licensed under the MIT license.
// See LICENSE file in the project root for full license information.
// See the LICENSE file in the project root for full license information.
//
// Part of this file may be third-party code, distributed under a compatible license.
// See THIRD-PARTY-NOTICES file in the project root for third-party copyright notices.
// -----------------------------------------------------------------------------------
// See the THIRD-PARTY-NOTICES file in the project root for third-party copyright notices.
// ---------------------------------------------------------------------------------------

using System.Text;
using Cecil.XmlDocNames;
using Mono.Cecil;

namespace ReSharper.ExportAnnotations
namespace ReSharper.ExportAnnotations;

/// <content />
public partial class AnnotationsExporter
{
/// <content />
public partial class AnnotationsExporter
{
private static string GetXmlName(AssemblyDefinition assembly) => assembly.Name.Name;
private static string GetXmlName(AssemblyDefinition assembly) => assembly.Name.Name;

private static string GetXmlName(ParameterReference parameter) => parameter.Name;
private static string GetXmlName(ParameterReference parameter) => parameter.Name;

private static string GetXmlName(MemberReference member)
=> new StringBuilder().AppendXmlDocName(member).ToString();
}
private static string GetXmlName(MemberReference member)
=> new StringBuilder().AppendXmlDocName(member).ToString();
}
Loading

0 comments on commit abafcad

Please sign in to comment.