Class: Mnenv::PlatformDetector

Inherits:
Object
  • Object
show all
Defined in:
lib/mnenv/platform_detector.rb

Overview

Centralized platform detection for consistent OS/architecture/variant detection across the codebase. Used by binary installer, repository, and CLI.

Defined Under Namespace

Classes: UnsupportedPlatform

Class Method Summary collapse

Class Method Details

.archString

Detect the current CPU architecture

Returns:

  • (String)

    ‘arm64’ or ‘x86_64’



28
29
30
31
32
33
34
35
36
37
# File 'lib/mnenv/platform_detector.rb', line 28

def arch
  case RbConfig::CONFIG['host_cpu']
  when /arm64|aarch64/
    'arm64'
  when /x86_64|x64/
    'x86_64'
  else
    'x86_64' # Default fallback
  end
end

.linux?Boolean

Check if running on Linux

Returns:

  • (Boolean)


70
71
72
# File 'lib/mnenv/platform_detector.rb', line 70

def linux?
  os == 'linux'
end

.macos?Boolean

Check if running on macOS

Returns:

  • (Boolean)


64
65
66
# File 'lib/mnenv/platform_detector.rb', line 64

def macos?
  os == 'darwin'
end

.musl?Boolean

Check if running on a musl-based system (Alpine Linux)

Returns:

  • (Boolean)


49
50
51
52
53
54
# File 'lib/mnenv/platform_detector.rb', line 49

def musl?
  File.exist?('/etc/alpine-release') ||
    File.symlink?('/lib/libc.musl-x86_64.so.1')
rescue StandardError
  false
end

.osString

Detect the current operating system

Returns:

  • (String)

    ‘linux’, ‘darwin’, or ‘windows’

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mnenv/platform_detector.rb', line 13

def os
  case RbConfig::CONFIG['host_os']
  when /linux/
    'linux'
  when /darwin/
    'darwin'
  when /mswin|mingw|cygwin/
    'windows'
  else
    raise UnsupportedPlatform, "Unsupported platform: #{RbConfig::CONFIG['host_os']}"
  end
end

.platform_candidatesArray<String>

Get all possible platform strings for binary matching (includes variant-specific and generic versions)

Returns:

  • (Array<String>)

    List of platform strings in order of preference



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mnenv/platform_detector.rb', line 96

def platform_candidates
  candidates = []

  # With variant (if applicable)
  candidates << "#{os}-#{variant}-#{arch}" if variant

  # Without variant
  candidates << "#{os}-#{arch}"

  candidates
end

.platform_stringString

Get a human-readable platform string

Returns:

  • (String)

    e.g., “linux-x86_64”, “darwin-arm64”, “linux-musl-x86_64”



86
87
88
89
90
91
# File 'lib/mnenv/platform_detector.rb', line 86

def platform_string
  parts = [os]
  parts << variant if variant
  parts << arch
  parts.join('-')
end

.to_hHash

Get platform info as a hash

Returns:

  • (Hash)

    With keys :os, :arch, :variant



76
77
78
79
80
81
82
# File 'lib/mnenv/platform_detector.rb', line 76

def to_h
  {
    os: os,
    arch: arch,
    variant: variant
  }
end

.variantString?

Detect libc variant (for Linux)

Returns:

  • (String, nil)

    ‘musl’ for Alpine/musl systems, nil for glibc



41
42
43
44
45
# File 'lib/mnenv/platform_detector.rb', line 41

def variant
  return nil unless os == 'linux'

  'musl' if musl?
end

.windows?Boolean

Check if running on Windows

Returns:

  • (Boolean)


58
59
60
# File 'lib/mnenv/platform_detector.rb', line 58

def windows?
  os == 'windows'
end