Author: spouliot
Date: 2008-02-17 16:40:01 -0500 (Sun, 17 Feb 2008)
New Revision: 96005

Added:
   trunk/mono-tools/gendarme/console/IgnoreFileList.cs
   trunk/mono-tools/gendarme/console/Settings.cs
Modified:
   trunk/mono-tools/gendarme/console/
   trunk/mono-tools/gendarme/console/ChangeLog
   trunk/mono-tools/gendarme/console/ConsoleRunner.cs
   trunk/mono-tools/gendarme/console/Makefile.am
   trunk/mono-tools/gendarme/console/console.mdp
Log:
2008-02-17  Sebastien Pouliot  <[EMAIL PROTECTED]>

        * ConsoleRunner.cs: Split with Settings and add support for ignoring
        defects based on a user supplied list.
        * IgnoreFileList.cs: New. File-based ignore list.
        * Settings.cs: New. Configuration support for runner.
        * console.mdp: Update MonoDevelop project file.
        * Makefile.am: Update build file.




Property changes on: trunk/mono-tools/gendarme/console
___________________________________________________________________
Name: svn:ignore
   - Makefile
Makefile.in

   + Makefile
Makefile.in
*.pidb


Modified: trunk/mono-tools/gendarme/console/ChangeLog
===================================================================
--- trunk/mono-tools/gendarme/console/ChangeLog 2008-02-17 21:37:19 UTC (rev 
96004)
+++ trunk/mono-tools/gendarme/console/ChangeLog 2008-02-17 21:40:01 UTC (rev 
96005)
@@ -1,3 +1,12 @@
+2008-02-17  Sebastien Pouliot  <[EMAIL PROTECTED]>
+
+       * ConsoleRunner.cs: Split with Settings and add support for ignoring
+       defects based on a user supplied list.
+       * IgnoreFileList.cs: New. File-based ignore list.
+       * Settings.cs: New. Configuration support for runner.
+       * console.mdp: Update MonoDevelop project file.
+       * Makefile.am: Update build file.
+
 2008-02-16  Sebastien Pouliot  <[EMAIL PROTECTED]> 
 
        * ConsoleRunner.cs: Handle no arguments. Show correct timing for

Modified: trunk/mono-tools/gendarme/console/ConsoleRunner.cs
===================================================================
--- trunk/mono-tools/gendarme/console/ConsoleRunner.cs  2008-02-17 21:37:19 UTC 
(rev 96004)
+++ trunk/mono-tools/gendarme/console/ConsoleRunner.cs  2008-02-17 21:40:01 UTC 
(rev 96005)
@@ -49,6 +49,7 @@
                private string html_file;
                private string log_file;
                private string xml_file;
+               private string ignore_file;
                private bool help;
                private bool quiet;
                private List<string> assembly_names;
@@ -61,6 +62,7 @@
                                { "log=",       v => log_file = v },
                                { "xml=",       v => xml_file = v },
                                { "html=",      v => html_file = v },
+                               { "ignore=",    v => ignore_file = v },
                                { "v|verbose",  v => ++VerbosityLevel },
                                { "quiet",      v => quiet = v != null },
                                { "h|?|help",   v => help = v != null },
@@ -107,83 +109,6 @@
                        Assemblies.Add (ad);
                }
 
-               static string GetFullPath (string filename)
-               {
-                       if (Path.GetDirectoryName (filename).Length > 0)
-                               return filename;
-                       return Path.Combine (Path.GetDirectoryName 
(Assembly.Location), filename);
-               }
-
-               static string GetAttribute (XmlElement xel, string name, string 
defaultValue)
-               {
-                       XmlAttribute xa = xel.Attributes [name];
-                       if (xa == null)
-                               return defaultValue;
-                       return xa.Value;
-               }
-
-               private static bool IsContainedInRuleSet (string rule, string 
mask)
-               {
-                       string [] ruleSet = mask.Split ('|');
-                       foreach (string entry in ruleSet) {
-                               if (String.Compare (rule, entry.Trim ()) == 0)
-                                       return true;
-                       }
-                       return false;
-               }
-
-               public static bool RuleFilter (Type type, object interfaceName)
-               {
-                       return (type.ToString () == (interfaceName as string));
-               }
-
-               public int LoadRulesFromAssembly (string assembly, string 
includeMask, string excludeMask)
-               {
-                       int total = 0;
-                       Assembly a = Assembly.LoadFile (Path.GetFullPath 
(assembly));
-                       foreach (Type t in a.GetTypes ()) {
-                               if (t.IsAbstract || t.IsInterface)
-                                       continue;
-
-                               if (includeMask != "*")
-                                       if (!IsContainedInRuleSet (t.Name, 
includeMask))
-                                               continue;
-
-                               if ((excludeMask != null) && 
(excludeMask.Length > 0))
-                                       if (IsContainedInRuleSet (t.Name, 
excludeMask))
-                                               continue;
-
-                               if (t.FindInterfaces (new TypeFilter 
(RuleFilter), "Gendarme.Framework.IRule").Length > 0) {
-                                       Rules.Add ((IRule) 
Activator.CreateInstance (t));
-                                       total++;
-                               }
-                       }
-                       return total;
-               }
-
-               bool LoadConfiguration ()
-               {
-                       XmlDocument doc = new XmlDocument ();
-                       doc.Load (config_file);
-                       if (doc.DocumentElement.Name != "gendarme")
-                               return false;
-
-                       bool result = false;
-                       foreach (XmlElement ruleset in 
doc.DocumentElement.SelectNodes ("ruleset")) {
-                               if (ruleset.Attributes ["name"].Value != 
rule_set)
-                                       continue;
-                               foreach (XmlElement assembly in 
ruleset.SelectNodes ("rules")) {
-                                       string include = GetAttribute 
(assembly, "include", "*");
-                                       string exclude = GetAttribute 
(assembly, "exclude", String.Empty);
-                                       string from = GetFullPath (GetAttribute 
(assembly, "from", String.Empty));
-
-                                       int n = LoadRulesFromAssembly (from, 
include, exclude);
-                                       result = (result || (n > 0));
-                               }
-                       }
-                       return result;
-               }
-
                int Report ()
                {
                        // generate text report (default, to console, if xml 
and html aren't specified)
@@ -220,9 +145,10 @@
 
                                Header ();
 
-                               // load configuration, including rules, and 
continue if
-                               // there's at least one rule to execute
-                               if (!LoadConfiguration () || (Rules.Count < 1))
+                               // load configuration, including rules
+                               Settings config = new Settings (this, 
config_file, rule_set);
+                               // and continue if there's at least one rule to 
execute
+                               if (!config.Load () || (Rules.Count < 1))
                                        return 3;
 
                                foreach (string name in assembly_names) {
@@ -231,6 +157,8 @@
                                                return result;
                                }
 
+                               IgnoreList = new IgnoreFileList (this, 
ignore_file);
+
                                // now that all rules and assemblies are know, 
time to initialize
                                Initialize ();
                                // before analizing the assemblies with the 
rules
@@ -239,9 +167,12 @@
                                return Report ();
                        }
                        catch (Exception e) {
-                               Console.WriteLine ("Uncatched exception 
occured. Please fill a bug report.");
-                               Console.WriteLine ("Rule:\t{0}", CurrentRule);
-                               Console.WriteLine ("Target:\t{0}", 
CurrentTarget);
+                               Console.WriteLine ();
+                               Console.WriteLine ("An uncatched exception 
occured. Please fill a bug report @ https://bugzilla.novell.com/";);
+                               if (CurrentRule != null)
+                                       Console.WriteLine ("Rule:\t{0}", 
CurrentRule);
+                               if (CurrentTarget != null)
+                                       Console.WriteLine ("Target:\t{0}", 
CurrentTarget);
                                Console.WriteLine ("Stack trace: {0}", e);
                                return 4;
                        }
@@ -256,8 +187,8 @@
                        DateTime end = DateTime.UtcNow;
                        Console.WriteLine (": {0} seconds.", (end - 
timer).TotalSeconds);
                        Console.WriteLine ();
-                       Console.WriteLine ("{0}{1} assemblies processed in {2} 
seconds.{0}",
-                               Environment.NewLine, Assemblies.Count, 
(DateTime.UtcNow - start).TotalSeconds);
+                       Console.WriteLine ("{0} assemblies processed in {1} 
seconds.",
+                               Assemblies.Count, (DateTime.UtcNow - 
start).TotalSeconds);
                }
 
                protected override void OnAssembly (RunnerEventArgs e)
@@ -288,13 +219,13 @@
                        Console.WriteLine ();
                }
 
-               private static Assembly assembly;
+               private static Assembly runner_assembly;
 
-               static Assembly Assembly {
+               static public Assembly Assembly {
                        get {
-                               if (assembly == null)
-                                       assembly = 
Assembly.GetExecutingAssembly ();
-                               return assembly;
+                               if (runner_assembly == null)
+                                       runner_assembly = 
Assembly.GetExecutingAssembly ();
+                               return runner_assembly;
                        }
                }
 

Added: trunk/mono-tools/gendarme/console/IgnoreFileList.cs
===================================================================
--- trunk/mono-tools/gendarme/console/IgnoreFileList.cs 2008-02-17 21:37:19 UTC 
(rev 96004)
+++ trunk/mono-tools/gendarme/console/IgnoreFileList.cs 2008-02-17 21:40:01 UTC 
(rev 96005)
@@ -0,0 +1,85 @@
+//
+// IgnoreFileList - Ignore defects based on a file descriptions
+//
+// Authors:
+//     Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.IO;
+
+using Gendarme.Framework;
+
+namespace Gendarme {
+
+       public class IgnoreFileList : BasicIgnoreList {
+
+               private string rule;
+
+               public IgnoreFileList (IRunner runner, string fileName)
+                       : base (runner)
+               {
+                       if (!String.IsNullOrEmpty (fileName) && File.Exists 
(fileName)) {
+                               Parse (fileName);
+                       }
+               }
+
+               private void Parse (string fileName)
+               {
+                       using (StreamReader sr = new StreamReader (fileName)) {
+                               string s = sr.ReadLine ();
+                               while (s != null) {
+                                       ProcessLine (s);
+                                       s = sr.ReadLine ();
+                               }
+                       }
+               }
+
+               private void ProcessLine (string line)
+               {
+                       if (line.Length < 1)
+                               return;
+
+                       switch (line [0]) {
+                       case '#': // comment
+                               break;
+                       case 'R': // rule
+                               rule = line.Substring (line.LastIndexOf (' ') + 
1);
+                               break;
+                       case 'A': // assembly
+                               AddAssembly (rule, line.Substring (2).Trim ());
+                               break;
+                       case 'T': // type (no space allowed)
+                               AddType (rule, line.Substring (line.LastIndexOf 
(' ') + 1));
+                               break;
+                       case 'M': // method
+                               AddMethod (rule, line.Substring (2).Trim ());
+                               break;
+                       default:
+                               Console.WriteLine ("Bad ignore entry : '{0}'", 
line);
+                               break;
+                       }
+               }
+       }
+}

Modified: trunk/mono-tools/gendarme/console/Makefile.am
===================================================================
--- trunk/mono-tools/gendarme/console/Makefile.am       2008-02-17 21:37:19 UTC 
(rev 96004)
+++ trunk/mono-tools/gendarme/console/Makefile.am       2008-02-17 21:40:01 UTC 
(rev 96005)
@@ -6,7 +6,16 @@
 
 gendarme_sources_in = ../AssemblyInfo.cs.in
 gendarme_generated_sources = $(gendarme_sources_in:.in=)
-gendarme_sources = ConsoleRunner.cs ResultWriter.cs TextResultWriter.cs 
XmlResultWriter.cs HtmlResultWriter.cs Options.cs
+gendarme_sources = \
+       ../AssemblyStaticInfo.cs \
+       ConsoleRunner.cs \
+       Settings.cs \
+       IgnoreFileList.cs \
+       Options.cs \
+       ResultWriter.cs \
+       HtmlResultWriter.cs \
+       TextResultWriter.cs \
+       XmlResultWriter.cs
 gendarme_resources = gendarme.xsl
 
 gendarme_build_sources = $(addprefix $(srcdir)/, $(gendarme_sources))

Added: trunk/mono-tools/gendarme/console/Settings.cs
===================================================================
--- trunk/mono-tools/gendarme/console/Settings.cs       2008-02-17 21:37:19 UTC 
(rev 96004)
+++ trunk/mono-tools/gendarme/console/Settings.cs       2008-02-17 21:40:01 UTC 
(rev 96005)
@@ -0,0 +1,128 @@
+//
+// Gendarme Console Settings
+//
+// Authors:
+//     Sebastien Pouliot <[EMAIL PROTECTED]>
+//
+// Copyright (C) 2008 Novell, Inc (http://www.novell.com)
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.IO;
+using System.Reflection;
+using System.Xml;
+
+using Gendarme.Framework;
+
+namespace Gendarme {
+
+       public class Settings {
+
+               private IRunner runner;
+               private string config_file;
+               private string rule_set;
+
+               public Settings (IRunner runner, string configurationFile, 
string ruleSet)
+               {
+                       this.runner = runner;
+                       this.config_file = configurationFile;
+                       this.rule_set = ruleSet;
+               }
+
+               static string GetFullPath (string filename)
+               {
+                       if (Path.GetDirectoryName (filename).Length > 0)
+                               return filename;
+                       return Path.Combine (Path.GetDirectoryName 
(ConsoleRunner.Assembly.Location), filename);
+               }
+
+               static string GetAttribute (XmlElement xel, string name, string 
defaultValue)
+               {
+                       XmlAttribute xa = xel.Attributes [name];
+                       if (xa == null)
+                               return defaultValue;
+                       return xa.Value;
+               }
+
+               private static bool IsContainedInRuleSet (string rule, string 
mask)
+               {
+                       string [] ruleSet = mask.Split ('|');
+                       foreach (string entry in ruleSet) {
+                               if (String.Compare (rule, entry.Trim (), 
StringComparison.OrdinalIgnoreCase) == 0)
+                                       return true;
+                       }
+                       return false;
+               }
+
+               private static bool RuleFilter (Type type, object interfaceName)
+               {
+                       return (type.ToString () == (interfaceName as string));
+               }
+
+               private int LoadRulesFromAssembly (string assembly, string 
includeMask, string excludeMask)
+               {
+                       int total = 0;
+                       Assembly a = Assembly.LoadFile (Path.GetFullPath 
(assembly));
+                       foreach (Type t in a.GetTypes ()) {
+                               if (t.IsAbstract || t.IsInterface)
+                                       continue;
+
+                               if (includeMask != "*")
+                                       if (!IsContainedInRuleSet (t.Name, 
includeMask))
+                                               continue;
+
+                               if ((excludeMask != null) && 
(excludeMask.Length > 0))
+                                       if (IsContainedInRuleSet (t.Name, 
excludeMask))
+                                               continue;
+
+                               if (t.FindInterfaces (new TypeFilter 
(RuleFilter), "Gendarme.Framework.IRule").Length > 0) {
+                                       runner.Rules.Add ((IRule) 
Activator.CreateInstance (t));
+                                       total++;
+                               }
+                       }
+                       return total;
+               }
+
+               public bool Load ()
+               {
+                       XmlDocument doc = new XmlDocument ();
+                       doc.Load (config_file);
+                       if (doc.DocumentElement.Name != "gendarme")
+                               return false;
+
+                       bool result = false;
+                       foreach (XmlElement ruleset in 
doc.DocumentElement.SelectNodes ("ruleset")) {
+                               if (ruleset.Attributes ["name"].Value != 
rule_set)
+                                       continue;
+                               foreach (XmlElement assembly in 
ruleset.SelectNodes ("rules")) {
+                                       string include = GetAttribute 
(assembly, "include", "*");
+                                       string exclude = GetAttribute 
(assembly, "exclude", String.Empty);
+                                       string from = GetFullPath (GetAttribute 
(assembly, "from", String.Empty));
+
+                                       int n = LoadRulesFromAssembly (from, 
include, exclude);
+                                       result = (result || (n > 0));
+                               }
+                       }
+                       return result;
+               }
+       }
+}

Modified: trunk/mono-tools/gendarme/console/console.mdp
===================================================================
--- trunk/mono-tools/gendarme/console/console.mdp       2008-02-17 21:37:19 UTC 
(rev 96004)
+++ trunk/mono-tools/gendarme/console/console.mdp       2008-02-17 21:40:01 UTC 
(rev 96005)
@@ -20,12 +20,17 @@
     <File name="gendarme.xsl" subtype="Code" buildaction="EmbedAsResource" />
     <File name="HtmlResultWriter.cs" subtype="Code" buildaction="Compile" />
     <File name="ResultWriter.cs" subtype="Code" buildaction="Compile" />
+    <File name="ChangeLog" subtype="Code" buildaction="Nothing" />
+    <File name="IgnoreFileList.cs" subtype="Code" buildaction="Compile" />
+    <File name="Options.cs" subtype="Code" buildaction="Compile" />
+    <File name="Settings.cs" subtype="Code" buildaction="Compile" />
   </Contents>
   <References>
     <ProjectReference type="Project" localcopy="True" refto="framework" />
-    <ProjectReference type="Assembly" localcopy="True" 
refto="../../lib/Mono.Cecil.dll" />
     <ProjectReference type="Gac" localcopy="True" refto="System, 
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
     <ProjectReference type="Gac" localcopy="True" refto="System.Xml, 
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
+    <ProjectReference type="Project" localcopy="True" refto="Mono.Cecil" />
+    <ProjectReference type="Gac" localcopy="True" refto="System.Core, 
Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
   </References>
   <DeployTargets />
 </Project>
\ No newline at end of file

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to