Skip to content

Commit

Permalink
Changed gem dependencies from install-time in gemspec to runtime when…
Browse files Browse the repository at this point in the history
… each of the generators is loaded. This allows use of github gems (i.e. relevance-rcov instead of rcov) and also allows you to install only the gems for the metrics you plan on using.
  • Loading branch information
alexrothenberg committed Jun 1, 2009
1 parent d6af508 commit 573afaf
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 8 deletions.
6 changes: 6 additions & 0 deletions lib/base/generator.rb
Expand Up @@ -33,6 +33,7 @@ class Generator
attr_reader :report, :template

def initialize(options={})
self.class.verify_dependencies!
create_metric_dir_if_missing
create_output_dir_if_missing
end
Expand Down Expand Up @@ -119,6 +120,11 @@ def generate_report
def round_to_tenths(decimal)
(decimal * 10).round / 10.0
end

# Allows subclasses to check for required gems
def self.verify_dependencies!
true
end

def emit #:nodoc:
raise <<-EOF
Expand Down
5 changes: 5 additions & 0 deletions lib/generators/flay.rb
Expand Up @@ -3,6 +3,11 @@ module MetricFu

class Flay < Generator

def self.verify_dependencies!
`flay --help`
raise 'sudo gem install flay # if you want the flay tasks' unless $?.success?
end

def emit
files_to_flay = MetricFu.flay[:dirs_to_flay].map{|dir| Dir[File.join(dir, "**/*.rb")] }
@output = `flay #{files_to_flay.join(" ")}`
Expand Down
5 changes: 5 additions & 0 deletions lib/generators/flog.rb
Expand Up @@ -3,6 +3,11 @@ module MetricFu
class Flog < Generator
attr_reader :pages

def self.verify_dependencies!
`flog --help`
raise 'sudo gem install flog # if you want the flog tasks' unless $?.success?
end

SCORE_FORMAT = "%0.2f"
METHOD_LINE_REGEX = /(\d+\.\d+):\s+([A-Za-z:]+#.*)/
OPERATOR_LINE_REGEX = /\s*(\d+\.\d+):\s(.*)$/
Expand Down
10 changes: 10 additions & 0 deletions lib/generators/rcov.rb
Expand Up @@ -5,6 +5,16 @@ module MetricFu
class Rcov < Generator
NEW_FILE_MARKER = ("=" * 80) + "\n"

def self.verify_dependencies!
`flay --help`
unless $?.success?
if RUBY_PLATFORM =~ /java/
raise 'running in jruby - rcov tasks not available'
else
raise 'sudo gem install rcov # if you want the rcov tasks'
end
end
end

class Line
attr_accessor :content, :was_run
Expand Down
5 changes: 5 additions & 0 deletions lib/generators/reek.rb
Expand Up @@ -3,6 +3,11 @@ module MetricFu
class Reek < Generator
REEK_REGEX = /^(\S+) (.*) \((.*)\)$/

def self.verify_dependencies!
`reek --help`
raise 'sudo gem install reek # if you want the reek tasks' unless $?.success?
end

def emit
files_to_reek = MetricFu.reek[:dirs_to_reek].map{|dir| Dir[File.join(dir, "**/*.rb")] }
@output = `reek #{files_to_reek.join(" ")}`
Expand Down
7 changes: 7 additions & 0 deletions lib/generators/roodi.rb
@@ -1,5 +1,12 @@
module MetricFu
class Roodi < Generator

def self.verify_dependencies!
`roodi --help`
raise 'sudo gem install roodi # if you want the roodi tasks' unless $?.success?
end


def emit
files_to_analyze = MetricFu.roodi[:dirs_to_roodi].map{|dir| Dir[File.join(dir, "**/*.rb")] }
@output = `roodi #{files_to_analyze.join(" ")}`
Expand Down
15 changes: 12 additions & 3 deletions lib/generators/saikuro.rb
Expand Up @@ -2,11 +2,12 @@ module MetricFu

class Saikuro < Generator

def self.verify_dependencies!
`ruby "#{saikuro}" --help`
raise "Please check #{saikuro} is a valid installation of saikuro" unless $?.success?
end

def emit
relative_path = [File.dirname(__FILE__), '..', '..',
'vendor', 'saikuro', 'saikuro.rb']
saikuro = File.expand_path(File.join(relative_path))

format_directories

Expand Down Expand Up @@ -81,6 +82,14 @@ def saikuro_results

private

def self.saikuro
relative_path = [File.dirname(__FILE__), '..', '..',
'vendor', 'saikuro', 'saikuro.rb']
File.expand_path(File.join(relative_path))
end
def saikuro
self.class.saikuro
end

end

Expand Down
5 changes: 0 additions & 5 deletions metric_fu.gemspec
Expand Up @@ -24,10 +24,5 @@ Gem::Specification.new do |s|
s.has_rdoc = true
s.rdoc_options = ["--main", "README"]
s.extra_rdoc_files = ["HISTORY", "Manifest.txt", "README"]
s.add_dependency("flay", [">= 1.2.1"])
s.add_dependency("flog", [">= 2.1.0"])
s.add_dependency("rcov", ["> 0.8.1"])
s.add_dependency("reek", [">= 1.0.0"])
s.add_dependency("roodi", [">= 1.3.5"])
s.add_dependency("chronic", [">= 0.2.3"])
end
22 changes: 22 additions & 0 deletions spec/base/generator_spec.rb
Expand Up @@ -154,6 +154,28 @@ def to_h
@concrete_class.should_receive(:to_h)
@concrete_class.generate_report
end

it "should raise error if the concrete class is missing a required dependency" do
concrete_class_with_missing_gem = Class.new(MetricFu::Generator) do
def self.verify_dependencies!
raise 'gem install something # if you want these tasks'
end
end
lambda { concrete_class_with_missing_gem.generate_report }.should raise_error("gem install something # if you want these tasks")
end

end

describe "instantiation" do
it "should fail is dependencies not verified" do
ConcreteClass.should_receive(:verify_dependencies!).and_raise("Missing a required gem. Please 'gem install something'")
lambda { ConcreteClass.new() }.should raise_error("Missing a required gem. Please 'gem install something'")
end

it "should succeed when dependencies verified" do
ConcreteClass.should_receive(:verify_dependencies!).and_return(true)
ConcreteClass.new()
end
end

end
3 changes: 3 additions & 0 deletions spec/generators/flay_spec.rb
@@ -1,6 +1,9 @@
require File.dirname(__FILE__) + '/../spec_helper.rb'

describe Flay do
before :each do
MetricFu::Flay.stub!(:verify_dependencies!).and_return(true)
end
describe "emit method" do
before :each do
MetricFu::Configuration.run {|config| config.flay = { :dirs_to_flay => ['app', 'lib'] } }
Expand Down
1 change: 1 addition & 0 deletions spec/generators/flog_spec.rb
Expand Up @@ -123,6 +123,7 @@
1.2: assignment
1.2: all
HERE
MetricFu::Flog.stub!(:verify_dependencies!).and_return(true)
end

describe "parse method" do
Expand Down
1 change: 1 addition & 0 deletions spec/generators/reek_spec.rb
Expand Up @@ -3,6 +3,7 @@
describe Reek do
describe "analyze method" do
before :each do
MetricFu::Reek.stub!(:verify_dependencies!).and_return(true)
@lines = <<-HERE
"app/controllers/activity_reports_controller.rb" -- 4 warnings:
ActivityReportsController#authorize_user calls current_user.primary_site_ids multiple times (Duplication)
Expand Down

0 comments on commit 573afaf

Please sign in to comment.