Module: Clacky::Utils::EnvironmentDetector
- Defined in:
- lib/clacky/utils/environment_detector.rb
Overview
Detects the current operating system environment and desktop path.
Class Method Summary collapse
-
.desktop_path ⇒ String?
Detect the desktop directory path for the current environment.
-
.linux_to_win_path(path) ⇒ String
Convert a Linux-side path to a Windows-style path via wslpath.
-
.open_file(path) ⇒ Boolean?
Open a file with the OS-default application.
-
.os_label ⇒ Object
Human-readable OS label for injection into session context.
-
.os_type ⇒ Symbol
Detect OS type.
-
.win_to_linux_path(path) ⇒ String
Convert a Windows-style path to a WSL/Linux-side path.
- .wsl? ⇒ Boolean
Class Method Details
.desktop_path ⇒ String?
Detect the desktop directory path for the current environment.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/clacky/utils/environment_detector.rb', line 78 def self.desktop_path return @desktop_path if defined?(@desktop_path) @desktop_path = case os_type when :wsl wsl_desktop_path when :macos macos_desktop_path when :linux linux_desktop_path else fallback_desktop_path end end |
.linux_to_win_path(path) ⇒ String
Convert a Linux-side path to a Windows-style path via wslpath. e.g. “/mnt/c/Users/foo/file.txt” → “C:Usersfoofile.txt” Returns the original path unchanged on non-WSL.
57 58 59 60 61 62 63 64 |
# File 'lib/clacky/utils/environment_detector.rb', line 57 def self.linux_to_win_path(path) return path unless os_type == :wsl Clacky::Utils::Encoding.cmd_to_utf8( `wslpath -w '#{path.gsub("'", "'\''")}'`, source_encoding: "UTF-8" ).strip end |
.open_file(path) ⇒ Boolean?
Open a file with the OS-default application. On WSL, uses “cmd.exe /c start” instead of explorer.exe so the opened window receives foreground focus even when called from a background thread (e.g. WEBrick request handler).
29 30 31 32 33 34 35 36 37 |
# File 'lib/clacky/utils/environment_detector.rb', line 29 def self.open_file(path) case os_type when :macos then system("open", path) when :linux then system("xdg-open", path) when :wsl win_path = linux_to_win_path(path) system("cmd.exe", "/c", "start", "", win_path) end end |
.os_label ⇒ Object
Human-readable OS label for injection into session context.
67 68 69 70 71 72 73 74 |
# File 'lib/clacky/utils/environment_detector.rb', line 67 def self.os_label case os_type when :wsl then "WSL/Windows" when :macos then "macOS" when :linux then "Linux" else "Unknown" end end |
.os_type ⇒ Symbol
Detect OS type.
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/clacky/utils/environment_detector.rb', line 9 def self.os_type return @os_type if defined?(@os_type) @os_type = if wsl? :wsl elsif RUBY_PLATFORM.include?("darwin") :macos elsif RUBY_PLATFORM.include?("linux") :linux else :unknown end end |
.win_to_linux_path(path) ⇒ String
Convert a Windows-style path to a WSL/Linux-side path. e.g. “C:/Users/foo/file.txt” → “/mnt/c/Users/foo/file.txt” Returns the original path unchanged on non-WSL or if already a Linux path.
44 45 46 47 48 49 50 |
# File 'lib/clacky/utils/environment_detector.rb', line 44 def self.win_to_linux_path(path) return path unless os_type == :wsl && path.match?(/\A[A-Za-z]:[\/\\]/) drive = path[0].downcase rest = path[2..].gsub("\\", "/") "/mnt/#{drive}#{rest}" end |
.wsl? ⇒ Boolean
93 94 95 96 97 98 |
# File 'lib/clacky/utils/environment_detector.rb', line 93 def self.wsl? File.exist?("/proc/version") && File.read("/proc/version").downcase.include?("microsoft") rescue false end |