nitmetrics: add CVS output facilities for inheritance metrics
[nit.git] / src / metrics / metrics_base.nit
index aaeb6ea..86d4953 100644 (file)
@@ -20,12 +20,45 @@ module metrics_base
 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
+       var opt_self = new OptionBool("Compute metrics about the usage of explicit and implicit self", "--self")
+       # --nullables
+       var opt_nullables = new OptionBool("Compute metrics on nullables send", "--nullables")
+       # --static-types
+       var opt_static_types = new OptionBool("Compute explicit static types metrics", "--static-types")
+       # --tables
+       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")
+
        var opt_dir = new OptionString("Directory where some statistics files are generated", "-d", "--dir")
        var output_dir: String = "."
 
        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
 
@@ -81,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