2015-12-16 08:07:31 +03:00
|
|
|
# frozen_string_literal: false
|
2003-08-31 07:25:11 +04:00
|
|
|
# OptionParser internal utility
|
|
|
|
|
|
|
|
class << OptionParser
|
2008-12-18 10:54:50 +03:00
|
|
|
def show_version(*pkgs)
|
2003-08-31 07:25:11 +04:00
|
|
|
progname = ARGV.options.program_name
|
2004-11-09 08:27:58 +03:00
|
|
|
result = false
|
|
|
|
show = proc do |klass, cname, version|
|
2003-09-05 06:32:24 +04:00
|
|
|
str = "#{progname}"
|
2004-11-09 08:27:58 +03:00
|
|
|
unless klass == ::Object and cname == :VERSION
|
|
|
|
version = version.join(".") if Array === version
|
|
|
|
str << ": #{klass}" unless klass == Object
|
|
|
|
str << " version #{version}"
|
|
|
|
end
|
|
|
|
[:Release, :RELEASE].find do |rel|
|
|
|
|
if klass.const_defined?(rel)
|
|
|
|
str << " (#{klass.const_get(rel)})"
|
|
|
|
end
|
2003-08-31 07:25:11 +04:00
|
|
|
end
|
|
|
|
puts str
|
2004-11-09 08:27:58 +03:00
|
|
|
result = true
|
2003-08-31 07:25:11 +04:00
|
|
|
end
|
2008-12-18 10:54:50 +03:00
|
|
|
if pkgs.size == 1 and pkgs[0] == "all"
|
2003-09-08 15:00:17 +04:00
|
|
|
self.search_const(::Object, /\AV(?:ERSION|ersion)\z/) do |klass, cname, version|
|
|
|
|
unless cname[1] == ?e and klass.const_defined?(:Version)
|
2004-11-09 08:27:58 +03:00
|
|
|
show.call(klass, cname.intern, version)
|
2003-09-08 15:00:17 +04:00
|
|
|
end
|
|
|
|
end
|
2003-08-31 07:25:11 +04:00
|
|
|
else
|
2008-12-18 10:54:50 +03:00
|
|
|
pkgs.each do |pkg|
|
2003-08-31 07:25:11 +04:00
|
|
|
begin
|
2004-11-09 08:27:58 +03:00
|
|
|
pkg = pkg.split(/::|\//).inject(::Object) {|m, c| m.const_get(c)}
|
2003-09-08 15:00:17 +04:00
|
|
|
v = case
|
|
|
|
when pkg.const_defined?(:Version)
|
2004-11-09 08:27:58 +03:00
|
|
|
pkg.const_get(n = :Version)
|
2003-09-08 15:00:17 +04:00
|
|
|
when pkg.const_defined?(:VERSION)
|
2004-11-09 08:27:58 +03:00
|
|
|
pkg.const_get(n = :VERSION)
|
2003-09-08 15:00:17 +04:00
|
|
|
else
|
2004-11-09 08:27:58 +03:00
|
|
|
n = nil
|
2003-09-08 15:00:17 +04:00
|
|
|
"unknown"
|
|
|
|
end
|
2004-11-09 08:27:58 +03:00
|
|
|
show.call(pkg, n, v)
|
2003-08-31 07:25:11 +04:00
|
|
|
rescue NameError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2004-11-09 08:27:58 +03:00
|
|
|
result
|
2003-08-31 07:25:11 +04:00
|
|
|
end
|
|
|
|
|
2008-12-18 10:54:50 +03:00
|
|
|
def each_const(path, base = ::Object)
|
|
|
|
path.split(/::|\//).inject(base) do |klass, name|
|
2003-09-05 06:32:24 +04:00
|
|
|
raise NameError, path unless Module === klass
|
|
|
|
klass.constants.grep(/#{name}/i) do |c|
|
|
|
|
klass.const_defined?(c) or next
|
2014-07-27 15:04:28 +04:00
|
|
|
klass.const_get(c)
|
2003-09-05 06:32:24 +04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2003-08-31 07:25:11 +04:00
|
|
|
def search_const(klass, name)
|
|
|
|
klasses = [klass]
|
|
|
|
while klass = klasses.shift
|
|
|
|
klass.constants.each do |cname|
|
|
|
|
klass.const_defined?(cname) or next
|
|
|
|
const = klass.const_get(cname)
|
2003-09-08 15:00:17 +04:00
|
|
|
yield klass, cname, const if name === cname
|
2003-08-31 07:25:11 +04:00
|
|
|
klasses << const if Module === const and const != ::Object
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|