host-os 0.1.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f86c0a89682b428496bf02a65b5807d1caaa30b5a9d70a34b27595436d1a3746
4
- data.tar.gz: b788605d3bdf9b5c851012502d665c6a8ef1ab4ef21b6fbf8828edaf40d78298
3
+ metadata.gz: 9a1718752411b34a816e31b58be6f1a3cd83aec89b851e74f6997998e173dcad
4
+ data.tar.gz: fc322f198a39c3b5b7792012b2206960e0889302a97716ee9bed733b485b642f
5
5
  SHA512:
6
- metadata.gz: e87cb31326255c7e359a5d2110ea81bb43971e9b6d524ccd12eefa7c38368e88c1aec56e68b3beccb06dbfbb078244cfbef0ceefdeacc6d9f856835eac177fc8
7
- data.tar.gz: e6e316ad63c38c00d01aeefa252ae2cfd3027d0669ac7b023e40b926c493cb11f78b8f237876d9964c91bc6088cf55e5dac64626e5f3b56dd4b43d3c9d21470c
6
+ metadata.gz: 3652cc6e8ddbc5c5489bec318e48f7ce5c73f1c84aa4940887ba080767a8173835e24b2739db27fff6a16f4aaa9f980d9d59edfa66c1ade9e4a296357043bbe2
7
+ data.tar.gz: 3fd4535322bb9d0de53af3d2ae0695e4199e31976fb472f0d631c1c57750d5c1eac6174abcd16b8e52b4ffa01b3e5ef642dc027c71e4f913fe0412885aeb93cd
data/.yardopts ADDED
@@ -0,0 +1,7 @@
1
+ --readme README.md
2
+ --title 'HostOS: Documentation'
3
+ --charset utf-8
4
+ --markup markdown
5
+ --tag comment
6
+ --hide-tag comment
7
+ 'lib/**/*.rb' - 'README.md' 'LICENSE'
data/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  BSD 3-Clause License
2
2
 
3
- Copyright (c) 2017-2021, Mike Blumtritt. All rights reserved.
3
+ Copyright (c) 2017-2025, Mike Blumtritt. All rights reserved.
4
4
 
5
5
  Redistribution and use in source and binary forms, with or without
6
6
  modification, are permitted provided that the following conditions are met:
data/README.md CHANGED
@@ -1,6 +1,4 @@
1
- # HostOS
2
-
3
- ![version](https://img.shields.io/gem/v/host-os?label=)
1
+ # HostOS ![version](https://img.shields.io/gem/v/host-os?label=)
4
2
 
5
3
  HostOS is a module that offers details about the host operating system, the current Ruby interpreter, and the environment currently in use.
6
4
 
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative('../host-os') unless defined?(HostOS.id)
4
+
5
+ module HostOS
6
+ #
7
+ # This module allows to identify the used Ruby interpreter.
8
+ #
9
+ # Besides here documented boolean attributes you can also check for any other
10
+ # boolean attribute or interpreter name:
11
+ #
12
+ # @example Query for the Opal interpreter
13
+ # HostOS.interpreter.opal?
14
+ # @example Query for TruffleRuby
15
+ # HostOS.interpreter.truffleruby?
16
+ #
17
+ module Interpreter
18
+ extend Helper
19
+
20
+ class << self
21
+ # @!attribute [r] mri?
22
+ # @return [true, false] whether the interpreter is the Yukihiro
23
+ # Matsumoto's C-based (default) Ruby Interpreter
24
+ def mri?
25
+ id == :mri
26
+ end
27
+ alias cruby? mri?
28
+ alias default? mri?
29
+
30
+ # @!attribute [r] cardinal?
31
+ # @return [true, false] whether the interpreter is the Parrot based
32
+ # Cardinal interpreter
33
+ def cardinal?
34
+ id == :cardinal
35
+ end
36
+ alias parrot? cardinal?
37
+
38
+ # @!attribute [r] jruby?
39
+ # @return [true, false] whether the interpreter is the Java based JRuby
40
+ # Interpreter
41
+ def jruby?
42
+ id == :jruby
43
+ end
44
+ alias java? jruby?
45
+
46
+ # @!attribute [r] rbx?
47
+ # @return [true, false] whether the interpreter is the Rubinius
48
+ # Interpreter
49
+ def rbx?
50
+ id == :rbx
51
+ end
52
+ alias rubinius? rbx?
53
+
54
+ # @!attribute [r] ree?
55
+ # @return [true, false] whether the interpreter is the Ruby Enterprise
56
+ # Edition
57
+ def ree?
58
+ id == :ree
59
+ end
60
+ alias enterprise? ree?
61
+
62
+ # @!attribute [r] jit_enabled?
63
+ # @return [true, false] whether the interpreter currently uses a JIT
64
+ # Compiler
65
+ def jit_enabled?
66
+ jit_type != :none
67
+ end
68
+
69
+ # @!attribute [r] jit_type
70
+ # @return [:mjit, :rjit, :yjit, :java, :none] type of currently used JIT
71
+ # Compiler
72
+ def jit_type
73
+ return :mjit if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled?
74
+ return :yjit if defined?(RubyVM::YJIT) && RubyVM::YJIT.enabled?
75
+ return :rjit if defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?
76
+ jruby? ? :java : :none
77
+ end
78
+
79
+ # @!visibility private
80
+ def to_s
81
+ @to_s ||=
82
+ {
83
+ cardinal: 'Cardinal',
84
+ jruby: 'JRuby',
85
+ mri: 'CRuby',
86
+ rby: 'Rubinius',
87
+ ree: 'Enterprise Ruby',
88
+ truffleruby: 'TruffleRuby'
89
+ }.compare_by_identity[
90
+ id
91
+ ] || id.to_s.upcase
92
+ end
93
+
94
+ # Path name of current Ruby executable.
95
+ # @!attribute [r] exe
96
+ # @return [String] complete path name to current Ruby executable
97
+ # @return [nil] when executable can not be detected
98
+ def exe
99
+ defined?(@exe) ? @exe : @exe = find_exe
100
+ end
101
+
102
+ # @!attribute [r] id
103
+ # @return [Symbol] interpreter identifier
104
+
105
+ # @!method is?(what)
106
+ # @param what [Symbol, String] the identifier to check
107
+ # @return [true, false] whether the interpreter is the given identifier
108
+
109
+ # @comment YARD requires this line
110
+
111
+ private
112
+
113
+ def identify
114
+ if defined?(RUBY_PLATFORM) && (RUBY_PLATFORM == 'parrot')
115
+ return :cardinal
116
+ end
117
+ return :mri unless defined?(RUBY_ENGINE)
118
+ return RUBY_ENGINE.to_sym if RUBY_ENGINE != 'ruby'
119
+ RUBY_DESCRIPTION.downcase.include?('enterprise') ? :ree : :mri
120
+ end
121
+
122
+ def find_exe
123
+ require 'rbconfig' unless defined?(RbConfig)
124
+ RbConfig.ruby
125
+ rescue LoadError
126
+ ENV['RUBY']
127
+ rescue NoMethodError
128
+ File.join(
129
+ RbConfig::CONFIG['bindir'],
130
+ RbConfig::CONFIG['ruby_install_name'] + RbConfig::CONFIG['EXEEXT']
131
+ )
132
+ end
133
+ end
134
+
135
+ @id = identify
136
+ end
137
+ end
@@ -10,176 +10,145 @@ module HostOS
10
10
  #
11
11
  # @note You need to require `host-os/support` explicitly.
12
12
  module Support
13
- # @attribute [r] dev_null
14
- # @return [String] name of or path to the null device
15
- # @note This attribute is only available on Windows, OS2 and Unix systems.
16
-
17
- # @attribute [r] open_command
18
- # @return [String] name of the open command
19
- # @note This attribute is only available on Windows, MacOS and Linux systems.
20
-
21
- # @attribute [r] rss_bytes
22
- # @return [Integer] number of bytes used by the current process
23
- # @note This attribute is only available on Windows and Unix or when using
24
- # JRuby
25
-
26
- # @attribute [r] suggested_thread_count
27
- # @return [Integer] suggested number of threads to use
28
-
29
- # @attribute [r] temp_dir
30
- # @return [String] name of the temporary directory
31
-
32
- # @!method app_config_path(app_name)
33
- # @param app_name [String] name of the application
34
- # @return [String] absolute name of the directory
35
- # Determines the name of the directory where application specific data should
36
- # be stored.
37
- # @note This method is only available on Windows and Posix-compatible
38
- # systems.
39
-
40
- # @comment YARD requires this emoty line :/
41
- module Windows
13
+ if defined?(File::NULL)
14
+ # @attribute [r] dev_null
15
+ # @return [String] name of or path to the null device
16
+ # @note This attribute is only available on
17
+ # Windows, OS2 and Unix systems for older Ruby versions.
42
18
  def dev_null
43
- 'NUL'
44
- end
45
-
46
- def open_command
47
- 'start'
19
+ File::NULL
48
20
  end
49
-
50
- def rss_bytes
51
- `tasklist /FI "PID eq #{Process.pid}" /FO CSV`.split(',"')[-1].tr(
52
- ',.',
53
- '__'
54
- ).to_i * 1024
21
+ elsif HostOS.windows?
22
+ def dev_null
23
+ 'NUL'
55
24
  end
56
-
57
- private
58
-
59
- def _app_config_path
60
- ENV['LOCALAPPDATA'] ||
61
- "#{ENV['USERPROFILE']}/Local Settings/Application Data"
25
+ elsif HostOS.os2?
26
+ def dev_null
27
+ 'nul'
62
28
  end
63
- end
64
-
65
- module Unix
29
+ elsif HostOS.unix?
66
30
  def dev_null
67
31
  '/dev/null'
68
32
  end
69
-
70
- def rss_bytes
71
- `ps -o rss= -p #{Process.pid}`.to_i * 1024
72
- end
73
-
74
- private
75
-
76
- def _app_config_path
77
- (ENV['XDG_CONFIG_HOME'] || '~/.config')
78
- end
79
33
  end
80
34
 
81
- module OS2
82
- def dev_null
83
- 'nul'
35
+ if HostOS.windows?
36
+ # @attribute [r] open_command
37
+ # @return [String] name of the open command
38
+ # @note This attribute is only available on Windows, MacOS and Linux systems.
39
+ def open_command
40
+ 'start'
84
41
  end
85
- end
86
-
87
- module MacOS
42
+ elsif HostOS.macosx?
88
43
  def open_command
89
44
  'open'
90
45
  end
91
-
92
- private
93
-
94
- def _app_config_path
95
- '~/Library/Application Support'
96
- end
97
- end
98
-
99
- module Linux
46
+ elsif HostOS.linux?
100
47
  def open_command
101
48
  'xdg-open'
102
49
  end
103
50
  end
104
51
 
105
- module AppConfigPath
106
- def app_config_path(app_name)
107
- File.expand_path(app_name, _app_config_path)
52
+ if HostOS.windows?
53
+ # @attribute [r] rss_bytes
54
+ # @return [Integer] number of bytes used by the current process
55
+ # @note This attribute is only available on Windows and Unix or when using
56
+ # JRuby
57
+ def rss_bytes
58
+ `tasklist /FI "PID eq #{Process.pid}" /FO CSV`.split(',"')[-1].tr(
59
+ ',.',
60
+ '__'
61
+ ).to_i * 1024
108
62
  end
109
- end
110
-
111
- module JRuby
63
+ elsif Interpreter.jruby?
112
64
  def rss_bytes
113
65
  require 'java' unless defined?(java)
114
66
  bean = java.lang.management.ManagementFactory.getMemoryMXBean
115
67
  bean.heap_memory_usage.used + bean.non_heap_memory_usage.used
116
68
  end
69
+ elsif HostOS.unix?
70
+ def rss_bytes
71
+ `ps -o rss= -p #{Process.pid}`.to_i * 1024
72
+ end
117
73
  end
118
74
 
119
- module Tools
120
- def suggested_thread_count
121
- @suggested_thread_count ||= find_suggested_thread_count
75
+ if HostOS.windows?
76
+ # @param app_name [String] name of the application
77
+ # @return [String] absolute name of the directory
78
+ # Determines the name of the directory where application specific data
79
+ # should be stored.
80
+ # @note This method is only available on Windows and Posix-compatible
81
+ # systems.
82
+ def app_config_path(app_name)
83
+ File.expand_path(
84
+ app_name,
85
+ ENV['LOCALAPPDATA'] ||
86
+ "#{ENV['USERPROFILE']}/Local Settings/Application Data"
87
+ )
122
88
  end
123
-
124
- def temp_dir
125
- @temp_dir ||= find_temp_dir
89
+ elsif HostOS.macosx?
90
+ def app_config_path(app_name)
91
+ File.expand_path(app_name, '~/Library/Application Support')
126
92
  end
127
-
128
- private
129
-
130
- def find_suggested_thread_count
131
- count = ENV['TC'].to_i
132
- count > 0 ? count : with_etc(4) { Etc.nprocessors }
93
+ elsif HostOS.unix?
94
+ def app_config_path(app_name)
95
+ File.expand_path(app_name, ENV['XDG_CONFIG_HOME'] || '~/.config')
133
96
  end
97
+ end
134
98
 
135
- def find_temp_dir
136
- return Dir.tmpdir if defined?(Dir.tmpdir)
137
- as_dir('TMPDIR') || as_dir('TMP') || as_dir('TEMP') ||
138
- as_dir(
139
- 'system temp dir',
140
- with_etc("#{ENV['LOCALAPPDATA']}/Temp") { Etc.systmpdir }
141
- ) || as_dir('/tmp', '/tmp') || as_dir('.', '.')
142
- end
99
+ # @attribute [r] suggested_thread_count
100
+ # @return [Integer] suggested number of threads to use
101
+ def suggested_thread_count
102
+ @suggested_thread_count ||= find_suggested_thread_count
103
+ end
143
104
 
144
- def as_dir(name, dirname = ENV[name])
145
- return if dirname.nil? || dirname.empty?
146
- dirname = File.expand_path(dirname)
147
- stat = File.stat(dirname)
148
- stat.directory? or warn("#{name} is not a valid directory - #{dirname}")
149
- stat.writable? or warn("#{name} is not writable - #{dirname}")
150
- (stat.world_writable? && !stat.sticky?) and
151
- warn("#{name} is world-writable - #{dirname}")
152
- dirname
153
- rescue SystemCallError
154
- nil
155
- end
105
+ # @attribute [r] temp_dir
106
+ # @return [String] name of the temporary directory
107
+ def temp_dir
108
+ @temp_dir ||= find_temp_dir
109
+ end
156
110
 
157
- def with_etc(default)
111
+ private
112
+
113
+ def find_suggested_thread_count
114
+ count = ENV['TC'].to_i
115
+ return count if count > 0
116
+ begin
158
117
  require('etc') unless defined?(Etc)
159
- yield
118
+ Etc.nprocessors
160
119
  rescue LoadError
161
- default
120
+ 4
162
121
  end
163
122
  end
164
- end
165
123
 
166
- extend Support::Windows if windows?
167
- extend Support::Unix if unix?
168
- extend Support::MacOS if macosx?
169
- extend Support::OS2 if os2?
170
- extend Support::Linux if linux?
171
- extend Support::AppConfigPath if windows? || unix?
172
- extend Support::JRuby if Interpreter.jruby?
173
- extend Support::Tools
124
+ def find_temp_dir
125
+ return Dir.tmpdir if defined?(Dir.tmpdir)
126
+ ret = as_dir('TMPDIR') || as_dir('TMP') || as_dir('TEMP')
127
+ return ret if ret
128
+ ret =
129
+ begin
130
+ require('etc') unless defined?(Etc)
131
+ Etc.systmpdir
132
+ rescue LoadError
133
+ "#{ENV['LOCALAPPDATA']}/Temp"
134
+ end
135
+ as_dir('system temp', ret) || as_dir('/tmp', '/tmp') || as_dir('.', '.')
136
+ end
174
137
 
175
- module Support
176
- private_constant :Windows
177
- private_constant :Unix
178
- private_constant :OS2
179
- private_constant :MacOS
180
- private_constant :Linux
181
- private_constant :AppConfigPath
182
- private_constant :JRuby
183
- private_constant :Tools
138
+ def as_dir(name, dirname = nil)
139
+ dirname ||= ENV[name]
140
+ return if dirname.nil? || dirname.empty?
141
+ dirname = File.expand_path(dirname)
142
+ stat = File.stat(dirname)
143
+ stat.directory? or warn("#{name} is not a valid directory - #{dirname}")
144
+ stat.writable? or warn("#{name} is not writable - #{dirname}")
145
+ (stat.world_writable? && !stat.sticky?) and
146
+ warn("#{name} is world-writable - #{dirname}")
147
+ dirname
148
+ rescue SystemCallError
149
+ nil
150
+ end
184
151
  end
152
+
153
+ extend Support
185
154
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HostOS
4
- # @return [String] the version number of the gem
5
- VERSION = '0.1.0'
4
+ # Version number of the gem.
5
+ VERSION = '0.2.2'
6
6
  end
data/lib/host-os.rb CHANGED
@@ -6,281 +6,65 @@
6
6
  # It helps you write environment-specific code in a clean way.
7
7
  #
8
8
  module HostOS
9
- module Helper
10
- def is?(what)
11
- return true if id == what
12
- id == (defined?(what.to_sym) ? what.to_sym : what.to_s.to_sym)
13
- end
14
-
15
- protected
16
-
17
- def respond_to_missing?(name, _include_all = false)
18
- (name[-1] == '?') || super
19
- end
20
-
21
- def method_missing(name, *args)
22
- return super if name[-1] != '?'
23
- return is?(name[0..-2]) if args.empty?
24
- raise(
25
- ArgumentError,
26
- "wrong number of arguments (given #{args.size}, expected 0)"
27
- )
28
- end
29
- end
30
- private_constant :Helper
31
-
32
- #
33
- # This module allows to identify the current environment by checking the `ENV`
34
- # for the following variables in order:
35
- #
36
- # * RAILS_ENV
37
- # * RACK_ENV
38
- # * ENVIRONMENT
39
- # * ENV
40
- #
41
- # You can check for any boolean attribute:
42
- #
43
- # @example Query if the environment is configured as "staging"
44
- # HostOS.env.staging?
45
- # @example Test if the environment is configured as "production"
46
- # HostOS.env.is? :production
47
- #
48
- # @note When no environment is configured 'prod
49
- #
50
- module Env
51
- extend Helper
52
-
53
- class << self
54
- # @attribute [r] production?
55
- # @return [true, false] whether the environment is configured as "production"
56
- # @note This will return true if the environment is not configured.
57
-
58
- # @attribute [r] test?
59
- # @return [true, false] whether the environment is configured as "test"
60
-
61
- # @attribute [r] development?
62
- # @return [true, false] whether the environment is configured as
63
- # "development"
64
-
65
- # @attribute [r] id
66
- # @return [Symbol] environment identifier
67
- def id
68
- ID
69
- end
70
-
71
- # @!method is?(what)
72
- # @param what [Symbol, String] the identifier to check
73
- # @return [true, false] whether the environment is the given identifier
74
-
75
- # @comment YARD requires this line
76
-
77
- private
78
-
79
- def identify
80
- found =
81
- (
82
- ENV['RAILS_ENV'] || ENV['RACK_ENV'] || ENV['ENVIRONMENT'] ||
83
- ENV['ENV']
84
- )
85
- return :production if found.nil? || found.empty?
86
- found.downcase.tr(' -', '__').to_sym
87
- end
88
- end
89
-
90
- # @return [Symbol] environment identifier
91
- ID = identify
92
- end
93
-
94
- #
95
- # This module allows to identify the used Ruby interpreter.
96
- #
97
- # Besides here documented boolean attributes you can also check for any other
98
- # boolean attribute or interpreter name:
99
- #
100
- # @example Query for the Opal interpreter
101
- # HostOS.interpreter.opal?
102
- # @example Query for TruffleRuby
103
- # HostOS.interpreter.truffleruby?
104
- #
105
- module Interpreter
106
- extend Helper
107
-
108
- class << self
109
- # @attribute [r] id
110
- # @return [Symbol] interpreter identifier
111
- def id
112
- ID
113
- end
114
-
115
- # @attribute [r] mri?
116
- # @return [true, false] whether the interpreter is the Yukihiro Matsumoto's
117
- # C-based (default) Ruby Interpreter
118
- def mri?
119
- ID == :mri
120
- end
121
- alias cruby? mri?
122
- alias default? mri?
123
-
124
- # @attribute [r] cardinal?
125
- # @return [true, false] whether the interpreter is the Parrot based Cardinal
126
- # interpreter
127
- def cardinal?
128
- ID == :cardinal
129
- end
130
- alias parrot? cardinal?
131
-
132
- # @attribute [r] jruby?
133
- # @return [true, false] whether the interpreter is the Java based JRuby
134
- # Interpreter
135
- def jruby?
136
- ID == :jruby
137
- end
138
- alias java? jruby?
139
-
140
- # @attribute [r] rbx?
141
- # @return [true, false] whether the interpreter is the Rubinius Interpreter
142
- def rbx?
143
- ID == :rbx
144
- end
145
- alias rubinius? rbx?
146
-
147
- # @attribute [r] ree?
148
- # @return [true, false] whether the interpreter is the Ruby Enterprise
149
- # Edition
150
- def ree?
151
- ID == :ree
152
- end
153
- alias enterprise? ree?
154
-
155
- # @attribute [r] jit_enabled?
156
- # @return [true, false] whether the interpreter currently uses a JIT
157
- # Compiler
158
- def jit_enabled?
159
- jit_type != :none
160
- end
161
-
162
- # @attribute [r] jit_type
163
- # @return [:mjit, :rjit, :yjit, :java, :none] type of currently used JIT
164
- # Compiler
165
- def jit_type
166
- return :mjit if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled?
167
- return :yjit if defined?(RubyVM::YJIT) && RubyVM::YJIT.enabled?
168
- return :rjit if defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?
169
- jruby? ? :java : :none
170
- end
171
-
172
- # @!visibility private
173
- def to_s
174
- case ID
175
- when :mri
176
- 'CRuby'
177
- when :ree
178
- 'Enterprise Ruby'
179
- when :cardinal
180
- 'Cardinal'
181
- when :jruby
182
- 'JRuby'
183
- when :rby
184
- 'Rubinius'
185
- when :truffleruby
186
- 'TruffleRuby'
187
- else
188
- ID.to_s.upcase
189
- end
190
- end
191
-
192
- # @!method is?(what)
193
- # @param what [Symbol, String] the identifier to check
194
- # @return [true, false] whether the interpreter is the given identifier
195
-
196
- # @comment YARD requires this line
197
-
198
- private
199
-
200
- def identify
201
- if defined?(RUBY_PLATFORM) && (RUBY_PLATFORM == 'parrot')
202
- return :cardinal
203
- end
204
- return :mri unless defined?(RUBY_ENGINE)
205
- return RUBY_ENGINE.to_sym if RUBY_ENGINE != 'ruby'
206
- RUBY_DESCRIPTION.downcase.include?('enterprise') ? :ree : :mri
207
- end
208
- end
209
-
210
- # @return [Symbol] interpreter identifier
211
- ID = identify
212
- end
213
-
214
- extend Helper
215
-
216
9
  class << self
217
- # @attribute [r] id
218
- # @return [Symbol] OS identifier
219
- def id
220
- ID
221
- end
222
-
223
- # @attribute [r] type
224
10
  # @return [:unix, :windows, :vms, :os2, :unknown] OS type
225
- def type
226
- TYPE
227
- end
11
+ attr_reader :type
228
12
 
229
- # @attribute [r] interpreter
13
+ # @!attribute [r] interpreter
230
14
  # @return [Interpreter] interpreter information
231
15
  def interpreter
232
16
  Interpreter
233
17
  end
234
18
 
235
- # @attribute [r] env
19
+ # @!attribute [r] env
236
20
  # @return [Env] environment information
237
21
  def env
238
22
  Env
239
23
  end
240
24
 
241
- # @attribute [r] unix?
25
+ # @!attribute [r] unix?
242
26
  # @return [true, false] whether the host OS is a Unix OS
243
27
  def unix?
244
- TYPE == :unix
28
+ @type == :unix
245
29
  end
246
30
 
247
- # @attribute [r] windows?
31
+ # @!attribute [r] windows?
248
32
  # @return [true, false] whether the host OS is a Windows OS
249
33
  def windows?
250
- TYPE == :windows
34
+ @type == :windows
251
35
  end
252
36
 
253
- # @attribute [r] vms?
37
+ # @!attribute [r] vms?
254
38
  # @return [true, false] whether the host OS is VMS
255
39
  def vms?
256
- TYPE == :vms
40
+ @type == :vms
257
41
  end
258
42
 
259
- # @attribute [r] os2?
43
+ # @!attribute [r] os2?
260
44
  # @return [true, false] whether the host OS is OS/2
261
45
  def os2?
262
- TYPE == :os2
46
+ @type == :os2
263
47
  end
264
48
 
265
- # @attribute [r] macosx?
49
+ # @!attribute [r] macosx?
266
50
  # @return [true, false] whether the host OS is identified as MacOS
267
51
  def macosx?
268
- ID == :macosx
52
+ @id == :macosx
269
53
  end
270
54
 
271
- # @attribute [r] linux?
55
+ # @!attribute [r] linux?
272
56
  # @return [true, false] whether the host OS is identified as Linux derivate
273
57
  def linux?
274
- ID == :linux
58
+ @id == :linux
275
59
  end
276
60
 
277
- # @attribute [r] cygwin?
61
+ # @!attribute [r] cygwin?
278
62
  # @return [true, false] whether the host OS is Windows/Cygwin
279
63
  def cygwin?
280
- ID == :cygwin
64
+ @id == :cygwin
281
65
  end
282
66
 
283
- # @attribute [r] posix?
67
+ # @!attribute [r] posix?
284
68
  # @return [true, false] whether the host OS is Posix compatible
285
69
  # This attribute is `true` when Posix compatible commands like `fork` are
286
70
  # available.
@@ -291,41 +75,41 @@ module HostOS
291
75
  # @param what [Symbol, String] the identifier to check
292
76
  # @return [true, false] whether the host OS is the given identifier or type
293
77
  def is?(what)
294
- return true if (ID == what) || (TYPE == what)
295
- what = defined?(what.to_sym) ? what.to_sym : what.to_s.to_sym
296
- (ID == what) || (TYPE == what)
78
+ return (@id == what) || (@type == what) if what.is_a?(Symbol)
79
+ if defined?(what.to_sym)
80
+ what = what.to_sym
81
+ return (@id == what) || (@type == what)
82
+ end
83
+ if defined?(what.to_s)
84
+ what = what.to_s.to_sym
85
+ return (@id == what) || (@type == what)
86
+ end
87
+ false
297
88
  end
298
89
 
90
+ # @!attribute [r] id
91
+ # @return [Symbol] OS identifier
92
+
299
93
  # @!visibility private
300
94
  def to_s
301
- case ID
302
- when :linux
303
- 'Linux'
304
- when :macosx
305
- 'MacOSX'
306
- when :freebsd
307
- 'FreeBSD'
308
- when :netbsd
309
- 'NetBSD'
310
- when :openbsd
311
- 'OpenBSD'
312
- when :dragonfly
313
- 'Dragonly'
314
- when :sunis
315
- 'SunOS'
316
- when :mswin
317
- 'MSWin'
318
- when :mingw
319
- 'MinGW'
320
- when :bccwin
321
- 'BCCWin'
322
- when :wince
323
- 'WinCE'
324
- when :windows, :cygwin
325
- ID.to_s.capitalize
326
- else
327
- ID.to_s.upcase
328
- end
95
+ @to_s ||=
96
+ {
97
+ bccwin: 'BCCWin',
98
+ cygwin: 'Cygwin',
99
+ dragonfly: 'Dragonly',
100
+ freebsd: 'FreeBSD',
101
+ linux: 'Linux',
102
+ macosx: 'MacOSX',
103
+ mingw: 'MinGW',
104
+ mswin: 'MSWin',
105
+ netbsd: 'NetBSD',
106
+ openbsd: 'OpenBSD',
107
+ sunos: 'SunOS',
108
+ wince: 'WinCE',
109
+ windows: 'Windows'
110
+ }.compare_by_identity[
111
+ @id
112
+ ] || @id.to_s.upcase
329
113
  end
330
114
 
331
115
  private
@@ -335,7 +119,7 @@ module HostOS
335
119
  id = RbConfig::CONFIG['host_os'].downcase
336
120
  id, type, normalized =
337
121
  [
338
- ['linux', :unix],
122
+ ['linux', :unix, :linux],
339
123
  ['arch', :unix, :linux],
340
124
  ['darwin', :unix, :macosx],
341
125
  ['mac', :unix, :macosx],
@@ -363,5 +147,92 @@ module HostOS
363
147
  end
364
148
  end
365
149
 
366
- ID, TYPE = identify
150
+ module Helper
151
+ attr_reader :id
152
+
153
+ def is?(what)
154
+ return id == what if what.is_a?(Symbol)
155
+ return id == what.to_sym if defined?(what.to_sym)
156
+ return id == what.to_s.to_sym if defined?(what.to_s)
157
+ false
158
+ end
159
+
160
+ private
161
+
162
+ def respond_to_missing?(name, _include_all = false)
163
+ (name[-1] == '?') || super
164
+ end
165
+
166
+ def method_missing(name, *args)
167
+ return super if name[-1] != '?'
168
+ return is?(name[0..-2]) if args.empty?
169
+ raise(
170
+ ArgumentError,
171
+ "wrong number of arguments (given #{args.size}, expected 0)"
172
+ )
173
+ end
174
+ end
175
+ private_constant :Helper
176
+
177
+ #
178
+ # This module allows to identify the current environment by checking the `ENV`
179
+ # for the following variables in order:
180
+ #
181
+ # * RAILS_ENV
182
+ # * RACK_ENV
183
+ # * ENVIRONMENT
184
+ # * ENV
185
+ #
186
+ # You can check for any boolean attribute:
187
+ #
188
+ # @example Query if the environment is configured as "staging"
189
+ # HostOS.env.staging?
190
+ # @example Test if the environment is configured as "production"
191
+ # HostOS.env.is? :production
192
+ #
193
+ # @note When no environment is configured `production` is assumed.
194
+ #
195
+ module Env
196
+ extend Helper
197
+
198
+ class << self
199
+ # @!attribute [r] production?
200
+ # @return [true, false] whether the environment is configured as "production"
201
+ # @note This will return true if the environment is not configured.
202
+
203
+ # @!attribute [r] test?
204
+ # @return [true, false] whether the environment is configured as "test"
205
+
206
+ # @!attribute [r] development?
207
+ # @return [true, false] whether the environment is configured as
208
+ # "development"
209
+
210
+ # @!attribute [r] id
211
+ # @return [Symbol] environment identifier
212
+
213
+ # @!method is?(what)
214
+ # @param what [Symbol, String] the identifier to check
215
+ # @return [true, false] whether the environment is the given identifier
216
+
217
+ # @comment YARD requires this line
218
+
219
+ private
220
+
221
+ def identify
222
+ found =
223
+ ENV['RAILS_ENV'] || ENV['RACK_ENV'] || ENV['ENVIRONMENT'] ||
224
+ ENV['ENV']
225
+ return :production if found.nil? || found.empty?
226
+ found.downcase.gsub(/\W/, '_').to_sym
227
+ end
228
+ end
229
+
230
+ @id = identify
231
+ end
232
+
233
+ autoload :Interpreter, "#{__dir__}/host-os/interpreter.rb"
234
+
235
+ extend Helper
236
+
237
+ @id, @type = identify
367
238
  end
metadata CHANGED
@@ -1,30 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: host-os
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Blumtritt
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2023-09-30 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies: []
13
12
  description: |
14
13
  This gem helps you write environment-specific code in a clean way.
15
14
  It provides a simple API to get information about the operating system,
16
15
  the current Ruby interpreter, and the configured environment.
17
- email:
18
16
  executables: []
19
17
  extensions: []
20
18
  extra_rdoc_files:
21
- - README.md
22
19
  - LICENSE
20
+ - README.md
23
21
  files:
22
+ - ".yardopts"
24
23
  - LICENSE
25
24
  - README.md
26
25
  - examples/current.rb
27
26
  - lib/host-os.rb
27
+ - lib/host-os/interpreter.rb
28
28
  - lib/host-os/support.rb
29
29
  - lib/host-os/version.rb
30
30
  - lib/host_os.rb
@@ -32,27 +32,25 @@ homepage: https://github.com/mblumtritt/host-os
32
32
  licenses:
33
33
  - BSD-3-Clause
34
34
  metadata:
35
+ rubygems_mfa_required: 'true'
35
36
  source_code_uri: https://github.com/mblumtritt/host-os
36
37
  bug_tracker_uri: https://github.com/mblumtritt/host-os/issues
37
- documentation_uri: https://rubydoc.info/gems/host-os
38
- rubygems_mfa_required: 'true'
39
- post_install_message:
38
+ documentation_uri: https://rubydoc.info/gems/host-os/0.2.2/HostOS
40
39
  rdoc_options: []
41
40
  require_paths:
42
41
  - lib
43
42
  required_ruby_version: !ruby/object:Gem::Requirement
44
43
  requirements:
45
- - - ">"
44
+ - - ">="
46
45
  - !ruby/object:Gem::Version
47
- version: '2.3'
46
+ version: '2.7'
48
47
  required_rubygems_version: !ruby/object:Gem::Requirement
49
48
  requirements:
50
49
  - - ">="
51
50
  - !ruby/object:Gem::Version
52
51
  version: '0'
53
52
  requirements: []
54
- rubygems_version: 3.4.19
55
- signing_key:
53
+ rubygems_version: 3.7.0
56
54
  specification_version: 4
57
55
  summary: This module offers details on the host OS, the present Ruby interpreter and
58
56
  environment.