module MetricFu
see github.com/grosser/pru/blob/master/bin/pru
CodeRay options used to analyze source code, because object Tokens is a list of tokens with specified types. :tab_width – tabulation width in spaces. Default: 8 :css – how to include the styles (:class и :style). Default: :class)
:wrap – wrap result in html tag :page, :div, :span or not to wrap (nil)
:line_numbers – how render line numbers (:table, :inline, :list or nil)
:line_number_start – first line number
:bold_every – make every n-th line number bold. Default: 10
Encapsulates the configuration options for each metric
Reads and writes external coverage files as BINARY
The MetricsTemplate class is the included template used by the HTML formatter. The only requirement for a template class is that it provides a write method to actually write out the template.
Creates an HTML document for a given analyzed file, with scored metrics annotating the relevant line.
Constants
- APP_ROOT
- LIB_ROOT
- LOADER
- SexpNode
- VERSION
Public Class Methods
The @configuration class variable holds a global type configuration object for any parts of the system to use. TODO Configuration
should probably be a singleton class
# File lib/metric_fu/configuration.rb, line 9 def self.configuration @configuration ||= Configuration.new end
# File lib/metric_fu/configuration.rb, line 13 def self.configure configuration.tap(&:configure_metrics) end
# File lib/metric_fu/reporting/graphs/graph.rb, line 2 def self.graph @graph ||= Graph.new end
# File lib/metric_fu/logger.rb, line 4 def self.logger @logger ||= ::MetricFu::Logger.new($stdout) end
# File lib/metric_fu/environment.rb, line 22 def MetricFu.rails? exists end
MetricFu.result
memoizes access to a Result
object, that will be used throughout the lifecycle of the MetricFu
app.
# File lib/metric_fu/reporting/result.rb, line 4 def self.result @result ||= Result.new end
Public Instance Methods
# File lib/metric_fu.rb, line 99 def artifact_dir MetricFu::Io::FileSystem.artifact_dir end
# File lib/metric_fu.rb, line 103 def artifact_subdirs %w(scratch output _data) end
Adds method x_dir relative to the metric_fu artifact directory to the given klass
And strips any leading non-alphanumerical character from the directory name
@param klass [Class] the klass to add methods for the specified artifact sub-directories @yieldreturn [Array<String>] Takes a list of directories and adds readers for each @example For the artifact sub-directory '_scratch', creates on the klass one method:
::scratch_dir (which notably has the leading underscore removed)
# File lib/metric_fu/loader.rb, line 52 def create_artifact_subdirs(klass) class << klass Array(yield).each do |dir| define_method("#{dir.gsub(/[^A-Za-z0-9]/, '')}_dir") do File.join(artifact_dir, dir) end end end end
TODO: Reduce duplication of directory logic Adds methods x_dir and _x_require for the directory x,
relative to the metric_fu lib, to the given klass
@param klass [Class] the klass to add methods for the specified directories @yieldreturn [Array<String>] Takes a list of directories and adds readers for each @example For the directory 'metrics', which is relative to the metric_fu lib directory,
creates on the klass two methods: ::metrics_dir which returns the full path ::metrics_require which takes a block of files to require relative to the metrics_dir
# File lib/metric_fu/loader.rb, line 35 def create_dirs(klass) class << klass Array(yield).each do |dir| define_method("#{dir}_dir") do File.join(lib_dir, dir) end module_eval(%(def #{dir}_require(&block); lib_require('#{dir}', &block); end), __FILE__, __LINE__) end end end
The time the metrics are generated
# File lib/metric_fu.rb, line 67 def current_time Time.now.localtime end
# File lib/metric_fu.rb, line 75 def lib_dir LIB_ROOT end
# File lib/metric_fu.rb, line 88 def library_dirs %w(metrics formatter reporting logging errors data_structures tasks) end
# File lib/metric_fu/loader.rb, line 81 def load_installed_metrics MetricFu.lib_require { "metric" } Dir.glob(File.join(MetricFu.metrics_dir, "**/metric.rb")).each do |metric_config| require metric_config end end
# File lib/metric_fu/loader.rb, line 74 def load_metric_configuration MetricFu.lib_require { "configuration" } load_installed_metrics MetricFu.configuration.configure_metrics load_user_configuration end
Load specified task task only once
if and only if rake is required and the task is not yet defined to prevent the task from being loaded multiple times
@param tasks_relative_path [String] 'metric_fu.rake' by default @param options [Hash] optional task_name to check if loaded @option options [String] :task_name The task_name to load, if not yet loaded
# File lib/metric_fu/loader.rb, line 99 def load_tasks(tasks_relative_path, options = { task_name: "" }) if defined?(Rake::Task) and not Rake::Task.task_defined?(options[:task_name]) load File.join(@lib_root, "tasks", *Array(tasks_relative_path)) end end
# File lib/metric_fu/loader.rb, line 88 def load_user_configuration file = File.join(MetricFu.run_dir, ".metrics") load file if File.exist?(file) end
# File lib/metric_fu.rb, line 81 def loader LOADER end
# File lib/metric_fu.rb, line 19 def metric_name "MetricFu" end
# File lib/metric_fu.rb, line 15 def metric_url "https://github.com/metricfu/metric_fu" end
# File lib/metric_fu.rb, line 48 def report_date_string report_time.strftime("%Y%m%d") end
Non-date-specific; Used to uniquely identify a report run
# File lib/metric_fu.rb, line 62 def report_fingerprint report_time.to_i.to_s end
@return [String] @example '20140323' Used to uniquely identify reports as a sortable reference to when the report was generated
# File lib/metric_fu.rb, line 57 def report_id report_date_string end
# File lib/metric_fu.rb, line 35 def report_name @report_name || self.report_name = run_path.basename.to_s end
# File lib/metric_fu.rb, line 39 def report_name=(report_name) @report_name = report_name end
The time at analyzed code state
# File lib/metric_fu.rb, line 44 def report_time Time.now end
# File lib/metric_fu.rb, line 113 def reset # TODO Don't like how this method needs to know # all of these class variables that are defined # in separate classes. @configuration = nil @graph = nil @result = nil end
# File lib/metric_fu.rb, line 11 def root @app_root ||= Pathname(APP_ROOT) end
# File lib/metric_fu.rb, line 71 def root_dir APP_ROOT end
# File lib/metric_fu.rb, line 122 def run(options) MetricFu::Run.new.run(options) end
# File lib/metric_fu.rb, line 23 def run_dir @run_dir ||= Dir.pwd end
# File lib/metric_fu.rb, line 31 def run_dir=(run_dir) @run_dir = run_dir end
# File lib/metric_fu.rb, line 126 def run_only(metrics_to_run_names, options) metrics_to_run_names = Array(metrics_to_run_names).map(&:to_s) MetricFu::Configuration.run do |config| config.configure_metrics.each do |metric| metric_name = metric.name.to_s if metrics_to_run_names.include?(metric_name) p "Enabling #{metric_name}" metric.enabled = true else p "Disabling #{metric_name}" metric.enabled = false end end end run(options) end
# File lib/metric_fu.rb, line 27 def run_path Pathname(run_dir) end
# File lib/metric_fu/loader.rb, line 62 def setup MetricFu.lib_require { "logger" } MetricFu.logger.debug_on = !!(ENV["MF_DEBUG"] =~ /true/i) load_metric_configuration MetricFu.lib_require { "reporter" } MetricFu.reporting_require { "result" } MetricFu.load_tasks("metric_fu.rake", task_name: "metrics:all") end