lib/upm/tool_class_methods.rb in upm-0.1.17 vs lib/upm/tool_class_methods.rb in upm-0.1.18

- old
+ new

@@ -1,9 +1,14 @@ module UPM class Tool class << self + def error(message) + $stderr.puts message + exit 1 + end + def tools; @@tools; end def register_tools! Dir["#{__dir__}/tools/*.rb"].each { |lib| require_relative(lib) } end @@ -13,28 +18,33 @@ pairs = open("/etc/os-release") do |io| io.read.scan(/^(\w+)="?(.+?)"?$/) end Hash[pairs] rescue Errno::ENOENT - {} + nil end end def current_os_names # eg: ID=ubuntu, ID_LIKE=debian if os_release os_release.values_at("ID", "ID_LIKE").compact else # `uname -s` => Darwin|FreeBSD|OpenBSD # `uname -o` => Android|Cygwin - [`uname -s`, `uname -o`].map(&:chomp).uniq + names = [`uname -s`] + names << `uname -o` unless names.first =~ /OpenBSD/ + names.map(&:chomp).uniq end end def nice_os_name - os_release.values_at("PRETTY_NAME", "NAME", "ID", "ID_LIKE").first || - (`uname -o`.chomp rescue nil) + if os_release + os_release.values_at("PRETTY_NAME", "NAME", "ID", "ID_LIKE").first + else + (`uname -o 2> /dev/null`.chomp rescue nil) + end end def installed @@tools.select { |tool| File.which(tool.identifying_binary) } end @@ -43,17 +53,17 @@ os_names = os_names ? [os_names].flatten : current_os_names tool = nil if os_names.any? - tool = @@tools.find { |name, tool| os_names.any? { |name| tool.os.include? name } } + tool = @@tools.find { |name, tool| os_names.any? { |name| tool.os&.include? name } } end if tool.nil? tool = @@tools.find { |name, tool| File.which(tool.identifying_binary) } end - tool.last + tool&.last end end end end