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.
-
.resolve_local_path(href) ⇒ Object
Resolve a "file://" URL or bare path into a real local path: strip "file://", percent-decode, WSL drive-letter normalize, then expand.
-
.reveal_file(path) ⇒ Boolean?
Reveal a file in the OS file manager (select/highlight it).
-
.win_to_linux_path(path) ⇒ Object
Convert a Windows-style path (incl. the "/C:/" three-slash-URL form) to a WSL /mnt path.
- .wsl? ⇒ Boolean
Class Method Details
.desktop_path ⇒ String?
Detect the desktop directory path for the current environment.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/clacky/utils/environment_detector.rb', line 101 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:Users\foo\file.txt" Returns the original path unchanged on non-WSL.
80 81 82 83 84 85 86 87 |
# File 'lib/clacky/utils/environment_detector.rb', line 80 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.
90 91 92 93 94 95 96 97 |
# File 'lib/clacky/utils/environment_detector.rb', line 90 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 |
.resolve_local_path(href) ⇒ Object
Resolve a "file://" URL or bare path into a real local path: strip "file://", percent-decode, WSL drive-letter normalize, then expand.
68 69 70 71 72 73 |
# File 'lib/clacky/utils/environment_detector.rb', line 68 def self.resolve_local_path(href) path = href.to_s.sub(%r{\Afile://}, "") path = CGI.unescape(path) path = win_to_linux_path(path) File.(path) 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) ⇒ Object
Convert a Windows-style path (incl. the "/C:/" three-slash-URL form) to a WSL /mnt path. No-op on non-WSL or non-drive-letter paths.
57 58 59 60 61 62 63 64 |
# File 'lib/clacky/utils/environment_detector.rb', line 57 def self.win_to_linux_path(path) return path unless os_type == :wsl m = path.match(%r{\A/?([A-Za-z]):[/\\](.*)}m) return path unless m "/mnt/#{m[1].downcase}/#{m[2].gsub("\\", "/")}" end |
.wsl? ⇒ Boolean
116 117 118 119 120 121 |
# File 'lib/clacky/utils/environment_detector.rb', line 116 def self.wsl? File.exist?("/proc/version") && File.read("/proc/version").downcase.include?("microsoft") rescue false end |