Class: Pik::Command
Direct Known Subclasses
Add, Config, Default, Devkit, GemSync, Help, Implode, Info, Install, List, Remove, Run, Switch, Tag, Tags, UnInstall, Update
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
Returns the value of attribute config.
13
14
15
|
# File 'lib/pik/commands/command.rb', line 13
def config
@config
end
|
Returns the value of attribute debug.
21
22
23
|
# File 'lib/pik/commands/command.rb', line 21
def debug
@debug
end
|
Returns the value of attribute options.
15
16
17
|
# File 'lib/pik/commands/command.rb', line 15
def options
@options
end
|
Returns the value of attribute output.
17
18
19
|
# File 'lib/pik/commands/command.rb', line 17
def output
@output
end
|
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),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
|
.clean_gem_batch ⇒ Object
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|
gem_bat.remove_line( /call.+pik.+bat/i )
gem_bat.write
end
end
|
23
24
25
|
# File 'lib/pik/commands/command.rb', line 23
def self.cmd_name
name.split('::').last.downcase.to_sym
end
|
.description ⇒ Object
27
28
29
|
# File 'lib/pik/commands/command.rb', line 27
def self.description
new.options.to_s
end
|
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
|
35
36
37
|
# File 'lib/pik/commands/command.rb', line 35
def self.it(str)
@summary = str
end
|
47
48
49
|
# File 'lib/pik/commands/command.rb', line 47
def self.names
@names ||= [cmd_name]
end
|
39
40
41
|
# File 'lib/pik/commands/command.rb', line 39
def self.summary
@summary
end
|
Instance Method Details
#actual_gem_home ⇒ Object
152
153
154
|
# File 'lib/pik/commands/command.rb', line 152
def actual_gem_home
gem_path.last
end
|
#add_sigint_handler ⇒ Object
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
|
95
96
97
|
# File 'lib/pik/commands/command.rb', line 95
def close
editors.each{|e| e.write }
end
|
176
177
178
|
# File 'lib/pik/commands/command.rb', line 176
def cmd_name
self.class.cmd_name
end
|
#command_options ⇒ Object
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_path ⇒ Object
142
143
144
145
146
|
# File 'lib/pik/commands/command.rb', line 142
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
124
125
126
|
# File 'lib/pik/commands/command.rb', line 124
def current_version?(string)
string == get_version
end
|
#default_gem_home ⇒ Object
148
149
150
|
# File 'lib/pik/commands/command.rb', line 148
def default_gem_home
gem_path.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
|
99
100
101
|
# File 'lib/pik/commands/command.rb', line 99
def editors
@editors ||= []
end
|
#find_config_from_path(path = Which::Ruby.find) ⇒ Object
136
137
138
139
140
|
# File 'lib/pik/commands/command.rb', line 136
def find_config_from_path(path=Which::Ruby.find)
config.find{|k,v|
Pathname(v[:path])== Pathname(path)
}.first rescue nil
end
|
156
157
158
|
# File 'lib/pik/commands/command.rb', line 156
def gem_path
`\"#{Which::Gem.exe}\" env gempath`.chomp.split(';').map{|p| Pathname(p).to_windows }
end
|
#get_version(path = Which::Ruby.find) ⇒ Object
128
129
130
131
132
133
134
|
# File 'lib/pik/commands/command.rb', line 128
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_options ⇒ Object
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/pik/commands/command.rb', line 109
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_version ⇒ Object
120
121
122
|
# File 'lib/pik/commands/command.rb', line 120
def pik_version
"pik " + Pik::VERSION
end
|
171
172
173
174
|
# File 'lib/pik/commands/command.rb', line 171
def sh(cmd)
puts cmd if debug
system(cmd)
end
|