Module: Ukiryu::Tools::Generator

Defined in:
lib/ukiryu/tools/generator.rb

Overview

Generator module for dynamically creating tool-specific classes

This module is responsible for:

  • Loading tool definitions from YAML as lutaml-model models

  • Generating tool classes (e.g., Ukiryu::Tools::Imagemagick)

  • Caching generated classes with bounded LRU cache

  • Providing constant autoloading via const_missing

Class Method Summary collapse

Class Method Details

.all_generatedHash

Get all generated tool classes

Returns:

  • (Hash)

    map of tool name to class



124
125
126
127
128
129
130
# File 'lib/ukiryu/tools/generator.rb', line 124

def all_generated
  result = {}
  generated_classes_cache.each_key do |key|
    result[key] = generated_classes_cache[key]
  end
  result
end

.available_toolsArray<Symbol>

Get a list of all available tool names

Returns:

  • (Array<Symbol>)

    list of tool names



135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ukiryu/tools/generator.rb', line 135

def available_tools
  register_path = Ukiryu::Register.default_register_path
  return [] unless register_path

  tools_dir = File.join(register_path, 'tools')
  return [] unless Dir.exist?(tools_dir)

  Dir.entries(tools_dir)
     .reject { |e| e.start_with?('.') }
     .map(&:to_sym)
end

.clear_cacheObject

Clear the cache of generated classes

Useful for testing or reloading profiles



109
110
111
# File 'lib/ukiryu/tools/generator.rb', line 109

def clear_cache
  generated_classes_cache.clear
end

.generate(tool_name) ⇒ Class

Get or generate a tool class

Parameters:

  • tool_name (Symbol, String)

    the tool name

Returns:

  • (Class)

    the tool class

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ukiryu/tools/generator.rb', line 26

def generate(tool_name)
  tool_name = tool_name.to_sym
  cached = generated_classes_cache[tool_name]
  return cached if cached

  # Load the tool definition as a lutaml-model
  tool_definition = load_tool_definition(tool_name)
  raise Ukiryu::Errors::ToolNotFoundError, "Tool not found: #{tool_name}" unless tool_definition

  # Get the compatible platform profile
  platform_profile = tool_definition.compatible_profile

  # Generate the tool class
  tool_class = generate_tool_class(tool_name, tool_definition, platform_profile)

  generated_classes_cache[tool_name] = tool_class
  tool_class
end

.generate_and_const_set(tool_name) ⇒ Class

Generate a tool class and const it in the Tools namespace

Parameters:

  • tool_name (Symbol)

    the tool name

Returns:

  • (Class)

    the tool class



96
97
98
99
100
101
102
103
104
# File 'lib/ukiryu/tools/generator.rb', line 96

def generate_and_const_set(tool_name)
  tool_class = generate(tool_name)
  class_name = tool_name.to_s.capitalize

  # Const the class in the Tools module
  Ukiryu::Tools.const_set(class_name, tool_class) unless Ukiryu::Tools.const_defined?(class_name)

  tool_class
end

.generate_tool_class(tool_name, tool_definition, platform_profile) ⇒ Class

Generate a tool class from a tool definition

Parameters:

Returns:

  • (Class)

    the generated tool class



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ukiryu/tools/generator.rb', line 71

def generate_tool_class(tool_name, tool_definition, platform_profile)
  Class.new(::Ukiryu::Tools::Base) do
    @tool_name = tool_name
    @tool_definition = tool_definition
    @platform_profile = platform_profile

    # Define class methods
    singleton_class.send(:define_method, :tool_name) do
      @tool_name
    end

    singleton_class.send(:define_method, :tool_definition) do
      @tool_definition
    end

    singleton_class.send(:define_method, :platform_profile) do
      @platform_profile
    end
  end
end

.generated?(tool_name) ⇒ Boolean

Check if a tool class has been generated

Parameters:

  • tool_name (Symbol)

    the tool name

Returns:

  • (Boolean)

    true if the class has been generated



117
118
119
# File 'lib/ukiryu/tools/generator.rb', line 117

def generated?(tool_name)
  generated_classes_cache.key?(tool_name.to_sym)
end

.generated_classes_cacheCache

Get the generated classes cache (bounded LRU cache)

Returns:

  • (Cache)

    the generated classes cache



18
19
20
# File 'lib/ukiryu/tools/generator.rb', line 18

def generated_classes_cache
  @generated_classes_cache ||= Ukiryu::Cache.new(max_size: 50, ttl: 3600)
end

.load_tool_definition(tool_name, options = {}) ⇒ Models::ToolDefinition?

Load a ToolDefinition model from the register

Parameters:

  • tool_name (Symbol)

    the tool name

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

    loading options

Options Hash (options):

  • :version (String)

    specific version to load

Returns:



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ukiryu/tools/generator.rb', line 51

def load_tool_definition(tool_name, options = {})
  # Load the YAML file content
  yaml_content = Ukiryu::Register.load_tool_yaml(tool_name, version: options[:version])
  return nil unless yaml_content

  # Use lutaml-model's from_yaml to parse
  tool_definition = Models::ToolDefinition.from_yaml(yaml_content)

  # Resolve profile inheritance (e.g., windows profiles inherit from unix)
  tool_definition&.resolve_inheritance!

  tool_definition
end