Module: Cinnabar::Utils::OS

Defined in:
lib/cinnabar/utils.rb

Class Method Summary collapse

Class Method Details

.linux?(host_os = RUBY_PLATFORM) ⇒ Boolean

Returns true if running on Linux.

Parameters:

  • host_os (String) (defaults to: RUBY_PLATFORM)

Returns:

  • (Boolean)


43
44
45
# File 'lib/cinnabar/utils.rb', line 43

def linux?(host_os = RUBY_PLATFORM)
  host_os.match?(/linux/i)
end

.macOS?(host_os = RUBY_PLATFORM) ⇒ Boolean

Returns true if running on macOS (Darwin).

Ruby convention prefers macos? over macOS?, but we provide both.

Examples:

Cinnabar::Utils::OS.macos? #=> true/false

Parameters:

  • host_os (String) (defaults to: RUBY_PLATFORM)

Returns:

  • (Boolean)


35
36
37
# File 'lib/cinnabar/utils.rb', line 35

def macOS?(host_os = RUBY_PLATFORM) # rubocop:disable Naming/MethodName
  host_os.match?(/darwin/i)
end

.windows?(host_os = RUBY_PLATFORM) ⇒ Boolean

Returns true if running on Windows (including MSYS/MinGW/Cygwin).

Examples:

Cinnabar::Utils::OS.windows? #=> true/false

Parameters:

  • host_os (String) (defaults to: RUBY_PLATFORM)

Returns:

  • (Boolean)


22
23
24
# File 'lib/cinnabar/utils.rb', line 22

def windows?(host_os = RUBY_PLATFORM)
  host_os.match?(/bccwin|cygwin|djgpp|mingw|mswin|wince/i)
end

.wsl?(proc_version = nil) ⇒ Boolean

Returns true if running under WSL (Windows Subsystem for Linux).

Returns:

  • (Boolean)


66
67
68
69
70
71
# File 'lib/cinnabar/utils.rb', line 66

def wsl?(proc_version = nil)
  proc_version ||= File.read('/proc/version')
  proc_version.match?(/(M|m)icrosoft/)
rescue StandardError
  false
end

.wsl_1?(proc_version = nil) ⇒ Boolean

Returns true if running under WSL 1

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/cinnabar/utils.rb', line 74

def wsl_1?(proc_version = nil)
  # wsl 1: Linux version 4.4.0-26100-Microsoft (Microsoft@Microsoft.com) (gcc version 5.4.0 (GCC) ) #7309-Microsoft Fri Jan 01 08:00:00 PST 2016 # rubocop:disable Layout/LineLength
  proc_version ||= File.read('/proc/version')
  proc_version.include?('Microsoft')
rescue StandardError
  false
end

.wsl_2?(proc_version = nil) ⇒ Boolean

Returns true if running under WSL 2

Examples:

Cinnabar::Utils::OS.wsl_2? #=> true/false

Parameters:

  • proc_version (String) (defaults to: nil)

Returns:

  • (Boolean)

See Also:



56
57
58
59
60
61
62
63
# File 'lib/cinnabar/utils.rb', line 56

def wsl_2?(proc_version = nil)
  proc_version ||= File.read('/proc/version')

  # proc_version.match?(/(?=.*microsoft)(?=.*-WSL)/)
  proc_version.include?('microsoft') && proc_version.include?('-WSL')
rescue StandardError
  false
end