Class: Vectory::Platform

Inherits:
Object
  • Object
show all
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.

Examples:

Check if running on Windows

Vectory::Platform.windows? # => true or false

Format a path for execution on the current platform

Vectory::Platform.path_for_execution("C:/Program Files/Inkscape/inkscape.exe")
# On Windows: "C:\\Program Files\\Inkscape\\inkscape.exe"
# On Unix:   "C:/Program Files/Inkscape/inkscape.exe"

Class Method Summary collapse

Class Method Details

.command_available?(command) ⇒ Boolean

Check if a command is available in the system PATH

Parameters:

  • command (String)

    the command to check

Returns:

  • (Boolean)

    true if command is found in 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_extensionString?

Get the appropriate shell command extension for the platform

Returns:

  • (String, nil)

    “.exe” on Windows, nil on Unix



82
83
84
# File 'lib/vectory/platform.rb', line 82

def command_extension
  windows? ? ".exe" : nil
end

.default_shellString

Get the default shell for the platform

Returns:

  • (String)

    shell command (e.g., “cmd.exe” on Windows, “sh” on Unix)



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_pathsArray<String>

Get the PATH environment variable as an array

Handles different PATH separators on Windows (;) vs Unix (:)

Returns:

  • (Array<String>)

    array of directory paths



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

Returns:

  • (Boolean)

    true if on Linux platform



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

Returns:

  • (Boolean)

    true if on macOS platform



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.

Parameters:

  • path (String)

    the file path to format

Returns:

  • (String)

    platform-formatted path



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_cacheObject

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

Returns:

  • (Boolean)

    true if on Windows platform



22
23
24
# File 'lib/vectory/platform.rb', line 22

def windows?
  Gem.win_platform?
end