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.



82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pik/commands/command.rb', line 82

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_BATCH.basename('.*')} #{self.class.names.join('|')}"
  command_options
  parse_options
  create(PIK_HOME) unless PIK_HOME.exist?
  delete_old_pik_batches
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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/pik/commands/command.rb', line 59

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))) }
    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

.clean_gem_batchObject



51
52
53
54
55
56
57
# File 'lib/pik/commands/command.rb', line 51

def self.clean_gem_batch
  BatchFile.open(PIK_BATCH) do |gem_bat|
    # remove old calls to .pik/pik batches
    gem_bat.remove_line( /call.+pik.+bat/i )
    gem_bat.write  
  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



78
79
80
# File 'lib/pik/commands/command.rb', line 78

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



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

def actual_gem_home
  get_gem_home(:last)
end

#add_sigint_handlerObject

Installs a sigint handler.



182
183
184
185
186
# File 'lib/pik/commands/command.rb', line 182

def add_sigint_handler
  trap 'INT' do
    raise QuitError
  end
end

#closeObject



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

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

#cmd_nameObject



176
177
178
# File 'lib/pik/commands/command.rb', line 176

def cmd_name
  self.class.cmd_name
end

#command_optionsObject



103
104
105
106
107
# File 'lib/pik/commands/command.rb', line 103

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

#create(home) ⇒ Object



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

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

#current_gem_bin_pathObject



141
142
143
144
145
# File 'lib/pik/commands/command.rb', line 141

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)


123
124
125
# File 'lib/pik/commands/command.rb', line 123

def current_version?(string)
  string == get_version
end

#default_gem_homeObject



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

def default_gem_home
  get_gem_home(:first)
end

#delete_old_pik_batches(cutoff = (Time.now - (2 * 60 * 60))) ⇒ Object



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

def delete_old_pik_batches( cutoff=(Time.now - (2 * 60 * 60)) )
  Dir[(PIK_HOME + "*.bat").to_ruby.to_s].each do |f|
    File.delete(f) if File.ctime(f) < cutoff 
  end
end

#editorsObject



99
100
101
# File 'lib/pik/commands/command.rb', line 99

def editors
  @editors ||= []
end

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



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

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

#get_gem_home(position) ⇒ Object



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

def get_gem_home(position)
  path = `#{Which::Ruby.exe} -rubygems -e\"require 'rubygems' ; puts Gem.default_path.#{position}\"`
  Pathname.new(path.chomp).to_windows    
end

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



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

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



109
110
111
112
113
114
115
116
117
# File 'lib/pik/commands/command.rb', line 109

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

#pik_versionObject



119
120
121
# File 'lib/pik/commands/command.rb', line 119

def pik_version
  "pik " + Pik::VERSION
end

#sh(cmd) ⇒ Object



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

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