Class: Monolith::GeneratorsController::Generator

Inherits:
Object
  • Object
show all
Defined in:
app/controllers/monolith/generators_controller.rb

Overview

Inline ActiveModel-like object

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass) ⇒ Generator

Returns a new instance of Generator.



67
68
69
70
71
# File 'app/controllers/monolith/generators_controller.rb', line 67

def initialize(klass)
  @klass = klass
  @namespace = klass.namespace
  @name = @namespace.split(':').last
end

Instance Attribute Details

#klassObject (readonly)

Returns the value of attribute klass.



38
39
40
# File 'app/controllers/monolith/generators_controller.rb', line 38

def klass
  @klass
end

#nameObject (readonly)

Returns the value of attribute name.



38
39
40
# File 'app/controllers/monolith/generators_controller.rb', line 38

def name
  @name
end

#namespaceObject (readonly)

Returns the value of attribute namespace.



38
39
40
# File 'app/controllers/monolith/generators_controller.rb', line 38

def namespace
  @namespace
end

Class Method Details

.allObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/monolith/generators_controller.rb', line 40

def self.all
  Rails.application.load_generators

  # Get all generator namespaces
  namespaces = Rails::Generators.hidden_namespaces + Rails::Generators.public_namespaces
  namespaces = namespaces.uniq.sort

  # Find and instantiate each generator
  generators = namespaces.map do |namespace|
    begin
      klass = Rails::Generators.find_by_namespace(namespace)
      klass && !klass.name.nil? ? new(klass) : nil
    rescue
      nil
    end
  end.compact

  generators
end

.find(namespace) ⇒ Object



60
61
62
63
64
65
# File 'app/controllers/monolith/generators_controller.rb', line 60

def self.find(namespace)
  Rails.application.load_generators

  klass = Rails::Generators.find_by_namespace(namespace)
  klass && new(klass)
end

Instance Method Details

#argumentsObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/controllers/monolith/generators_controller.rb', line 81

def arguments
  return [] unless klass.respond_to?(:arguments)

  klass.arguments.map do |arg|
    {
      name: arg.name,
      type: arg.type,
      required: arg.required?,
      description: arg.description,
      default: arg.default
    }
  end
rescue
  []
end

#class_optionsObject



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/monolith/generators_controller.rb', line 97

def class_options
  return {} unless klass.respond_to?(:class_options)

  klass.class_options.transform_values do |option|
    {
      type: option.type,
      default: option.default,
      description: option.description,
      aliases: option.aliases,
      banner: option.banner
    }
  end
rescue
  {}
end

#descriptionObject



77
78
79
# File 'app/controllers/monolith/generators_controller.rb', line 77

def description
  klass.desc rescue nil
end

#invoke(args, options = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'app/controllers/monolith/generators_controller.rb', line 125

def invoke(args, options = {})
  require 'stringio'

  # Capture output
  output = StringIO.new
  original_stdout = $stdout
  original_stderr = $stderr
  $stdout = output
  $stderr = output

  begin
    # Convert options hash to array format Thor expects
    thor_args = args.dup
    options.each do |key, value|
      next if value.blank?
      thor_args << "--#{key}=#{value}"
    end

    # Invoke the generator
    Rails::Generators.invoke(namespace, thor_args, behavior: :invoke, destination_root: Rails.root)

    {
      success: true,
      output: output.string,
      error: nil
    }
  rescue => e
    {
      success: false,
      output: output.string,
      error: "#{e.class}: #{e.message}"
    }
  ensure
    $stdout = original_stdout
    $stderr = original_stderr
  end
end

#source_locationObject



113
114
115
116
117
118
119
120
121
122
123
# File 'app/controllers/monolith/generators_controller.rb', line 113

def source_location
  return nil unless klass.respond_to?(:instance_methods)

  method = klass.instance_method(:initialize) rescue nil
  return nil unless method

  file, line = method.source_location
  file
rescue
  nil
end

#to_paramObject



73
74
75
# File 'app/controllers/monolith/generators_controller.rb', line 73

def to_param
  namespace
end