host-os 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +28 -0
- data/README.md +94 -0
- data/examples/current.rb +45 -0
- data/lib/host-os/support.rb +185 -0
- data/lib/host-os/version.rb +6 -0
- data/lib/host-os.rb +367 -0
- data/lib/host_os.rb +3 -0
- metadata +59 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f86c0a89682b428496bf02a65b5807d1caaa30b5a9d70a34b27595436d1a3746
|
4
|
+
data.tar.gz: b788605d3bdf9b5c851012502d665c6a8ef1ab4ef21b6fbf8828edaf40d78298
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e87cb31326255c7e359a5d2110ea81bb43971e9b6d524ccd12eefa7c38368e88c1aec56e68b3beccb06dbfbb078244cfbef0ceefdeacc6d9f856835eac177fc8
|
7
|
+
data.tar.gz: e6e316ad63c38c00d01aeefa252ae2cfd3027d0669ac7b023e40b926c493cb11f78b8f237876d9964c91bc6088cf55e5dac64626e5f3b56dd4b43d3c9d21470c
|
data/LICENSE
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
BSD 3-Clause License
|
2
|
+
|
3
|
+
Copyright (c) 2017-2021, Mike Blumtritt. All rights reserved.
|
4
|
+
|
5
|
+
Redistribution and use in source and binary forms, with or without
|
6
|
+
modification, are permitted provided that the following conditions are met:
|
7
|
+
|
8
|
+
1. Redistributions of source code must retain the above copyright notice, this
|
9
|
+
list of conditions and the following disclaimer.
|
10
|
+
|
11
|
+
2. Redistributions in binary form must reproduce the above copyright notice,
|
12
|
+
this list of conditions and the following disclaimer in the documentation
|
13
|
+
and/or other materials provided with the distribution.
|
14
|
+
|
15
|
+
3. Neither the name of the copyright holder nor the names of its
|
16
|
+
contributors may be used to endorse or promote products derived from
|
17
|
+
this software without specific prior written permission.
|
18
|
+
|
19
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
20
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
21
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
22
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
23
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
24
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
25
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
26
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
27
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
28
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
# HostOS
|
2
|
+
|
3
|
+
![version](https://img.shields.io/gem/v/host-os?label=)
|
4
|
+
|
5
|
+
HostOS is a module that offers details about the host operating system, the current Ruby interpreter, and the environment currently in use.
|
6
|
+
|
7
|
+
- Gem: [rubygems.org](https://rubygems.org/gems/host-os)
|
8
|
+
- Source: [github.com](https://github.com/mblumtritt/host-os)
|
9
|
+
- Help: [rubydoc.info](https://rubydoc.info/gems/host-os/HostOS)
|
10
|
+
|
11
|
+
## Description
|
12
|
+
|
13
|
+
This gem helps you write environment-specific code in a clean way.
|
14
|
+
It provides a simple API to get information about the operating system, the current Ruby interpreter, and the configured environment.
|
15
|
+
|
16
|
+
## Usage
|
17
|
+
|
18
|
+
Here is a very simple example of OS dependent code:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
require 'host-os'
|
22
|
+
|
23
|
+
if HostOS.unix?
|
24
|
+
puts 'Hello Unix world!'
|
25
|
+
elsif HostOS.windows?
|
26
|
+
puts 'Clean your Windows!'
|
27
|
+
elsif HostOS.os2? || HostOS.vms?
|
28
|
+
puts 'Hello old school!'
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
You are free to write your code in whatever way you prefer. Here is a functionally identical but alternative to the example above:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
require 'host-os'
|
36
|
+
|
37
|
+
if HostOS.is? :unix
|
38
|
+
puts 'Hello Unix world!'
|
39
|
+
elsif HostOS.is? 'windows'
|
40
|
+
puts 'Clean your Windows!'
|
41
|
+
elsif %i[os2 vms].include?(HostOS.type)
|
42
|
+
puts 'Hello old school!'
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
The module also assists with environment- or interpreter-specific code:
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
require 'host-os'
|
50
|
+
|
51
|
+
Logger.log_level = HostOS.env.production? ? Logger::WARN : Logger::INFO
|
52
|
+
# => set the log level to 'WARN' in production, 'INFO' otherwise
|
53
|
+
|
54
|
+
require 'java' if HostOS.interpreter.jruby?
|
55
|
+
# => load the Java support for JRuby
|
56
|
+
```
|
57
|
+
|
58
|
+
There are additional methods that can support you on various platforms:
|
59
|
+
|
60
|
+
```ruby
|
61
|
+
require 'host-os/support'
|
62
|
+
|
63
|
+
HostOS.dev_null
|
64
|
+
# => returns 'NUL' on Windows, 'nul' for OS2 and '/dev/null' on Unix platforms
|
65
|
+
|
66
|
+
HostOS.app_config_path('my_app')
|
67
|
+
# => returns the directory name where 'my_app' stores its configuration files
|
68
|
+
|
69
|
+
HostOS.rss_bytes
|
70
|
+
# => memory consumtion of current process in bytes
|
71
|
+
# means "total in memory footprint"
|
72
|
+
```
|
73
|
+
|
74
|
+
📕 See [the online help](https://rubydoc.info/gems/host-os/HostOS) for more details.
|
75
|
+
|
76
|
+
## Installation
|
77
|
+
|
78
|
+
This gem is compatible with Ruby 2.3 and higher. You can install it in your system with
|
79
|
+
|
80
|
+
```shell
|
81
|
+
gem install host-os
|
82
|
+
```
|
83
|
+
|
84
|
+
or you can use [Bundler](http://gembundler.com/) to add HostOS just to your own project:
|
85
|
+
|
86
|
+
```shell
|
87
|
+
bundle add 'host-os'
|
88
|
+
```
|
89
|
+
|
90
|
+
After that you only need one line of code to have everything together
|
91
|
+
|
92
|
+
```ruby
|
93
|
+
require 'host-os'
|
94
|
+
```
|
data/examples/current.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# This example prints an overview of detected information about your current
|
5
|
+
# OS, environment and Ruby interpreter.
|
6
|
+
#
|
7
|
+
# Just run
|
8
|
+
# $ ruby examples/current.rb
|
9
|
+
#
|
10
|
+
# or play around with eg.:
|
11
|
+
# $ ruby --jit examples/current.rb
|
12
|
+
# $ ENV=test ruby examples/current.rb
|
13
|
+
#
|
14
|
+
|
15
|
+
require 'host-os/support'
|
16
|
+
|
17
|
+
puts <<~INFORMATION
|
18
|
+
# OS Information
|
19
|
+
- id: #{HostOS.id}
|
20
|
+
- type: #{HostOS.type}
|
21
|
+
- POSIX compatible: #{HostOS.posix?}
|
22
|
+
- suggested_thread_count: #{HostOS.suggested_thread_count}
|
23
|
+
- temp_dir: #{HostOS.temp_dir}
|
24
|
+
- dev_null: #{defined?(HostOS.dev_null) ? HostOS.dev_null : '*unsupported*'}
|
25
|
+
- open_command: #{
|
26
|
+
defined?(HostOS.open_command) ? HostOS.open_command : '*unsupported*'
|
27
|
+
}
|
28
|
+
- rss_bytes: #{
|
29
|
+
defined?(HostOS.rss_bytes) ? HostOS.rss_bytes : '*unsupported*'
|
30
|
+
}
|
31
|
+
- app_config_path: #{
|
32
|
+
if defined?(HostOS.app_config_path)
|
33
|
+
HostOS.app_config_path('')
|
34
|
+
else
|
35
|
+
'*unsupported*'
|
36
|
+
end
|
37
|
+
}
|
38
|
+
|
39
|
+
# Environment
|
40
|
+
- id: #{HostOS.env.id}
|
41
|
+
|
42
|
+
# Ruby Interpreter
|
43
|
+
- id: #{HostOS.interpreter.id}
|
44
|
+
- JIT: #{HostOS.interpreter.jit_type}
|
45
|
+
INFORMATION
|
@@ -0,0 +1,185 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative('../host-os') unless defined?(HostOS.id)
|
4
|
+
|
5
|
+
module HostOS
|
6
|
+
# This module provides helpful support methods for the {HostOS} module.
|
7
|
+
#
|
8
|
+
# It adds attributes and methods depending on the detected operating system and
|
9
|
+
# Ruby interpreter.
|
10
|
+
#
|
11
|
+
# @note You need to require `host-os/support` explicitly.
|
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
|
42
|
+
def dev_null
|
43
|
+
'NUL'
|
44
|
+
end
|
45
|
+
|
46
|
+
def open_command
|
47
|
+
'start'
|
48
|
+
end
|
49
|
+
|
50
|
+
def rss_bytes
|
51
|
+
`tasklist /FI "PID eq #{Process.pid}" /FO CSV`.split(',"')[-1].tr(
|
52
|
+
',.',
|
53
|
+
'__'
|
54
|
+
).to_i * 1024
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def _app_config_path
|
60
|
+
ENV['LOCALAPPDATA'] ||
|
61
|
+
"#{ENV['USERPROFILE']}/Local Settings/Application Data"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
module Unix
|
66
|
+
def dev_null
|
67
|
+
'/dev/null'
|
68
|
+
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
|
+
end
|
80
|
+
|
81
|
+
module OS2
|
82
|
+
def dev_null
|
83
|
+
'nul'
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
module MacOS
|
88
|
+
def open_command
|
89
|
+
'open'
|
90
|
+
end
|
91
|
+
|
92
|
+
private
|
93
|
+
|
94
|
+
def _app_config_path
|
95
|
+
'~/Library/Application Support'
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
module Linux
|
100
|
+
def open_command
|
101
|
+
'xdg-open'
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
module AppConfigPath
|
106
|
+
def app_config_path(app_name)
|
107
|
+
File.expand_path(app_name, _app_config_path)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
module JRuby
|
112
|
+
def rss_bytes
|
113
|
+
require 'java' unless defined?(java)
|
114
|
+
bean = java.lang.management.ManagementFactory.getMemoryMXBean
|
115
|
+
bean.heap_memory_usage.used + bean.non_heap_memory_usage.used
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
module Tools
|
120
|
+
def suggested_thread_count
|
121
|
+
@suggested_thread_count ||= find_suggested_thread_count
|
122
|
+
end
|
123
|
+
|
124
|
+
def temp_dir
|
125
|
+
@temp_dir ||= find_temp_dir
|
126
|
+
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 }
|
133
|
+
end
|
134
|
+
|
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
|
143
|
+
|
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
|
156
|
+
|
157
|
+
def with_etc(default)
|
158
|
+
require('etc') unless defined?(Etc)
|
159
|
+
yield
|
160
|
+
rescue LoadError
|
161
|
+
default
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
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
|
174
|
+
|
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
|
184
|
+
end
|
185
|
+
end
|
data/lib/host-os.rb
ADDED
@@ -0,0 +1,367 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
#
|
4
|
+
# HostOS is a module that provides information about the operating system,
|
5
|
+
# the current Ruby interpreter ({interpreter}) and configured environment ({env}).
|
6
|
+
# It helps you write environment-specific code in a clean way.
|
7
|
+
#
|
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
|
+
class << self
|
217
|
+
# @attribute [r] id
|
218
|
+
# @return [Symbol] OS identifier
|
219
|
+
def id
|
220
|
+
ID
|
221
|
+
end
|
222
|
+
|
223
|
+
# @attribute [r] type
|
224
|
+
# @return [:unix, :windows, :vms, :os2, :unknown] OS type
|
225
|
+
def type
|
226
|
+
TYPE
|
227
|
+
end
|
228
|
+
|
229
|
+
# @attribute [r] interpreter
|
230
|
+
# @return [Interpreter] interpreter information
|
231
|
+
def interpreter
|
232
|
+
Interpreter
|
233
|
+
end
|
234
|
+
|
235
|
+
# @attribute [r] env
|
236
|
+
# @return [Env] environment information
|
237
|
+
def env
|
238
|
+
Env
|
239
|
+
end
|
240
|
+
|
241
|
+
# @attribute [r] unix?
|
242
|
+
# @return [true, false] whether the host OS is a Unix OS
|
243
|
+
def unix?
|
244
|
+
TYPE == :unix
|
245
|
+
end
|
246
|
+
|
247
|
+
# @attribute [r] windows?
|
248
|
+
# @return [true, false] whether the host OS is a Windows OS
|
249
|
+
def windows?
|
250
|
+
TYPE == :windows
|
251
|
+
end
|
252
|
+
|
253
|
+
# @attribute [r] vms?
|
254
|
+
# @return [true, false] whether the host OS is VMS
|
255
|
+
def vms?
|
256
|
+
TYPE == :vms
|
257
|
+
end
|
258
|
+
|
259
|
+
# @attribute [r] os2?
|
260
|
+
# @return [true, false] whether the host OS is OS/2
|
261
|
+
def os2?
|
262
|
+
TYPE == :os2
|
263
|
+
end
|
264
|
+
|
265
|
+
# @attribute [r] macosx?
|
266
|
+
# @return [true, false] whether the host OS is identified as MacOS
|
267
|
+
def macosx?
|
268
|
+
ID == :macosx
|
269
|
+
end
|
270
|
+
|
271
|
+
# @attribute [r] linux?
|
272
|
+
# @return [true, false] whether the host OS is identified as Linux derivate
|
273
|
+
def linux?
|
274
|
+
ID == :linux
|
275
|
+
end
|
276
|
+
|
277
|
+
# @attribute [r] cygwin?
|
278
|
+
# @return [true, false] whether the host OS is Windows/Cygwin
|
279
|
+
def cygwin?
|
280
|
+
ID == :cygwin
|
281
|
+
end
|
282
|
+
|
283
|
+
# @attribute [r] posix?
|
284
|
+
# @return [true, false] whether the host OS is Posix compatible
|
285
|
+
# This attribute is `true` when Posix compatible commands like `fork` are
|
286
|
+
# available.
|
287
|
+
def posix?
|
288
|
+
Process.respond_to?(:fork)
|
289
|
+
end
|
290
|
+
|
291
|
+
# @param what [Symbol, String] the identifier to check
|
292
|
+
# @return [true, false] whether the host OS is the given identifier or type
|
293
|
+
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)
|
297
|
+
end
|
298
|
+
|
299
|
+
# @!visibility private
|
300
|
+
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
|
329
|
+
end
|
330
|
+
|
331
|
+
private
|
332
|
+
|
333
|
+
def identify
|
334
|
+
require('rbconfig') unless defined?(RbConfig)
|
335
|
+
id = RbConfig::CONFIG['host_os'].downcase
|
336
|
+
id, type, normalized =
|
337
|
+
[
|
338
|
+
['linux', :unix],
|
339
|
+
['arch', :unix, :linux],
|
340
|
+
['darwin', :unix, :macosx],
|
341
|
+
['mac', :unix, :macosx],
|
342
|
+
['freebsd', :unix],
|
343
|
+
['netbsd', :unix],
|
344
|
+
['openbsd', :unix],
|
345
|
+
['dragonfly', :unix],
|
346
|
+
['aix', :unix],
|
347
|
+
['irix', :unix],
|
348
|
+
['hpux', :unix],
|
349
|
+
['solaris', :unix, :sunos],
|
350
|
+
['sunos', :unix, :sunos],
|
351
|
+
['windows', :windows],
|
352
|
+
['cygwin', :windows],
|
353
|
+
['mswin', :windows],
|
354
|
+
['mingw', :windows],
|
355
|
+
['bccwin', :windows],
|
356
|
+
['djgpp', :windows],
|
357
|
+
['wince', :windows],
|
358
|
+
['emc', :windows],
|
359
|
+
['vms', :vms],
|
360
|
+
['os2', :os2]
|
361
|
+
].find { |info| id.include?(info[0]) }
|
362
|
+
id ? [normalized || id.to_sym, type] : %i[unknown unknown]
|
363
|
+
end
|
364
|
+
end
|
365
|
+
|
366
|
+
ID, TYPE = identify
|
367
|
+
end
|
data/lib/host_os.rb
ADDED
metadata
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: host-os
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Blumtritt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-09-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |
|
14
|
+
This gem helps you write environment-specific code in a clean way.
|
15
|
+
It provides a simple API to get information about the operating system,
|
16
|
+
the current Ruby interpreter, and the configured environment.
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files:
|
21
|
+
- README.md
|
22
|
+
- LICENSE
|
23
|
+
files:
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- examples/current.rb
|
27
|
+
- lib/host-os.rb
|
28
|
+
- lib/host-os/support.rb
|
29
|
+
- lib/host-os/version.rb
|
30
|
+
- lib/host_os.rb
|
31
|
+
homepage: https://github.com/mblumtritt/host-os
|
32
|
+
licenses:
|
33
|
+
- BSD-3-Clause
|
34
|
+
metadata:
|
35
|
+
source_code_uri: https://github.com/mblumtritt/host-os
|
36
|
+
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:
|
40
|
+
rdoc_options: []
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.3'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
requirements: []
|
54
|
+
rubygems_version: 3.4.19
|
55
|
+
signing_key:
|
56
|
+
specification_version: 4
|
57
|
+
summary: This module offers details on the host OS, the present Ruby interpreter and
|
58
|
+
environment.
|
59
|
+
test_files: []
|