Class: Pik::Command

Inherits:
Object show all
Defined in:
lib/pik/commands/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = ARGV, config_ = nil) ⇒ Command

Returns a new instance of Command.



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/pik/commands/command.rb', line 74

def initialize(args=ARGV, config_=nil)
  @args    = args
  @options = OptionParser.new
  @config  = config_ || ConfigFile.new
  @hl      = HighLine.new
  add_sigint_handler
  options.program_name = "pik #{self.class.names.join('|')}"
  command_options
  parse_options
  create(PIK_HOME) unless PIK_HOME.exist?
  delete_old_pik_script
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/pik/commands/command.rb', line 13

def config
  @config
end

#debugObject

Returns the value of attribute debug.



21
22
23
# File 'lib/pik/commands/command.rb', line 21

def debug
  @debug
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/pik/commands/command.rb', line 15

def options
  @options
end

#outputObject

Returns the value of attribute output.



17
18
19
# File 'lib/pik/commands/command.rb', line 17

def output
  @output
end

#versionObject

Returns the value of attribute version.



19
20
21
# File 'lib/pik/commands/command.rb', line 19

def version
  @version
end

Class Method Details

.aka(*aliases) ⇒ Object



43
44
45
# File 'lib/pik/commands/command.rb', line 43

def self.aka(*aliases)
  @names = names + aliases
end

.choose_from(patterns, config) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pik/commands/command.rb', line 51

def self.choose_from(patterns, config)
  if patterns.empty?
    possibles = config.keys  
  else
    possibles = patterns.map{|p| config.keys.grep(Regexp.new(Regexp.escape(p.to_s),Regexp::IGNORECASE) ) }
    possibles = possibles.inject{|m,v| m & v }.flatten.uniq
  end
  case possibles.size
  when 0
    return nil
  when 1
    return possibles.first
  else
    hl.say('Select which Ruby you want:')
    ver = hl.choose(*possibles)
    return ver
  end
end

.cmd_nameObject



23
24
25
# File 'lib/pik/commands/command.rb', line 23

def self.cmd_name
  name.split('::').last.downcase.to_sym
end

.descriptionObject



27
28
29
# File 'lib/pik/commands/command.rb', line 27

def self.description
  new.options.to_s
end

.hlObject



70
71
72
# File 'lib/pik/commands/command.rb', line 70

def self.hl
  @hl ||= HighLine.new
end

.inherited(subclass) ⇒ Object



31
32
33
# File 'lib/pik/commands/command.rb', line 31

def self.inherited(subclass)
  Commands.add(subclass)
end

.it(str) ⇒ Object



35
36
37
# File 'lib/pik/commands/command.rb', line 35

def self.it(str)
  @summary = str
end

.namesObject



47
48
49
# File 'lib/pik/commands/command.rb', line 47

def self.names
  @names ||= [cmd_name]
end

.summaryObject



39
40
41
# File 'lib/pik/commands/command.rb', line 39

def self.summary
  @summary 
end

Instance Method Details

#actual_gem_homeObject



144
145
146
# File 'lib/pik/commands/command.rb', line 144

def actual_gem_home
  gem_path.last
end

#add_sigint_handlerObject

Installs a sigint handler.



172
173
174
175
176
# File 'lib/pik/commands/command.rb', line 172

def add_sigint_handler
  trap 'INT' do
    raise QuitError
  end
end

#closeObject



87
88
89
# File 'lib/pik/commands/command.rb', line 87

def close
  editors.each{|e| e.write }
end

#cmd_nameObject



166
167
168
# File 'lib/pik/commands/command.rb', line 166

def cmd_name
  self.class.cmd_name
end

#command_optionsObject



95
96
97
98
99
# File 'lib/pik/commands/command.rb', line 95

def command_options
  options.separator ""
  options.separator self.class.summary
  options.separator ""
end

#create(home) ⇒ Object



152
153
154
155
# File 'lib/pik/commands/command.rb', line 152

def create(home)
  puts "creating #{home}"
  home.mkpath
end

#current_gem_bin_pathObject



134
135
136
137
138
# File 'lib/pik/commands/command.rb', line 134

def current_gem_bin_path
  cfg = config[find_config_from_path]
  p = cfg[:gem_home] || default_gem_home 
  p + 'bin'
end

#current_version?(string) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/pik/commands/command.rb', line 116

def current_version?(string)
  string == get_version
end

#default_gem_homeObject



140
141
142
# File 'lib/pik/commands/command.rb', line 140

def default_gem_home
  gem_path.first
end

#delete_old_pik_scriptObject



157
158
159
# File 'lib/pik/commands/command.rb', line 157

def delete_old_pik_script
  SCRIPT_FILE.path.delete if SCRIPT_FILE.path.exist?
end

#editorsObject



91
92
93
# File 'lib/pik/commands/command.rb', line 91

def editors
  @editors ||= []
end

#find_config_from_path(path = Which::Ruby.find) ⇒ Object



128
129
130
131
132
# File 'lib/pik/commands/command.rb', line 128

def find_config_from_path(path=Which::Ruby.find)
  config.find{|k,v| 
    Pathname(v[:path])== Pathname(path)
  }.first rescue nil
end

#gem_pathObject



148
149
150
# File 'lib/pik/commands/command.rb', line 148

def gem_path
  `\"#{Which::Gem.exe}\" env gempath`.chomp.split(';').map{|p| Pathname(p).to_windows }
end

#get_version(path = Which::Ruby.find) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/pik/commands/command.rb', line 120

def get_version(path=Which::Ruby.find)
  cmd = Which::Ruby.exe(path)
  ruby_ver = `"#{cmd}" -v`
  ruby_ver =~ /ruby (\d\.\d\.\d)/i
  major    = $1.gsub('.','')
  "#{major}: #{ruby_ver.strip}"
end

#parse_optionsObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/pik/commands/command.rb', line 101

def parse_options
  options.on("--version", "-V", "Pik version") do |value|
    @version = true
    puts pik_version
  end
  options.on("--debug", "-d", "Outputs debug information") do |value|
    @debug = true
  end
  options.parse! @args 
end

#pik_versionObject



112
113
114
# File 'lib/pik/commands/command.rb', line 112

def pik_version
  "pik " + Pik::VERSION
end

#sh(cmd) ⇒ Object



161
162
163
164
# File 'lib/pik/commands/command.rb', line 161

def sh(cmd)
  puts cmd if debug
  system(cmd)
end