Permalink
Browse files

Changed gem dependencies from install-time in gemspec to runtime when…

… 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 May 29, 2009
1 parent d6af508 commit 573afaf19e7a578aa0b161569290169d9298cf96
@@ -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
@@ -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
@@ -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(" ")}`
@@ -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(.*)$/
@@ -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
@@ -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(" ")}`
@@ -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(" ")}`
@@ -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

@@ -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

@@ -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
@@ -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
@@ -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'] } }
@@ -123,6 +123,7 @@
1.2: assignment
1.2: all
HERE
MetricFu::Flog.stub!(:verify_dependencies!).and_return(true)
end

describe "parse method" do
@@ -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)

0 comments on commit 573afaf

Please sign in to comment.