Module: OS
- Defined in:
- lib/atk/os.rb
Overview
Groups
Constant Summary collapse
- CACHE =
create a singleton class
Class.new do attr_accessor :is_windows, :is_mac, :is_linux, :is_unix, :is_debian, :is_ubuntu, :version end.new
Class Method Summary collapse
- .has_command(name_of_executable) ⇒ Object
- .is?(adjective) ⇒ Boolean
- .path_for_executable(name_of_executable) ⇒ Object
- .version ⇒ Object
Class Method Details
.has_command(name_of_executable) ⇒ Object
142 143 144 |
# File 'lib/atk/os.rb', line 142 def self.has_command(name_of_executable) return OS.path_for_executable(name_of_executable) != nil end |
.is?(adjective) ⇒ Boolean
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/atk/os.rb', line 29 def self.is?(adjective) # summary: # this is a function created for convenience, so it doesn't have to be perfect # you can use it to ask about random qualities of the current OS and get a boolean response # convert to string (if its a symbol) adjective = adjective.to_s.downcase case adjective when 'windows' if CACHE::is_windows == nil CACHE::is_windows = (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil end return CACHE::is_windows when 'mac' if CACHE::is_mac == nil CACHE::is_mac = (/darwin/ =~ RUBY_PLATFORM) != nil end return CACHE::is_mac when 'linux' if CACHE::is_linux == nil CACHE::is_linux = (not OS.is?(:windows)) && (not OS.is?(:mac)) end return CACHE::is_linux when 'unix' if CACHE::is_unix == nil CACHE::is_unix = not(OS.is?(:windows)) end return CACHE::is_unix when 'debian' if CACHE::is_debian == nil CACHE::is_debian = File.file?('/etc/debian_version') end return CACHE::is_debian when 'ubuntu' if CACHE::is_ubuntu == nil CACHE::is_ubuntu = OS.has_command('lsb_release') && `lsb_release -a`.match(/Distributor ID:[\s\t]*Ubuntu/) end return CACHE::is_ubuntu end end |
.path_for_executable(name_of_executable) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/atk/os.rb', line 95 def self.path_for_executable(name_of_executable) program = name_of_executable # this method was extracted from the ptools gem, credit should go to them # https://github.com/djberg96/ptools/blob/master/lib/ptools.rb # this complex method is in favor of just calling the command line because command line calls are slow path=ENV['PATH'] if path.nil? || path.empty? raise ArgumentError, "path cannot be empty" end # Bail out early if an absolute path is provided. if program =~ /^\/|^[a-z]:[\\\/]/i program += WIN32EXTS if MSWINDOWS && File.extname(program).empty? found = Dir[program].first if found && File.executable?(found) && !File.directory?(found) return found else return nil end end # Iterate over each path glob the dir + program. path.split(File::PATH_SEPARATOR).each{ |dir| dir = File.(dir) next unless File.exist?(dir) # In case of bogus second argument file = File.join(dir, program) # Dir[] doesn't handle backslashes properly, so convert them. Also, if # the program name doesn't have an extension, try them all. if MSWINDOWS file = file.tr("\\", "/") file += WIN32EXTS if File.extname(program).empty? end found = Dir[file].first # Convert all forward slashes to backslashes if supported if found && File.executable?(found) && !File.directory?(found) found.tr!(File::SEPARATOR, File::ALT_SEPARATOR) if File::ALT_SEPARATOR return found end } return nil end |
.version ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/atk/os.rb', line 69 def self.version return CACHE::version if CACHE::version != nil # these statements need to be done in order from least to greatest if OS.is?("ubuntu") version_info = `lsb_release -a` version = Version.extract_from(version_info) name_area = version_info.match(/Codename: *(.+)/) if name_area version.codename = name_area[1].strip end elsif OS.is?("debian") # FUTURE: support debian version version = nil elsif OS.is?('mac') version = Version.extract_from(`system_profiler SPSoftwareDataType`) agreement_file = `cat '/System/Library/CoreServices/Setup Assistant.app/Contents/Resources/en.lproj/OSXSoftwareLicense.rtf'` codename_match = agreement_file.match(/SOFTWARE LICENSE AGREEMENT FOR *(?:macOS)? *(.+)\\\n/) if codename_match version.codename = codename_match[1].strip end elsif OS.is?('windows') version = nil end CACHE::version = version end |