Class: XigInstaller
- Inherits:
-
Object
show all
- Defined in:
- lib/xig_installer.rb
Defined Under Namespace
Classes: CommandNotExist, DirectoryNotExist, NetIRCNotInstalled, TargetGatewayNotInstalled, TargetGatewayNotValid
Constant Summary
collapse
- VERSION =
'0.0.2'
- TARGET_DIR =
RbConfig::CONFIG['bindir']
- XIG_GLOB =
'*ig.rb'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
Returns a new instance of XigInstaller.
20
21
22
23
24
25
26
|
# File 'lib/xig_installer.rb', line 20
def initialize
@commands = nil
@gateways = nil
@gateway_dir = nil
@max_cmdname_length = nil
@target_dir = TARGET_DIR
end
|
Instance Attribute Details
#target_dir ⇒ Object
Returns the value of attribute target_dir.
27
28
29
|
# File 'lib/xig_installer.rb', line 27
def target_dir
@target_dir
end
|
Instance Method Details
#_cmd_list_available ⇒ Object
129
130
131
|
# File 'lib/xig_installer.rb', line 129
def _cmd_list_available
gateways
end
|
#_cmd_list_installed ⇒ Object
133
134
135
136
137
|
# File 'lib/xig_installer.rb', line 133
def _cmd_list_installed
gateways.reject { |e|
!installed?( e )
}
end
|
#_cmd_list_update ⇒ Object
139
140
141
142
143
|
# File 'lib/xig_installer.rb', line 139
def _cmd_list_update
_cmd_list_installed.reject { |e|
!different?( e )
}
end
|
#_copy(targets) ⇒ Object
170
171
172
173
174
|
# File 'lib/xig_installer.rb', line 170
def _copy( targets )
targets.each { |e|
cp( File.join( gateway_dir, e ), @target_dir, :preserve => true )
}
end
|
#_remove(targets) ⇒ Object
188
189
190
191
192
|
# File 'lib/xig_installer.rb', line 188
def _remove( targets )
targets.each { |e|
rm( File.join( @target_dir, e ) )
}
end
|
#cmd_install(*targets) ⇒ Object
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/xig_installer.rb', line 145
def cmd_install( *targets )
available = gateways & targets
if ( targets.size == 0 )
_copy( gateways )
elsif ( available == targets )
_copy( targets )
else
raise TargetGatewayNotValid, (targets - available).join( ', ' )
end
end
|
#cmd_list(subcmd = nil) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/xig_installer.rb', line 116
def cmd_list( subcmd = nil )
subcmds = methods.grep( /\A_cmd_list_(.*)\z/ ).map { |c|
c.sub( /\A_cmd_list_(.*)\z/, '\1' )
}
if ( subcmd == 'help' )
subcmds
elsif ( subcmds.include?( subcmd ) )
puts send( "_cmd_list_#{subcmd}" )
else
puts _cmd_list_available
end
end
|
#cmd_uninstall(*targets) ⇒ Object
176
177
178
179
180
181
182
183
184
185
186
|
# File 'lib/xig_installer.rb', line 176
def cmd_uninstall( *targets )
installed = _cmd_list_installed
available = installed & targets
if ( targets.size == 0 )
_remove( installed )
elsif ( available == targets )
_remove( targets )
else
raise TargetGatewayNotInstalled, (targets - installed).join( ', ' )
end
end
|
#cmd_upgrade(*targets) ⇒ Object
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/xig_installer.rb', line 156
def cmd_upgrade( *targets )
if ( targets.size == 0 )
_cmd_list_installed.map { |e|
different?( e ) ? cmd_install( e ).to_s : nil
}.compact
else
targets.reject { |e|
!installed?( e )
}.map { |e|
different?( e ) ? cmd_install( e ).to_s : nil
}.compact
end
end
|
#commands ⇒ Object
51
52
53
54
55
56
57
58
59
|
# File 'lib/xig_installer.rb', line 51
def commands
if ( !@commands )
@commands = methods.grep( /\Acmd_(.*)\z/ ).map { |e|
e.sub( /\Acmd_(.*)\z/, '\1' )
}
end
return @commands
end
|
#different?(name) ⇒ Boolean
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/xig_installer.rb', line 67
def different?( name )
begin
src = File.stat( File.join( gateway_dir, name ) )
begin
dest = File.stat( File.join( @target_dir, name ) )
if ( src.mtime != dest.mtime )
return true
else
return false
end
rescue Errno::ENOENT
return true
end
rescue Errno::ENOENT
raise NetIRCNotInstalled
end
end
|
#exec_cmd(cmd) ⇒ Object
43
44
45
46
47
48
49
|
# File 'lib/xig_installer.rb', line 43
def exec_cmd( cmd )
if commands.include?( cmd )
send( "cmd_#{cmd}", *ARGV )
else
raise CommandNotExist
end
end
|
#gateway_dir ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/xig_installer.rb', line 101
def gateway_dir
if ( @gateway_dir.nil? )
net_irc = `gem which net/irc`.chomp
if ( File.exist?( net_irc ) )
@gateway_dir = (Pathname( net_irc ).
parent.parent.parent + 'examples').to_s
else
@gateway_dir = false
end
end
return @gateway_dir
end
|
#gateways(dir = nil) ⇒ Object
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/xig_installer.rb', line 85
def gateways( dir = nil )
dir ||= gateway_dir
if ( dir )
if ( File.exist?( dir ) )
Dir.chdir( dir ) {
Dir.glob( XIG_GLOB )
}
else
raise DirectoryNotExist, dir
end
else
raise NetIRCNotInstalled
end
end
|
#installed?(name) ⇒ Boolean
61
62
63
64
65
|
# File 'lib/xig_installer.rb', line 61
def installed?( name )
Dir.chdir( @target_dir ) {
Dir.glob( XIG_GLOB ).include?( name )
}
end
|
#max_cmdname_length ⇒ Object
258
259
260
261
262
263
264
265
266
|
# File 'lib/xig_installer.rb', line 258
def max_cmdname_length
if ( !@max_cmdname_length )
@max_cmdname_length = commands.map { |e|
e.size
}.max
end
return @max_cmdname_length
end
|
#parse_args ⇒ Object
268
269
270
271
|
# File 'lib/xig_installer.rb', line 268
def parse_args
parser.parse!( ARGV )
return ARGV.shift
end
|
#parser ⇒ Object
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
# File 'lib/xig_installer.rb', line 273
def parser
return OptionParser.new { |opt|
opt.banner = ''
opt.on( '-t', '--target DIR', 'target directory' ) { |d|
dir = File.expand_path( d )
if ( File.exist?( dir ) and File.directory?( dir ) )
@target_dir = dir
else
raise DirectoryNotExist, d
end
}
opt.on( '-g', '--gateway DIR', 'gateway source directory' ) { |d|
dir = File.expand_path( d )
if ( File.exist?( dir ) and File.directory?( dir ) )
@gateway_dir = dir
else
raise DirectoryNotExist, d
end
}
}
end
|
#run ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/xig_installer.rb', line 29
def run
if ( cmd = parse_args() )
if ( commands.include?( cmd ) )
exec_cmd( cmd )
elsif ( cmd == 'help' )
usage( ARGV.shift )
elsif ( cmd =~ /\A(?:--)?version\z/ )
usage( 'version' )
end
else
usage
end
end
|
#simple_help(cmd) ⇒ Object
249
250
251
252
253
254
255
256
|
# File 'lib/xig_installer.rb', line 249
def simple_help( cmd )
if ( commands.include?( cmd ) )
return sprintf( "%-*s %s",
max_cmdname_length,
cmd,
send( "usage_#{cmd}" ).lines.first.chomp )
end
end
|
#usage(cmd = nil) ⇒ Object
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
# File 'lib/xig_installer.rb', line 194
def usage( cmd = nil )
puts "#{self.class} ver.#{VERSION}"
if ( commands.include?( cmd ) )
puts "\n"
puts send( "usage_#{cmd}" )
elsif ( cmd == 'version' )
;
else
puts ERB.new( <<EOD, nil, '-' ).result( binding )
Usage: #{File.basename( $0 )} command [options] [TARGET]
Commands:
<%- commands.sort.each do |e| -%>
<%= simple_help( e ) %>
<%- end -%>
Options:
<%= parser.help.sub( /[\r\n]/, '' ) %>
EOD
end
exit
end
|
#usage_install ⇒ Object
231
232
233
234
235
|
# File 'lib/xig_installer.rb', line 231
def usage_install
ERB.new( <<EOD ).result( binding )
install specified or all gateways
EOD
end
|
#usage_list(subcmd = nil) ⇒ Object
218
219
220
221
222
223
224
225
226
227
228
229
|
# File 'lib/xig_installer.rb', line 218
def usage_list( subcmd = nil )
ERB.new( <<EOD, nil, '-' ).result( binding )
show gateway list
Usage: #{File.basename( $0 )} list [subcommand]
Subcommands:
<%- cmd_list( 'help' ).sort.each do |cmd| -%>
<%= cmd %>
<%- end -%>
EOD
end
|
#usage_uninstall ⇒ Object
243
244
245
246
247
|
# File 'lib/xig_installer.rb', line 243
def usage_uninstall
ERB.new( <<EOD ).result( binding )
uninstall specified or all gateways
EOD
end
|
#usage_upgrade ⇒ Object
237
238
239
240
241
|
# File 'lib/xig_installer.rb', line 237
def usage_upgrade
ERB.new( <<EOD ).result( binding )
upgrade specified or all gateways if need
EOD
end
|