Class: Pik::Command

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

Direct Known Subclasses

Add, Checkup, Config, Default, GemSync, Help, Implode, Install, List, Remove, Run, Switch, Tag, Tags

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.



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

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

#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



41
42
43
# File 'lib/pik/commands/command.rb', line 41

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

.choose_from(patterns, config) ⇒ Object



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

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



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

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



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

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

.descriptionObject



25
26
27
# File 'lib/pik/commands/command.rb', line 25

def self.description
  new.options.to_s
end

.hlObject



76
77
78
# File 'lib/pik/commands/command.rb', line 76

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

.inherited(subclass) ⇒ Object



29
30
31
# File 'lib/pik/commands/command.rb', line 29

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

.it(str) ⇒ Object



33
34
35
# File 'lib/pik/commands/command.rb', line 33

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

.namesObject



45
46
47
# File 'lib/pik/commands/command.rb', line 45

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

.summaryObject



37
38
39
# File 'lib/pik/commands/command.rb', line 37

def self.summary
  @summary 
end

Instance Method Details

#add_sigint_handlerObject

Installs a sigint handler.



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

def add_sigint_handler
  trap 'INT' do
    raise QuitError
  end
end

#closeObject



93
94
95
# File 'lib/pik/commands/command.rb', line 93

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

#cmd_nameObject



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

def cmd_name
  self.class.cmd_name
end

#command_optionsObject



101
102
103
104
105
# File 'lib/pik/commands/command.rb', line 101

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

#create(home) ⇒ Object



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

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

#current_gem_bin_pathObject



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

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)


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

def current_version?(string)
  string == get_version
end

#default_gem_homeObject



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

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

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



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

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



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

def editors
  @editors ||= []
end

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

def current_path?(config_path)

@path ||= SearchPath.new(ENV['PATH'])
@path.find{|dir| Pathname(dir) == Pathname(config_path) }

end



132
133
134
135
136
# File 'lib/pik/commands/command.rb', line 132

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

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



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

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



107
108
109
110
111
112
113
# File 'lib/pik/commands/command.rb', line 107

def parse_options
  options.on("--version", "-V", "Pik version") do |value|
    puts "pik " + Pik::VERSION
    @version = true
  end
  options.parse! @args 
end