Class: Makit::Commands::Strategies::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/commands/strategies/factory.rb

Overview

Factory for creating command execution strategies with fallback support

This factory allows easy switching between different execution strategies (ChildProcess, Open3, etc.) with automatic fallback to Open3 if the preferred strategy fails to load or execute.

Examples:

Using environment variable to control strategy

# Use ChildProcess (default)
MAKIT_STRATEGY=childprocess rake

# Force Open3 usage
MAKIT_STRATEGY=open3 rake

# Auto-detect (try ChildProcess, fallback to Open3)
MAKIT_STRATEGY=auto rake

Constant Summary collapse

STRATEGIES =

Available strategy types

{
  "childprocess" => "ChildProcess",
  "open3" => "Synchronous",
  "auto" => "AutoDetect",
}.freeze

Class Method Summary collapse

Class Method Details

.childprocess_available?Boolean

Check if ChildProcess strategy is available

Returns:

  • (Boolean)

    true if ChildProcess can be loaded



125
126
127
128
129
130
131
132
# File 'lib/makit/commands/strategies/factory.rb', line 125

def self.childprocess_available?
  begin
    require "childprocess"
    true
  rescue LoadError
    false
  end
end

.create(options = {}) ⇒ Strategies::Base

Get the configured strategy instance

Parameters:

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

    strategy options

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/makit/commands/strategies/factory.rb', line 36

def self.create(options = {})
  strategy_type = determine_strategy_type

  begin
    case strategy_type
    when "childprocess"
      create_childprocess_strategy(options)
    when "open3"
      create_open3_strategy(options)
    when "auto"
      create_auto_detect_strategy(options)
    else
      Makit::Logging.warn("Unknown strategy '#{strategy_type}', falling back to Open3") if defined?(Makit::Logging)
      create_open3_strategy(options)
    end
  rescue => e
    # Ultimate fallback - create basic synchronous strategy
    Makit::Logging.warn("Failed to create strategy: #{e.message}") if defined?(Makit::Logging)
    Synchronous.new
  end
end

.create_auto_detect_strategy(options) ⇒ Strategies::Base

Create auto-detect strategy that tries ChildProcess first

Parameters:

  • options (Hash)

    strategy options

Returns:



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/makit/commands/strategies/factory.rb', line 98

def self.create_auto_detect_strategy(options)
  # Always use Open3 for now to avoid circular dependency issues
  begin
    Makit::Logging.debug("Using Open3 strategy") if defined?(Makit::Logging)
    Synchronous.new(**options)
  rescue => e
    # Fallback to basic strategy if there are any issues
    Makit::Logging.warn("Failed to create Open3 strategy: #{e.message}") if defined?(Makit::Logging)
    Synchronous.new
  end
end

.create_childprocess_strategy(options) ⇒ Strategies::Base

Create ChildProcess strategy with fallback

Parameters:

  • options (Hash)

    strategy options

Returns:



75
76
77
78
79
80
81
82
83
84
# File 'lib/makit/commands/strategies/factory.rb', line 75

def self.create_childprocess_strategy(options)
  begin
    require_relative "child_process"
    ChildProcess.new(**options)
  rescue LoadError, StandardError => e
    Makit::Logging.warn("Failed to load ChildProcess strategy: #{e.message}")
    Makit::Logging.info("Falling back to Open3 strategy")
    create_open3_strategy(options)
  end
end

.create_open3_strategy(options) ⇒ Strategies::Base

Create Open3 strategy

Parameters:

  • options (Hash)

    strategy options

Returns:



90
91
92
# File 'lib/makit/commands/strategies/factory.rb', line 90

def self.create_open3_strategy(options)
  Synchronous.new(**options)
end

.determine_strategy_typeString

Get the current strategy type from environment or default

Returns:



61
62
63
64
65
66
67
68
69
# File 'lib/makit/commands/strategies/factory.rb', line 61

def self.determine_strategy_type
  env_strategy = ENV["MAKIT_STRATEGY"]&.downcase

  if env_strategy && STRATEGIES.key?(env_strategy)
    env_strategy
  else
    "auto" # Default to auto-detect
  end
end

.strategy_infoHash

Get information about available strategies

Returns:

  • (Hash)

    strategy information



113
114
115
116
117
118
119
120
# File 'lib/makit/commands/strategies/factory.rb', line 113

def self.strategy_info
  {
    current: determine_strategy_type,
    available: STRATEGIES.keys,
    childprocess_available: childprocess_available?,
    open3_available: true, # Always available
  }
end