Class: Vectory::Platform
- Inherits:
-
Object
- Object
- Vectory::Platform
- Defined in:
- lib/vectory/platform.rb
Overview
Platform abstraction for centralized OS-specific behavior
This class provides a single source of truth for platform detection and platform-specific path handling, eliminating duplicated logic across InkscapeWrapper, GhostscriptWrapper, and other classes.
Class Method Summary collapse
-
.command_available?(command) ⇒ Boolean
Check if a command is available in the system PATH.
-
.command_extension ⇒ String?
Get the appropriate shell command extension for the platform.
-
.default_shell ⇒ String
Get the default shell for the platform.
-
.executable_search_paths ⇒ Array<String>
Get the PATH environment variable as an array.
-
.linux? ⇒ Boolean
Detect if running on Linux.
-
.macos? ⇒ Boolean
Detect if running on macOS.
-
.path_for_execution(path) ⇒ String
Format a file path for execution on the current platform.
-
.reset_cache ⇒ Object
private
Reset cached values (primarily for testing).
-
.windows? ⇒ Boolean
Detect if running on Windows.
Class Method Details
.command_available?(command) ⇒ Boolean
Check if a command is available in the system PATH
72 73 74 75 76 77 |
# File 'lib/vectory/platform.rb', line 72 def command_available?(command) executable_search_paths.any? do |dir| executable_path = File.join(dir, command) File.executable?(executable_path) && !File.directory?(executable_path) end end |
.command_extension ⇒ String?
Get the appropriate shell command extension for the platform
82 83 84 |
# File 'lib/vectory/platform.rb', line 82 def command_extension windows? ? ".exe" : nil end |
.default_shell ⇒ String
Get the default shell for the platform
89 90 91 92 93 94 95 |
# File 'lib/vectory/platform.rb', line 89 def default_shell if windows? "cmd.exe" else "sh" end end |
.executable_search_paths ⇒ Array<String>
Get the PATH environment variable as an array
Handles different PATH separators on Windows (;) vs Unix (:)
61 62 63 64 65 66 |
# File 'lib/vectory/platform.rb', line 61 def executable_search_paths @executable_search_paths ||= begin path_sep = windows? ? ";" : ":" (ENV["PATH"] || "").split(path_sep) end end |
.linux? ⇒ Boolean
Detect if running on Linux
36 37 38 |
# File 'lib/vectory/platform.rb', line 36 def linux? RbConfig::CONFIG["host_os"].include?("linux") end |
.macos? ⇒ Boolean
Detect if running on macOS
29 30 31 |
# File 'lib/vectory/platform.rb', line 29 def macos? RbConfig::CONFIG["host_os"].include?("darwin") end |
.path_for_execution(path) ⇒ String
Format a file path for execution on the current platform
On Windows, converts forward slashes to backslashes and quotes paths with spaces. On Unix-like systems, returns the path unchanged.
47 48 49 50 51 52 53 54 |
# File 'lib/vectory/platform.rb', line 47 def path_for_execution(path) return path unless path formatted_path = windows? ? path.gsub("/", "\\") : path # Quote paths with spaces to prevent shell parsing issues formatted_path[/\s/] ? "\"#{formatted_path}\"" : formatted_path end |
.reset_cache ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Reset cached values (primarily for testing)
100 101 102 |
# File 'lib/vectory/platform.rb', line 100 def reset_cache @executable_search_paths = nil end |
.windows? ⇒ Boolean
Detect if running on Windows
22 23 24 |
# File 'lib/vectory/platform.rb', line 22 def windows? Gem.win_platform? end |