Skip to content

Commit

Permalink
Merge pull request #122 from sungam3r/modifiers-order
Browse files Browse the repository at this point in the history
Order of keywords
  • Loading branch information
danielmarbach committed Nov 4, 2019
2 parents a5fc652 + b5d0380 commit 82effdb
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 57 deletions.
54 changes: 11 additions & 43 deletions src/PublicApiGenerator/ApiGenerator.cs
Expand Up @@ -9,7 +9,6 @@
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using ICustomAttributeProvider = Mono.Cecil.ICustomAttributeProvider;
using TypeAttributes = System.Reflection.TypeAttributes;
Expand Down Expand Up @@ -60,15 +59,6 @@ static string RemoveUnnecessaryWhiteSpace(string publicApi)
// TODO: Better handle namespaces - using statements? - requires non-qualified type names
static string CreatePublicApiForAssembly(AssemblyDefinition assembly, Func<TypeDefinition, bool> shouldIncludeType, bool shouldIncludeAssemblyAttributes, string[] whitelistedNamespacePrefixes, AttributeFilter attributeFilter)
{
var publicApiBuilder = new StringBuilder();
var cgo = new CodeGeneratorOptions
{
BracingStyle = "C",
BlankLinesBetweenMembers = false,
VerbatimOrder = false,
IndentString = " "
};

using (var provider = new CSharpCodeProvider())
{
var compileUnit = new CodeCompileUnit();
Expand Down Expand Up @@ -99,17 +89,18 @@ static string CreatePublicApiForAssembly(AssemblyDefinition assembly, Func<TypeD

using (var writer = new StringWriter())
{
var cgo = new CodeGeneratorOptions
{
BracingStyle = "C",
BlankLinesBetweenMembers = false,
VerbatimOrder = false,
IndentString = " "
};

provider.GenerateCodeFromCompileUnit(compileUnit, writer, cgo);
var typeDeclarationText = NormaliseGeneratedCode(writer);
publicApiBuilder.AppendLine(typeDeclarationText);
return CodeNormalizer.NormalizeGeneratedCode(writer);
}
}
return NormaliseLineEndings(publicApiBuilder.ToString().Trim());
}

static string NormaliseLineEndings(string value)
{
return Regex.Replace(value, @"\r\n|\n\r|\r|\n", Environment.NewLine);
}

static bool IsDelegate(TypeDefinition publicType)
Expand Down Expand Up @@ -169,29 +160,6 @@ static bool IsDotNetTypeMember(IMemberDefinition m, string[] whitelistedNamespac
}
}

static string NormaliseGeneratedCode(StringWriter writer)
{
var gennedClass = writer.ToString();
const string autoGeneratedHeader = @"^//-+\s*$.*^//-+\s*$";
const string emptyGetSet = @"\s+{\s+get\s+{\s+}\s+set\s+{\s+}\s+}";
const string emptyGet = @"\s+{\s+get\s+{\s+}\s+}";
const string emptySet = @"\s+{\s+set\s+{\s+}\s+}";
const string getSet = @"\s+{\s+get;\s+set;\s+}";
const string get = @"\s+{\s+get;\s+}";
const string set = @"\s+{\s+set;\s+}";
gennedClass = Regex.Replace(gennedClass, autoGeneratedHeader, string.Empty,
RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.Singleline);
gennedClass = Regex.Replace(gennedClass, emptyGetSet, " { get; set; }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, getSet, " { get; set; }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, emptyGet, " { get; }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, emptySet, " { set; }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, get, " { get; }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, set, " { set; }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, @"\s+{\s+}", " { }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, @"\)\s+;", ");", RegexOptions.IgnorePatternWhitespace);
return gennedClass;
}

static CodeTypeDeclaration CreateTypeDeclaration(TypeDefinition publicType, string[] whitelistedNamespacePrefixes, AttributeFilter attributeFilter)
{
if (IsDelegate(publicType))
Expand Down Expand Up @@ -225,9 +193,9 @@ static CodeTypeDeclaration CreateTypeDeclaration(TypeDefinition publicType, stri

var declarationName = string.Empty;
if (@readonly)
declarationName += "readonly ";
declarationName += CodeNormalizer.readonlyMarker;
if (@static)
declarationName += "static ";
declarationName += CodeNormalizer.staticMarker;

declarationName += name;

Expand Down
43 changes: 43 additions & 0 deletions src/PublicApiGenerator/CodeNormalizer.cs
@@ -0,0 +1,43 @@
using System;
using System.IO;
using System.Text.RegularExpressions;

namespace PublicApiGenerator
{
class CodeNormalizer
{
const string autoGeneratedHeader = @"^//-+\s*$.*^//-+\s*$";
const string emptyGetSet = @"\s+{\s+get\s+{\s+}\s+set\s+{\s+}\s+}";
const string emptyGet = @"\s+{\s+get\s+{\s+}\s+}";
const string emptySet = @"\s+{\s+set\s+{\s+}\s+}";
const string getSet = @"\s+{\s+get;\s+set;\s+}";
const string get = @"\s+{\s+get;\s+}";
const string set = @"\s+{\s+set;\s+}";

// https://github.com/ApiApprover/ApiApprover/issues/80
internal const string staticMarker = "static_C91E2709-C00B-4CAB-8BBC-B2B11DC75E50 ";
internal const string readonlyMarker = "readonly_79D3ED2A-0B60-4C3B-8432-941FE471A38B ";

internal static string NormalizeGeneratedCode(StringWriter writer)
{
var gennedClass = writer.ToString();

gennedClass = Regex.Replace(gennedClass, autoGeneratedHeader, string.Empty,
RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline | RegexOptions.Singleline);
gennedClass = Regex.Replace(gennedClass, emptyGetSet, " { get; set; }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, getSet, " { get; set; }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, emptyGet, " { get; }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, emptySet, " { set; }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, get, " { get; }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, set, " { set; }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, @"\s+{\s+}", " { }", RegexOptions.IgnorePatternWhitespace);
gennedClass = Regex.Replace(gennedClass, @"\)\s+;", ");", RegexOptions.IgnorePatternWhitespace);

gennedClass = gennedClass.Replace("class " + staticMarker, "static class ");
gennedClass = gennedClass.Replace("struct " + readonlyMarker, "readonly struct ");
gennedClass = Regex.Replace(gennedClass, @"\r\n|\n\r|\r|\n", Environment.NewLine);

return gennedClass;
}
}
}
2 changes: 1 addition & 1 deletion src/PublicApiGeneratorTests/Class_event_attributes.cs
@@ -1,4 +1,4 @@
using System;
using System;
using PublicApiGeneratorTests.Examples;
using Xunit;

Expand Down
6 changes: 3 additions & 3 deletions src/PublicApiGeneratorTests/Class_modifiers.cs
@@ -1,4 +1,4 @@
using PublicApiGeneratorTests.Examples;
using PublicApiGeneratorTests.Examples;
using Xunit;

namespace PublicApiGeneratorTests
Expand All @@ -24,7 +24,7 @@ public void Should_output_static_modifier()
AssertPublicApi(typeof(StaticClass),
@"namespace PublicApiGeneratorTests.Examples
{
public class static StaticClass { }
public static class StaticClass { }
}");
}

Expand Down Expand Up @@ -58,4 +58,4 @@ public sealed class SealedClass
}
}
// ReSharper restore ClassNeverInstantiated.Global
}
}
8 changes: 4 additions & 4 deletions src/PublicApiGeneratorTests/Method_extensions.cs
@@ -1,4 +1,4 @@
using PublicApiGeneratorTests.Examples;
using PublicApiGeneratorTests.Examples;
using System.Collections.Generic;
using Xunit;

Expand All @@ -13,7 +13,7 @@ public void Should_output_extension_methods()
AssertPublicApi(typeof(StringExtensions),
@"namespace PublicApiGeneratorTests.Examples
{
public class static StringExtensions
public static class StringExtensions
{
public static bool CheckLength(this string value, int length) { }
}
Expand All @@ -26,7 +26,7 @@ public void Should_output_generic_extension_methods()
AssertPublicApi(typeof(GenericExtensions),
@"namespace PublicApiGeneratorTests.Examples
{
public class static GenericExtensions
public static class GenericExtensions
{
public static PublicApiGeneratorTests.Examples.Configurator<T> Add<T>(this PublicApiGeneratorTests.Examples.Configurator<T> configurator)
where T : class { }
Expand Down Expand Up @@ -65,4 +65,4 @@ public static class GenericExtensions
}
// ReSharper restore ClassNeverInstantiated.Global
// ReSharper restore UnusedMember.Global
}
}
2 changes: 1 addition & 1 deletion src/PublicApiGeneratorTests/NullableTests.cs
Expand Up @@ -31,7 +31,7 @@ public void Should_Annotate_VoidReturn()
AssertPublicApi(typeof(VoidReturn),
@"namespace PublicApiGeneratorTests.Examples
{
public class static VoidReturn
public static class VoidReturn
{
public static void ShouldBeEquivalentTo(this object? actual, object? expected) { }
}
Expand Down
4 changes: 2 additions & 2 deletions src/PublicApiGeneratorTests/Property_visibility.cs
@@ -1,4 +1,4 @@
using PublicApiGeneratorTests.Examples;
using PublicApiGeneratorTests.Examples;
using Xunit;

namespace PublicApiGeneratorTests
Expand Down Expand Up @@ -325,4 +325,4 @@ public class ClassWithProtectedInternalGetterPublicSetter
// ReSharper restore UnusedMember.Local
// ReSharper restore UnusedMember.Global
// ReSharper restore ClassNeverInstantiated.Global
}
}
5 changes: 2 additions & 3 deletions src/PublicApiGeneratorTests/Struct_readonly.cs
@@ -1,4 +1,3 @@
using System;
using PublicApiGeneratorTests.Examples;
using Xunit;

Expand All @@ -12,7 +11,7 @@ public void Should_output()
AssertPublicApi<ReadonlyStruct>(
@"namespace PublicApiGeneratorTests.Examples
{
public struct readonly ReadonlyStruct { }
public readonly struct ReadonlyStruct { }
}");
}
}
Expand All @@ -25,4 +24,4 @@ namespace Examples
}
}
// ReSharper restore ClassNeverInstantiated.Global
}
}

0 comments on commit 82effdb

Please sign in to comment.