/// <summary> /// Building main entry point. /// </summary> /// <param name="abstractOpen"></param> /// <param name="output"></param> /// <param name="fxCopPath">FxCopCmd.exe path</param> /// <param name="fXCopOut">FxCop result directory</param> /// <param name="row">CSV file second row</param> public static void Start(AbstractOpen abstractOpen, string output, string fxCopPath, string fXCopOut, string outListFilePath) { WriteMsg.WriteLine("Starting analysis..."); MainDeclaration.Instance.CSSIExtension = (abstractOpen is FileOpen) ? Constants.CSSIEXTENSION : Constants.LCSSIEXTENSION; MainDeclaration.Instance.Solution = abstractOpen.Solution; foreach (Project project in abstractOpen.TopologicallySortedProjectDependencies) { if (Commons.Common.CheckOuterFilters(project.FilePath)) { continue; } WriteMsg.WriteWithBreak("Analyzing {0}...", WriteMsg.MsgLevel.Normal, 4, project.AssemblyName); Analyse(project, output, fxCopPath, fXCopOut); } MainDeclaration.Instance.Statistics[0].CSharpASGBuildingTime = MainDeclaration.Instance.CSSIWatch.ElapsedSeconds; MainDeclaration.Instance.Statistics[0].LIMASGBildingTime = MainDeclaration.Instance.LIMWatch.ElapsedSeconds; LineMetrics.Calculate(); WriteMsg.WriteLine("Analysis finished"); if (!string.IsNullOrEmpty(outListFilePath)) { File.WriteAllText(outListFilePath, string.Join(Environment.NewLine, outfiles)); } MainDeclaration.Instance.FxCopTasks.WaitAll(); }
public Filter(string path) { Lines = new List <FilterLine>(); StreamReader file = null; try { file = new StreamReader(path); string line; while ((line = file.ReadLine()) != null) { if (line[0] == '-' || line[0] == '+') { Lines.Add(ProcessLine(line)); } } } catch (FileNotFoundException) { WriteMsg.WriteWithBreak("Warning: filter file \"{0}\" not found!", WriteMsg.MsgLevel.Warning, 0, path); } finally { file?.Dispose(); } Lines.Reverse(); //we will check from the last line, and the first match determines if the file is needed or not }
private static bool ppHelp(Option o, string[] args) { WriteMsg.WriteWithBreak("Usage:", WriteMsg.MsgLevel.Silent, 2); WriteMsg.WriteWithBreak("", WriteMsg.MsgLevel.Silent); WriteMsg.WriteWithBreak("CSAN [options] -input:(file.cs|file.csproj|file.sln) -output:folder", WriteMsg.MsgLevel.Silent, 4); WriteMsg.WriteWithBreak("", WriteMsg.MsgLevel.Silent); Arguments.WriteHelp(OPTIONS_OBJ, o.Internal); //if called from -helpi, then this property will be true, so internals will be written out as well Environment.Exit(1); return(true); }
/// <summary> /// Processes the \Q and \E anchors in regular expressions /// </summary> private static string ProcessEscapingAnchors(string regex) { if (string.IsNullOrEmpty(regex)) { return(regex); } var sb = new StringBuilder(); int i = 0, j = 0; bool open = false; for (; i < regex.Length - 1; i++) { if (!open && regex.Substring(i, 2) == "\\Q") //i a \-re mutat { open = true; if (i != 0) { sb.Append(regex.Substring(j, i - j)); } i++; //i a Q-ra mutat, majd a ciklus végén lépteti egyel a for j = i + 1; //emiatt, a j-t i+1re állítjuk } else if (regex.Substring(i, 2) == "\\E") { if (!open) { WriteMsg.WriteWithBreak("Warning: \\E anchor found without opening \\Q in one of the filter files. Ignoring it.", WriteMsg.MsgLevel.Warning); } else { open = false; sb.Append(Regex.Escape(regex.Substring(j, i - j))); i++; j = i + 1; } } } if (i != j) { if (open) { sb.Append(Regex.Escape(regex.Substring(j, i - j + 1))); } else { sb.Append(regex.Substring(j, i - j + 1)); } } return(sb.ToString()); }
/// <summary> /// Checks FxCopPath and tries to find FxCop if not specified /// </summary> private static bool CheckFxCopPath() { if (string.IsNullOrEmpty(FXCOPPATH) && RUNFXCOP) { WriteMsg.WriteWithBreak("No FxCopPath specified, looking for it in installed Visual Studio directories..."); string relativePath = @"Team Tools\Static Analysis Tools\FxCop\"; string vsCommonTools = Environment.GetEnvironmentVariable("VS140COMNTOOLS"); string version = "2015"; if (string.IsNullOrEmpty(vsCommonTools)) { vsCommonTools = Environment.GetEnvironmentVariable("VS120COMNTOOLS"); version = "2013"; if (string.IsNullOrEmpty(vsCommonTools)) { vsCommonTools = Environment.GetEnvironmentVariable("VS100COMNTOOLS"); version = "2010"; } } if (!string.IsNullOrEmpty(vsCommonTools)) { FXCOPPATH = Path.GetFullPath(vsCommonTools + ".." + Path.DirectorySeparatorChar + ".." + Path.DirectorySeparatorChar + relativePath); if (File.Exists(FXCOPPATH + "FxCopCmd.exe")) { WriteMsg.WriteWithBreak("Found Visual Studio " + version); WriteMsg.WriteWithBreak("FxCop static analysis tool will be used from: " + FXCOPPATH); } else { FXCOPPATH = null; WriteMsg.WriteWithBreak("Could not find FxCop on your computer. FxCop analysis will be skipped.", WriteMsg.MsgLevel.Warning); } } else { WriteMsg.WriteWithBreak("Could not find FxCop on your computer. FxCop analysis will be skipped.", WriteMsg.MsgLevel.Warning); } } else if (RUNFXCOP && !File.Exists(FXCOPPATH.EndsWith(".exe") ? FXCOPPATH : FXCOPPATH + "FxCopCmd.exe")) { WriteMsg.WriteWithBreak("Could not find FxCop at the specified location: {0}", WriteMsg.MsgLevel.Error, 0, FXCOPPATH); return(false); } return(true); }
/// <summary> /// Binary file will be analysed by FxCop. /// </summary> /// <param name="inputBinary">Binary file location</param> /// <param name="resultOutputPath">FxCop result directory</param> /// <param name="fxCopPath">FxCopCmd.exe path</param> private static void StartFxCopAnalysis(string inputBinary, string resultOutputPath, string fxCopPath) { if (string.IsNullOrEmpty(fxCopPath)) { return; } string output_filename = Path.GetFileNameWithoutExtension(inputBinary) + ".xml"; string output_directory = Path.GetDirectoryName(inputBinary); string result = Path.Combine(output_directory, output_filename); if (!string.IsNullOrEmpty(resultOutputPath) && resultOutputPath[resultOutputPath.Length - 1] != Path.DirectorySeparatorChar) { resultOutputPath += Path.DirectorySeparatorChar; } System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo() { CreateNoWindow = false, UseShellExecute = false, FileName = fxCopPath.EndsWith(".exe") ? fxCopPath : fxCopPath + "FxCopCmd.exe", WorkingDirectory = fxCopPath.EndsWith(".exe") ? Path.GetDirectoryName(fxCopPath) : fxCopPath, WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden, Arguments = string.Format("/f:\"{0}\" /d:\"{1}\" /r:\"{2}\" /o:\"{3}{4}\" /gac", inputBinary, output_directory, (fxCopPath.Contains(".exe") ? Path.GetDirectoryName(fxCopPath) : fxCopPath) + "Rules", resultOutputPath, output_filename), RedirectStandardOutput = true, RedirectStandardError = true }; if (MainDeclaration.Instance.BuildTask != null) { MainDeclaration.Instance.BuildTask.Wait(); } if (!File.Exists(inputBinary)) { WriteMsg.WriteWithBreak("[FxCopAnalysis] {0} is missing, skipped", WriteMsg.MsgLevel.Warning, 4, inputBinary); return; } WriteMsg.WriteWithBreak("[FxCopAnalysis] Starting FxCop analysis for {0}...", WriteMsg.MsgLevel.Normal, 4, inputBinary); try { string output = string.Format("{0}{1}{2}.log", resultOutputPath, Path.DirectorySeparatorChar, output_filename); using (System.Diagnostics.Process exeProcess = new System.Diagnostics.Process()) using (StreamWriter sw = new StreamWriter(output, false)) { exeProcess.OutputDataReceived += (sender, e) => { if (e.Data != null) { sw.WriteLine(e.Data); } }; exeProcess.ErrorDataReceived += (sender, e) => { if (e.Data != null) { sw.WriteLine(e.Data); } }; exeProcess.StartInfo = startInfo; exeProcess.Start(); exeProcess.BeginOutputReadLine(); exeProcess.BeginErrorReadLine(); exeProcess.WaitForExit(); } WriteMsg.WriteWithBreak("[FxCopAnalysis] FxCop for {0} terminated", WriteMsg.MsgLevel.Normal, 4, Path.GetFileName(inputBinary)); } catch (FileNotFoundException e) { if (File.Exists(result)) { File.Delete(result); } WriteMsg.WriteWithBreak("[FxCopAnalysis][FileNotFoundException] {0}: {1}", WriteMsg.MsgLevel.Warning, 4, e.Message, startInfo.FileName); } catch (System.ComponentModel.Win32Exception e) { if (File.Exists(result)) { File.Delete(result); } WriteMsg.WriteWithBreak("[FxCopAnalysis][Win32Exception] " + e.Message, WriteMsg.MsgLevel.Warning, 4); } catch (System.IO.IOException e) { if (File.Exists(result)) { File.Delete(result); } WriteMsg.WriteWithBreak("[FxCopAnalysis][IOException] " + e.Message, WriteMsg.MsgLevel.Warning, 4); } catch (Exception e) { if (File.Exists(result)) { File.Delete(result); } WriteMsg.WriteWithBreak("[FxCopAnalysis][{0}] {1}", WriteMsg.MsgLevel.Warning, 4, e.GetType().ToString(), e.Message); } }