/// <summary> /// Uses Gendarme to do a static code analysis on a specific file! (through filtering) Only works on files in either the editor or runtime assemblies. /// This does require that Gendarme is installed on your computer (https://github.com/spouliot/gendarme/downloads) and may require you to tweak the /// configuration file! /// </summary> /// <param name="aFileName">Path to a code file. Can be relative or absolute.</param> /// <param name="aLog">Want this method to log the info for you? Sure!</param> /// <param name="aIgnoreFile">If you want to use a custom ignore file, provide it here, otherwise it'll just pull from the example/default ignore file. For examples: https://github.com/mono/mono-tools/blob/master/gendarme/self-test.ignore</param> /// <param name="aFilter">What's the lowest severity item you want to see in the analysis? Anything below this is ignored.</param> /// <returns> /// Returns all items scraped from the analysis, or an empty list on failure. Fields not present in the analysis /// will be empty strings in the list data. /// </returns> public static ICollection<AnalysisItem> AnalyzeCodePath(string aFileName, bool aLog = true, Uri aIgnoreFile = null, GendarmeSeverity aFilter = GendarmeSeverity.Medium) { if (!File.Exists(aFileName) && !Directory.Exists(aFileName)) return new List<AnalysisItem>(); // identify which assembly we need to look in for the file string assembly = runtimeAssembly; if (aFileName.IndexOf(@"/Editor/", StringComparison.OrdinalIgnoreCase) >= 0 || aFileName.IndexOf(@"\Editor\", StringComparison.OrdinalIgnoreCase) >= 0){ assembly = editorAssembly; } // get the items, and filter them based on what source it's from Uri assemblyPath = new Uri(ProjectPath+assembly); Uri filePath = new Uri(Path.GetFullPath(aFileName)); ICollection<AnalysisItem> items = AnalyzeCode(assemblyPath, false, aIgnoreFile, aFilter); List<AnalysisItem> finalItems = new List<AnalysisItem>(); foreach(AnalysisItem item in items) { if (!string.IsNullOrEmpty(item.source)) { if (new Uri(item.source).ToString().StartsWith(filePath.ToString())) finalItems.Add(item); } } if (aLog) LogItems(finalItems); return finalItems; }
/// <summary> /// Uses Gendarme to do a static code analysis of your assembly! This does require that Gendarme is installed on your computer (https://github.com/spouliot/gendarme/downloads) /// and may require you to tweak the configuration file! /// </summary> /// <param name="aAssembly">Path to an assembly. Any .exe, .dll or whatever! Preferably with debug symbols present.</param> /// <param name="aLog">Want this method to log the info for you? Sure!</param> /// <param name="aIgnoreFile">If you want to use a custom ignore file, provide it here, otherwise it'll just pull from the example/default ignore file. For examples: https://github.com/mono/mono-tools/blob/master/gendarme/self-test.ignore</param> /// <param name="aFilter">What's the lowest severity item you want to see in the analysis? Anything below this is ignored.</param> /// <returns> /// Returns all items scraped from the analysis, or an empty list on failure. Fields not present in the analysis /// will be empty strings in the list data. /// </returns> public static ICollection<AnalysisItem> AnalyzeCode(Uri aAssembly, bool aLog = true, Uri aIgnoreFile = null, GendarmeSeverity aFilter = GendarmeSeverity.Medium) { if (aAssembly == null) return new List<AnalysisItem>(); if (!LoadConfig(configPath)) { Debug.LogError(string.Format("Config file for UnityGendarmerie was not found at {0}!", configPath)); return new List<AnalysisItem>(); } if (!GendarmePresent()) { Debug.LogError(string.Format("Gendarme was not found at {0}! You may wish to install it, and set the correct path in the configuration file! https://github.com/spouliot/gendarme/downloads", gendarmePath)); return new List<AnalysisItem>(); } List<AnalysisItem> items = null; string ignoreFile = aIgnoreFile != null? aIgnoreFile.AbsolutePath : new Uri(ProjectPath+defaultIgnore).AbsolutePath; using (Process proc = new Process()) { ProcessStartInfo info = proc.StartInfo; info.FileName = gendarmePath; info.WorkingDirectory = ProjectPath; info.Arguments = string.Format("--ignore '{0}' --severity {1}+ '{2}'", ignoreFile, aFilter, aAssembly.AbsolutePath).Replace('\'', '"'); info.UseShellExecute = false; info.RedirectStandardOutput = true; info.CreateNoWindow = true; info.RedirectStandardError = true; proc.Start(); items = ScrapeAnalysis(proc.StandardOutput.ReadToEnd()); if (aLog) LogItems(items); string err = proc.StandardError.ReadToEnd(); if (!string.IsNullOrEmpty(err)) Debug.LogError(err); } return items; }