Class: Yap::Cli::Commands::Addon::Search

Inherits:
Object
  • Object
show all
Defined in:
lib/yap/cli/commands/addon/search.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#allObject

Returns the value of attribute all.



5
6
7
# File 'lib/yap/cli/commands/addon/search.rb', line 5

def all
  @all
end

#localObject

Returns the value of attribute local.



5
6
7
# File 'lib/yap/cli/commands/addon/search.rb', line 5

def local
  @local
end

#prereleaseObject

Returns the value of attribute prerelease.



5
6
7
# File 'lib/yap/cli/commands/addon/search.rb', line 5

def prerelease
  @prerelease
end

#search_termObject

Returns the value of attribute search_term.



5
6
7
# File 'lib/yap/cli/commands/addon/search.rb', line 5

def search_term
  @search_term
end

#show_gem_nameObject

Returns the value of attribute show_gem_name.



6
7
8
# File 'lib/yap/cli/commands/addon/search.rb', line 6

def show_gem_name
  @show_gem_name
end

#versionObject

Returns the value of attribute version.



5
6
7
# File 'lib/yap/cli/commands/addon/search.rb', line 5

def version
  @version
end

Instance Method Details

#extract_gem_names_from_search(search_results) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/yap/cli/commands/addon/search.rb', line 23

def extract_gem_names_from_search(search_results)
  search_results.lines.map(&:chomp).map do |line|
    results = line.scan(/^(yap-shell-addon-(\S+))\s*\((\d+\.\d+\.\d+)\)/)
    gem_name, _, _ = results.flatten
    gem_name
  end.compact
end

#gem_search(options) ⇒ Object



31
32
33
34
35
# File 'lib/yap/cli/commands/addon/search.rb', line 31

def gem_search(options)
  search_results = `gem search #{options.join(' ')} yap-shell | grep #{search_term}`
  gem_names = extract_gem_names_from_search search_results
  gem_specs_for_gems gem_names
end

#gem_specs_for_gems(gem_names) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/yap/cli/commands/addon/search.rb', line 37

def gem_specs_for_gems(gem_names)
  options = []
  options << (local ? '-l' : '-r')
  options << "--version #{version}" if version
  gem_names.map do |gem_name|
    YAML.load `gem spec #{options.join(' ')} #{gem_name}`
  end
end


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/yap/cli/commands/addon/search.rb', line 46

def print_addons(gemspecs)
  headers = {
    name: (show_gem_name ? "Rubygem" : "Addon Name"),
    version: "Version",
    date: "Date Released",
    author: "Author",
    summary: "Summary"
  }
  value_for = {
    name: -> (s) {
      if show_gem_name
        s.name
      else
        s.name.scan(/^yap-shell-addon-(\S+)/).flatten.first
      end
    },
    version: -> (s) { s.version.to_s },
    date: -> (s) { s.date.to_date.to_s },
    author: -> (s) { s.author },
    summary: -> (s) { s.summary }
  }

  longest = {
    name: gemspecs.map{ |s| value_for[:name].call(s) }.concat(Array(headers[:name])).map(&:length).max,
    version: gemspecs.map{ |s| value_for[:version].call(s) }.concat(Array(headers[:version])).map(&:length).max,
    date: gemspecs.map{ |s| value_for[:date].call(s) }.concat(Array(headers[:date])).map(&:length).max,
    author: gemspecs.map{ |s| value_for[:author].call(s) }.concat(Array(headers[:author])).map(&:length).max,
    summary: gemspecs.map{ |s| value_for[:summary].call(s) }.concat(Array(headers[:summary])).map(&:length).max
  }

  spacing = "  "
  format_string = longest.reduce([]) do |str, (key, maxlength)|
    str << "%-#{maxlength}s"
  end.join(spacing)

  output = gemspecs.map do |gemspec|
    values = longest.keys.map { |key| value_for[key].call(gemspec) }
    sprintf "#{format_string}", *values
  end
  output = output.join("\n")

  highlighted_output = output.gsub(/(#{Regexp.escape(search_term)})/) do
    Term::ANSIColor.cyan($1)
  end

  puts Term::ANSIColor.bright_black(
    sprintf("#{format_string}", *longest.keys.map { |key| headers[key] })
  )
  puts highlighted_output
end

#processObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/yap/cli/commands/addon/search.rb', line 8

def process
  options = []

  if search_term.nil?
    puts "No addons found. Please provide a search term."
  end

  options << '--all' if all
  options << '--local' if local
  options << '--prerelease' if prerelease
  options << "--version #{version}" if version

  print_addons gem_search(options)
end