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:
p, [--path=PATH] # Path to repository from which statistics should be generated.
# 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
l, [--language=LANGUAGE] # Language of written statistics.
# 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.
# Default: .
c, [--comment=COMMENT] # The string which is used for comments.
# Default: ///
# Default: //

View File

@ -4,18 +4,18 @@ require "thor"
class GitStats::CLI < Thor
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 :from, :aliases => :f, :desc => 'Commit from where statistics should start.'
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 :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'
def generate
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]
}.render_all
end

View File

@ -4,12 +4,13 @@ module GitStats
delegate :add_command_observer, to: :@repo
delegate :render_all, to: :@view
def initialize(repo_path, out_path, first_commit_sha = nil, last_commit_sha = "HEAD", tree_path = ".", comment_string = "///")
validate_repo_path(repo_path)
def initialize(options)
validate_repo_path(options[: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)
@repo = GitData::Repo.new(options)
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?
end

View File

@ -6,8 +6,6 @@ module GitStats
class Repo
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,
:files_count, :binary_files, :text_files, :lines_count, :comments_count, to: :last_commit
@ -17,6 +15,26 @@ module GitStats
@tree_path ||= "."
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
@authors ||= run_and_parse("git shortlog -se #{commit_range} #{tree_path}").map do |author|
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}"
end
def last_commit_sha
@last_commit_sha ||= 'HEAD'
end
def short_stats
@short_stats ||= commits.map(&:short_stat)
end
@ -106,7 +120,6 @@ module GitStats
end
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))
end

View File

@ -5,10 +5,8 @@ module GitStats
module GitData
class Tree
include HashInitializable
attr_reader :repo, :relative_path
def initialize(params)
super(params)
end
def authors
@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
let(:repo_path) { 'repo_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) }
@ -15,10 +15,8 @@ describe GitStats::Generator do
it 'should pass command observer to repo' do
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)
generator = GitStats::Generator.new(repo_path, out_path)
GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, out_path: out_path).and_return(repo)
observer = double('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
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')
GitStats::StatsView::ViewData.should_receive(:new).with(repo).and_return(view_data)