edouard-metric_fu 1.0.3.2 → 1.0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. data/lib/base/base_template.rb +134 -0
  2. data/lib/base/configuration.rb +187 -0
  3. data/lib/base/generator.rb +147 -0
  4. data/lib/base/md5_tracker.rb +52 -0
  5. data/lib/base/report.rb +100 -0
  6. data/lib/generators/churn.rb +86 -0
  7. data/lib/generators/flay.rb +29 -0
  8. data/lib/generators/flog.rb +127 -0
  9. data/lib/generators/rcov.rb +77 -0
  10. data/lib/generators/reek.rb +32 -0
  11. data/lib/generators/roodi.rb +24 -0
  12. data/lib/generators/saikuro.rb +211 -0
  13. data/lib/generators/stats.rb +43 -0
  14. data/lib/metric_fu.rb +21 -0
  15. data/lib/templates/awesome/awesome_template.rb +30 -0
  16. data/lib/templates/awesome/churn.html.erb +19 -0
  17. data/lib/templates/awesome/default.css +62 -0
  18. data/lib/templates/awesome/flay.html.erb +22 -0
  19. data/lib/templates/awesome/flog.html.erb +42 -0
  20. data/lib/templates/awesome/index.html.erb +28 -0
  21. data/lib/templates/awesome/rcov.html.erb +32 -0
  22. data/lib/templates/awesome/reek.html.erb +30 -0
  23. data/lib/templates/awesome/roodi.html.erb +17 -0
  24. data/lib/templates/awesome/saikuro.html.erb +71 -0
  25. data/lib/templates/awesome/stats.html.erb +41 -0
  26. data/lib/templates/standard/churn.html.erb +30 -0
  27. data/lib/templates/standard/default.css +64 -0
  28. data/lib/templates/standard/flay.html.erb +33 -0
  29. data/lib/templates/standard/flog.html.erb +52 -0
  30. data/lib/templates/standard/index.html.erb +38 -0
  31. data/lib/templates/standard/rcov.html.erb +42 -0
  32. data/lib/templates/standard/reek.html.erb +41 -0
  33. data/lib/templates/standard/roodi.html.erb +28 -0
  34. data/lib/templates/standard/saikuro.html.erb +83 -0
  35. data/lib/templates/standard/standard_template.rb +26 -0
  36. data/lib/templates/standard/stats.html.erb +54 -0
  37. data/tasks/metric_fu.rake +15 -0
  38. data/tasks/railroad.rake +39 -0
  39. data/vendor/saikuro/saikuro.rb +1214 -0
  40. metadata +40 -1
@@ -0,0 +1,134 @@
1
+ module MetricFu
2
+
3
+ # The Template class is intended as an abstract class for concrete
4
+ # template classes to subclass. It provides a variety of utility
5
+ # methods to make templating a bit easier. However, classes do not
6
+ # have to inherit from here in order to provide a template. The only
7
+ # requirement for a template class is that it provides a #write method
8
+ # to actually write out the template. See StandardTemplate for an
9
+ # example.
10
+ class Template
11
+ attr_accessor :report
12
+
13
+ private
14
+ # Creates a new erb evaluated result from the passed in section.
15
+ #
16
+ # @param section String
17
+ # The section name of
18
+ #
19
+ # @return String
20
+ # The erb evaluated string
21
+ def erbify(section)
22
+ require 'erb'
23
+ erb_doc = File.read(template(section))
24
+ ERB.new(erb_doc).result(binding)
25
+ end
26
+
27
+ # Determines whether a template file exists for a given section
28
+ # of the full template.
29
+ #
30
+ # @param section String
31
+ # The section of the template to check against
32
+ #
33
+ # @return Boolean
34
+ # Does a template file exist for this section or not?
35
+ def template_exists?(section)
36
+ File.exist?(template(section))
37
+ end
38
+
39
+ # Copies an instance variable mimicing the name of the section
40
+ # we are trying to render, with a value equal to the passed in
41
+ # constant. Allows the concrete template classes to refer to
42
+ # that instance variable from their ERB rendering
43
+ #
44
+ # @param section String
45
+ # The name of the instance variable to create
46
+ #
47
+ # @param contents Object
48
+ # The value to set as the value of the created instance
49
+ # variable
50
+ def create_instance_var(section, contents)
51
+ instance_variable_set("@#{section}", contents)
52
+ end
53
+
54
+ # Generates the filename of the template file to load and
55
+ # evaluate. In this case, the path to the template directory +
56
+ # the section name + .html.erb
57
+ #
58
+ # @param section String
59
+ # A section of the template to render
60
+ #
61
+ # @return String
62
+ # A file path
63
+ def template(section)
64
+ File.join(this_directory, section.to_s + ".html.erb")
65
+ end
66
+
67
+ # Returns the filename that the template will render into for
68
+ # a given section. In this case, the section name + '.html'
69
+ #
70
+ # @param section String
71
+ # A section of the template to render
72
+ #
73
+ # @return String
74
+ # The output filename
75
+ def output_filename(section)
76
+ section.to_s + ".html"
77
+ end
78
+
79
+ # Returns the contents of a given css file in order to
80
+ # render it inline into a template.
81
+ #
82
+ # @param css String
83
+ # The name of a css file to open
84
+ #
85
+ # @return String
86
+ # The contents of the css file
87
+ def inline_css(css)
88
+ open(File.join(this_directory, css)) { |f| f.read }
89
+ end
90
+
91
+ # Provides a link to open a file through the textmate protocol
92
+ # on Darwin, or otherwise, a simple file link.
93
+ #
94
+ # @param name String
95
+ #
96
+ # @param line Integer
97
+ # The line number to link to, if textmate is available. Defaults
98
+ # to nil
99
+ #
100
+ # @return String
101
+ # An anchor link to a textmate reference or a file reference
102
+ def link_to_filename(name, line = nil)
103
+ filename = File.expand_path(name)
104
+ if MetricFu.configuration.platform.include?('darwin')
105
+ "<a href='txmt://open/?url=file://" \
106
+ +"#{filename}&line=#{line}'>#{name}:#{line}</a>"
107
+ else
108
+ "<a href='file://#{filename}'>#{name}:#{line}</a>"
109
+ end
110
+ end
111
+
112
+
113
+ # Provides a brain dead way to cycle between two values during
114
+ # an iteration of some sort. Pass in the first_value, the second_value,
115
+ # and the cardinality of the iteration.
116
+ #
117
+ # @param first_value Object
118
+ #
119
+ # @param second_value Object
120
+ #
121
+ # @param iteration Integer
122
+ # The number of times through the iteration.
123
+ #
124
+ # @return Object
125
+ # The first_value if iteration is even. The second_value if
126
+ # iteration is odd.
127
+ def cycle(first_value, second_value, iteration)
128
+ return first_value if iteration % 2 == 0
129
+ return second_value
130
+ end
131
+
132
+
133
+ end
134
+ end
@@ -0,0 +1,187 @@
1
+ module MetricFu
2
+
3
+ # A list of metrics which are available in the MetricFu system.
4
+ #
5
+ # These are metrics which have been developed for the system. Of
6
+ # course, in order to use these metrics, their respective gems must
7
+ # be installed on the system.
8
+ AVAILABLE_METRICS = [:churn, :flog, :flay, :reek,
9
+ :roodi, :saikuro, :rcov]
10
+
11
+
12
+ # The @@configuration class variable holds a global type configuration
13
+ # object for any parts of the system to use.
14
+ def self.configuration
15
+ @@configuration ||= Configuration.new
16
+ end
17
+
18
+ # = Configuration
19
+ #
20
+ # The Configuration class, as it sounds, provides methods for
21
+ # configuring the behaviour of MetricFu.
22
+ #
23
+ # == Customization for Rails
24
+ #
25
+ # The Configuration class checks for the presence of a
26
+ # 'config/environment.rb' file. If the file is present, it assumes
27
+ # it is running in a Rails project. If it is, it will:
28
+ #
29
+ # * Add 'app' to the @code_dirs directory to include the
30
+ # code in the app directory in the processing
31
+ # * Add :stats to the list of metrics to run to get the Rails stats
32
+ # task
33
+ #
34
+ # == Customization for CruiseControl.rb
35
+ #
36
+ # The Configuration class checks for the presence of a
37
+ # 'CC_BUILD_ARTIFACTS' environment variable. If it's found
38
+ # it will change the default output directory from the default
39
+ # "tmp/metric_fu to the directory represented by 'CC_BUILD_ARTIFACTS'
40
+ #
41
+ # == Deprications
42
+ #
43
+ # The Configuration class checks for several deprecated constants
44
+ # that were previously used to configure MetricFu. These include
45
+ # CHURN_OPTIONS, DIRECTORIES_TO_FLOG, SAIKURO_OPTIONS,
46
+ # and MetricFu::SAIKURO_OPTIONS.
47
+ #
48
+ # These have been replaced by config.churn, config.flog and
49
+ # config.saikuro respectively.
50
+ class Configuration
51
+
52
+ def initialize #:nodoc:#
53
+ warn_about_deprecated_config_options
54
+ reset
55
+ add_attr_accessors_to_self
56
+ add_class_methods_to_metric_fu
57
+ end
58
+
59
+ # Searches through the instance variables of the class and
60
+ # creates a class method on the MetricFu module to read the value
61
+ # of the instance variable from the Configuration class.
62
+ def add_class_methods_to_metric_fu
63
+ instance_variables.each do |name|
64
+ method_name = name[1..-1].to_sym
65
+ method = <<-EOF
66
+ def self.#{method_name}
67
+ configuration.send(:#{method_name})
68
+ end
69
+ EOF
70
+ MetricFu.module_eval(method)
71
+ end
72
+ end
73
+
74
+ # Searches through the instance variables of the class and creates
75
+ # an attribute accessor on this instance of the Configuration
76
+ # class for each instance variable.
77
+ def add_attr_accessors_to_self
78
+ instance_variables.each do |name|
79
+ method_name = name[1..-1].to_sym
80
+ MetricFu::Configuration.send(:attr_accessor, method_name)
81
+ end
82
+ end
83
+
84
+ # Check if certain constants that are deprecated have been
85
+ # assigned. If so, warn the user about them, and the
86
+ # fact that they will have no effect.
87
+ def warn_about_deprecated_config_options
88
+ if defined?(::MetricFu::CHURN_OPTIONS)
89
+ raise("Use config.churn instead of MetricFu::CHURN_OPTIONS")
90
+ end
91
+ if defined?(::MetricFu::DIRECTORIES_TO_FLOG)
92
+ raise("Use config.flog[:dirs_to_flog] "+
93
+ "instead of MetricFu::DIRECTORIES_TO_FLOG")
94
+ end
95
+ if defined?(::MetricFu::SAIKURO_OPTIONS)
96
+ raise("Use config.saikuro instead of MetricFu::SAIKURO_OPTIONS")
97
+ end
98
+ if defined?(SAIKURO_OPTIONS)
99
+ raise("Use config.saikuro instead of SAIKURO_OPTIONS")
100
+ end
101
+ end
102
+
103
+ # This allows us to have a nice syntax like:
104
+ #
105
+ # MetricFu.run do |config|
106
+ # config.base_directory = 'tmp/metric_fu'
107
+ # end
108
+ #
109
+ # See the README for more information on configuration options.
110
+ def self.run
111
+ yield MetricFu.configuration
112
+ end
113
+
114
+ # This does the real work of the Configuration class, by setting
115
+ # up a bunch of instance variables to represent the configuration
116
+ # of the MetricFu app.
117
+ def reset
118
+ @base_directory = ENV['CC_BUILD_ARTIFACTS'] || 'tmp/metric_fu'
119
+ @scratch_directory = File.join(@base_directory, 'scratch')
120
+ @output_directory = File.join(@base_directory, 'output')
121
+ @metric_fu_root_directory = File.join(File.dirname(__FILE__),
122
+ '..', '..')
123
+ @template_directory = File.join(@metric_fu_root_directory,
124
+ 'lib', 'templates')
125
+ @template_class = StandardTemplate
126
+ set_metrics
127
+ set_code_dirs
128
+ @flay = { :dirs_to_flay => @code_dirs }
129
+ @flog = { :dirs_to_flog => @code_dirs }
130
+ @reek = { :dirs_to_reek => @code_dirs }
131
+ @roodi = { :dirs_to_roodi => @code_dirs }
132
+ @saikuro = { :output_directory => @scratch_directory + '/saikuro',
133
+ :input_directory => @code_dirs,
134
+ :cyclo => "",
135
+ :filter_cyclo => "0",
136
+ :warn_cyclo => "5",
137
+ :error_cyclo => "7",
138
+ :formater => "text"}
139
+ @churn = {}
140
+ @stats = {}
141
+ @rcov = { :test_files => ['test/**/*_test.rb',
142
+ 'spec/**/*_spec.rb'],
143
+ :rcov_opts => ["--sort coverage",
144
+ "--no-html",
145
+ "--text-coverage",
146
+ "--no-color",
147
+ "--profile",
148
+ "--rails",
149
+ "--exclude /gems/,/Library/,/usr/,spec"]}
150
+ end
151
+
152
+ # Perform a simple check to try and guess if we're running
153
+ # against a rails app.
154
+ #
155
+ # @todo This should probably be made a bit more robust.
156
+ def rails?
157
+ @rails = File.exist?("config/environment.rb")
158
+ end
159
+
160
+ # Add the :stats task to the AVAILABLE_METRICS constant if we're
161
+ # running within rails.
162
+ def set_metrics
163
+ if rails?
164
+ @metrics = MetricFu::AVAILABLE_METRICS + [:stats]
165
+ else
166
+ @metrics = MetricFu::AVAILABLE_METRICS
167
+ end
168
+ end
169
+
170
+ # Add the 'app' directory if we're running within rails.
171
+ def set_code_dirs
172
+ if rails?
173
+ @code_dirs = ['app', 'lib']
174
+ else
175
+ @code_dirs = ['lib']
176
+ end
177
+ end
178
+
179
+ def platform #:nodoc:
180
+ return PLATFORM
181
+ end
182
+
183
+ def is_cruise_control_rb?
184
+ !!ENV['CC_BUILD_ARTIFACTS']
185
+ end
186
+ end
187
+ end
@@ -0,0 +1,147 @@
1
+ module MetricFu
2
+
3
+ # = Generator
4
+ #
5
+ # The Generator class is an abstract class that provides the
6
+ # skeleton for producing different types of metrics.
7
+ #
8
+ # It drives the production of the metrics through a template
9
+ # method - #generate_report(options={}). This method calls
10
+ # #emit, #analyze and #to_h in order to produce the metrics.
11
+ #
12
+ # To implement a concrete class to generate a metric, therefore,
13
+ # the class must implement those three methods.
14
+ #
15
+ # * #emit should take care of running the metric tool and
16
+ # gathering its output.
17
+ # * #analyze should take care of manipulating the output from
18
+ # #emit and making it possible to store it in a programmatic way.
19
+ # * #to_h should provide a hash representation of the output from
20
+ # #analyze ready to be serialized into yaml at some point.
21
+ #
22
+ # == Pre-conditions
23
+ #
24
+ # Based on the class name of the concrete class implementing a
25
+ # Generator, the Generator class will create a 'metric_directory'
26
+ # named after the class under the MetricFu.scratch_directory, where
27
+ # any output from the #emit method should go.
28
+ #
29
+ # It will also create the MetricFu.output_directory if neccessary, and
30
+ # in general setup the directory structure that the MetricFu system
31
+ # expects.
32
+ class Generator
33
+ attr_reader :report, :template
34
+
35
+ def initialize(options={})
36
+ create_metric_dir_if_missing
37
+ create_output_dir_if_missing
38
+ end
39
+
40
+ # Creates a new generator and returns the output of the
41
+ # #generate_report method. This is the typical way to
42
+ # generate a new MetricFu report. For more information see
43
+ # the #generate_report instance method.
44
+ #
45
+ # @params options Hash
46
+ # A currently unused hash to configure the Generator
47
+ #
48
+ # @see generate_report
49
+ def self.generate_report(options={})
50
+ generator = self.new(options)
51
+ generator.generate_report
52
+ end
53
+
54
+ # Provides the unqualified class name of an implemented concrete
55
+ # class, as a string. For example:
56
+ #
57
+ # class Flay < Generator; end
58
+ # klass = Flay.new
59
+ # klass.class_name
60
+ # > "flay"
61
+ #
62
+ # @return String
63
+ # The unqualified class name of this concrete class, returned
64
+ # as a string.
65
+ def self.class_name
66
+ self.to_s.split('::').last.downcase
67
+ end
68
+
69
+ # Returns the directory where the Generator will write any output
70
+ def self.metric_directory
71
+ File.join(MetricFu.scratch_directory, class_name)
72
+ end
73
+
74
+ def create_metric_dir_if_missing #:nodoc:
75
+ unless File.directory?(metric_directory)
76
+ FileUtils.mkdir_p(metric_directory, :verbose => false)
77
+ end
78
+ end
79
+
80
+ def create_output_dir_if_missing #:nodoc:
81
+ unless File.directory?(MetricFu.output_directory)
82
+ FileUtils.mkdir_p(MetricFu.output_directory, :verbose => false)
83
+ end
84
+ end
85
+
86
+ # @return String
87
+ # The path of the metric directory this class is using.
88
+ def metric_directory
89
+ self.class.metric_directory
90
+ end
91
+
92
+
93
+ # Defines some hook methods for the concrete classes to hook into.
94
+ %w[emit analyze].each do |meth|
95
+ define_method("before_#{meth}".to_sym) {}
96
+ define_method("after_#{meth}".to_sym) {}
97
+ end
98
+ define_method("before_to_h".to_sym) {}
99
+
100
+ # Provides a template method to drive the production of a metric
101
+ # from a concrete implementation of this class. Each concrete
102
+ # class must implement the three methods that this template method
103
+ # calls: #emit, #analyze and #to_h. For more details, see the
104
+ # class documentation.
105
+ #
106
+ # This template method also calls before_emit, after_emit... etc.
107
+ # methods to allow extra hooks into the processing methods, and help
108
+ # to keep the logic of your Generators clean.
109
+ def generate_report
110
+ %w[emit analyze].each do |meth|
111
+ send("before_#{meth}".to_sym)
112
+ send("#{meth}".to_sym)
113
+ send("after_#{meth}".to_sym)
114
+ end
115
+ before_to_h()
116
+ to_h()
117
+ end
118
+
119
+ def round_to_tenths(decimal)
120
+ (decimal * 10).round / 10.0
121
+ end
122
+
123
+ def emit #:nodoc:
124
+ raise <<-EOF
125
+ This method must be implemented by a concrete class descending
126
+ from Generator. See generator class documentation for more
127
+ information.
128
+ EOF
129
+ end
130
+
131
+ def analyze #:nodoc:
132
+ raise <<-EOF
133
+ This method must be implemented by a concrete class descending
134
+ from Generator. See generator class documentation for more
135
+ information.
136
+ EOF
137
+ end
138
+
139
+ def to_h #:nodoc:
140
+ raise <<-EOF
141
+ This method must be implemented by a concrete class descending
142
+ from Generator. See generator class documentation for more
143
+ information.
144
+ EOF
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,52 @@
1
+ require 'digest/md5'
2
+ require 'fileutils'
3
+
4
+ module MetricFu
5
+ class MD5Tracker
6
+
7
+ @@unchanged_md5s = []
8
+
9
+ class << self
10
+ def md5_dir(path_to_file, base_dir)
11
+ File.join(base_dir,
12
+ path_to_file.split('/')[0..-2].join('/'))
13
+ end
14
+
15
+ def md5_file(path_to_file, base_dir)
16
+ File.join(md5_dir(path_to_file, base_dir),
17
+ path_to_file.split('/').last.sub(/\.[a-z]+/, '.md5'))
18
+ end
19
+
20
+ def track(path_to_file, base_dir)
21
+ md5 = Digest::MD5.hexdigest(File.read(path_to_file))
22
+ FileUtils.mkdir_p(md5_dir(path_to_file, base_dir), :verbose => false)
23
+ f = File.new(md5_file(path_to_file, base_dir), "w")
24
+ f.puts(md5)
25
+ f.close
26
+ md5
27
+ end
28
+
29
+ def file_changed?(path_to_file, base_dir)
30
+ orig_md5_file = md5_file(path_to_file, base_dir)
31
+ return !!track(path_to_file, base_dir) unless File.exist?(orig_md5_file)
32
+
33
+ current_md5 = ""
34
+ file = File.open(orig_md5_file, 'r')
35
+ file.each_line { |line| current_md5 << line }
36
+ file.close
37
+ current_md5.chomp!
38
+
39
+ new_md5 = Digest::MD5.hexdigest(File.read(path_to_file))
40
+ new_md5.chomp!
41
+
42
+ @@unchanged_md5s << path_to_file if new_md5 == current_md5
43
+
44
+ return new_md5 != current_md5
45
+ end
46
+
47
+ def file_already_counted?(path_to_file)
48
+ return @@unchanged_md5s.include?(path_to_file)
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,100 @@
1
+ module MetricFu
2
+
3
+ # MetricFu.report memoizes access to a Report object, that will be
4
+ # used throughout the lifecycle of the MetricFu app.
5
+ def self.report
6
+ @report ||= Report.new
7
+ end
8
+
9
+ # = Report
10
+ #
11
+ # The Report class is responsible two things:
12
+ #
13
+ # It adds information to the yaml report, produced by the system
14
+ # as a whole, for each of the generators used in this test run.
15
+ #
16
+ # It also handles passing the information from each generator used
17
+ # in this test run out to the template class set in
18
+ # MetricFu::Configuration.
19
+ class Report
20
+
21
+ # Renders the result of the report_hash into a yaml serialization
22
+ # ready for writing out to a file.
23
+ #
24
+ # @return YAML
25
+ # A YAML object containing the results of the report generation
26
+ # process
27
+ def to_yaml
28
+ report_hash.to_yaml
29
+ end
30
+
31
+
32
+ def report_hash #:nodoc:
33
+ @report_hash ||= {}
34
+ end
35
+
36
+ # Instantiates a new template class based on the configuration set
37
+ # in MetricFu::Configuration, or through the MetricFu.config block
38
+ # in your rake file (defaults to the included StandardTemplate) and
39
+ # assigns the report_hash to the report_hash to the template and
40
+ # asks itself to write itself out.
41
+ def save_templatized_report
42
+ @template = MetricFu.template_class.new
43
+ @template.report = report_hash
44
+ @template.write
45
+ end
46
+
47
+ # Adds a hash from a passed report, produced by one of the Generator
48
+ # classes to the aggregate report_hash managed by this hash.
49
+ #
50
+ # @param report_type Hash
51
+ # The hash to add to the aggregate report_hash
52
+ def add(report_type)
53
+ clazz = MetricFu.const_get(report_type.to_s.capitalize)
54
+ report_hash.merge!(clazz.generate_report)
55
+ end
56
+
57
+ # Saves the passed in content to the passed in directory. If
58
+ # a filename is passed in it will be used as the name of the
59
+ # file, otherwise it will default to 'index.html'
60
+ #
61
+ # @param content String
62
+ # A string containing the content (usually html) to be written
63
+ # to the file.
64
+ #
65
+ # @param dir String
66
+ # A dir containing the path to the directory to write the file in.
67
+ #
68
+ # @param file String
69
+ # A filename to save the path as. Defaults to 'index.html'.
70
+ #
71
+ def save_output(content, dir, file='index.html')
72
+ open("#{dir}/#{file}", "w") do |f|
73
+ f.puts content
74
+ end
75
+ end
76
+
77
+ # Checks to discover whether we should try and open the results
78
+ # of the report in the browser on this system. We only try and open
79
+ # in the browser if we're on OS X and we're not running in a
80
+ # CruiseControl.rb environment. See MetricFu.configuration for more
81
+ # details about how we make those guesses.
82
+ #
83
+ # @return Boolean
84
+ # Should we open in the browser or not?
85
+ def open_in_browser?
86
+ MetricFu.configuration.platform.include?('darwin') &&
87
+ ! MetricFu.configuration.is_cruise_control_rb?
88
+ end
89
+
90
+ # Shows 'index.html' from the passed directory in the browser
91
+ # if we're able to open the browser on this platform.
92
+ #
93
+ # @param dir String
94
+ # The directory path where the 'index.html' we want to open is
95
+ # stored
96
+ def show_in_browser(dir)
97
+ system("open #{dir}/index.html") if open_in_browser?
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,86 @@
1
+ require 'chronic'
2
+ require 'generator'
3
+ module MetricFu
4
+
5
+ class Churn < Generator
6
+
7
+ def initialize(options={})
8
+ super
9
+ if File.exist?(".git")
10
+ @source_control = Git.new(MetricFu.churn[:start_date])
11
+ elsif File.exist?(".svn")
12
+ @source_control = Svn.new(MetricFu.churn[:start_date])
13
+ else
14
+ raise "Churning requires a subversion or git repo"
15
+ end
16
+ @minimum_churn_count = MetricFu.churn[:minimum_churn_count] || 5
17
+ end
18
+
19
+ def emit
20
+ @changes = parse_log_for_changes.reject {|file, change_count| change_count < @minimum_churn_count}
21
+ end
22
+
23
+ def analyze
24
+ @changes = @changes.to_a.sort {|x,y| y[1] <=> x[1]}
25
+ @changes = @changes.map {|change| {:file_path => change[0], :times_changed => change[1] }}
26
+ end
27
+
28
+ def to_h
29
+ {:churn => {:changes => @changes}}
30
+ end
31
+
32
+ private
33
+
34
+ def parse_log_for_changes
35
+ changes = {}
36
+
37
+ logs = @source_control.get_logs
38
+ logs.each do |line|
39
+ changes[line] ? changes[line] += 1 : changes[line] = 1
40
+ end
41
+ changes
42
+ end
43
+
44
+
45
+ class SourceControl
46
+ def initialize(start_date=nil)
47
+ @start_date = start_date
48
+ end
49
+ end
50
+
51
+ class Git < SourceControl
52
+ def get_logs
53
+ `git log #{date_range} --name-only --pretty=format:`.split(/\n/).reject{|line| line == ""}
54
+ end
55
+
56
+ private
57
+ def date_range
58
+ if @start_date
59
+ date = Chronic.parse(@start_date)
60
+ "--after=#{date.strftime('%Y-%m-%d')}"
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ class Svn < SourceControl
67
+ def get_logs
68
+ `svn log #{date_range} --verbose`.split(/\n/).map { |line| clean_up_svn_line(line) }.compact
69
+ end
70
+
71
+ private
72
+ def date_range
73
+ if @start_date
74
+ date = Chronic.parse(@start_date)
75
+ "--revision {#{date.strftime('%Y-%m-%d')}}:{#{Time.now.strftime('%Y-%m-%d')}}"
76
+ end
77
+ end
78
+
79
+ def clean_up_svn_line(line)
80
+ m = line.match(/\W*[A,M]\W+(\/.*)\b/)
81
+ m ? m[1] : nil
82
+ end
83
+ end
84
+
85
+ end
86
+ end