File: platform.rb

package info (click to toggle)
ruby-tty-platform 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 152 kB
  • sloc: ruby: 215; makefile: 4
file content (139 lines) | stat: -rw-r--r-- 3,450 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
68
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
94
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
# frozen_string_literal: true

require 'rbconfig'

require_relative 'platform/version'

module TTY
  # Detects system platform properties
  #
  # @api public
  class Platform
    WINDOWS_PATTERN = /(cygwin|mswin|mingw|bccwin|wince|emx)/i

    UNIX_PATTERN = /(aix|arch|darwin|linux|(net|free|open)bsd|cygwin|solaris|irix|hpux)/i

    LINUX_PATTERN = /linux|arch/i

    MAC_PATTERN = /darwin/i

    # Returns processor name, e.g. 'amdk6'
    #
    # @api public
    attr_reader :cpu

    # Returns the system/OS name, e.g. 'darwin'
    #
    # @api public
    attr_reader :os

    # Returns system's release version, e.g. '10.8.1'
    #
    # @api public
    attr_reader :version

    # Create platform properties
    #
    # @api public
    def initialize(arch = nil)
      @cpu, @os, @version = *detect_system_properties(arch)
    end

    # Add operating system query methods
    constants.grep(/PATTERN/).each do |pattern|
      name = pattern.to_s.split('_').first.downcase
      system_query = :"#{name}?"
      define_method(system_query) do
        match_os?(self.class.const_get(pattern))
      end
      self.class.class_eval do
        define_method(system_query) do
          new.public_send(system_query)
        end
      end
    end

    # Detect if using windows path delimiter
    #
    # @return [Boolean]
    #
    # @api public
    def windows_file_path?
      ::File::ALT_SEPARATOR == '\\'
    end

    # Check if platform matches given systems
    #
    # @return [Boolean]
    #
    # @api public
    def match_os?(matcher)
      !!(@os =~ matcher)
    end

    # Queries for system architecture information
    #
    # @return [String]
    #
    # @api public
    def architecture
      RbConfig::CONFIG['arch']
    end

    # @return [Array[String]]
    #
    # @api public
    def to_a
      [@cpu, @os, @version]
    end

    # String representation
    #
    # @api public
    def to_s
      to_a.compact.join('-')
    end

    private

    # Infer system properties from architecture information
    #
    # @return [Array[String, String String]]
    #
    # @api private
    def detect_system_properties(arch)
      parts = (arch || architecture).split('-', 2)

      if parts.length == 1
        @cpu, system = nil, parts.shift
      else
        @cpu, system = *parts
      end
      @os, @version = *find_os_and_version(system)
      [@cpu, @os, @version]
    end

    # @param [String] system
    #
    # @api private
    def find_os_and_version(system)
      case system
      when /aix(\d+(\.\d+)*)?/         then ['aix',       $1]
      when /bccwin(\d+(\.\d+)*)?/      then ['bccwin',    $1]
      when /bitrig(\d+(\.\d+)*)?/      then ['bitrig',    $1]
      when /cygwin/                    then ['cygwin',    nil]
      when /darwin(\d+(\.\d+)*)?/      then ['darwin',    $1]
      when /emx/                       then ['emx',       nil]
      when /freebsd(\d+(\.\d+)*)?/     then ['freebsd',   $1]
      when /linux(\d+(\.\d+)*)?/       then ['linux',     $1]
      when /mingw32/                   then ['mingw32',   nil]
      when /(mswin\d+)((\_|-)(\d+))?/  then [$1,          $4]
      when /netbsdelf/                 then ['netbsdelf', nil]
      when /openbsd(\d+(\.\d+)*)?/     then ['openbsd',   $1]
      when /solaris(\d+(\.\d+)*)?/     then ['solaris',   $1]
      when /wince(\d+(\.\d+)*)?/       then ['wince',     $1]
      else ['unknown', nil]
      end
    end
  end # Platform
end # TTY