Class: Ukiryu::Tools::Base Abstract

Inherits:
Object
  • Object
show all
Includes:
CommandBuilder
Defined in:
lib/ukiryu/tools/base.rb

Overview

This class is abstract.

Abstract base class for all tool-specific classes

This class provides the common interface that all tool classes inherit from. Tool-specific classes are dynamically generated by Tools::Generator.

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CommandBuilder

#build_env_vars, #detect_delimiter, #format_arg, #format_flag, #format_option

Constructor Details

#initializeBase

Initialize the tool instance

Sets up shell and platform instance variables for CommandBuilder



139
140
141
142
143
# File 'lib/ukiryu/tools/base.rb', line 139

def initialize
  runtime = Ukiryu::Runtime.instance
  @platform = runtime.platform
  @shell = runtime.shell
end

Class Attribute Details

.platform_profileModels::PlatformProfile (readonly)

Get the platform profile model

Returns:



38
39
40
# File 'lib/ukiryu/tools/base.rb', line 38

def platform_profile
  @platform_profile
end

.tool_definitionModels::ToolDefinition (readonly)

Get the tool definition model

Returns:



33
34
35
# File 'lib/ukiryu/tools/base.rb', line 33

def tool_definition
  @tool_definition
end

.tool_nameSymbol (readonly)

Get the tool name symbol

Returns:

  • (Symbol)

    the tool name



28
29
30
# File 'lib/ukiryu/tools/base.rb', line 28

def tool_name
  @tool_name
end

Class Method Details

.action_class_for(command_name) ⇒ Class

Get the action class for a specific command

Parameters:

  • command_name (Symbol)

    the command name

Returns:

  • (Class)

    the action class for this command

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ukiryu/tools/base.rb', line 71

def action_class_for(command_name)
  command_name = command_name.to_sym
  @action_classes ||= {}
  return @action_classes[command_name] if @action_classes[command_name]

  # Get command from platform profile model
  command_def = @platform_profile.command(command_name.to_s)
  raise ArgumentError, "Unknown command: #{command_name}" unless command_def

  # Generate the action class using ClassGenerator
  action_class = Ukiryu::Tools::ClassGenerator.generate_action_class(self, command_name, command_def)
  @action_classes[command_name] = action_class
  action_class
end

.action_for(command_name) ⇒ Class

Alias for action_class_for

Parameters:

  • command_name (Symbol)

    the command name

Returns:

  • (Class)

    the action class for this command



90
91
92
# File 'lib/ukiryu/tools/base.rb', line 90

def action_for(command_name)
  action_class_for(command_name)
end

.available?Boolean

Check if the tool is available on this system

Returns:

  • (Boolean)

    true if the tool can be executed



106
107
108
109
110
111
112
113
114
# File 'lib/ukiryu/tools/base.rb', line 106

def available?
  @available ||= begin
    # Check for compatible profile first
    return false unless platform_profile

    executable = Ukiryu::ExecutableFinder.find_executable(@tool_definition.name.to_s, @tool_definition)
    !executable.nil?
  end
end

.options_class_for(command_name) ⇒ Class

Get the options class for a specific command

Parameters:

  • command_name (Symbol)

    the command name

Returns:

  • (Class)

    the options class for this command

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ukiryu/tools/base.rb', line 44

def options_class_for(command_name)
  command_name = command_name.to_sym
  @options_classes ||= {}
  return @options_classes[command_name] if @options_classes[command_name]

  # Get command from platform profile model
  command_def = @platform_profile.command(command_name.to_s)
  raise ArgumentError, "Unknown command: #{command_name}" unless command_def

  # Generate the options class using ClassGenerator
  options_class = Ukiryu::Tools::ClassGenerator.generate_options_class(self, command_name, command_def)
  @options_classes[command_name] = options_class
  options_class
end

.options_for(command_name) ⇒ Class

Alias for options_class_for (more idiomatic)

Parameters:

  • command_name (Symbol)

    the command name

Returns:

  • (Class)

    the options class for this command



63
64
65
# File 'lib/ukiryu/tools/base.rb', line 63

def options_for(command_name)
  options_class_for(command_name)
end

.response_class_for(command_name) ⇒ Class

Get the response class for a command

Parameters:

  • command_name (Symbol)

    the command name

Returns:

  • (Class)

    the response class

Raises:

  • (ArgumentError)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ukiryu/tools/base.rb', line 120

def response_class_for(command_name)
  command_name = command_name.to_sym
  @response_classes ||= {}
  return @response_classes[command_name] if @response_classes[command_name]

  # Get command from platform profile model
  command_def = @platform_profile.command(command_name.to_s)
  raise ArgumentError, "Unknown command: #{command_name}" unless command_def

  # Generate the response class using ClassGenerator
  response_class = Ukiryu::Tools::ClassGenerator.generate_response_class(self, command_name, command_def)
  @response_classes[command_name] = response_class
  response_class
end

.versionVersion?

Get version information for this tool

Returns:

  • (Version, nil)

    version object if tool is available



97
98
99
100
101
# File 'lib/ukiryu/tools/base.rb', line 97

def version
  return nil unless available?

  new.version
end

Instance Method Details

#action_class_for(command_name) ⇒ Class

Get the action class for a command

Parameters:

  • command_name (Symbol)

    the command name

Returns:

  • (Class)

    the action class



201
202
203
# File 'lib/ukiryu/tools/base.rb', line 201

def action_class_for(command_name)
  self.class.action_for(command_name)
end

#action_for(command_name) ⇒ Action::Base

Get a new action instance for a command

Parameters:

  • command_name (Symbol)

    the command name

Returns:



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

def action_for(command_name)
  self.class.action_for(command_name).new(self)
end

#available?Boolean

Check if the tool is available

Returns:

  • (Boolean)

    true if the tool can be executed



217
218
219
# File 'lib/ukiryu/tools/base.rb', line 217

def available?
  self.class.available?
end

#execute(command_name, options, execution_timeout:) ⇒ Response::Base

Execute a command with options object

Parameters:

  • command_name (Symbol)

    the command to execute

  • options (Object)

    the options object

  • execution_timeout (Integer)

    timeout in seconds for command execution (required)

Returns:

Raises:

  • (ArgumentError)


244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/ukiryu/tools/base.rb', line 244

def execute(command_name, options, execution_timeout:)
  command_name = command_name.to_sym
  # Get command from platform profile model
  command_def = self.class.platform_profile.command(command_name.to_s)

  raise ArgumentError, "Unknown command: #{command_name}" unless command_def

  # Convert options to hash
  params = if options.is_a?(Hash)
             options.transform_keys(&:to_sym)
           else
             Ukiryu::OptionsBuilder.to_hash(options)
           end

  # Build arguments
  args = build_args(command_def, params)

  # Find executable using ExecutableFinder
  executable = Ukiryu::ExecutableFinder.find_executable(self.class.tool_definition.name.to_s,
                                                        self.class.tool_definition)

  # Get shell
  shell_sym = Ukiryu::Runtime.instance.shell

  # Build environment variables (including env var sets)
  env = build_execution_env(command_def, @platform_profile, params)

  # Execute with explicit timeout
  result = Ukiryu::Executor.execute(
    executable,
    args,
    env: env,
    timeout: execution_timeout,
    shell: shell_sym
  )

  # Build response
  build_response(command_name, result)
end

#nameString

Get the tool name from the tool definition

Returns:

  • (String)

    the tool name



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

def name
  self.class.tool_definition.name
end

#options_class_for(command_name) ⇒ Class

Get the options class for a command

Parameters:

  • command_name (Symbol)

    the command name

Returns:

  • (Class)

    the options class



185
186
187
# File 'lib/ukiryu/tools/base.rb', line 185

def options_class_for(command_name)
  self.class.options_for(command_name)
end

#options_for(command_name) ⇒ Options::Base

Get a new options instance for a command

Parameters:

  • command_name (Symbol)

    the command name

Returns:



177
178
179
# File 'lib/ukiryu/tools/base.rb', line 177

def options_for(command_name)
  self.class.options_for(command_name).new
end

#platform_profileModels::PlatformProfile

Get the platform profile (for backward compatibility)

Returns:



169
170
171
# File 'lib/ukiryu/tools/base.rb', line 169

def platform_profile
  self.class.platform_profile
end

#tool_definitionModels::ToolDefinition

Get the tool definition (for backward compatibility)

Returns:



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

def tool_definition
  self.class.tool_definition
end

#tool_nameSymbol

Get the tool name for this instance

Returns:

  • (Symbol)

    the tool name



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

def tool_name
  self.class.tool_name
end

#unavailability_reasonString?

Get the reason why the tool is not available

Returns nil if the tool is available, or a string explaining why not. This helps users understand issues like:

  • Tool not installed

  • Wrong version installed (e.g., impostor tool)

Returns:

  • (String, nil)

    reason for unavailability, or nil if available



229
230
231
232
233
234
235
236
# File 'lib/ukiryu/tools/base.rb', line 229

def unavailability_reason
  return nil if available?

  return executable_not_found_message if executable.nil?
  return version_mismatch_message if version_mismatch?

  generic_unavailable_message
end

#versionString?

Get version information

Returns:

  • (String, nil)

    version string if tool is available



208
209
210
211
212
# File 'lib/ukiryu/tools/base.rb', line 208

def version
  return @version if defined?(@version)

  @version = detect_version
end