Class: Ukiryu::ExecutionContext

Inherits:
Object
  • Object
show all
Defined in:
lib/ukiryu/execution_context.rb,
lib/ukiryu/execution_context.rb

Overview

Execution context for dependency injection and execution-scoped configuration

Provides a non-singleton alternative to Runtime.instance for better testability. Wraps runtime configuration and execution-specific options in a single object.

Examples:

Using ExecutionContext in production

context = Ukiryu::ExecutionContext.current
context.platform  # => :macos
context.shell     # => :bash
context.register  # => '/path/to/register'

Using ExecutionContext in tests

context = Ukiryu::ExecutionContext.new(
  platform: :linux,
  shell: :zsh,
  register_path: '/test/register'
)
context.platform  # => :linux
context.shell     # => :zsh

Defined Under Namespace

Classes: ThreadLocal

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(platform: nil, shell: nil, register_path: nil, timeout: nil, debug: false, metrics: false, options: {}) ⇒ ExecutionContext

Create a new execution context

Parameters:

  • platform (Symbol) (defaults to: nil)

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

  • shell (Symbol) (defaults to: nil)

    the shell (:bash, :zsh, :fish, :powershell, :cmd)

  • register_path (String, nil) (defaults to: nil)

    the register path

  • timeout (Integer, nil) (defaults to: nil)

    execution timeout in seconds

  • debug (Boolean) (defaults to: false)

    debug mode flag

  • metrics (Boolean) (defaults to: false)

    metrics collection flag

  • options (Hash) (defaults to: {})

    additional options



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/ukiryu/execution_context.rb', line 144

def initialize(platform: nil,
               shell: nil,
               register_path: nil,
               timeout: nil,
               debug: false,
               metrics: false,
               options: {})
  @platform = platform
  @shell = shell
  @register_path = register_path
  @timeout = timeout
  @debug = debug
  @metrics = metrics
  @options = options
end

Instance Attribute Details

#debugBoolean (readonly)

Debug mode enabled

Returns:

  • (Boolean)

    true if debug mode is enabled



123
124
125
# File 'lib/ukiryu/execution_context.rb', line 123

def debug
  @debug
end

#metricsBoolean (readonly)

Metrics collection enabled

Returns:

  • (Boolean)

    true if metrics are enabled



128
129
130
# File 'lib/ukiryu/execution_context.rb', line 128

def metrics
  @metrics
end

#optionsHash (readonly)

User-defined options hash

Returns:

  • (Hash)

    additional options



133
134
135
# File 'lib/ukiryu/execution_context.rb', line 133

def options
  @options
end

#platformSymbol (readonly)

Platform (:macos, :linux, :windows)

Returns:

  • (Symbol)

    the platform



103
104
105
# File 'lib/ukiryu/execution_context.rb', line 103

def platform
  @platform
end

#register_pathString? (readonly)

Register path for tool profiles

Returns:

  • (String, nil)

    the register path



113
114
115
# File 'lib/ukiryu/execution_context.rb', line 113

def register_path
  @register_path
end

#shellSymbol (readonly)

Shell (:bash, :zsh, :fish, :powershell, :cmd)

Returns:

  • (Symbol)

    the shell



108
109
110
# File 'lib/ukiryu/execution_context.rb', line 108

def shell
  @shell
end

#timeoutInteger? (readonly)

Execution timeout in seconds

Returns:

  • (Integer, nil)

    the timeout



118
119
120
# File 'lib/ukiryu/execution_context.rb', line 118

def timeout
  @timeout
end

Class Method Details

.currentExecutionContext

Get the current execution context

Creates a new context from the global Runtime if none is set.

Returns:



52
53
54
# File 'lib/ukiryu/execution_context.rb', line 52

def current
  @current_context.value ||= from_runtime
end

.current=(context) ⇒ void

This method returns an undefined value.

Set the current execution context

Parameters:



60
61
62
# File 'lib/ukiryu/execution_context.rb', line 60

def current=(context)
  @current_context.value = context
end

.from_runtimeExecutionContext

Create a context from the global Runtime

Returns:



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ukiryu/execution_context.rb', line 80

def from_runtime
  runtime = Ukiryu::Runtime.instance
  new(
    platform: runtime.platform,
    shell: runtime.shell,
    register_path: Ukiryu::Register.default_register_path,
    timeout: Ukiryu::Config.timeout,
    debug: Ukiryu::Config.debug,
    metrics: Ukiryu::Config.metrics
  )
end

.reset_current!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 current context (mainly for testing)



95
96
97
# File 'lib/ukiryu/execution_context.rb', line 95

def reset_current!
  @current_context.value = nil
end

.with_context(context) { ... } ⇒ Object

Execute a block with a temporary context

Parameters:

Yields:

  • the block to execute

Returns:

  • (Object)

    the block’s return value



69
70
71
72
73
74
75
# File 'lib/ukiryu/execution_context.rb', line 69

def with_context(context)
  old_context = @current_context.value
  @current_context.value = context
  yield
ensure
  @current_context.value = old_context
end

Instance Method Details

#inspectString

Inspect

Returns:

  • (String)

    the inspection string



244
245
246
# File 'lib/ukiryu/execution_context.rb', line 244

def inspect
  "#<#{self.class} #{self}>"
end

#linux?Boolean

Check if running on Linux

Returns:

  • (Boolean)

    true if on Linux



200
201
202
# File 'lib/ukiryu/execution_context.rb', line 200

def linux?
  on_platform?(:linux)
end

#macos?Boolean

Check if running on macOS

Returns:

  • (Boolean)

    true if on macOS



193
194
195
# File 'lib/ukiryu/execution_context.rb', line 193

def macos?
  on_platform?(:macos)
end

#merge(changes) ⇒ ExecutionContext

Create a new context with merged options

Parameters:

  • changes (Hash)

    the changes to merge

Returns:



222
223
224
225
226
227
228
229
230
231
232
# File 'lib/ukiryu/execution_context.rb', line 222

def merge(changes)
  self.class.new(
    platform: changes.fetch(:platform, @platform),
    shell: changes.fetch(:shell, @shell),
    register_path: changes.fetch(:register_path, @register_path),
    timeout: changes.fetch(:timeout, @timeout),
    debug: changes.fetch(:debug, @debug),
    metrics: changes.fetch(:metrics, @metrics),
    options: @options.merge(changes.fetch(:options, {}))
  )
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



171
172
173
# File 'lib/ukiryu/execution_context.rb', line 171

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

#shell_classClass

Get the shell class for this context

Returns:

  • (Class)

    the shell class



163
164
165
# File 'lib/ukiryu/execution_context.rb', line 163

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

#to_sString

String representation

Returns:

  • (String)

    the context as a string



237
238
239
# File 'lib/ukiryu/execution_context.rb', line 237

def to_s
  "ExecutionContext(platform=#{@platform}, shell=#{@shell}, register=#{@register_path})"
end

#unix_shell?Boolean

Check if using a Unix-like shell

Returns:

  • (Boolean)

    true if using bash, zsh, fish, or sh



207
208
209
# File 'lib/ukiryu/execution_context.rb', line 207

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



179
180
181
# File 'lib/ukiryu/execution_context.rb', line 179

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

#windows?Boolean

Check if running on Windows

Returns:

  • (Boolean)

    true if on Windows



186
187
188
# File 'lib/ukiryu/execution_context.rb', line 186

def windows?
  on_platform?(:windows)
end

#windows_shell?Boolean

Check if using a Windows shell

Returns:

  • (Boolean)

    true if using powershell or cmd



214
215
216
# File 'lib/ukiryu/execution_context.rb', line 214

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