Module: MetaCon::CLIHelpers

Extended by:
CLIHelpers
Included in:
CLIHelpers, Command, Loaders::PythonBrew, Loaders::RVM, SelfInstall
Defined in:
lib/metacon/cli_helpers.rb

Defined Under Namespace

Classes: SymVer

Constant Summary collapse

CONTEXT_OS =
`uname -s`.strip.downcase.to_sym
ESCS =
{:normal    =>"\e[0m",    :black     =>"\e[0;30m", :blue         =>"\e[0;34m",
:green     =>"\e[0;32m", :cyan      =>"\e[0;36m", :red          =>"\e[0;31m",
:purple    =>"\e[0;35m", :brown     =>"\e[0;33m", :gray         =>"\e[0;37m",
:dark_gray =>"\e[1;30m", :light_blue=>"\e[1;34m", :light_green  =>"\e[1;32m",
:light_cyan=>"\e[1;36m", :light_red =>"\e[1;31m", :light_purple =>"\e[1;35m",
:yellow    =>"\e[1;33m", :white     =>"\e[1;37m", :bg_black     =>"\e[40m",
:bg_red    =>"\e[41m",   :bg_green  =>"\e[42m",   :bg_yellow    =>"\e[43m",
:bg_blue   =>"\e[44m",   :bg_magenta=>"\e[45m",   :bg_cyan      =>"\e[46m",
:bg_white  =>"\e[47m",   :"="       =>"\e[44;1;37m", :"=="      =>"\e[4;1;37m",
:"==="     =>"\e[1;34m"}
ANSI =

Instance Method Summary collapse

Instance Method Details

#best_profile_fileObject



126
127
128
129
130
131
132
133
# File 'lib/metacon/cli_helpers.rb', line 126

def best_profile_file
  prof = Dir[ENV['HOME']+'/.bashrc']
  prof = Dir[ENV['HOME']+'/.zshrc'] if prof.size == 0
  prof = Dir[ENV['HOME']+'/.bash_profile'] if prof.size == 0
  prof = Dir[ENV['HOME']+'/.profile'] if prof.size == 0
  return prof[0] unless prof.size == 0
  return nil
end

#cfail(str) ⇒ Object



76
# File 'lib/metacon/cli_helpers.rb', line 76

def cfail(str) color_puts("|{red Fail:} #{str}") end

#chd(*args) ⇒ Object



83
84
85
86
87
# File 'lib/metacon/cli_helpers.rb', line 83

def chd(*args)
  dir = f(args)
  color_puts "|{gray cd #{dir}}"
  Dir.chdir(dir)
end

#check_tool(name, vcmd, vpos = 1, vexp = '0', inst = '', wmissing = false, wvers = true) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/metacon/cli_helpers.rb', line 93

def check_tool(name, vcmd, vpos=1, vexp='0', inst='', wmissing=false, wvers=true)
  vs = `#{vcmd}`
  if vs.length > 0
    v = vs.strip.split(/\s+/)[vpos - 1]
    require 'pp'
    if v.as_version < vexp.as_version
      if wvers
        cwarn("#{name} expected to be >= version #{vexp} but only #{v} found. You may need to upgrade.")
        return false
      else
        cfail("#{name} expected to be >= version #{vexp} but only #{v} found. Please upgrade.")
      end
    end
  else
    if wmissing
      cwarn("#{name} not found. To install: #{inst}")
    else
      cfail("#{name} not found. To install: #{inst}")
      return false
    end
  end
  return true
end

#color_puts(str, emit = true) ⇒ Object Also known as: cputs



65
66
67
68
69
70
71
72
# File 'lib/metacon/cli_helpers.rb', line 65

def color_puts(str, emit=true)
  begin
    r = false
    str.gsub!(/\|\{([^ \|]+ )([^\}\|]*)\}/){r=true; "#{ESCS[$1.strip.to_sym]}#{$2}#{ESCS[:normal]}"}
  end while r
  puts str if emit
  return str
end

#command_exists?(cmd) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/metacon/cli_helpers.rb', line 89

def command_exists?(cmd)
  `( command -v #{cmd} )`.length > 0
end

#cstr2(str, for_ps1 = true) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/metacon/cli_helpers.rb', line 52

def cstr2(str, for_ps1=true)
  fmtres = str.gsub(/(<\|[^>]+>)+/) do
    parts = $1
    cmdseq = parts.gsub(/<\|([^>]+)>/) do
      ctrl = $1.split(/\s*\|\s*/).compact
      ctrl = ctrl.map{|k| ANSI[k]}.join(';')
      "\e[#{ctrl}m"
    end
    if for_ps1 then '\\[' + cmdseq + '\\]'
    else cmdseq end
  end
end

#cwarn(str) ⇒ Object



75
# File 'lib/metacon/cli_helpers.rb', line 75

def cwarn(str) color_puts("|{brown Warning:} #{str}") end

#darwin?Boolean

Returns:

  • (Boolean)


27
# File 'lib/metacon/cli_helpers.rb', line 27

def darwin?() CONTEXT_OS == :darwin end

#fj(*args) ⇒ Object

Shortcut for File.join



81
# File 'lib/metacon/cli_helpers.rb', line 81

def fj(*args) File.expand_path(File.join(args)) end

#included(by) ⇒ Object



5
# File 'lib/metacon/cli_helpers.rb', line 5

def included(by); by.extend(self) end

#linux?Boolean

Returns:

  • (Boolean)


28
# File 'lib/metacon/cli_helpers.rb', line 28

def linux?()  CONTEXT_OS == :linux  end

#mac?Boolean

Returns:

  • (Boolean)


26
# File 'lib/metacon/cli_helpers.rb', line 26

def mac?()    CONTEXT_OS == :darwin end

#result(str) ⇒ Object



79
# File 'lib/metacon/cli_helpers.rb', line 79

def result(str) color_puts(" |{green #{str}}\n") end

#shellescape(str) ⇒ Object



117
118
119
120
121
122
123
124
# File 'lib/metacon/cli_helpers.rb', line 117

def shellescape(str)
  return "''" if str.empty?
  str = str.dup
  str.strip!
  str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/n, "\\\\\\1")
  str.gsub!(/\n/, "'\n'")
  return str
end

#status(str) ⇒ Object



78
# File 'lib/metacon/cli_helpers.rb', line 78

def status(str) color_puts("\n|{== #{str}}") end