From 885dd8b8ac7225f0ba36d598cee482f5c9a3edb4 Mon Sep 17 00:00:00 2001 From: Kasper Kiiskinen Date: Thu, 12 Oct 2017 10:16:55 +0300 Subject: [PATCH 1/4] Updated DiagnosticDescriptors. --- .../ConfigurationFileGenerator.cs | 6 +- .../DiagnosticDescriptors.cs | 216 +++++------------- 2 files changed, 56 insertions(+), 166 deletions(-) diff --git a/UnityEngineAnalyzer.CLI/ConfigurationFileGenerator.cs b/UnityEngineAnalyzer.CLI/ConfigurationFileGenerator.cs index b8e0ee5..8f0906d 100644 --- a/UnityEngineAnalyzer.CLI/ConfigurationFileGenerator.cs +++ b/UnityEngineAnalyzer.CLI/ConfigurationFileGenerator.cs @@ -18,11 +18,11 @@ public void GenerateConfigurationFile() var rootJson = new JObject(); - foreach (var type in allTypes) + foreach (var typeInfo in allTypes) { - if (type.BaseType == typeof(DiagnosticAnalyzer)) + if (typeInfo.BaseType == typeof(DiagnosticAnalyzer)) { - rootJson.Add(new JProperty(type.Name, true)); + rootJson.Add(new JProperty(typeInfo.Name, true)); //TODO SupportedDiagnostics.IsEnabledByDefault; } } diff --git a/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs b/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs index b17296f..d4fa7b5 100644 --- a/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs +++ b/UnityEngineAnalyzer/UnityEngineAnalyzer/DiagnosticDescriptors.cs @@ -1,4 +1,5 @@ using Microsoft.CodeAnalysis; +using System.Resources; using UnityEngineAnalyzer.Animator; using UnityEngineAnalyzer.AOT; using UnityEngineAnalyzer.Camera; @@ -17,168 +18,57 @@ namespace UnityEngineAnalyzer { static class DiagnosticDescriptors { - //NOTES: Naming of Descriptors are a bit inconsistant - //NOTES: The Resource Reading code seems repetative - - public static readonly DiagnosticDescriptor DoNotUseOnGUI = new DiagnosticDescriptor( - id: DiagnosticIDs.DoNotUseOnGUI, - title: new LocalizableResourceString(nameof(DoNotUseOnGUIResources.Title), DoNotUseOnGUIResources.ResourceManager, typeof(DoNotUseOnGUIResources)), - messageFormat: new LocalizableResourceString(nameof(DoNotUseOnGUIResources.MessageFormat), DoNotUseOnGUIResources.ResourceManager, typeof(DoNotUseOnGUIResources)), - category: DiagnosticCategories.GC, - defaultSeverity: DiagnosticSeverity.Info, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(DoNotUseOnGUIResources.Description), DoNotUseOnGUIResources.ResourceManager, typeof(DoNotUseOnGUIResources))); - - public static readonly DiagnosticDescriptor DoNotUseStringMethods = new DiagnosticDescriptor( - id: DiagnosticIDs.DoNotUseStringMethods, - title: new LocalizableResourceString(nameof(DoNotUseStringMethodsResources.Title), DoNotUseStringMethodsResources.ResourceManager, typeof(DoNotUseStringMethodsResources)), - messageFormat: new LocalizableResourceString(nameof(DoNotUseStringMethodsResources.MessageFormat), DoNotUseStringMethodsResources.ResourceManager, typeof(DoNotUseStringMethodsResources)), - category: DiagnosticCategories.StringMethods, - defaultSeverity: DiagnosticSeverity.Info, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(DoNotUseStringMethodsResources.Description), DoNotUseStringMethodsResources.ResourceManager, typeof(DoNotUseStringMethodsResources))); - - public static readonly DiagnosticDescriptor DoNotUseCoroutines = new DiagnosticDescriptor( - id: DiagnosticIDs.DoNotUseCoroutines, - title: new LocalizableResourceString(nameof(DoNotUseCoroutinesResources.Title), DoNotUseCoroutinesResources.ResourceManager, typeof(DoNotUseCoroutinesResources)), - messageFormat: new LocalizableResourceString(nameof(DoNotUseCoroutinesResources.MessageFormat), DoNotUseCoroutinesResources.ResourceManager, typeof(DoNotUseCoroutinesResources)), - category: DiagnosticCategories.GC, - defaultSeverity: DiagnosticSeverity.Info, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(DoNotUseCoroutinesResources.Description), DoNotUseCoroutinesResources.ResourceManager, typeof(DoNotUseCoroutinesResources))); - - - public static readonly DiagnosticDescriptor EmptyMonoBehaviourMethod = new DiagnosticDescriptor( - id: DiagnosticIDs.EmptyMonoBehaviourMethod, - title: new LocalizableResourceString(nameof(EmptyMonoBehaviourMethodsResources.Title), EmptyMonoBehaviourMethodsResources.ResourceManager, typeof(EmptyMonoBehaviourMethodsResources)), - messageFormat: new LocalizableResourceString(nameof(EmptyMonoBehaviourMethodsResources.MessageFormat), EmptyMonoBehaviourMethodsResources.ResourceManager, typeof(EmptyMonoBehaviourMethodsResources)), - category: DiagnosticCategories.Miscellaneous, - defaultSeverity: DiagnosticSeverity.Warning, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(EmptyMonoBehaviourMethodsResources.Description), EmptyMonoBehaviourMethodsResources.ResourceManager, typeof(EmptyMonoBehaviourMethodsResources))); - - public static readonly DiagnosticDescriptor UseCompareTag = new DiagnosticDescriptor( - id: DiagnosticIDs.UseCompareTag, - title: new LocalizableResourceString(nameof(UseCompareTagResources.Title), UseCompareTagResources.ResourceManager, typeof(UseCompareTagResources)), - messageFormat: new LocalizableResourceString(nameof(UseCompareTagResources.MessageFormat), UseCompareTagResources.ResourceManager, typeof(UseCompareTagResources)), - category: DiagnosticCategories.GC, - defaultSeverity: DiagnosticSeverity.Warning, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(UseCompareTagResources.Description), UseCompareTagResources.ResourceManager, typeof(UseCompareTagResources))); - - public static readonly DiagnosticDescriptor DoNotUseFindMethodsInUpdate = new DiagnosticDescriptor( - id: DiagnosticIDs.DoNotUseFindMethodsInUpdate, - title: new LocalizableResourceString(nameof(DoNotUseFindMethodsInUpdateResources.Title), DoNotUseFindMethodsInUpdateResources.ResourceManager, typeof(DoNotUseFindMethodsInUpdateResources)), - messageFormat: new LocalizableResourceString(nameof(DoNotUseFindMethodsInUpdateResources.MessageFormat), DoNotUseFindMethodsInUpdateResources.ResourceManager, typeof(DoNotUseFindMethodsInUpdateResources)), - category: DiagnosticCategories.Performance, - defaultSeverity: DiagnosticSeverity.Warning, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(DoNotUseFindMethodsInUpdateResources.Description), DoNotUseFindMethodsInUpdateResources.ResourceManager, typeof(DoNotUseFindMethodsInUpdateResources))); - - public static readonly DiagnosticDescriptor DoNotUseFindMethodsInUpdateRecursive = new DiagnosticDescriptor( - id: DiagnosticIDs.DoNotUseFindMethodsInUpdate, - title: new LocalizableResourceString(nameof(DoNotUseFindMethodsInUpdateResources.Title), DoNotUseFindMethodsInUpdateResources.ResourceManager, typeof(DoNotUseFindMethodsInUpdateResources)), - messageFormat: new LocalizableResourceString(nameof(DoNotUseFindMethodsInUpdateResources.MessageFormatRecursive), DoNotUseFindMethodsInUpdateResources.ResourceManager, typeof(DoNotUseFindMethodsInUpdateResources)), - category: DiagnosticCategories.Performance, - defaultSeverity: DiagnosticSeverity.Warning, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(DoNotUseFindMethodsInUpdateResources.Description), DoNotUseFindMethodsInUpdateResources.ResourceManager, typeof(DoNotUseFindMethodsInUpdateResources))); - - public static readonly DiagnosticDescriptor DoNotUseRemoting = new DiagnosticDescriptor( - id: DiagnosticIDs.DoNotUseRemoting, - title: new LocalizableResourceString(nameof(DoNotUseRemotingResources.Title), DoNotUseRemotingResources.ResourceManager, typeof(DoNotUseRemotingResources)), - messageFormat: new LocalizableResourceString(nameof(DoNotUseRemotingResources.MessageFormat), DoNotUseRemotingResources.ResourceManager, typeof(DoNotUseRemotingResources)), - category: DiagnosticCategories.AOT, - defaultSeverity: DiagnosticSeverity.Info, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(DoNotUseRemotingResources.Description), DoNotUseRemotingResources.ResourceManager, typeof(DoNotUseRemotingResources))); - - public static readonly DiagnosticDescriptor DoNotUseReflectionEmit = new DiagnosticDescriptor( - id: DiagnosticIDs.DoNotUseReflectionEmit, - title: new LocalizableResourceString(nameof(DoNotUseReflectionEmitResources.Title), DoNotUseReflectionEmitResources.ResourceManager, typeof(DoNotUseReflectionEmitResources)), - messageFormat: new LocalizableResourceString(nameof(DoNotUseReflectionEmitResources.MessageFormat), DoNotUseReflectionEmitResources.ResourceManager, typeof(DoNotUseReflectionEmitResources)), - category: DiagnosticCategories.AOT, - defaultSeverity: DiagnosticSeverity.Info, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(DoNotUseReflectionEmitResources.Description), DoNotUseReflectionEmitResources.ResourceManager, typeof(DoNotUseReflectionEmitResources))); - - public static readonly DiagnosticDescriptor TypeGetType = new DiagnosticDescriptor( - id: DiagnosticIDs.TypeGetType, - title: new LocalizableResourceString(nameof(TypeGetTypeResources.Title), TypeGetTypeResources.ResourceManager, typeof(TypeGetTypeResources)), - messageFormat: new LocalizableResourceString(nameof(TypeGetTypeResources.MessageFormat), TypeGetTypeResources.ResourceManager, typeof(TypeGetTypeResources)), - category: DiagnosticCategories.AOT, - defaultSeverity: DiagnosticSeverity.Info, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(TypeGetTypeResources.Description), TypeGetTypeResources.ResourceManager, typeof(TypeGetTypeResources))); - - public static readonly DiagnosticDescriptor DoNotUseForEachInUpdate = new DiagnosticDescriptor( - id: DiagnosticIDs.DoNotUseForEachInUpdate, - title: new LocalizableResourceString(nameof(DoNotUseForEachInUpdateResources.Title), DoNotUseForEachInUpdateResources.ResourceManager, typeof(DoNotUseForEachInUpdateResources)), - messageFormat: new LocalizableResourceString(nameof(DoNotUseForEachInUpdateResources.MessageFormat), DoNotUseForEachInUpdateResources.ResourceManager, typeof(DoNotUseForEachInUpdateResources)), - category: DiagnosticCategories.Performance, - defaultSeverity: DiagnosticSeverity.Warning, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(DoNotUseForEachInUpdateResources.Description), DoNotUseForEachInUpdateResources.ResourceManager, typeof(DoNotUseForEachInUpdateResources)) - ); - - public static readonly DiagnosticDescriptor UnsealedDerivedClass = new DiagnosticDescriptor( - id: DiagnosticIDs.UnsealedDerivedClass, - title: new LocalizableResourceString(nameof(UnsealedDerivedClassResources.Title), UnsealedDerivedClassResources.ResourceManager, typeof(UnsealedDerivedClassResources)), - messageFormat: new LocalizableResourceString(nameof(UnsealedDerivedClassResources.MessageFormat), UnsealedDerivedClassResources.ResourceManager, typeof(UnsealedDerivedClassResources)), - category: DiagnosticCategories.Performance, - defaultSeverity: DiagnosticSeverity.Warning, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(UnsealedDerivedClassResources.Description), UnsealedDerivedClassResources.ResourceManager, typeof(UnsealedDerivedClassResources)) - ); - - public static readonly DiagnosticDescriptor InvokeFunctionMissing = new DiagnosticDescriptor( - id: DiagnosticIDs.InvokeFunctionMissing, - title: new LocalizableResourceString(nameof(InvokeFunctionMissingResources.Title), InvokeFunctionMissingResources.ResourceManager, typeof(InvokeFunctionMissingResources)), - messageFormat: new LocalizableResourceString(nameof(InvokeFunctionMissingResources.MessageFormat), InvokeFunctionMissingResources.ResourceManager, typeof(InvokeFunctionMissingResources)), - category: DiagnosticCategories.Performance, - defaultSeverity: DiagnosticSeverity.Warning, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(InvokeFunctionMissingResources.Description), InvokeFunctionMissingResources.ResourceManager, typeof(InvokeFunctionMissingResources)) - ); - - public static readonly DiagnosticDescriptor DoNotUseStateName = new DiagnosticDescriptor( - id: DiagnosticIDs.DoNotUseStateNameInAnimator, - title: new LocalizableResourceString(nameof(DoNotUseStateNameResource.Title), DoNotUseStateNameResource.ResourceManager, typeof(DoNotUseStateNameResource)), - messageFormat: new LocalizableResourceString(nameof(DoNotUseStateNameResource.MessageFormat), DoNotUseStateNameResource.ResourceManager, typeof(DoNotUseStateNameResource)), - category: DiagnosticCategories.Performance, - defaultSeverity: DiagnosticSeverity.Warning, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(DoNotUseStateNameResource.Description), DoNotUseStateNameResource.ResourceManager, typeof(DoNotUseStateNameResource)) - ); - - public static readonly DiagnosticDescriptor CameraMainIsSlow = new DiagnosticDescriptor( - id: DiagnosticIDs.CameraMainIsSlow, - title: new LocalizableResourceString(nameof(CameraMainResource.Title), CameraMainResource.ResourceManager, typeof(CameraMainResource)), - messageFormat: new LocalizableResourceString(nameof(CameraMainResource.MessageFormat), CameraMainResource.ResourceManager, typeof(CameraMainResource)), - category: DiagnosticCategories.Performance, - defaultSeverity: DiagnosticSeverity.Warning, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(CameraMainResource.Description), CameraMainResource.ResourceManager, typeof(CameraMainResource)) - ); - - public static readonly DiagnosticDescriptor DoNotUseStringPropertyNames = new DiagnosticDescriptor( - id: DiagnosticIDs.DoNotUseStringPropertyNamesInMaterial, - title: new LocalizableResourceString(nameof(DoNotUseStringPropertyNamesResource.Title), DoNotUseStringPropertyNamesResource.ResourceManager, typeof(DoNotUseStringPropertyNamesResource)), - messageFormat: new LocalizableResourceString(nameof(DoNotUseStringPropertyNamesResource.MessageFormat), DoNotUseStringPropertyNamesResource.ResourceManager, typeof(DoNotUseStringPropertyNamesResource)), - category: DiagnosticCategories.Performance, - defaultSeverity: DiagnosticSeverity.Warning, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(DoNotUseStringPropertyNamesResource.Description), DoNotUseStringPropertyNamesResource.ResourceManager, typeof(DoNotUseStringPropertyNamesResource)) - ); - - public static readonly DiagnosticDescriptor UseNonAllocMethods = new DiagnosticDescriptor( - id: DiagnosticIDs.PhysicsUseNonAllocMethods, - title: new LocalizableResourceString(nameof(UseNonAllocMethodsResources.Title), UseNonAllocMethodsResources.ResourceManager, typeof(UseNonAllocMethodsResources)), - messageFormat: new LocalizableResourceString(nameof(UseNonAllocMethodsResources.MessageFormat), UseNonAllocMethodsResources.ResourceManager, typeof(UseNonAllocMethodsResources)), - category: DiagnosticCategories.GC, - defaultSeverity: DiagnosticSeverity.Warning, - isEnabledByDefault: true, - description: new LocalizableResourceString(nameof(UseNonAllocMethodsResources.Description), UseNonAllocMethodsResources.ResourceManager, typeof(UseNonAllocMethodsResources)) - ); + public static readonly DiagnosticDescriptor DoNotUseOnGUI; + public static readonly DiagnosticDescriptor DoNotUseStringMethods; + public static readonly DiagnosticDescriptor DoNotUseCoroutines; + public static readonly DiagnosticDescriptor EmptyMonoBehaviourMethod; + public static readonly DiagnosticDescriptor UseCompareTag; + public static readonly DiagnosticDescriptor DoNotUseFindMethodsInUpdate; + public static readonly DiagnosticDescriptor DoNotUseFindMethodsInUpdateRecursive; + public static readonly DiagnosticDescriptor DoNotUseRemoting; + public static readonly DiagnosticDescriptor DoNotUseReflectionEmit; + public static readonly DiagnosticDescriptor TypeGetType; + public static readonly DiagnosticDescriptor DoNotUseForEachInUpdate; + public static readonly DiagnosticDescriptor UnsealedDerivedClass; + public static readonly DiagnosticDescriptor InvokeFunctionMissing; + public static readonly DiagnosticDescriptor DoNotUseStateName; + public static readonly DiagnosticDescriptor DoNotUseStringPropertyNames; + public static readonly DiagnosticDescriptor UseNonAllocMethods; + public static readonly DiagnosticDescriptor CameraMainIsSlow; + + static DiagnosticDescriptors() + { + DoNotUseOnGUI = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseOnGUI, DiagnosticCategories.GC, DiagnosticSeverity.Info); + DoNotUseStringMethods = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseStringMethods, DiagnosticCategories.GC, DiagnosticSeverity.Info); + DoNotUseCoroutines = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseCoroutines, DiagnosticCategories.GC, DiagnosticSeverity.Info); + EmptyMonoBehaviourMethod = CreateDiagnosticDescriptor(DiagnosticIDs.EmptyMonoBehaviourMethod, DiagnosticCategories.Miscellaneous, DiagnosticSeverity.Warning); + UseCompareTag = CreateDiagnosticDescriptor(DiagnosticIDs.UseCompareTag, DiagnosticCategories.GC, DiagnosticSeverity.Warning); + DoNotUseFindMethodsInUpdate = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseFindMethodsInUpdate, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); + DoNotUseFindMethodsInUpdateRecursive = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseFindMethodsInUpdate, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); + DoNotUseRemoting = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseRemoting, DiagnosticCategories.AOT, DiagnosticSeverity.Info); + DoNotUseReflectionEmit = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseReflectionEmit, DiagnosticCategories.AOT, DiagnosticSeverity.Info); + TypeGetType = CreateDiagnosticDescriptor(DiagnosticIDs.TypeGetType, DiagnosticCategories.AOT, DiagnosticSeverity.Info); + DoNotUseForEachInUpdate = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseForEachInUpdate, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); + UnsealedDerivedClass = CreateDiagnosticDescriptor(DiagnosticIDs.UnsealedDerivedClass, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); + InvokeFunctionMissing = CreateDiagnosticDescriptor(DiagnosticIDs.InvokeFunctionMissing, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); + DoNotUseStateName = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseStateNameInAnimator, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); + DoNotUseStringPropertyNames = CreateDiagnosticDescriptor(DiagnosticIDs.DoNotUseStringPropertyNamesInMaterial, DiagnosticCategories.Performance, DiagnosticSeverity.Warning); + UseNonAllocMethods = CreateDiagnosticDescriptor(DiagnosticIDs.PhysicsUseNonAllocMethods, DiagnosticCategories.GC, DiagnosticSeverity.Warning); + CameraMainIsSlow = CreateDiagnosticDescriptor(DiagnosticIDs.CameraMainIsSlow, DiagnosticCategories.GC, DiagnosticSeverity.Warning); + } + + private static DiagnosticDescriptor CreateDiagnosticDescriptor(string id, string category, DiagnosticSeverity severity, bool isEnabledByDefault = true) + { + var resourceManager = new ResourceManager(typeof(T)); + + return new DiagnosticDescriptor( + id: id, + title: new LocalizableResourceString("Title", resourceManager, typeof(T)), + messageFormat: new LocalizableResourceString("MessageFormat", resourceManager, typeof(T)), + category: category, + defaultSeverity: severity, + isEnabledByDefault: isEnabledByDefault, + description: new LocalizableResourceString("Description", resourceManager, typeof(T))); + } } } From 4b8f846aed6f04ad25c3eb60c7df9d288bac03b3 Mon Sep 17 00:00:00 2001 From: Kasper Kiiskinen Date: Mon, 16 Oct 2017 10:27:08 +0300 Subject: [PATCH 2/4] Added command line parser. --- UnityEngineAnalyzer.CLI/AnalyzerReport.cs | 5 +++ UnityEngineAnalyzer.CLI/Options.cs | 17 +++++++++ UnityEngineAnalyzer.CLI/Program.cs | 35 ++++++++++--------- .../Reporting/DiagnosticInfo.cs | 2 -- .../UnityEngineAnalyzer.CLI.csproj | 4 +++ UnityEngineAnalyzer.CLI/packages.config | 1 + readme.md | 8 +++-- 7 files changed, 51 insertions(+), 21 deletions(-) create mode 100644 UnityEngineAnalyzer.CLI/Options.cs diff --git a/UnityEngineAnalyzer.CLI/AnalyzerReport.cs b/UnityEngineAnalyzer.CLI/AnalyzerReport.cs index 4444837..815472c 100644 --- a/UnityEngineAnalyzer.CLI/AnalyzerReport.cs +++ b/UnityEngineAnalyzer.CLI/AnalyzerReport.cs @@ -17,6 +17,11 @@ public void AddExporter(IAnalyzerExporter exporter) _exporters.Add(exporter); } + public int GetExporterCount() + { + return _exporters.Count; + } + public void AppendDiagnostics(IEnumerable diagnosticResults) { if (_exporters.Count == 0) diff --git a/UnityEngineAnalyzer.CLI/Options.cs b/UnityEngineAnalyzer.CLI/Options.cs new file mode 100644 index 0000000..f885f47 --- /dev/null +++ b/UnityEngineAnalyzer.CLI/Options.cs @@ -0,0 +1,17 @@ +using CommandLine; +using System.Collections.Generic; + +namespace UnityEngineAnalyzer.CLI +{ + internal class Options + { + [ValueOption(0)] + public string ProjectFile { get; set; } + + [Option('e', "exporter", HelpText = "Exporters to be used.")] + public IEnumerable Exporters { get; set; } + + [Option('c', "configuration", HelpText = "Custom json configuration to be used.")] + public string ConfigurationFile { get; set; } + } +} \ No newline at end of file diff --git a/UnityEngineAnalyzer.CLI/Program.cs b/UnityEngineAnalyzer.CLI/Program.cs index 402967d..070268d 100644 --- a/UnityEngineAnalyzer.CLI/Program.cs +++ b/UnityEngineAnalyzer.CLI/Program.cs @@ -17,52 +17,55 @@ static Program() AvailableExporters.Add(nameof(ConsoleAnalyzerExporter), typeof(ConsoleAnalyzerExporter)); } - public static void Main(string[] args) { try { - //TODO: Use a proper parser for the commands + var options = new Options(); + var isValid = CommandLine.Parser.Default.ParseArgumentsStrict(args, options); - if (args.Length <= 0) + if (isValid == false || options.ProjectFile == null) { return; } var startTime = DateTime.Now; - var fileName = args[0]; + var fileName = options.ProjectFile; var fileInfo = new FileInfo(fileName); //NOTE: This could be configurable via the CLI at some point var report = new AnalyzerReport(); - int optinalArgumentIndex = 1; - if (args.Length > optinalArgumentIndex && AvailableExporters.ContainsKey(args[optinalArgumentIndex])) + if (options.Exporters != null) { - var exporterInstance = Activator.CreateInstance(AvailableExporters[args[optinalArgumentIndex]]); - report.AddExporter(exporterInstance as IAnalyzerExporter); - - optinalArgumentIndex++; + foreach (var exporter in options.Exporters) + { + if (AvailableExporters.ContainsKey(exporter)) + { + var exporterInstance = Activator.CreateInstance(AvailableExporters[exporter]); + report.AddExporter(exporterInstance as IAnalyzerExporter); + } + } } - else - { + + if (report.GetExporterCount() == 0) + { //It's generally a good idea to make sure that the Console Exporter is last since it is interactive report.AddExporter(new JsonAnalyzerExporter()); report.AddExporter(new ConsoleAnalyzerExporter()); } - report.InitializeReport(fileInfo); var tasks = new List(); if (fileInfo.Exists) { FileInfo configFileInfo = null; - if (args.Length > optinalArgumentIndex) + + if (options.ConfigurationFile != null) { - var configFileName = args[optinalArgumentIndex]; - configFileInfo = new FileInfo(configFileName); + configFileInfo = new FileInfo(options.ConfigurationFile); } var solutionAnalyzer = new SolutionAnalyzer(); diff --git a/UnityEngineAnalyzer.CLI/Reporting/DiagnosticInfo.cs b/UnityEngineAnalyzer.CLI/Reporting/DiagnosticInfo.cs index c2fd93e..88cedd0 100644 --- a/UnityEngineAnalyzer.CLI/Reporting/DiagnosticInfo.cs +++ b/UnityEngineAnalyzer.CLI/Reporting/DiagnosticInfo.cs @@ -1,7 +1,5 @@ namespace UnityEngineAnalyzer.CLI.Reporting { - - public class DiagnosticInfo { //TODO: Rename this to something like AnalysisResult diff --git a/UnityEngineAnalyzer.CLI/UnityEngineAnalyzer.CLI.csproj b/UnityEngineAnalyzer.CLI/UnityEngineAnalyzer.CLI.csproj index 30bf4a9..c9fe854 100644 --- a/UnityEngineAnalyzer.CLI/UnityEngineAnalyzer.CLI.csproj +++ b/UnityEngineAnalyzer.CLI/UnityEngineAnalyzer.CLI.csproj @@ -33,6 +33,9 @@ 4 + + ..\packages\CommandLineParser.1.9.71\lib\net45\CommandLine.dll + ..\packages\Microsoft.CodeAnalysis.Common.1.3.2\lib\net45\Microsoft.CodeAnalysis.dll True @@ -105,6 +108,7 @@ + diff --git a/UnityEngineAnalyzer.CLI/packages.config b/UnityEngineAnalyzer.CLI/packages.config index 5c06964..ef2d310 100644 --- a/UnityEngineAnalyzer.CLI/packages.config +++ b/UnityEngineAnalyzer.CLI/packages.config @@ -1,5 +1,6 @@  + diff --git a/readme.md b/readme.md index 3f16c1b..95c0177 100644 --- a/readme.md +++ b/readme.md @@ -10,11 +10,13 @@ Comand Line Interface In order to use the Command Line Interface (CLI), download the latest release of UnityEngineAnalyzer then unzip the archive (https://github.com/vad710/UnityEngineAnalyzer/releases). 1. Open a Command Prompt or Powershell Window -1. Run `UnityEngineAnalyzer.CLI.exe ` +1. Run `UnityEngineAnalyzer.CLI.exe ` 1. Observe the analysis results -1. (Optional) In the same location as the project file are `report.json` and `UnityReport.html` files containig the results of the analysis +1. (Optional) In the same location as the project file are `report.json` and `UnityReport.html` files containig the results of the analysis + * Use command `-e customexporter exporter2 ...` to load custom exporters 1. (Optional) configuration file path. - * Enable / Disable analyzers you want to run. + * Use command `-c configureFilePath.json` to load custom configurations + * Configuration json, allows to enable / disable analyzers Example: From 032779636ced7b5376bc0069ba5f1bf1a8d020c4 Mon Sep 17 00:00:00 2001 From: Kasper Kiiskinen Date: Mon, 16 Oct 2017 10:54:32 +0300 Subject: [PATCH 3/4] MinimalSeverity can be now given as CLI argument. --- UnityEngineAnalyzer.CLI/Options.cs | 4 +++ UnityEngineAnalyzer.CLI/Program.cs | 4 +-- .../Reporting/AnalyzerExporter.cs | 21 ++++++++++++++ .../Reporting/ConsoleAnalyzerExporter.cs | 3 ++ .../Reporting/JsonAnalyzerExporter.cs | 16 +++++------ .../StandardOutputAnalyzerReporter.cs | 28 ++++++++++--------- .../UnityEngineAnalyzer.CLI.csproj | 1 + readme.md | 3 ++ 8 files changed, 57 insertions(+), 23 deletions(-) create mode 100644 UnityEngineAnalyzer.CLI/Reporting/AnalyzerExporter.cs diff --git a/UnityEngineAnalyzer.CLI/Options.cs b/UnityEngineAnalyzer.CLI/Options.cs index f885f47..e45d863 100644 --- a/UnityEngineAnalyzer.CLI/Options.cs +++ b/UnityEngineAnalyzer.CLI/Options.cs @@ -1,5 +1,6 @@ using CommandLine; using System.Collections.Generic; +using UnityEngineAnalyzer.CLI.Reporting; namespace UnityEngineAnalyzer.CLI { @@ -13,5 +14,8 @@ internal class Options [Option('c', "configuration", HelpText = "Custom json configuration to be used.")] public string ConfigurationFile { get; set; } + + [Option('s', "severity", DefaultValue = DiagnosticInfo.DiagnosticInfoSeverity.Warning, HelpText = "Minimal severity to be reported.")] + public DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity { get; set; } } } \ No newline at end of file diff --git a/UnityEngineAnalyzer.CLI/Program.cs b/UnityEngineAnalyzer.CLI/Program.cs index 070268d..800fcf8 100644 --- a/UnityEngineAnalyzer.CLI/Program.cs +++ b/UnityEngineAnalyzer.CLI/Program.cs @@ -52,8 +52,8 @@ public static void Main(string[] args) if (report.GetExporterCount() == 0) { //It's generally a good idea to make sure that the Console Exporter is last since it is interactive - report.AddExporter(new JsonAnalyzerExporter()); - report.AddExporter(new ConsoleAnalyzerExporter()); + report.AddExporter(new JsonAnalyzerExporter(options.MinimalSeverity)); + report.AddExporter(new ConsoleAnalyzerExporter(options.MinimalSeverity)); } report.InitializeReport(fileInfo); diff --git a/UnityEngineAnalyzer.CLI/Reporting/AnalyzerExporter.cs b/UnityEngineAnalyzer.CLI/Reporting/AnalyzerExporter.cs new file mode 100644 index 0000000..9be9ea2 --- /dev/null +++ b/UnityEngineAnalyzer.CLI/Reporting/AnalyzerExporter.cs @@ -0,0 +1,21 @@ +using System; +using System.IO; + +namespace UnityEngineAnalyzer.CLI.Reporting +{ + public abstract class AnalyzerExporter : IAnalyzerExporter + { + //NOTE: Can we use using.static.DiagnosticsInfo; C#6 feature? + protected DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity; + + public AnalyzerExporter(DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity) + { + this.MinimalSeverity = MinimalSeverity; + } + + public abstract void AppendDiagnostic(DiagnosticInfo diagnosticInfo); + public abstract void FinalizeExporter(TimeSpan duration); + public abstract void InitializeExporter(FileInfo projectFile); + public abstract void NotifyException(Exception exception); + } +} diff --git a/UnityEngineAnalyzer.CLI/Reporting/ConsoleAnalyzerExporter.cs b/UnityEngineAnalyzer.CLI/Reporting/ConsoleAnalyzerExporter.cs index 5a7127f..7705934 100644 --- a/UnityEngineAnalyzer.CLI/Reporting/ConsoleAnalyzerExporter.cs +++ b/UnityEngineAnalyzer.CLI/Reporting/ConsoleAnalyzerExporter.cs @@ -5,6 +5,9 @@ namespace UnityEngineAnalyzer.CLI.Reporting { public class ConsoleAnalyzerExporter : StandardOutputAnalyzerReporter { + public ConsoleAnalyzerExporter(DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity) : base(MinimalSeverity) + { + } public override void FinalizeExporter(TimeSpan duration) { diff --git a/UnityEngineAnalyzer.CLI/Reporting/JsonAnalyzerExporter.cs b/UnityEngineAnalyzer.CLI/Reporting/JsonAnalyzerExporter.cs index 4f3c8c3..0e2e4b9 100644 --- a/UnityEngineAnalyzer.CLI/Reporting/JsonAnalyzerExporter.cs +++ b/UnityEngineAnalyzer.CLI/Reporting/JsonAnalyzerExporter.cs @@ -1,25 +1,25 @@ using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using Newtonsoft.Json; namespace UnityEngineAnalyzer.CLI.Reporting { - public class JsonAnalyzerExporter : IAnalyzerExporter + public class JsonAnalyzerExporter : AnalyzerExporter { private const string JsonReportFileName = "report.json"; private const string HtmlReportFileName = "UnityReport.html"; - private const DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity = DiagnosticInfo.DiagnosticInfoSeverity.Warning; - private JsonTextWriter _jsonWriter; private readonly JsonSerializer _jsonSerializer = new JsonSerializer(); private readonly List _exceptions = new List(); private string _destinationReportFile; + public JsonAnalyzerExporter(DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity) : base(MinimalSeverity) + { + } - public void AppendDiagnostic(DiagnosticInfo diagnosticInfo) + public override void AppendDiagnostic(DiagnosticInfo diagnosticInfo) { if (diagnosticInfo.Severity >= MinimalSeverity) { @@ -27,7 +27,7 @@ public void AppendDiagnostic(DiagnosticInfo diagnosticInfo) } } - public void FinalizeExporter(TimeSpan duration) + public override void FinalizeExporter(TimeSpan duration) { _jsonWriter.WriteEndArray(); @@ -50,7 +50,7 @@ public void FinalizeExporter(TimeSpan duration) //Process.Start(_destinationReportFile); } - public void InitializeExporter(FileInfo projectFile) + public override void InitializeExporter(FileInfo projectFile) { if (!projectFile.Exists) { @@ -81,7 +81,7 @@ public void InitializeExporter(FileInfo projectFile) _jsonSerializer.Formatting = Formatting.Indented; } - public void NotifyException(Exception exception) + public override void NotifyException(Exception exception) { _exceptions.Add(exception); } diff --git a/UnityEngineAnalyzer.CLI/Reporting/StandardOutputAnalyzerReporter.cs b/UnityEngineAnalyzer.CLI/Reporting/StandardOutputAnalyzerReporter.cs index 3be7a79..be177d1 100644 --- a/UnityEngineAnalyzer.CLI/Reporting/StandardOutputAnalyzerReporter.cs +++ b/UnityEngineAnalyzer.CLI/Reporting/StandardOutputAnalyzerReporter.cs @@ -3,14 +3,16 @@ namespace UnityEngineAnalyzer.CLI.Reporting { - public class StandardOutputAnalyzerReporter : IAnalyzerExporter + public class StandardOutputAnalyzerReporter : AnalyzerExporter { protected const string ConsoleSeparator = "\t"; - protected const DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity = DiagnosticInfo.DiagnosticInfoSeverity.Warning; - protected const string FailurePrefix = "# "; - public void AppendDiagnostic(DiagnosticInfo diagnosticInfo) + public StandardOutputAnalyzerReporter(DiagnosticInfo.DiagnosticInfoSeverity MinimalSeverity) : base(MinimalSeverity) + { + } + + public override void AppendDiagnostic(DiagnosticInfo diagnosticInfo) { if (diagnosticInfo.Severity < MinimalSeverity) { @@ -48,15 +50,7 @@ private ConsoleColor ConsoleColorFromSeverity(DiagnosticInfo.DiagnosticInfoSever } } - public virtual void FinalizeExporter(TimeSpan duration) - { - } - - public virtual void InitializeExporter(FileInfo projectFile) - { - } - - public virtual void NotifyException(Exception exception) + public override void NotifyException(Exception exception) { Console.ForegroundColor = ConsoleColor.Red; @@ -72,5 +66,13 @@ public virtual void NotifyException(Exception exception) Console.ResetColor(); } + + public override void FinalizeExporter(TimeSpan duration) + { + } + + public override void InitializeExporter(FileInfo projectFile) + { + } } } diff --git a/UnityEngineAnalyzer.CLI/UnityEngineAnalyzer.CLI.csproj b/UnityEngineAnalyzer.CLI/UnityEngineAnalyzer.CLI.csproj index c9fe854..48e3142 100644 --- a/UnityEngineAnalyzer.CLI/UnityEngineAnalyzer.CLI.csproj +++ b/UnityEngineAnalyzer.CLI/UnityEngineAnalyzer.CLI.csproj @@ -109,6 +109,7 @@ + diff --git a/readme.md b/readme.md index 95c0177..48609ee 100644 --- a/readme.md +++ b/readme.md @@ -17,6 +17,9 @@ In order to use the Command Line Interface (CLI), download the latest release of 1. (Optional) configuration file path. * Use command `-c configureFilePath.json` to load custom configurations * Configuration json, allows to enable / disable analyzers +1. (Optional) minimal severity for reports + * Use command `-s Info/Warning/Error` to defined used minimal severity for reporting + * Default is Warning Example: From 2bb14dc92b295f102f806a35443b57651075a6ae Mon Sep 17 00:00:00 2001 From: Kasperki Date: Tue, 17 Oct 2017 16:40:43 +0300 Subject: [PATCH 4/4] fix typo --- .../Material/DoNotUseStringPropertyNamesAnalyzer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnityEngineAnalyzer/UnityEngineAnalyzer/Material/DoNotUseStringPropertyNamesAnalyzer.cs b/UnityEngineAnalyzer/UnityEngineAnalyzer/Material/DoNotUseStringPropertyNamesAnalyzer.cs index b18cad7..9e87a36 100644 --- a/UnityEngineAnalyzer/UnityEngineAnalyzer/Material/DoNotUseStringPropertyNamesAnalyzer.cs +++ b/UnityEngineAnalyzer/UnityEngineAnalyzer/Material/DoNotUseStringPropertyNamesAnalyzer.cs @@ -61,7 +61,7 @@ private static void AnalyzeNode(SyntaxNodeAnalysisContext context) var containingClass = methodSymbol.ContainingType; - // check if the method is the one from UnityEngine.Animator + // check if the method is the one from UnityEngine.Material if (containingClass.ContainingNamespace.Name.Equals("UnityEngine") && containingClass.Name.Equals("Material")) { if (methodSymbol.Parameters[0].Type.MetadataName == "String")