Module: Stable::Utils::Platform
- Defined in:
- lib/stable/utils/platform.rb
Overview
Platform detection utilities for cross-platform compatibility
Class Method Summary collapse
- .current ⇒ Object
- .find_pids_by_port(port) ⇒ Object
- .home_directory ⇒ Object
- .hosts_file ⇒ Object
- .linux? ⇒ Boolean
- .macos? ⇒ Boolean
- .package_manager ⇒ Object
- .port_in_use?(port) ⇒ Boolean
- .unix? ⇒ Boolean
- .windows? ⇒ Boolean
Class Method Details
.current ⇒ Object
24 25 26 27 28 29 30 |
# File 'lib/stable/utils/platform.rb', line 24 def current return :macos if macos? return :linux if linux? return :windows if windows? :unknown end |
.find_pids_by_port(port) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/stable/utils/platform.rb', line 65 def find_pids_by_port(port) case current when :macos, :linux # Use lsof to find PIDs listening on the port output = `lsof -i tcp:#{port} -sTCP:LISTEN -t 2>/dev/null`.strip return [] if output.empty? output.split("\n").map(&:to_i) when :windows # On Windows, this is more complex. For now, return empty array # Could potentially parse netstat output in the future [] else [] end end |
.home_directory ⇒ Object
46 47 48 49 50 |
# File 'lib/stable/utils/platform.rb', line 46 def home_directory return ENV.fetch('USERPROFILE', nil) if windows? Dir.home end |
.hosts_file ⇒ Object
39 40 41 42 43 44 |
# File 'lib/stable/utils/platform.rb', line 39 def hosts_file return '/etc/hosts' if unix? return 'C:\Windows\System32\drivers\etc\hosts' if windows? '/etc/hosts' # fallback end |
.linux? ⇒ Boolean
12 13 14 |
# File 'lib/stable/utils/platform.rb', line 12 def linux? !!(RUBY_PLATFORM =~ /linux/) end |
.macos? ⇒ Boolean
8 9 10 |
# File 'lib/stable/utils/platform.rb', line 8 def macos? !!(RUBY_PLATFORM =~ /darwin/) end |
.package_manager ⇒ Object
32 33 34 35 36 37 |
# File 'lib/stable/utils/platform.rb', line 32 def package_manager return :brew if macos? return detect_linux_package_manager if linux? :unknown end |
.port_in_use?(port) ⇒ Boolean
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/stable/utils/platform.rb', line 52 def port_in_use?(port) case current when :macos, :linux # Use lsof on Unix-like systems system("lsof -i tcp:#{port} -sTCP:LISTEN > /dev/null 2>&1") when :windows # Use netstat on Windows system("netstat -an | findstr :#{port} > nul 2>&1") else false end end |
.unix? ⇒ Boolean
20 21 22 |
# File 'lib/stable/utils/platform.rb', line 20 def unix? !windows? end |
.windows? ⇒ Boolean
16 17 18 |
# File 'lib/stable/utils/platform.rb', line 16 def windows? !!(RUBY_PLATFORM =~ /mingw|mswin|win32/) end |