refactor near default values of parameters

This commit is contained in:
Tomasz Gieniusz 2014-06-22 15:26:03 +02:00
parent 06f7daa860
commit c3caf0180a
6 changed files with 35 additions and 25 deletions

View File

@ -36,7 +36,7 @@ It browses the repository and outputs html page with statistics.
Options: Options:
p, [--path=PATH] # Path to repository from which statistics should be generated. p, [--path=PATH] # Path to repository from which statistics should be generated.
# Default: . # Default: .
o, [--output=OUTPUT] # Output path where statistics should be written. o, [--out-path=OUT_PATH] # Output path where statistics should be written.
# Default: ./git_stats # Default: ./git_stats
l, [--language=LANGUAGE] # Language of written statistics. l, [--language=LANGUAGE] # Language of written statistics.
# Default: en # Default: en
@ -47,7 +47,7 @@ It browses the repository and outputs html page with statistics.
d, [--tree=TREE] # Tree where statistics should be generated. d, [--tree=TREE] # Tree where statistics should be generated.
# Default: . # Default: .
c, [--comment=COMMENT] # The string which is used for comments. c, [--comment=COMMENT] # The string which is used for comments.
# Default: /// # Default: //

View File

@ -4,18 +4,18 @@ require "thor"
class GitStats::CLI < Thor class GitStats::CLI < Thor
option :path, :aliases => :p, :default => '.', :desc => 'Path to repository from which statistics should be generated.' option :path, :aliases => :p, :default => '.', :desc => 'Path to repository from which statistics should be generated.'
option :output, :aliases => :o, :default => './git_stats', :desc => 'Output path where statistics should be written.' option :out_path, :aliases => :o, :default => './git_stats', :desc => 'Output path where statistics should be written.'
option :language, :aliases => :l, :default => 'en', :desc => 'Language of written statistics.' option :language, :aliases => :l, :default => 'en', :desc => 'Language of written statistics.'
option :from, :aliases => :f, :desc => 'Commit from where statistics should start.' option :from, :aliases => :f, :desc => 'Commit from where statistics should start.'
option :to, :aliases => :t, :default => 'HEAD', :desc => 'Commit where statistics should stop.' option :to, :aliases => :t, :default => 'HEAD', :desc => 'Commit where statistics should stop.'
option :silent, :aliases => :s, :type => :boolean, :desc => 'Silent mode. Don\'t output anything.' option :silent, :aliases => :s, :type => :boolean, :desc => 'Silent mode. Don\'t output anything.'
option :tree, :aliases => :d, :default => '.', :desc => 'Tree where statistics should be generated.' option :tree, :aliases => :d, :default => '.', :desc => 'Tree where statistics should be generated.'
option :comment, :aliases => :c, :default => '///', :desc => 'The string which is used for comments.' option :comment, :aliases => :c, :default => '//', :desc => 'The string which is used for comments.'
desc 'generate', 'Generates the statistics of a repository' desc 'generate', 'Generates the statistics of a repository'
def generate def generate
I18n.locale = options[:language] I18n.locale = options[:language]
GitStats::Generator.new(options[:path], options[:output], options[:from], options[:to], options[:tree], options[:comment]) { |g| GitStats::Generator.new(options) { |g|
g.add_command_observer { |command, result| puts "#{command}" } unless options[:silent] g.add_command_observer { |command, result| puts "#{command}" } unless options[:silent]
}.render_all }.render_all
end end

View File

@ -4,12 +4,13 @@ module GitStats
delegate :add_command_observer, to: :@repo delegate :add_command_observer, to: :@repo
delegate :render_all, to: :@view delegate :render_all, to: :@view
def initialize(repo_path, out_path, first_commit_sha = nil, last_commit_sha = "HEAD", tree_path = ".", comment_string = "///") def initialize(options)
validate_repo_path(repo_path)
@repo = GitData::Repo.new(path: repo_path, first_commit_sha: first_commit_sha, last_commit_sha: last_commit_sha, tree_path: tree_path, comment_string: comment_string) validate_repo_path(options[:path])
@repo = GitData::Repo.new(options)
view_data = StatsView::ViewData.new(@repo) view_data = StatsView::ViewData.new(@repo)
@view = StatsView::View.new(view_data, out_path) @view = StatsView::View.new(view_data, options[:out_path])
yield self if block_given? yield self if block_given?
end end

View File

@ -6,8 +6,6 @@ module GitStats
class Repo class Repo
include HashInitializable include HashInitializable
attr_reader :path, :first_commit_sha, :last_commit_sha, :tree_path, :comment_string
delegate :files, :files_by_extension, :files_by_extension_count, :lines_by_extension, delegate :files, :files_by_extension, :files_by_extension_count, :lines_by_extension,
:files_count, :binary_files, :text_files, :lines_count, :comments_count, to: :last_commit :files_count, :binary_files, :text_files, :lines_count, :comments_count, to: :last_commit
@ -17,6 +15,26 @@ module GitStats
@tree_path ||= "." @tree_path ||= "."
end end
def path
@path ||= '.'
end
def first_commit_sha
@first_commit_sha
end
def last_commit_sha
@last_commit_sha ||= 'HEAD'
end
def tree_path
@tree_path ||= '.'
end
def comment_string
@comment_string ||= '//'
end
def authors def authors
@authors ||= run_and_parse("git shortlog -se #{commit_range} #{tree_path}").map do |author| @authors ||= run_and_parse("git shortlog -se #{commit_range} #{tree_path}").map do |author|
Author.new(repo: self, name: author[:name], email: author[:email]) Author.new(repo: self, name: author[:name], email: author[:email])
@ -81,10 +99,6 @@ module GitStats
@first_commit_sha.blank? ? last_commit_sha : "#@first_commit_sha..#{last_commit_sha}" @first_commit_sha.blank? ? last_commit_sha : "#@first_commit_sha..#{last_commit_sha}"
end end
def last_commit_sha
@last_commit_sha ||= 'HEAD'
end
def short_stats def short_stats
@short_stats ||= commits.map(&:short_stat) @short_stats ||= commits.map(&:short_stat)
end end
@ -106,7 +120,6 @@ module GitStats
end end
def project_name def project_name
# @project_name ||= File.basename(path)
@project_name ||= (File.expand_path(File.join(path, tree_path)).sub(File.dirname(File.expand_path(path))+File::SEPARATOR,"") || File.basename(path)) @project_name ||= (File.expand_path(File.join(path, tree_path)).sub(File.dirname(File.expand_path(path))+File::SEPARATOR,"") || File.basename(path))
end end

View File

@ -5,10 +5,8 @@ module GitStats
module GitData module GitData
class Tree class Tree
include HashInitializable include HashInitializable
attr_reader :repo, :relative_path attr_reader :repo, :relative_path
def initialize(params)
super(params)
end
def authors def authors
@authors ||= run_and_parse("git shortlog -se #{commit_range}").map do |author| @authors ||= run_and_parse("git shortlog -se #{commit_range}").map do |author|

View File

@ -4,7 +4,7 @@ require 'spec_helper'
describe GitStats::Generator do describe GitStats::Generator do
let(:repo_path) { 'repo_path' } let(:repo_path) { 'repo_path' }
let(:out_path) { 'out_path' } let(:out_path) { 'out_path' }
let(:generator) { GitStats::Generator.new(repo_path, out_path) } let(:generator) { GitStats::Generator.new(path: repo_path, out_path: out_path) }
before { Dir.stub(:exists? => true) } before { Dir.stub(:exists? => true) }
@ -15,9 +15,7 @@ describe GitStats::Generator do
it 'should pass command observer to repo' do it 'should pass command observer to repo' do
repo = double('repo') repo = double('repo')
GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, first_commit_sha: nil, last_commit_sha: "HEAD", tree_path: ".", :comment_string=>"///").and_return(repo) GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, out_path: out_path).and_return(repo)
generator = GitStats::Generator.new(repo_path, out_path)
observer = double('observer') observer = double('observer')
repo.should_receive(:add_command_observer).with(observer) repo.should_receive(:add_command_observer).with(observer)
@ -27,7 +25,7 @@ describe GitStats::Generator do
it 'should render all templates with view data for this repo' do it 'should render all templates with view data for this repo' do
repo = double('repo') repo = double('repo')
GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, first_commit_sha: nil, last_commit_sha: "HEAD", tree_path: ".", :comment_string=>"///").and_return(repo) GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, out_path: out_path).and_return(repo)
view_data = double('view_data') view_data = double('view_data')
GitStats::StatsView::ViewData.should_receive(:new).with(repo).and_return(view_data) GitStats::StatsView::ViewData.should_receive(:new).with(repo).and_return(view_data)