Class: Gemstat::GemstatTasks

Inherits:
Thor
  • Object
show all
Defined in:
lib/gemstat/cli.rb

Instance Method Summary collapse

Instance Method Details

#also_required(gem) ⇒ Object



114
115
116
117
# File 'lib/gemstat/cli.rb', line 114

def also_required(gem)
  # fake implementation
  print `find #{File.dirname(__FILE__)} | grep Gemfile$ | xargs grep "gem '#{gem}'" | sed 's/\\/Gemfile:gem.*//g' | sed 's/.*\\///g' | sed 's/:.*//g'`
end

#dependency(gem) ⇒ Object



120
121
122
123
# File 'lib/gemstat/cli.rb', line 120

def dependency(gem)
  # fake implementation
  print `find #{File.dirname(__FILE__)}/cache/gemfiles/#{gem[0]} | egrep "#{gem[0]}/#{gem}:.*/Gemfile" | xargs cat | sed "s/gem '//g" | sed "s/'//g"`
end

#listObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gemstat/cli.rb', line 29

def list
  population = Population.new(options[:directory])

  puts "  " + "-"*44
  puts "  Gemfile                Gems in Use"
  puts "  " + "-"*44
  population.dependencies.sort{|x,y|x.name <=> y.name}.each {|item|
    if item.gems.count==0 then
      count = "   -"
    else
      count = "#{item.gems.count.to_s[0..3].rjust(4)} " + pluralize(item.gems.count, "gem")
    end
    puts "  #{item.name[0..43].ljust(44)} #{count} "
  }
  puts "  " + "-"*44
  puts
end

#look_like(something) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/gemstat/cli.rb', line 68

def look_like(something)
  sample = Dependency.new(get_folder_path(something))
  population = Population.new(options[:directory])
  result = population.compute_similarity_score(sample, options[:reverse])
  if result.count > 0 then
    result.each_with_index do |item, i|
      puts "#{item[0].ljust(30)}(#{item[1][:score].round(3)}pt)"
      exit if i > options[:count]
    end
  else
    puts "There are not similar one found like #{something} in the #{options[:directory]} directory."
  end
end


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/gemstat/cli.rb', line 49

def popular
  population = Population.new(options[:directory])
  ranking = population.dependencies.map {|item| item.gems}.flatten.inject(Hash.new(0)){|hash,a| hash[a]+=1; hash}

  puts "Which is the most popular gem?"
  puts "Here is the list of gems grouped by count of dependencies."
  puts "  " + "-"*77
  puts "  #   Dependent Gem"
  puts "  " + "-"*77
  ranking.inject({}) { |h,(k,v)| (h[v] ||= []) << k; h }.sort.reverse.each {|item| puts "  #{item[0]}   #{item[1].sort.join(', ')}" }
  puts "  " + "-"*77
  puts ""

end

#suggest_for(something) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gemstat/cli.rb', line 86

def suggest_for(something)
  sample = Dependency.new(get_folder_path(something))
  population = Population.new(options[:directory])
  result = population.compute_similarity_score(sample, options[:reverse])
  if result.count > 0 then
    suggestion = {}
    result = result.select{|key,item| item[:score]< 1.0 }
    result.each do |entry|
      (entry[1][:gems] - sample.gems).each do |gem|
        suggestion[gem]=0.0 if suggestion[gem].nil?
        suggestion[gem]+=entry[1][:score]
      end
    end
    if suggestion.count > 0 then
      options[:reverse] ? suggestion = suggestion.sort{|a,b| a[1] <=> b[1]} : suggestion = suggestion.sort{|a,b| b[1] <=> a[1]}
      suggestion.each_with_index do |item, i|
        puts "#{item[0].ljust(30)}(#{item[1].round(3)}pt)"
        exit if i > options[:count]
      end
    else
      puts "There are not recommendation for #{something} based on the dependencies found at #{options[:directory]} directory."
    end
  else
    puts "There are not similar one found like #{something} in the #{options[:directory]} directory."
  end
end

#update(exp = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/gemstat/cli.rb', line 7

def update(exp=nil)
  puts "This task will download more than hundred thousand gems(which will require huge network traffic to download tons of gem binary files). Do you want to proceed?[Y/n]"
  if $stdin.gets.chomp.upcase == "Y" then
    fetcher = Gem::Mirror::Fetcher.new(:retries => 3, :skiperror => true)
    (`gem list -r #{exp} | sed 's/ (/(/g'`).each_line do |gem|
      gem_name = gem.sub(/\(.*/,'')
      dest = "#{File.dirname(__FILE__)}/cache/gemfiles/#{gem[0].downcase}/#{gem.chomp.sub(/\(/,':').sub(/,.*/,'').sub(/\)/,'').sub(/\s.*/,'')}"
      fetcher.fetch "http://rubygems.org/gems/#{gem.chomp.sub(/\(/,'-').sub(/,.*/,'').sub(/\)/,'').sub(/\s.*/,'')}.gem", "#{dest}.gem"
      `mkdir #{dest}; tar xvf #{dest}.gem -C #{dest}; gzip -dc #{dest}/metadata.gz | grep -e "\s\sname:" | sed 's/\s\sname: //g' | xargs -I% echo "gem '%'" | grep -v -e "gem '.* .*'" >> #{dest}/Gemfile`
      #`mkdir #{dest}/tmp; tar xzvf #{dest}/data.tar.gz -C #{dest}/tmp; cp #{dest}/tmp/Gemfile #{dest}/Gemfile; cp #{dest}/tmp/*.gemspec #{dest}/`
      `rm -rf #{dest}.gem #{dest}/metadata.gz #{dest}/data.tar.gz #{dest}/checksums.yaml.gz`
    end
  end
end

#versionObject



23
24
25
# File 'lib/gemstat/cli.rb', line 23

def version
  puts Gemstat::VERSION
end