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

[#188] Refactoring of SpecPresent module

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