From 7f797b121f049df1f8b023d67583bc8e1f940ebd Mon Sep 17 00:00:00 2001 From: Olly Levett Date: Wed, 10 Oct 2018 20:25:21 +0100 Subject: [PATCH] housekeeping: tidying of build warnings in event builder --- src/EventBuilder/AutoPlatform.cs | 4 +- .../Cecil/DelegateTemplateInformation.cs | 9 +- .../Cecil/EventTemplateInformation.cs | 169 +++++++++--------- .../Cecil/StaticEventTemplateInformation.cs | 5 +- src/EventBuilder/CommandLineOptions.cs | 2 + src/EventBuilder/Entities/PublicTypeInfo.cs | 2 +- src/EventBuilder/ExitCode.cs | 22 +++ src/EventBuilder/Platforms/Android.cs | 1 - src/EventBuilder/Platforms/BasePlatform.cs | 4 +- src/EventBuilder/Platforms/IPlatform.cs | 4 +- src/EventBuilder/Platforms/Mac.cs | 2 +- src/EventBuilder/Platforms/iOS.cs | 6 +- src/EventBuilder/Platforms/tvOS.cs | 2 +- src/EventBuilder/Program.cs | 20 +-- 14 files changed, 142 insertions(+), 110 deletions(-) create mode 100644 src/EventBuilder/ExitCode.cs diff --git a/src/EventBuilder/AutoPlatform.cs b/src/EventBuilder/AutoPlatform.cs index cd3218a72f..2b8b45bedc 100644 --- a/src/EventBuilder/AutoPlatform.cs +++ b/src/EventBuilder/AutoPlatform.cs @@ -15,10 +15,12 @@ public enum AutoPlatform /// Android, +#pragma warning disable SA1300 // Element should begin with upper-case letter /// /// iOS platform. /// iOS, +#pragma warning restore SA1300 // Element should begin with upper-case letter /// /// Mac platform. @@ -60,4 +62,4 @@ public enum AutoPlatform /// Essentials } -} \ No newline at end of file +} diff --git a/src/EventBuilder/Cecil/DelegateTemplateInformation.cs b/src/EventBuilder/Cecil/DelegateTemplateInformation.cs index 0da61ba7c0..7f322e00b6 100644 --- a/src/EventBuilder/Cecil/DelegateTemplateInformation.cs +++ b/src/EventBuilder/Cecil/DelegateTemplateInformation.cs @@ -1,8 +1,9 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. using System; +using System.Globalization; using System.Linq; using EventBuilder.Entities; using Mono.Cecil; @@ -69,7 +70,11 @@ public static NamespaceInfo[] Create(AssemblyDefinition[] targetAssemblies) string.Join( ", ", z.Parameters.Select( - a => string.Format("{0} {1}", a.ParameterType.FullName, a.Name))), + a => string.Format( + CultureInfo.InvariantCulture, + "{0} {1}", + a.ParameterType.FullName, + a.Name))), ParameterTypeList = string.Join(", ", z.Parameters.Select(a => a.ParameterType.FullName)), ParameterNameList = string.Join(", ", z.Parameters.Select(a => a.Name)) diff --git a/src/EventBuilder/Cecil/EventTemplateInformation.cs b/src/EventBuilder/Cecil/EventTemplateInformation.cs index 5e189efd83..1af816d172 100644 --- a/src/EventBuilder/Cecil/EventTemplateInformation.cs +++ b/src/EventBuilder/Cecil/EventTemplateInformation.cs @@ -1,8 +1,10 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; using System.Collections.Generic; +using System.Globalization; using System.Linq; using EventBuilder.Entities; using Mono.Cecil; @@ -35,6 +37,86 @@ public static class EventTemplateInformation { "Tizen.NUI.EventHandlerWithReturnType`3", "Tizen.NUI.EventHandlerWithReturnType" }, }; + /// + /// Creates namespace information from the specified target assemblies. + /// + /// The target assemblies. + /// The namespaces in the target assemblies. + public static NamespaceInfo[] Create(AssemblyDefinition[] targetAssemblies) + { + var publicTypesWithEvents = targetAssemblies + .SelectMany(SafeTypes.GetSafeTypes) + .Where(x => x.IsPublic && !x.HasGenericParameters) + .Select(x => new { Type = x, Events = GetPublicEvents(x) }) + .Where(x => x.Events.Length > 0) + .ToArray(); + + var garbageNamespaceList = new[] + { + "Windows.UI.Xaml.Data", + "Windows.UI.Xaml.Interop", + "Windows.UI.Xaml.Input", + "MonoTouch.AudioToolbox", + "MonoMac.AudioToolbox", + "ReactiveUI.Events", + + // Winforms + "System.Collections.Specialized", + "System.Configuration", + "System.ComponentModel.Design", + "System.ComponentModel.Design.Serialization", + "System.CodeDom", + "System.Data.SqlClient", + "System.Data.OleDb", + "System.Data.Odbc", + "System.Data.Common", + "System.Drawing.Design", + "System.Media", + "System.Net", + "System.Net.Mail", + "System.Net.NetworkInformation", + "System.Net.Sockets", + "System.ServiceProcess.Design", + "System.Windows.Input", + "System.Windows.Forms.ComponentModel.Com2Interop", + "System.Windows.Forms.Design", + "System.Timers" + }; + + var namespaceData = publicTypesWithEvents + .GroupBy(x => x.Type.Namespace) + .Where(x => !garbageNamespaceList.Contains(x.Key)) + .Select(x => new NamespaceInfo + { + Name = x.Key, + Types = x.Select(y => new PublicTypeInfo + { + Name = y.Type.Name, + Type = y.Type, + Events = y.Events.Select(z => new PublicEventInfo + { + Name = z.Name, + EventHandlerType = GetRealTypeName(z.EventType), + EventArgsType = GetEventArgsTypeForEvent(z), + ObsoleteEventInfo = GetObsoleteInfo(z) + }).ToArray() + }).ToArray() + }).ToArray(); + + foreach (var type in namespaceData.SelectMany(x => x.Types)) + { + var parentWithEvents = GetParents(type.Type).FirstOrDefault(x => GetPublicEvents(x).Any()); + if (parentWithEvents == null) + { + continue; + } + + type.Parent = new ParentInfo { Name = parentWithEvents.FullName }; + } + + return namespaceData; + } + private static string RenameBogusTypes(string typeName) { if (SubstitutionList.ContainsKey(typeName)) @@ -88,6 +170,7 @@ private static string GetRealTypeName(TypeDefinition t) } var ret = string.Format( + CultureInfo.InvariantCulture, "{0}<{1}>", RenameBogusTypes(t.Namespace + "." + t.Name), string.Join(",", t.GenericParameters.Select(x => GetRealTypeName(x.Resolve())))); @@ -105,6 +188,7 @@ private static string GetRealTypeName(TypeReference t) } var ret = string.Format( + CultureInfo.InvariantCulture, "{0}<{1}>", RenameBogusTypes(generic.Namespace + "." + generic.Name), string.Join(",", generic.GenericArguments.Select(GetRealTypeName))); @@ -123,7 +207,8 @@ private static string GetRealTypeName(TypeReference t) private static ObsoleteEventInfo GetObsoleteInfo(EventDefinition ei) { - var obsoleteAttribute = ei.CustomAttributes.FirstOrDefault(attr => attr.AttributeType.FullName.Equals("System.ObsoleteAttribute")); + var obsoleteAttribute = ei.CustomAttributes + .FirstOrDefault(attr => attr.AttributeType.FullName.Equals("System.ObsoleteAttribute", StringComparison.InvariantCulture)); if (obsoleteAttribute == null) { @@ -144,86 +229,6 @@ private static EventDefinition[] GetPublicEvents(TypeDefinition t) .ToArray(); } - /// - /// Creates namespace information from the specified target assemblies. - /// - /// The target assemblies. - /// The namespaces in the target assemblies. - public static NamespaceInfo[] Create(AssemblyDefinition[] targetAssemblies) - { - var publicTypesWithEvents = targetAssemblies - .SelectMany(SafeTypes.GetSafeTypes) - .Where(x => x.IsPublic && !x.HasGenericParameters) - .Select(x => new { Type = x, Events = GetPublicEvents(x) }) - .Where(x => x.Events.Length > 0) - .ToArray(); - - var garbageNamespaceList = new[] - { - "Windows.UI.Xaml.Data", - "Windows.UI.Xaml.Interop", - "Windows.UI.Xaml.Input", - "MonoTouch.AudioToolbox", - "MonoMac.AudioToolbox", - "ReactiveUI.Events", - - // Winforms - "System.Collections.Specialized", - "System.Configuration", - "System.ComponentModel.Design", - "System.ComponentModel.Design.Serialization", - "System.CodeDom", - "System.Data.SqlClient", - "System.Data.OleDb", - "System.Data.Odbc", - "System.Data.Common", - "System.Drawing.Design", - "System.Media", - "System.Net", - "System.Net.Mail", - "System.Net.NetworkInformation", - "System.Net.Sockets", - "System.ServiceProcess.Design", - "System.Windows.Input", - "System.Windows.Forms.ComponentModel.Com2Interop", - "System.Windows.Forms.Design", - "System.Timers" - }; - - var namespaceData = publicTypesWithEvents - .GroupBy(x => x.Type.Namespace) - .Where(x => !garbageNamespaceList.Contains(x.Key)) - .Select(x => new NamespaceInfo - { - Name = x.Key, - Types = x.Select(y => new PublicTypeInfo - { - Name = y.Type.Name, - Type = y.Type, - Events = y.Events.Select(z => new PublicEventInfo - { - Name = z.Name, - EventHandlerType = GetRealTypeName(z.EventType), - EventArgsType = GetEventArgsTypeForEvent(z), - ObsoleteEventInfo = GetObsoleteInfo(z) - }).ToArray() - }).ToArray() - }).ToArray(); - - foreach (var type in namespaceData.SelectMany(x => x.Types)) - { - var parentWithEvents = GetParents(type.Type).FirstOrDefault(x => GetPublicEvents(x).Any()); - if (parentWithEvents == null) - { - continue; - } - - type.Parent = new ParentInfo { Name = parentWithEvents.FullName }; - } - - return namespaceData; - } - private static IEnumerable GetParents(TypeDefinition type) { var current = type.BaseType != null diff --git a/src/EventBuilder/Cecil/StaticEventTemplateInformation.cs b/src/EventBuilder/Cecil/StaticEventTemplateInformation.cs index 25d0a4f70b..fbb59abf42 100644 --- a/src/EventBuilder/Cecil/StaticEventTemplateInformation.cs +++ b/src/EventBuilder/Cecil/StaticEventTemplateInformation.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System.Collections.Generic; +using System.Globalization; using System.Linq; using EventBuilder.Entities; using Mono.Cecil; @@ -116,6 +117,7 @@ private static string GetRealTypeName(TypeDefinition t) } var ret = string.Format( + CultureInfo.InvariantCulture, "{0}<{1}>", t.Namespace + "." + t.Name, string.Join(",", t.GenericParameters.Select(x => GetRealTypeName(x.Resolve())))); @@ -133,6 +135,7 @@ private static string GetRealTypeName(TypeReference t) } var ret = string.Format( + CultureInfo.InvariantCulture, "{0}<{1}>", generic.Namespace + "." + generic.Name, string.Join(",", generic.GenericArguments.Select(GetRealTypeName))); diff --git a/src/EventBuilder/CommandLineOptions.cs b/src/EventBuilder/CommandLineOptions.cs index 99772c9f78..a66248394a 100644 --- a/src/EventBuilder/CommandLineOptions.cs +++ b/src/EventBuilder/CommandLineOptions.cs @@ -42,7 +42,9 @@ public class CommandLineOptions /// Manual generation using the specified assemblies. Use with --platform=NONE. /// [ValueList(typeof(List))] +#pragma warning disable CA2227 // Collection properties should be read only public List Assemblies { get; set; } +#pragma warning restore CA2227 // Collection properties should be read only /// /// Gets the usage. diff --git a/src/EventBuilder/Entities/PublicTypeInfo.cs b/src/EventBuilder/Entities/PublicTypeInfo.cs index f7bcf91708..ecd6a8f6bf 100644 --- a/src/EventBuilder/Entities/PublicTypeInfo.cs +++ b/src/EventBuilder/Entities/PublicTypeInfo.cs @@ -50,6 +50,6 @@ public class PublicTypeInfo /// /// Gets or sets the multi parameter methods. /// - public MultiParameterMethod[] MultiParameterMethods { get; set; } + public IEnumerable MultiParameterMethods { get; set; } } } diff --git a/src/EventBuilder/ExitCode.cs b/src/EventBuilder/ExitCode.cs new file mode 100644 index 0000000000..eb1117eca9 --- /dev/null +++ b/src/EventBuilder/ExitCode.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +namespace EventBuilder +{ + /// + /// The exit/return code (aka %ERRORLEVEL%) on application exit. + /// + public enum ExitCode + { + /// + /// Success + /// + Success = 0, + + /// + /// Error + /// + Error = 1, + } +} diff --git a/src/EventBuilder/Platforms/Android.cs b/src/EventBuilder/Platforms/Android.cs index d51f68aa08..1f9bff0768 100644 --- a/src/EventBuilder/Platforms/Android.cs +++ b/src/EventBuilder/Platforms/Android.cs @@ -37,7 +37,6 @@ public Android(string referenceAssembliesLocation) } else { - var assemblies = Directory.GetFiles( Path.Combine(referenceAssembliesLocation, "MonoAndroid"), diff --git a/src/EventBuilder/Platforms/BasePlatform.cs b/src/EventBuilder/Platforms/BasePlatform.cs index 9b6864bee0..a30021275a 100644 --- a/src/EventBuilder/Platforms/BasePlatform.cs +++ b/src/EventBuilder/Platforms/BasePlatform.cs @@ -25,9 +25,9 @@ protected BasePlatform() public abstract AutoPlatform Platform { get; } /// - public List Assemblies { get; set; } + public List Assemblies { get; } /// - public List CecilSearchDirectories { get; set; } + public List CecilSearchDirectories { get; } } } diff --git a/src/EventBuilder/Platforms/IPlatform.cs b/src/EventBuilder/Platforms/IPlatform.cs index 2193a28963..9c9955704c 100644 --- a/src/EventBuilder/Platforms/IPlatform.cs +++ b/src/EventBuilder/Platforms/IPlatform.cs @@ -19,12 +19,12 @@ public interface IPlatform /// /// Gets or sets the assemblies. /// - List Assemblies { get; set; } + List Assemblies { get; } /// /// Gets or sets the cecil search directories. /// Cecil when run on Mono needs some direction as to the location of the platform specific MSCORLIB. /// - List CecilSearchDirectories { get; set; } + List CecilSearchDirectories { get; } } } diff --git a/src/EventBuilder/Platforms/Mac.cs b/src/EventBuilder/Platforms/Mac.cs index f34aad8778..564d050602 100644 --- a/src/EventBuilder/Platforms/Mac.cs +++ b/src/EventBuilder/Platforms/Mac.cs @@ -7,11 +7,11 @@ namespace EventBuilder.Platforms { - // ReSharper disable once InconsistentNaming /// /// Mac platform assemblies and events. /// /// + // ReSharper disable once InconsistentNaming public class Mac : BasePlatform { /// diff --git a/src/EventBuilder/Platforms/iOS.cs b/src/EventBuilder/Platforms/iOS.cs index cba87f42eb..4ebc76e600 100644 --- a/src/EventBuilder/Platforms/iOS.cs +++ b/src/EventBuilder/Platforms/iOS.cs @@ -1,4 +1,4 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. @@ -13,7 +13,11 @@ namespace EventBuilder.Platforms /// /// // ReSharper disable once InconsistentNaming +#pragma warning disable IDE1006 // Naming Styles +#pragma warning disable SA1300 // Element should begin with upper-case letter public class iOS : BasePlatform +#pragma warning restore SA1300 // Element should begin with upper-case letter +#pragma warning restore IDE1006 // Naming Styles { /// /// Initializes a new instance of the class. diff --git a/src/EventBuilder/Platforms/tvOS.cs b/src/EventBuilder/Platforms/tvOS.cs index e6d4964124..f3138c9a54 100644 --- a/src/EventBuilder/Platforms/tvOS.cs +++ b/src/EventBuilder/Platforms/tvOS.cs @@ -7,11 +7,11 @@ namespace EventBuilder.Platforms { - // ReSharper disable once InconsistentNaming /// /// TV OS platform assemblies and events. /// /// + // ReSharper disable once InconsistentNaming public class TVOS : BasePlatform { /// diff --git a/src/EventBuilder/Program.cs b/src/EventBuilder/Program.cs index 580d7a2dc1..d93ae0b10e 100644 --- a/src/EventBuilder/Program.cs +++ b/src/EventBuilder/Program.cs @@ -16,21 +16,12 @@ namespace EventBuilder { - internal class Program + internal static class Program { - /// - /// The exit/return code (aka %ERRORLEVEL%) on application exit. - /// - public enum ExitCode - { - Success = 0, - Error = 1 - } - private static string _mustacheTemplate = "DefaultTemplate.mustache"; private static string _referenceAssembliesLocation = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework"; - private static void Main(string[] args) + public static void Main(string[] args) { Log.Logger = new LoggerConfiguration() .ReadFrom.AppSettings() @@ -79,12 +70,11 @@ private static void Main(string[] args) } platform = new Bespoke(); - platform.Assemblies = options.Assemblies; + platform.Assemblies.AddRange(options.Assemblies); if (PlatformHelper.IsRunningOnMono()) { - platform.CecilSearchDirectories = - platform.Assemblies.Select(Path.GetDirectoryName).Distinct().ToList(); + platform.CecilSearchDirectories.AddRange(platform.Assemblies.Select(Path.GetDirectoryName).Distinct().ToList()); } else { @@ -135,7 +125,7 @@ private static void Main(string[] args) break; default: - throw new ArgumentOutOfRangeException(); + throw new ArgumentException($"Platform not {options.Platform} supported"); } ExtractEventsFromAssemblies(platform);