Пример #1
0
        private static void DoParse(GetOptions options, string[] args)
        {
            options.Add("-append", "Append to log files");
            options.Add("-exclude-check=", "Don't do checks if the checkID contains PARAM");
            options.Add("-exclude-name=", "Don't check types/methods where the full name contains PARAM");
            options.Add("-help", "-?", "Show this help list and exit");
            options.Add("-html", "Generate an html report");
            options.Add("-ignore-breaking", "Skip rules where the fix may break binary compatibility");
            options.Add("-include-check=", "Undo -exclude-check");
            options.Add("-include-name=", "Undo -exclude-name");
            options.Add("-include-breaking", "Undo -ignore-breaking");
            options.Add("-include-localized", "Undo -not-localized");
            options.Add("-not-localized", "Disable localization rules");
            options.Add("-out=stdout", "Path to write the report to");
            options.Add("-profile=", "Use a named option set");
            options.Add("-quiet", "Don't print progress");
            options.Add("-only-type=", "Only check types where the full name contains PARAM");
            options.Add("-set=", "Change a setting, e.g. -set:maxBoxes:0");
            options.Add("-severity=Nitpick", "Only check rules with this severity or higher");
            options.Add("-text", "Generate a text report, this is the default");
            options.Add("-usage", "Show usage syntax and exit");
            options.Add("-verbose", "-V", "Progress prints names and rules");
            options.Add("-version", "Display version and licensing information and exit");
            options.Add("-xml", "Generate an xml report");
#if DEBUG
            options.Add("-check-xml", "Compiles the C# code examples in the xml and reports any errors");
            options.Add("-generate-html-violations=", "Generates html docs from the xml");
            options.Add("-dump-strings", "Dump setters and ctors with string parameters");
#endif

            options.Parse(args);
        }
Пример #2
0
		private static void DoParse(GetOptions options, string[] args)
		{
			options.Add("-exe=", "Path to the smokey exe");
			options.Add("-asm=", "Comma separated list of assemblies");
			options.Add("-help", "-?", "Show this help list and exit");
			options.Add("-version", "Display version and licensing information and exit");

			options.Parse(args);
		}
Пример #3
0
        private static void DoShowHelp(GetOptions options)
        {
            Version version = Assembly.GetExecutingAssembly().GetName().Version;

            Console.WriteLine("Smokey {0} - Copyright (C) 2007-2008 Jesse Jones", version);             // TODO: update the year as needed
            Console.WriteLine("Analyzes assemblies and reports problems with the code.");
            Console.WriteLine();
            Console.WriteLine("Usage: mono smokey [options] assembly");
            Console.WriteLine();

            options.ShowHelp();

            Console.WriteLine();
            DoShowProfileHelp();
        }
Пример #4
0
		public static int Main(string[] args)
		{ 
			int err = 0; 	
			
			try
			{
				GetOptions options = new GetOptions();
				DoParse(options, args);
				
				if (!DoProcessOptions(options))
				{
					if (DoValidate(options)) 
					{
						string[] assemblies = options.Value("-asm").Split(',');
						
						Analyze analyze = new Analyze();
						if (!analyze.Run(options.Value("-exe"), assemblies))
							err = 1;
					}
					else
					{
						err = -2;
					}
				}
			}
			catch (MalformedCommandLineException m)
			{
				Console.Error.WriteLine(m.Message);
				err = -2;
			}
			catch (Exception e)
			{
				Exception n = e;
				int indent = 0;
				while (n != null)
				{
					Console.Error.WriteLine("{0}{1}", new string(' ', 3*indent), n.Message);
					n = n.InnerException;
					++indent;
				}
				Console.Error.WriteLine("exception stack trace:");
				Console.Error.WriteLine(e.StackTrace);
				err = -1;
			}
			
			return err;
		}		
Пример #5
0
        private static bool DoProcessOptions(GetOptions options)
        {
            bool processed = true;

            if (options.Has("-help"))
            {
                DoShowHelp(options);
            }

            else if (options.Has("-version"))
            {
                DoShowVersion();
            }

            else if (options.Has("-usage"))
            {
                DoShowUsage();
            }

#if DEBUG
            else if (options.Has("-check-xml"))
            {
                CheckXml checker = new CheckXml();
                checker.Check();
            }
            else if (options.Has("-generate-html-violations"))
            {
                ViolationDatabase.Init();

                HtmlViolations html = new HtmlViolations();
                html.Write(options.Value("-generate-html-violations"));
            }
            else if (options.Has("-dump-strings"))
            {
                string assemblyPath = options.Operands[0];
                DumpStrings.Dump(assemblyPath);
            }
#endif
            else
            {
                processed = false;
            }

            return(processed);
        }
Пример #6
0
        private static void DoPostOptionsInit(GetOptions options, string[] args)
        {
            if (options.Has("-not-localized"))
            {
                Settings.Add("*localized*", "false");
            }

            Log.Init(options.Has("-append"));
            Log.InfoLine(true, "started up on {0}, version {1}", DateTime.Now, Assembly.GetExecutingAssembly().GetName().Version);
            Log.InfoLine(true, "arguments are '{0}'", string.Join(", ", args));

            string paths = Settings.Get("custom", string.Empty);

            foreach (string path in paths.Split(':'))
            {
                if (path.Length > 0)
                {
                    try
                    {
                        Unused.Value = System.Reflection.Assembly.LoadFrom(path);                               // need to load these before we init the logger
                    }
                    catch (Exception e)
                    {
                        Console.Error.WriteLine("Couldn't load the '{0}' custom assembly. See the log for details.", path);
                        Log.ErrorLine(true, e.Message);
                        Log.ErrorLine(true, e.StackTrace);
                    }
                }
            }

#if DEBUG
            AssertTraceListener.Install();
#endif

            foreach (string entry in options.Values("-set"))
            {
                string key   = entry.Split(':')[0];
                string value = entry.Split(':')[1];
                Settings.Add(key, value);
            }

            ViolationDatabase.Init();
        }
Пример #7
0
		private static bool DoValidate(GetOptions options)
		{
			bool valid = true;
			
			// can't use both -verbose and -quiet
			if (options.Has("-verbose") && options.Has("-quiet"))
			{
				Console.Error.WriteLine("can't use both -verbose and -quiet");
				valid = false;
			}
			
			// -text, -html, and -xml are exclusive
			if (options.Has("-text") && (options.Has("-html") || options.Has("-xml")))
			{
				Console.Error.WriteLine("-text, -html, and -xml are exclusive options");
				valid = false;
			}
			if (options.Has("-html") && options.Has("-xml"))
			{
				Console.Error.WriteLine("-text, -html, and -xml are exclusive options");
				valid = false;
			}
			
			// -exclude-name values must be valid
			string[] names = options.Values("-exclude-name");
			if (!DoTypeMethodsValid(names, "-exclude-name"))
			{
				valid = false;
			}
						
			// -include-name values must be valid
			names = options.Values("-include-name");
			if (!DoTypeMethodsValid(names, "-include-name"))
			{
				valid = false;
			}
			
			// -severity must be legit
			foreach (string severity in options.Values("-severity"))
			{
				if (severity != "Error" && severity != "Warning" && severity != "Nitpick")
				{
					Console.Error.WriteLine("severity must be Error, Warning, or Nitpick");
					valid = false;
				}
			}
			
			// -set must be well formed
			string[] values = options.Values("-set");
			foreach (string value in values)
			{
				if (!value.Contains(":"))
				{
					Console.Error.WriteLine("-set:{0} is missing a colon", value);
					valid = false;
				}
			}
						
			// must have one assembly
			if (options.Operands.Length != 1)	
			{
				Console.Error.WriteLine("one assembly must be specified");
				valid = false;
			}
			
			return valid;
		}		
Пример #8
0
		private static void DoParse(GetOptions options, string[] args)
		{
			options.Add("-append", "Append to log files");
			options.Add("-exclude-check=", "Don't do checks if the checkID contains PARAM");
			options.Add("-exclude-name=", "Don't check types/methods where the full name contains PARAM");
			options.Add("-help", "-?", "Show this help list and exit");
			options.Add("-html", "Generate an html report");
			options.Add("-ignore-breaking", "Skip rules where the fix may break binary compatibility");
			options.Add("-include-check=", "Undo -exclude-check");
			options.Add("-include-name=", "Undo -exclude-name");
			options.Add("-include-breaking", "Undo -ignore-breaking");
			options.Add("-include-localized", "Undo -not-localized");
			options.Add("-not-localized", "Disable localization rules");
			options.Add("-out=stdout", "Path to write the report to");
			options.Add("-profile=", "Use a named option set");
			options.Add("-quiet", "Don't print progress");
			options.Add("-only-type=", "Only check types where the full name contains PARAM");
			options.Add("-set=", "Change a setting, e.g. -set:maxBoxes:0");
			options.Add("-severity=Nitpick", "Only check rules with this severity or higher");
			options.Add("-text", "Generate a text report, this is the default");
			options.Add("-usage", "Show usage syntax and exit");
			options.Add("-verbose", "-V", "Progress prints names and rules");
			options.Add("-version", "Display version and licensing information and exit");
			options.Add("-xml", "Generate an xml report");			
#if DEBUG			
			options.Add("-check-xml", "Compiles the C# code examples in the xml and reports any errors");
			options.Add("-generate-html-violations=", "Generates html docs from the xml");
			options.Add("-dump-strings", "Dump setters and ctors with string parameters");
#endif

			options.Parse(args);
		}
Пример #9
0
		private static bool DoProcessOptions(GetOptions options)
		{
			bool processed = true;
						
			if (options.Has("-help"))
				DoShowHelp(options);
				
			else if (options.Has("-version"))
				DoShowVersion();
				
			else if (options.Has("-usage"))
				DoShowUsage();
				
#if DEBUG
			else if (options.Has("-check-xml"))
			{
				CheckXml checker = new CheckXml();
				checker.Check();
			}
			else if (options.Has("-generate-html-violations"))
			{
				ViolationDatabase.Init();
	
				HtmlViolations html = new HtmlViolations();
				html.Write(options.Value("-generate-html-violations"));
			}
			else if (options.Has("-dump-strings"))
			{
				string assemblyPath = options.Operands[0];
				DumpStrings.Dump(assemblyPath);
			}
#endif
			else	
				processed = false;
			
			return processed;
		}
Пример #10
0
		private static bool DoProcessOptions(GetOptions options)
		{
			bool processed = true;
						
			if (options.Has("-help"))
				DoShowHelp(options);
				
			else if (options.Has("-version"))
				DoShowVersion();
								
			else	
				processed = false;
			
			return processed;
		}
Пример #11
0
		private static void DoPostOptionsInit(GetOptions options, string[] args)
		{				
			if (options.Has("-not-localized"))
				Settings.Add("*localized*", "false");

			Log.Init(options.Has("-append"));
			Log.InfoLine(true, "started up on {0}, version {1}", DateTime.Now, Assembly.GetExecutingAssembly().GetName().Version);
			Log.InfoLine(true, "arguments are '{0}'", string.Join(", ", args));

			string paths = Settings.Get("custom", string.Empty);
			foreach (string path in paths.Split(':'))
			{
				if (path.Length > 0)
				{
					try
					{
						Unused.Value = System.Reflection.Assembly.LoadFrom(path);	// need to load these before we init the logger
					}
					catch (Exception e)
					{
						Console.Error.WriteLine("Couldn't load the '{0}' custom assembly. See the log for details.", path);
						Log.ErrorLine(true, e.Message);
						Log.ErrorLine(true, e.StackTrace);
					}
				}
			}

#if DEBUG
			AssertTraceListener.Install();
#endif
						
			foreach (string entry in options.Values("-set"))
			{
				string key = entry.Split(':')[0];
				string value = entry.Split(':')[1];
				Settings.Add(key, value);
			}
			
			ViolationDatabase.Init();
		}
Пример #12
0
		private static void DoShowHelp(GetOptions options)
		{
			Version version = Assembly.GetExecutingAssembly().GetName().Version;

			Console.WriteLine("functest {0} - Copyright (C) 2007-2008 Jesse Jones", version);	// TODO: update the year as needed
			Console.WriteLine("Functional test for Smokey.");
			Console.WriteLine();
			
			options.ShowHelp();
		}
Пример #13
0
        private static bool DoRun(GetOptions options)
        {
            Progress progress = null;

            if (!options.Has("-quiet"))
            {
                progress = new Progress(options.Has("-verbose"));
            }

            Watchdog watcher = new Watchdog(options.Has("-verbose"));

            Error[] errors = null;
            try
            {
                DateTime startTime = DateTime.Now;

                AnalyzeAssembly analyzer = new AnalyzeAssembly(delegate(string name)
                {
                    progress.Add(name);
                    watcher.Add(name);
                });

                List <string> excluded = new List <string>(options.Values("-exclude-check"));
                if (excluded.IndexOf("R1035") < 0)                              // ValidateArgs2 is disabled by default
                {
                    excluded.Add("R1035");
                }

                analyzer.ExcludedChecks = excluded.Except(options.Values("-include-check"));
                analyzer.ExcludeNames   = options.Values("-exclude-name").Except(options.Values("-include-name"));

                string[] onlyType = options.Values("-only-type");

                string[] severities   = options.Values("-severity");
                Severity severity     = (Severity)Enum.Parse(typeof(Severity), severities[severities.Length - 1]);
                bool     ignoreBreaks = options.Has("-ignore-breaking") && !options.Has("-include-breaking");
                errors = analyzer.Analyze(options.Operands[0], onlyType, severity, ignoreBreaks);
                TimeSpan elapsed = DateTime.Now - startTime;

                string assemblyPath = options.Operands[0];
                string outFile      = options.Value("-out");

                if (options.Has("-xml"))
                {
                    XmlReport.Report(assemblyPath, outFile, errors);
                }
                else if (options.Has("-html"))
                {
                    HtmlReport.Report(assemblyPath, outFile, errors, analyzer.NumRules, elapsed);
                }
                else
                {
                    TextReport.Report(assemblyPath, outFile, errors, analyzer.NumRules, elapsed);
                }
            }
            finally
            {
                if (progress != null)
                {
                    progress.Dispose();
                }
                watcher.Dispose();
            }

            int count = errors.Count(x => x.Violation.Severity != Severity.Nitpick);

            return(count == 0);
        }
Пример #14
0
        public static int Main(string[] inArgs)
        {
            int err = 0;

            try
            {
                Profile.Start("App");

                DoPreOptionsInit();
                string[] args = DoExpandProfile(inArgs);

                GetOptions options = new GetOptions();
                DoParse(options, args);

                if (!DoProcessOptions(options))
                {
                    if (DoValidate(options))
                    {
                        DoPostOptionsInit(options, args);
                        if (!DoRun(options))
                        {
                            err = 1;
                        }
                    }
                    else
                    {
                        err = 2;
                    }
                }

                Profile.Stop("App");
#if PROFILE
                Console.WriteLine(Profile.GetResults());
#endif
            }
            catch (MalformedCommandLineException m)
            {
                Console.Error.WriteLine(m.Message);
                err = 2;
            }
            catch (Exception e)
            {
                Exception n      = e;
                int       indent = 0;
                while (n != null)
                {
                    Console.Error.WriteLine("{0}{1}", new string(' ', 3 * indent), n.Message);
                    n = n.InnerException;
                    ++indent;
                }

                Console.Error.WriteLine("exception stack trace:");
                Console.Error.WriteLine(e.StackTrace);

                err = 3;
            }
            finally
            {
#if DEBUG
                if (Log.NumErrors > 0 && Log.NumWarnings > 0)
                {
                    Console.Error.WriteLine("There were {0} errors and {1} warnings!!!", Log.NumErrors, Log.NumWarnings);
                }

                else if (Log.NumErrors > 0)
                {
                    Console.Error.WriteLine("There were {0} errors!!!", Log.NumErrors.ToString());
                }

                else if (Log.NumWarnings > 0)
                {
                    Console.Error.WriteLine("There were {0} warnings!!!", Log.NumWarnings.ToString());
                }
#endif
                Log.Flush();
            }

            return(err);
        }
Пример #15
0
        private static bool DoValidate(GetOptions options)
        {
            bool valid = true;

            // can't use both -verbose and -quiet
            if (options.Has("-verbose") && options.Has("-quiet"))
            {
                Console.Error.WriteLine("can't use both -verbose and -quiet");
                valid = false;
            }

            // -text, -html, and -xml are exclusive
            if (options.Has("-text") && (options.Has("-html") || options.Has("-xml")))
            {
                Console.Error.WriteLine("-text, -html, and -xml are exclusive options");
                valid = false;
            }
            if (options.Has("-html") && options.Has("-xml"))
            {
                Console.Error.WriteLine("-text, -html, and -xml are exclusive options");
                valid = false;
            }

            // -exclude-name values must be valid
            string[] names = options.Values("-exclude-name");
            if (!DoTypeMethodsValid(names, "-exclude-name"))
            {
                valid = false;
            }

            // -include-name values must be valid
            names = options.Values("-include-name");
            if (!DoTypeMethodsValid(names, "-include-name"))
            {
                valid = false;
            }

            // -severity must be legit
            foreach (string severity in options.Values("-severity"))
            {
                if (severity != "Error" && severity != "Warning" && severity != "Nitpick")
                {
                    Console.Error.WriteLine("severity must be Error, Warning, or Nitpick");
                    valid = false;
                }
            }

            // -set must be well formed
            string[] values = options.Values("-set");
            foreach (string value in values)
            {
                if (!value.Contains(":"))
                {
                    Console.Error.WriteLine("-set:{0} is missing a colon", value);
                    valid = false;
                }
            }

            // must have one assembly
            if (options.Operands.Length != 1)
            {
                Console.Error.WriteLine("one assembly must be specified");
                valid = false;
            }

            return(valid);
        }
Пример #16
0
		private static void DoShowHelp(GetOptions options)
		{
			Version version = Assembly.GetExecutingAssembly().GetName().Version;

			Console.WriteLine("Smokey {0} - Copyright (C) 2007-2008 Jesse Jones", version);	// TODO: update the year as needed
			Console.WriteLine("Analyzes assemblies and reports problems with the code.");
			Console.WriteLine();
			Console.WriteLine("Usage: mono smokey [options] assembly");
			Console.WriteLine();
			
			options.ShowHelp();

			Console.WriteLine();
			DoShowProfileHelp();
		}
Пример #17
0
		public static int Main(string[] inArgs)
		{ 
			int err = 0; 	
			
			try
			{
				Profile.Start("App");
				
				DoPreOptionsInit();
				string[] args = DoExpandProfile(inArgs);

				GetOptions options = new GetOptions();
				DoParse(options, args);
				
				if (!DoProcessOptions(options))
				{
					if (DoValidate(options)) 
					{
						DoPostOptionsInit(options, args);
						if (!DoRun(options))
							err = 1;
					}
					else
					{
						err = 2;
					}
				}
				
				Profile.Stop("App");
#if PROFILE
				Console.WriteLine(Profile.GetResults());
#endif
			}
			catch (MalformedCommandLineException m)
			{
				Console.Error.WriteLine(m.Message);
				err = 2;
			}
			catch (Exception e)
			{
				Exception n = e;
				int indent = 0;
				while (n != null)
				{
					Console.Error.WriteLine("{0}{1}", new string(' ', 3*indent), n.Message);
					n = n.InnerException;
					++indent;
				}

				Console.Error.WriteLine("exception stack trace:");
				Console.Error.WriteLine(e.StackTrace);

				err = 3;
			}
			finally
			{
#if DEBUG
				if (Log.NumErrors > 0 && Log.NumWarnings > 0)
					Console.Error.WriteLine("There were {0} errors and {1} warnings!!!", Log.NumErrors, Log.NumWarnings);

				else if (Log.NumErrors > 0)
					Console.Error.WriteLine("There were {0} errors!!!", Log.NumErrors.ToString());

				else if (Log.NumWarnings > 0)
					Console.Error.WriteLine("There were {0} warnings!!!", Log.NumWarnings.ToString());				
#endif
				Log.Flush();
			}
			
			return err;
		}
Пример #18
0
		private static bool DoValidate(GetOptions options)
		{
			bool valid = true;
			
			// -exe and -asm are required
			if (!options.Has("-exe"))
			{
				Console.Error.WriteLine("-exe is required");
				valid = false;
			}
			if (!options.Has("-asm"))
			{
				Console.Error.WriteLine("-asm is required");
				valid = false;
			}
			
			// should not be any operands
			if (options.Operands.Length > 0)
			{
				Console.Error.WriteLine("operands are not supported");
				valid = false;
			}
			
			return valid;
		}		
Пример #19
0
		private static bool DoRun(GetOptions options)
		{ 
			Progress progress = null;
			if (!options.Has("-quiet"))
				progress = new Progress(options.Has("-verbose"));
				
			Watchdog watcher = new Watchdog(options.Has("-verbose"));
			
			Error[] errors = null;
			try
			{
				DateTime startTime = DateTime.Now;

				AnalyzeAssembly analyzer = new AnalyzeAssembly(delegate (string name)
				{
					progress.Add(name);
					watcher.Add(name);
				});
				
				List<string> excluded = new List<string>(options.Values("-exclude-check"));
				if (excluded.IndexOf("R1035") < 0)		// ValidateArgs2 is disabled by default
					excluded.Add("R1035");
				
				analyzer.ExcludedChecks = excluded.Except(options.Values("-include-check"));
				analyzer.ExcludeNames = options.Values("-exclude-name").Except(options.Values("-include-name"));
				
				string[] onlyType = options.Values("-only-type");
				
				string[] severities = options.Values("-severity");
				Severity severity = (Severity) Enum.Parse(typeof(Severity), severities[severities.Length - 1]);
				bool ignoreBreaks = options.Has("-ignore-breaking") && !options.Has("-include-breaking");
				errors = analyzer.Analyze(options.Operands[0], onlyType, severity, ignoreBreaks);
				TimeSpan elapsed = DateTime.Now - startTime;
				
				string assemblyPath = options.Operands[0];
				string outFile = options.Value("-out");
				
				if (options.Has("-xml"))
					XmlReport.Report(assemblyPath, outFile, errors);
				else if (options.Has("-html"))
					HtmlReport.Report(assemblyPath, outFile, errors, analyzer.NumRules, elapsed);
				else
					TextReport.Report(assemblyPath, outFile, errors, analyzer.NumRules, elapsed);
			}
			finally
			{
				if (progress != null)
					progress.Dispose();
				watcher.Dispose();
			}
			
			int count = errors.Count(x => x.Violation.Severity != Severity.Nitpick);
						
			return count == 0;
		}
Пример #20
0
		public void Init()
		{
			m_options = new GetOptions();
		}