nitmetrics: compute scalar module count
[nit.git] / src / metrics / metrics_base.nit
index e6926af..86d4953 100644 (file)
@@ -21,6 +21,11 @@ import modelbuilder
 
 redef class ToolContext
 
+       # --all
+       var opt_all = new OptionBool("Compute all metrics", "--all")
+
+       # --inheritance
+       var opt_inheritance = new OptionBool("Compute metrics about inheritance usage", "--inheritance")
        # --refinement
        var opt_refinement = new OptionBool("Compute metrics about refinement usage", "--refinement")
        # --self
@@ -33,6 +38,8 @@ redef class ToolContext
        var opt_tables = new OptionBool("Compute tables metrics", "--tables")
        # --rta
        var opt_rta = new OptionBool("Compute RTA metrics", "--rta")
+       # --generate-csv
+       var opt_generate_csv = new OptionBool("Generate CVS format metrics", "--generate-csv")
        # --generate_hyperdoc
        var opt_generate_hyperdoc = new OptionBool("Generate Hyperdoc", "--generate_hyperdoc")
 
@@ -42,12 +49,15 @@ redef class ToolContext
        redef init
        do
                super
+               self.option_context.add_option(opt_all)
+               self.option_context.add_option(opt_inheritance)
                self.option_context.add_option(opt_refinement)
                self.option_context.add_option(opt_self)
                self.option_context.add_option(opt_nullables)
                self.option_context.add_option(opt_static_types)
                self.option_context.add_option(opt_tables)
                self.option_context.add_option(opt_rta)
+               self.option_context.add_option(opt_generate_csv)
                self.option_context.add_option(opt_generate_hyperdoc)
                self.option_context.add_option(opt_dir)
        end
@@ -104,6 +114,42 @@ private class CounterSorter[E: Object]
        redef fun compare(a,b) do return self.counter.map[a] <=> self.counter.map[b]
 end
 
+# Helper class to output metrics as CVS formatted files
+class CSVDocument
+       private var file: String
+       private var header: Array[String] = new Array[String]
+       private var lines: Array[Array[String]] = new Array[Array[String]]
+
+       init(file: String) do self.file = file
+
+       fun set_header(values: Object...) do
+               header.clear
+               for value in values do header.add(value.to_s)
+       end
+
+       fun add_line(values: Object...) do
+               if values.length != header.length then
+                       print "CSV error: header declares {header.length} columns, line contains {values.length} values"
+                       abort
+               end
+               var line = new Array[String]
+               for value in values do line.add(value.to_s)
+               lines.add(line)
+       end
+
+       redef fun to_s do
+               var str = header.join(";") + "\n"
+               for line in lines do str += line.join(";") + "\n"
+               return str
+       end
+
+       fun save do
+               var out = new OFStream.open(self.file)
+               out.write(self.to_s)
+               out.close
+       end
+end
+
 # Helper function to display n/d and handle division by 0
 fun div(n: Int, d: Int): String
 do