Commit 8347b776 authored by Fabio Pelosin's avatar Fabio Pelosin

[#188] Refactoring of SpecPresent module

parent 3ee0c410
module Pod
class Command
autoload :ErrorReport, 'cocoapods/command/error_report'
autoload :DisplayPods, 'cocoapods/command/print_pod'
autoload :SetPresent, 'cocoapods/command/set_present'
autoload :Install, 'cocoapods/command/install'
autoload :List, 'cocoapods/command/list'
autoload :Repo, 'cocoapods/command/repo'
......
......@@ -14,40 +14,40 @@ module Pod
end
def self.options
" --stats Show additional stats (like GitHub watchers and forks)\n" +
SetPresent.set_present_options +
super
end
include DisplayPods
include SetPresent
extend Executable
executable :git
def initialize(argv)
@stats = argv.option('--stats')
#TODO: accept only integers
parse_set_options(argv)
@days = argv.arguments.first
unless @days == nil || @days =~ /^[0-9]+$/
super
end
end
def dir
File.expand_path '~/.cocoapods/master'
config.repos_dir + 'master'
end
def list_directory_at_commit(commit)
def dir_list_from_commit(commit)
Dir.chdir(dir) { git("ls-tree --name-only -r #{commit}") }
end
def commit_at_days_ago (days)
return 'HEAD' if days == 0
def commit_from_days_ago (days)
Dir.chdir(dir) { git("rev-list -n1 --before=\"#{days} day ago\" master") }
end
def pods_at_days_ago (days)
commit = commit_at_days_ago(days)
dir_list = list_directory_at_commit(commit)
def spec_names_from_commit (commit)
dir_list = dir_list_from_commit(commit)
# Keep only directories
# Keep only subdirectories
dir_list.gsub!(/^[^\/]*$/,'')
#Clean pod names
# Keep only subdirectories name
dir_list.gsub!(/(.*)\/[0-9].*/,'\1')
result = dir_list.split("\n").uniq
......@@ -55,7 +55,13 @@ module Pod
result
end
def all_pods_sets
def new_specs_set(commit)
#TODO: find the changes for all repos
new_specs = spec_names_from_commit('HEAD') - spec_names_from_commit(commit)
sets = all_specs_set.select { |set| new_specs.include?(set.name) }
end
def all_specs_set
result = []
Source.all.each do |source|
source.pod_sets.each do |set|
......@@ -66,23 +72,21 @@ module Pod
end
def list_new
#TODO: find the changes for all repos
new_pods = pods_at_days_ago(0) - pods_at_days_ago(@days)
sets = all_pods_sets.select {|set| new_pods.include?(set.name) }
puts
sets = new_specs_set(commit_from_days_ago(@days))
present_sets(sets)
if !list
if sets.count != 0
puts "#{sets.count} new pods were added in the last #{@days} days"
puts
display_pod_list(sets, @stats)
else
puts "No new pods were added in the last #{@days} days"
end
puts
end
end
end
def list_all
display_pod_list(all_pods_sets, @stats)
present_sets(all_specs_set)
end
def run
......
......@@ -12,15 +12,15 @@ module Pod
end
def self.options
" --stats Show additional stats (like GitHub watchers and forks)\n" +
" --full Search by name, summary, and description\n" +
SetPresent.set_present_options +
super
end
include DisplayPods
include SetPresent
def initialize(argv)
@stats = argv.option('--stats')
parse_set_options(argv)
@full_text_search = argv.option('--full')
unless @query = argv.arguments.first
super
......@@ -29,7 +29,7 @@ module Pod
def run
sets = Source.search_by_name(@query.strip, @full_text_search)
display_pod_list(sets, @stats)
present_sets(sets)
end
end
end
......
module Pod
class Command
module DisplayPods
module SetPresent
def self.set_present_options
" --name-only Show only the names of the pods\n" +
" --stats Show additional stats (like GitHub watchers and forks)\n"
end
def list
@list
end
def parse_set_options(argv)
@stats = argv.option('--stats')
@list = argv.option('--name-only')
end
def display_pod_list(array, stats = false)
def present_sets(array)
array.each do |set|
puts_pod(set, stats)
present_set(set)
end
end
def puts_pod(set, stats = false)
def present_set(set)
if @list
puts set.name
else
puts "\e[32m--> #{set.name} (#{set.versions.reverse.join(", ")})\e[0m"
puts_wrapped_text(set.specification.summary)
......@@ -17,9 +33,15 @@ module Pod
source = spec.source.reject {|k,_| k == :commit || k == :tag }.values.first
puts_detail('Homepage', spec.homepage)
puts_detail('Source', source)
puts_github_info(source) if stats
if @stats
stats = stats(source)
puts_detail('Watchers', stats[:watchers])
puts_detail('Forks', stats[:forks])
end
puts
end
end
# adapted from http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/
def puts_wrapped_text(txt, col = 80, indentation = 4)
......@@ -29,22 +51,22 @@ module Pod
def puts_detail(title,string)
return if !string
# 8 is the length of homepage which might be displayed alone
# 8 is the length of homepage
number_of_spaces = ((8 - title.length) > 0) ? (8 - title.length) : 0
spaces = ' ' * number_of_spaces
puts " - #{title}: #{spaces + string}"
end
def puts_github_info(url)
def stats(url)
original_url, username, reponame = *(url.match(/[:\/]([\w\-]+)\/([\w\-]+)\.git/).to_a)
result = {}
if original_url
repo_info = `curl -s -m 2 http://github.com/api/v2/json/repos/show/#{username}/#{reponame}`
watchers = repo_info.match(/\"watchers\"\W*:\W*([0-9]+)/).to_a[1]
forks = repo_info.match(/\"forks\"\W*:\W*([0-9]+)/).to_a[1]
puts_detail('Watchers', watchers)
puts_detail('Forks', forks)
result[:watchers] = repo_info.match(/\"watchers\"\W*:\W*([0-9]+)/).to_a[1]
result[:forks] = repo_info.match(/\"forks\"\W*:\W*([0-9]+)/).to_a[1]
end
result
end
end
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment