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.
-
.reveal_file(path) ⇒ Boolean?
Reveal a file in the OS file manager (select/highlight it).
-
.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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/clacky/utils/environment_detector.rb', line 94 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.
73 74 75 76 77 78 79 80 |
# File 'lib/clacky/utils/environment_detector.rb', line 73 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.
83 84 85 86 87 88 89 90 |
# File 'lib/clacky/utils/environment_detector.rb', line 83 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 |
.reveal_file(path) ⇒ Boolean?
Reveal a file in the OS file manager (select/highlight it). macOS: open -R; Linux: xdg-open on parent dir; WSL: explorer /select
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/clacky/utils/environment_detector.rb', line 43 def self.reveal_file(path) case os_type when :macos then system("open", "-R", path) when :linux then system("xdg-open", File.dirname(path)) when :wsl win_path = linux_to_win_path(path) system("explorer.exe", "/select,#{win_path}") else nil 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.
60 61 62 63 64 65 66 |
# File 'lib/clacky/utils/environment_detector.rb', line 60 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
109 110 111 112 113 114 |
# File 'lib/clacky/utils/environment_detector.rb', line 109 def self.wsl? File.exist?("/proc/version") && File.read("/proc/version").downcase.include?("microsoft") rescue false end |