[closes #11] using highline to parse command line options

This commit is contained in:
Tomasz Gieniusz 2013-04-06 14:52:01 +02:00
parent 8f67bcdc64
commit 8448cda8ba
10 changed files with 36 additions and 32 deletions

View File

@ -20,8 +20,8 @@ It browses the repository and outputs html page with statistics.
### Generator
$ git_stats repo_path output_directory language_code first_commit_sha last_commit_sha
$ favorite_browser output_directory/index.html
$ git_stats
<follow instructions on the screen>
### API usage example

View File

@ -8,4 +8,4 @@ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
require "git_stats/cli"
cli = GitStats::CLI.new
cli.start(*ARGV)
cli.start

View File

@ -25,4 +25,5 @@ Gem::Specification.new do |gem|
gem.add_dependency('haml')
gem.add_dependency('lazy_high_charts')
gem.add_dependency('i18n')
gem.add_dependency('highline')
end

View File

@ -8,6 +8,7 @@ require 'git_stats/i18n'
require 'git_stats/by_field_finder'
require 'git_stats/cli'
require 'git_stats/generator'
require 'git_stats/validator'
require 'git_stats/git_data/activity'
require 'git_stats/git_data/author'

View File

@ -1,13 +1,25 @@
# -*- encoding : utf-8 -*-
require "git_stats"
require "highline/import"
class GitStats::CLI
def start(*args)
raise "Wrong number of arguments\nUsage: git_stats repo_path output_path <output_lang>" unless [2, 3, 4, 5].include? args.size
def start
repo_path, out_path, lang, first_commit_sha, last_commit_sha = args
I18n.locale = lang || :en
repo_path = ask("Repo path? ") { |q|
q.default = "."
q.validate = lambda { |p| GitStats::Validator.new.valid_repo_path?(p) }
q.responses[:not_valid] = "Given path is not a git repository, try again"
}
out_path = ask("Output dir? ") { |q| q.default = "./git_stats" }
I18n.locale = ask("Language? ") { |q| q.default = "en"; q.answer_type = Symbol }
specify_range = agree("Want to specify commits range? ") { |q| q.default = "no" }
if specify_range
first_commit_sha = ask("Starting commit sha? ") { |q| q.default = nil }
last_commit_sha = ask("Ending commit sha? ") { |q| q.default = "HEAD" }
end
GitStats::Generator.new(repo_path, out_path, first_commit_sha, last_commit_sha) { |g|
g.add_command_observer { |command, result| puts "#{command}" }

View File

@ -4,7 +4,7 @@ module GitStats
delegate :add_command_observer, to: :@repo
delegate :render_all, to: :@view
def initialize(repo_path, out_path, first_commit_sha, last_commit_sha)
def initialize(repo_path, out_path, first_commit_sha = nil, last_commit_sha = "HEAD")
validate_repo_path(repo_path)
@repo = GitData::Repo.new(path: repo_path, first_commit_sha: first_commit_sha, last_commit_sha: last_commit_sha)
@ -18,7 +18,7 @@ module GitStats
def validate_repo_path(repo_path)
raise ArgumentError, "#{repo_path} is not a git repository" unless (Dir.exists?("#{repo_path}/.git") || File.exists?("#{repo_path}/HEAD"))
raise ArgumentError, "#{repo_path} is not a git repository" unless Validator.new.valid_repo_path?(repo_path)
end
end

View File

@ -68,7 +68,7 @@ module GitStats
end
def commit_range
@first_commit_sha ? "#@first_commit_sha..#{last_commit_sha}" : last_commit_sha
@first_commit_sha.blank? ? last_commit_sha : "#@first_commit_sha..#{last_commit_sha}"
end
def last_commit_sha

View File

@ -0,0 +1,10 @@
# -*- encoding : utf-8 -*-
module GitStats
class Validator
def valid_repo_path?(repo_path)
Dir.exists?("#{repo_path}/.git") || File.exists?("#{repo_path}/HEAD")
end
end
end

View File

@ -1,20 +0,0 @@
# -*- encoding : utf-8 -*-
require 'spec_helper'
describe GitStats::CLI do
let(:repo_path) { 'repo_path' }
let(:out_path) { 'out_path' }
it 'should invoke generator with console arguments given' do
generator = double('generator')
GitStats::Generator.should_receive(:new).with(repo_path, out_path).and_return(generator)
generator.should_receive(:render_all)
subject.start(repo_path, out_path)
end
it 'should raise error when 2 arguments are not given' do
expect { subject.start("only one argument") }.to raise_error
expect { subject.start("too", "much", "arguments") }.to raise_error
end
end

View File

@ -15,7 +15,7 @@ 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).and_return(repo)
GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, first_commit_sha: nil, last_commit_sha: "HEAD").and_return(repo)
generator = GitStats::Generator.new(repo_path, out_path)
@ -27,7 +27,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).and_return(repo)
GitStats::GitData::Repo.should_receive(:new).with(path: repo_path, first_commit_sha: nil, last_commit_sha: "HEAD").and_return(repo)
view_data = double('view_data')
GitStats::StatsView::ViewData.should_receive(:new).with(repo).and_return(view_data)