src: mass rename project->package
[nit.git] / src / metrics / inheritance_metrics.nit
index 56ae196..1287c0f 100644 (file)
 # Collect metrics about inheritance usage
 module inheritance_metrics
 
-import model
+import metrics_base
 import mmodules_metrics
 import mclasses_metrics
-import phase
-import frontend
 
 redef class ToolContext
-       var inheritance_metrics_phase = new InheritanceMetricsPhase(self, null)
+       var inheritance_metrics_phase: Phase = new InheritanceMetricsPhase(self, null)
 end
 
 # Extract metrics about inheritance from model.
 private class InheritanceMetricsPhase
        super Phase
-       redef fun process_mainmodule(mainmodule)
+       redef fun process_mainmodule(mainmodule, given_mmodules)
        do
                if not toolcontext.opt_inheritance.value and not toolcontext.opt_all.value then return
+               var csv = toolcontext.opt_csv.value
+               var out = "{toolcontext.opt_dir.value or else "metrics"}/inheritance"
+               out.mkdir
 
                print toolcontext.format_h1("\n# Inheritance metrics")
 
-               var hmetrics = new InheritanceMetricSet
+               var hmetrics = new MetricSet
                hmetrics.register(new MDUI(mainmodule))
                hmetrics.register(new MDUIC(mainmodule))
                hmetrics.register(new MDUII(mainmodule))
@@ -44,7 +45,7 @@ private class InheritanceMetricsPhase
                hmetrics.register(new MIFC(mainmodule))
                hmetrics.register(new MIFI(mainmodule))
 
-               var cmetrics = new MClassMetricSet
+               var cmetrics = new MetricSet
                cmetrics.register(new CNOAC(mainmodule))
                cmetrics.register(new CNOPC(mainmodule))
                cmetrics.register(new CNOCC(mainmodule))
@@ -58,11 +59,11 @@ private class InheritanceMetricsPhase
                var model = toolcontext.modelbuilder.model
                var mmodules = new HashSet[MModule]
                var mclasses = new HashSet[MClass]
-               for mproject in model.mprojects do
+               for mpackage in model.mpackages do
 
-                       print toolcontext.format_h2("\n ## project {mproject}")
+                       print toolcontext.format_h2("\n ## package {mpackage}")
 
-                       for mgroup in mproject.mgroups do
+                       for mgroup in mpackage.mgroups do
                                if mgroup.mmodules.is_empty then continue
 
                                # Scalar metrics
@@ -73,67 +74,27 @@ private class InheritanceMetricsPhase
                                if mod_mclasses.is_empty then continue
                                mmodules.add_all(mgroup.mmodules)
                                mclasses.add_all(mod_mclasses)
-                               cmetrics.collect(new HashSet[MClass].from(mod_mclasses), mainmodule)
-                               for name, metric in cmetrics.metrics do
-                                       if metric isa IntMetric then
-                                               print toolcontext.format_h4("\t{name}: {metric.desc}")
-                                               print toolcontext.format_p("\t    avg: {metric.avg}")
-                                               var max = metric.max
-                                               print toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                               var min = metric.min
-                                               print toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                                       end
-                               end
-                               hmetrics.collect(new HashSet[MModule].from(mgroup.mmodules), mainmodule)
-                               for name, metric in hmetrics.metrics do
-                                       if metric isa FloatMetric then
-                                               print toolcontext.format_h4("\t{name}: {metric.desc}")
-                                               print toolcontext.format_p("\t    avg: {metric.avg}")
-                                               var max = metric.max
-                                               print toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                               var min = metric.min
-                                               print toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                                       end
-                               end
+                               cmetrics.clear
+                               cmetrics.collect(new HashSet[MClass].from(mod_mclasses))
+                               cmetrics.to_console(1, not toolcontext.opt_nocolors.value)
+                               if csv then cmetrics.to_csv.save("{out}/{mgroup}_classes.csv")
+                               hmetrics.clear
+                               hmetrics.collect(new HashSet[MModule].from(mgroup.mmodules))
+                               hmetrics.to_console(1, not toolcontext.opt_nocolors.value)
+                               if csv then hmetrics.to_csv.save("{out}/{mgroup}_inheritance.csv")
                        end
                end
                if not mclasses.is_empty then
                        # Global metrics
                        print toolcontext.format_h2("\n ## global metrics")
-                       cmetrics.collect(mclasses, mainmodule)
-                       for name, metric in cmetrics.metrics do
-                               if metric isa IntMetric then
-                                       print toolcontext.format_h4("\t{name}: {metric.desc}")
-                                       print toolcontext.format_p("\t    avg: {metric.avg}")
-                                       var max = metric.max
-                                       print toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                       var min = metric.min
-                                       print toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                               end
-                       end
-                       hmetrics.collect(mmodules, mainmodule)
-                       for name, metric in hmetrics.metrics do
-                               if metric isa FloatMetric then
-                                       print toolcontext.format_h4("\t{name}: {metric.desc}")
-                                       print toolcontext.format_p("\t    avg: {metric.avg}")
-                                       var max = metric.max
-                                       print toolcontext.format_p("\t    max: {max.first} ({max.second})")
-                                       var min = metric.min
-                                       print toolcontext.format_p("\t    min: {min.first} ({min.second})")
-                               end
-                       end
-               end
-       end
-end
-
-# Metric Set used to collect data about inheritance in each module
-class InheritanceMetricSet
-       super MetricSet
-       redef type METRIC: MModuleMetric
-       fun collect(mmodules: Set[MModule], mainmodule: MModule) do
-               clear
-               for metric in metrics.values do
-                       metric.collect(mmodules)
+                       cmetrics.clear
+                       cmetrics.collect(mclasses)
+                       cmetrics.to_console(1, not toolcontext.opt_nocolors.value)
+                       if csv then cmetrics.to_csv.save("{out}/summary_classes.csv")
+                       hmetrics.clear
+                       hmetrics.collect(mmodules)
+                       hmetrics.to_console(1, not toolcontext.opt_nocolors.value)
+                       if csv then hmetrics.to_csv.save("{out}/summary_inheritance.csv")
                end
        end
 end