diff --git a/README.md b/README.md index 234e1c21f..5a42f9229 100644 --- a/README.md +++ b/README.md @@ -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 + ### API usage example diff --git a/bin/git_stats b/bin/git_stats index 8311ddbc1..aea13ad69 100755 --- a/bin/git_stats +++ b/bin/git_stats @@ -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 diff --git a/git_stats.gemspec b/git_stats.gemspec index ed93c368b..1de892edf 100644 --- a/git_stats.gemspec +++ b/git_stats.gemspec @@ -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 diff --git a/lib/git_stats/base.rb b/lib/git_stats/base.rb index 48258e715..314b2f5b7 100644 --- a/lib/git_stats/base.rb +++ b/lib/git_stats/base.rb @@ -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' diff --git a/lib/git_stats/cli.rb b/lib/git_stats/cli.rb index 8f4f96a78..90a5aa2cc 100644 --- a/lib/git_stats/cli.rb +++ b/lib/git_stats/cli.rb @@ -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 " 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}" } diff --git a/lib/git_stats/generator.rb b/lib/git_stats/generator.rb index b2e49c273..60b13b84d 100644 --- a/lib/git_stats/generator.rb +++ b/lib/git_stats/generator.rb @@ -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 diff --git a/lib/git_stats/git_data/repo.rb b/lib/git_stats/git_data/repo.rb index 8f0e24c62..6b61381c8 100644 --- a/lib/git_stats/git_data/repo.rb +++ b/lib/git_stats/git_data/repo.rb @@ -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 diff --git a/lib/git_stats/validator.rb b/lib/git_stats/validator.rb new file mode 100644 index 000000000..a02333d7b --- /dev/null +++ b/lib/git_stats/validator.rb @@ -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 \ No newline at end of file diff --git a/spec/git_data/cli_spec.rb b/spec/git_data/cli_spec.rb deleted file mode 100644 index c66e1bbed..000000000 --- a/spec/git_data/cli_spec.rb +++ /dev/null @@ -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 diff --git a/spec/git_data/generator_spec.rb b/spec/git_data/generator_spec.rb index bee0e6e0e..c70e9e1de 100644 --- a/spec/git_data/generator_spec.rb +++ b/spec/git_data/generator_spec.rb @@ -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)