Class: Babushka::Cmdline::Helpers

Inherits:
Object
  • Object
show all
Extended by:
LogHelpers
Defined in:
lib/babushka/cmdline/helpers.rb

Class Method Summary collapse

Methods included from LogHelpers

debug, deprecated!, log, log_block, log_error, log_ok, log_stderr, log_warn, removed!

Class Method Details

.generate_list_for(to_list, filter_str) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/babushka/cmdline/helpers.rb', line 90

def self.generate_list_for to_list, filter_str
  context = to_list == :deps ? Base.program_name : ':template =>'
  match_str = filter_str.try(:downcase)
  Base.sources.all_present.each {|source|
    source.load!
  }.map {|source|
    [source, source.send(to_list).items]
  }.map {|(source,items)|
    if match_str.nil? || source.name.downcase[match_str]
      [source, items]
    else
      [source, items.select {|item| item.name.downcase[match_str] }]
    end
  }.select {|(_,items)|
    !items.empty?
  }.sort_by {|(source,_)|
    source.name
  }.each {|(source,items)|
    indent = (items.map {|item| "#{source.name}:#{item.name}".length }.max || 0) + 3
    log ""
    log "# #{source.name} (#{source.type})#{" - #{source.uri}" unless source.implicit?}"
    log "# #{items.length} #{to_list.to_s.chomp(items.length == 1 ? 's' : '')}#{" matching '#{filter_str}'" unless filter_str.nil?}:"
    items.each {|dep|
      log "#{context} #{"'#{source.name}:#{dep.name}'".ljust(indent)}"
    }
  }
end

.github_autosource_regexObject



80
81
82
# File 'lib/babushka/cmdline/helpers.rb', line 80

def self.github_autosource_regex
  /^\w+\:\/\/github\.com\/(.*)\/babushka-deps(\.git)?/
end


32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/babushka/cmdline/helpers.rb', line 32

def self.print_examples
  log "\nExamples:"
  log "  # Inspect the 'system' dep (and all its sub-deps) without touching the system.".colorize('grey')
  log "  #{Base.program_name} system --dry-run"
  log "\n"
  log "  # Meet the 'fish' dep (i.e. install fish and all its dependencies).".colorize('grey')
  log "  #{Base.program_name} fish"
  log "\n"
  log "  # Meet the 'user setup' dep, printing lots of debugging (including realtime".colorize('grey')
  log "  # shell command output).".colorize('grey')
  log "  #{Base.program_name} 'user setup' --debug"
end


25
26
27
28
29
30
# File 'lib/babushka/cmdline/helpers.rb', line 25

def self.print_handlers
  log "\nCommands:"
  Handler.all.each {|handler|
    log "  #{handler.name.ljust(10)} #{handler.description}"
  }
end


45
46
47
48
# File 'lib/babushka/cmdline/helpers.rb', line 45

def self.print_notes
  log "\nCommands can be abbrev'ed, as long as they remain unique."
  log "  e.g. '#{Base.program_name} l' is short for '#{Base.program_name} list'."
end


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/babushka/cmdline/helpers.rb', line 64

def self.print_search_results search_term, results
  log "The webservice knows about #{results.length} dep#{'s' unless results.length == 1} that match#{'es' if results.length == 1} '#{search_term}':"
  log ""
  Logging.log_table(
    ['Name', 'Source', 'Runs', '', 'Command'],
    results
  )
  if (custom_sources = results.select {|r| r[1][github_autosource_regex].nil? }.length) > 0
    log ""
    log "#{custom_sources == 1 ? 'This source has a custom URI' : 'These sources have custom URIs'}, so babushka can't discover #{custom_sources == 1 ? 'it' : 'them'} automatically."
    log "   You can run #{custom_sources == 1 ? 'its' : 'their'} deps in the same way, though, once you add #{custom_sources == 1 ? 'it' : 'them'} manually:"
    log "   $ #{Base.program_name} sources -a <alias> <uri>"
    log "   $ #{Base.program_name} <alias>:<dep>"
  end
end


16
17
18
19
20
21
22
23
# File 'lib/babushka/cmdline/helpers.rb', line 16

def self.print_usage
  log "\nThe gist:"
  log "  #{Base.program_name} <command> [options]"
  log "\nAlso:"
  log "  #{Base.program_name} help <command>  # Print command-specific usage info"
  log "  #{Base.program_name} <dep name>      # A shortcut for 'babushka meet <dep name>'"
  log "  #{Base.program_name} babushka        # Update babushka itself (what babushka.me/up does)"
end


8
9
10
11
12
13
14
# File 'lib/babushka/cmdline/helpers.rb', line 8

def self.print_version opts = {}
  if opts[:full]
    log "Babushka v#{VERSION} (#{Base.ref}), (c) 2012 Ben Hoskings <[email protected]>"
  else
    log "#{VERSION} (#{Base.ref})"
  end
end

.search_results_for(q) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/babushka/cmdline/helpers.rb', line 50

def self.search_results_for q
  YAML.load(search_webservice_for(q).body).sort_by {|i|
    -i[:runs_this_week]
  }.map {|i|
    [
      i[:name],
      i[:source_uri],
      ((i[:runs_this_week] && i[:runs_this_week] > 0) ? "#{i[:runs_this_week]} this week" : "#{i[:total_runs]} ever"),
      ((i[:runs_this_week] && i[:runs_this_week] > 0) ? "#{(i[:success_rate_this_week] * 100).round}%" : ((i[:total_runs] && i[:total_runs] > 0) ? "#{(i[:total_success_rate] * 100).round}%" : '')),
      (i[:source_uri][github_autosource_regex] ? "#{Base.program_name} #{$1}:#{"'" if i[:name][/\s/]}#{i[:name]}#{"'" if i[:name][/\s/]}" : '')
    ]
  }
end

.search_webservice_for(q) ⇒ Object



84
85
86
87
88
# File 'lib/babushka/cmdline/helpers.rb', line 84

def self.search_webservice_for q
  Net::HTTP.start('babushka.me') {|http|
    http.get URI.escape("/deps/search.yaml/#{q}")
  }
end