Class: Ukiryu::Runtime

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/ukiryu/runtime.rb

Overview

Runtime singleton for centralized platform and shell detection.

This class provides a single source of truth for platform and shell detection across the entire application, eliminating redundant detection calls and ensuring consistency.

Examples:

platform = Ukiryu::Runtime.instance.platform
shell = Ukiryu::Runtime.instance.shell

Instance Method Summary collapse

Constructor Details

#initializeRuntime

Initialize the runtime with cached values



19
20
21
22
23
24
25
# File 'lib/ukiryu/runtime.rb', line 19

def initialize
  @platform = nil
  @shell = nil
  @platform_cached = false
  @shell_cached = false
  @locked = false
end

Instance Method Details

#linux?Boolean

Check if running on Linux

Returns:

  • (Boolean)

    true if on Linux



148
149
150
# File 'lib/ukiryu/runtime.rb', line 148

def linux?
  on_platform?(:linux)
end

#lock!Object

Lock the runtime to prevent further changes

This should be called after initial configuration is complete.



86
87
88
# File 'lib/ukiryu/runtime.rb', line 86

def lock!
  @locked = true
end

#macos?Boolean

Check if running on macOS

Returns:

  • (Boolean)

    true if on macOS



141
142
143
# File 'lib/ukiryu/runtime.rb', line 141

def macos?
  on_platform?(:macos)
end

#on_platform?(plat) ⇒ Boolean

Check if running on a specific platform

Parameters:

  • plat (Symbol)

    the platform to check

Returns:

  • (Boolean)

    true if running on the specified platform



119
120
121
# File 'lib/ukiryu/runtime.rb', line 119

def on_platform?(plat)
  platform == plat.to_sym
end

#platformSymbol

Get the current platform (cached)

Returns:

  • (Symbol)

    the detected platform (:macos, :linux, :windows)



30
31
32
33
34
35
36
# File 'lib/ukiryu/runtime.rb', line 30

def platform
  return @platform if @platform_cached

  @platform = Ukiryu::Platform.detect
  @platform_cached = true
  @platform
end

#platform=(value) ⇒ Object

Manually set the platform (for testing)

Parameters:

  • value (Symbol)

    the platform to set



66
67
68
69
70
71
# File 'lib/ukiryu/runtime.rb', line 66

def platform=(value)
  raise 'Runtime is locked' if @locked

  @platform = value&.to_sym
  @platform_cached = true
end

#platform_classClass

Get the platform class for the current platform

Returns:

  • (Class)

    the platform class



104
105
106
# File 'lib/ukiryu/runtime.rb', line 104

def platform_class
  Ukiryu::Platform.class_for(platform)
end

#reset!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 the runtime cache (for testing)



93
94
95
96
97
98
99
# File 'lib/ukiryu/runtime.rb', line 93

def reset!
  @platform = nil
  @shell = nil
  @platform_cached = false
  @shell_cached = false
  @locked = false
end

#shellSymbol

Get the current shell (cached)

Priority:

  1. Explicitly set shell (via shell=)

  2. Config.shell (from –shell CLI option, UKIRYU_SHELL env, or programmatic config)

  3. Auto-detected shell

Returns:

  • (Symbol)

    the detected shell



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ukiryu/runtime.rb', line 46

def shell
  return @shell if @shell_cached

  # Check for explicit override
  override = shell_override
  if override
    @shell = override
    @shell_cached = true
    return @shell
  end

  # Auto-detect
  @shell = Ukiryu::Shell.detect
  @shell_cached = true
  @shell
end

#shell=(value) ⇒ Object

Manually set the shell (for testing)

Parameters:

  • value (Symbol)

    the shell to set



76
77
78
79
80
81
# File 'lib/ukiryu/runtime.rb', line 76

def shell=(value)
  raise 'Runtime is locked' if @locked

  @shell = value&.to_sym
  @shell_cached = true
end

#shell_classClass

Get the shell class for the current shell

Returns:

  • (Class)

    the shell class



111
112
113
# File 'lib/ukiryu/runtime.rb', line 111

def shell_class
  Ukiryu::Shell.class_for(shell)
end

#unix_shell?Boolean

Check if using a Unix-like shell

Returns:

  • (Boolean)

    true if using bash, zsh, fish, or sh



155
156
157
# File 'lib/ukiryu/runtime.rb', line 155

def unix_shell?
  %i[bash zsh fish sh].include?(shell)
end

#using_shell?(sh) ⇒ Boolean

Check if using a specific shell

Parameters:

  • sh (Symbol)

    the shell to check

Returns:

  • (Boolean)

    true if using the specified shell



127
128
129
# File 'lib/ukiryu/runtime.rb', line 127

def using_shell?(sh)
  shell == sh.to_sym
end

#windows?Boolean

Check if running on Windows

Returns:

  • (Boolean)

    true if on Windows



134
135
136
# File 'lib/ukiryu/runtime.rb', line 134

def windows?
  on_platform?(:windows)
end

#windows_shell?Boolean

Check if using a Windows shell

Returns:

  • (Boolean)

    true if using powershell or cmd



162
163
164
# File 'lib/ukiryu/runtime.rb', line 162

def windows_shell?
  %i[powershell cmd].include?(shell)
end