Class: RubyLlmAgents::MigrateStructureGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/ruby_llm_agents/migrate_structure_generator.rb

Overview

Migration generator for moving from old directory structure to new

Migrates from:

app/{root}/agents/
app/{root}/image/generators/
app/{root}/audio/speakers/
app/{root}/text/embedders/

To:

app/agents/
app/agents/images/
app/agents/audio/
app/agents/embedders/

Usage:

rails generate ruby_llm_agents:migrate_structure
rails generate ruby_llm_agents:migrate_structure --dry-run
rails generate ruby_llm_agents:migrate_structure --source-root=ai

Constant Summary collapse

PATH_MAPPING =

Maps old paths to new paths Format: old_subpath => new_path (relative to app/)

{
  # Task agents
  "agents" => "agents",

  # Image agents (flatten operation types)
  "image/generators" => "agents/images",
  "image/analyzers" => "agents/images",
  "image/editors" => "agents/images",
  "image/upscalers" => "agents/images",
  "image/variators" => "agents/images",
  "image/transformers" => "agents/images",
  "image/background_removers" => "agents/images",
  "image/pipelines" => "agents/images",

  # Audio agents
  "audio/speakers" => "agents/audio",
  "audio/transcribers" => "agents/audio",

  # Text operations
  "text/embedders" => "agents/embedders",
  "text/moderators" => "agents/moderators",

  # Tools
  "tools" => "tools"
}.freeze
NAMESPACE_MAPPING =

Namespace transformations Format: old_namespace_pattern => new_namespace

{
  # Remove root namespace from task agents
  /\A(\w+)::(\w+Agent)\z/ => '\2',

  # Image namespaces
  /\A(\w+)::Image::(\w+)\z/ => 'Images::\2',

  # Audio namespaces
  /\A(\w+)::Audio::(\w+)\z/ => 'Audio::\2',

  # Text namespaces -> Embedders/Moderators
  /\A(\w+)::Text::(\w+Embedder)\z/ => 'Embedders::\2',
  /\A(\w+)::Text::(\w+Moderator)\z/ => 'Moderators::\2'
}.freeze

Instance Method Summary collapse

Instance Method Details

#check_prerequisitesObject



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/generators/ruby_llm_agents/migrate_structure_generator.rb', line 94

def check_prerequisites
  @source_root_dir = detect_source_root

  if @source_root_dir.nil?
    say_status :skip, "No old structure detected. Nothing to migrate.", :yellow
    say ""
    say "If you have an existing structure, specify the source root:"
    say "  rails generate ruby_llm_agents:migrate_structure --source-root=llm"
    raise Thor::Error, "Migration aborted: no source found"
  end

  say_status :found, "Old structure at app/#{@source_root_dir}/", :green
end

#cleanup_old_directoriesObject



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/generators/ruby_llm_agents/migrate_structure_generator.rb', line 223

def cleanup_old_directories
  return if @files_to_migrate.nil? || @files_to_migrate.empty?

  say ""
  say_status :cleanup, "Cleaning up old directories", :green

  # Collect all old directories
  old_dirs = PATH_MAPPING.keys.map do |old_subpath|
    Rails.root.join("app", @source_root_dir, old_subpath)
  end.select { |d| File.directory?(d) }

  # Sort by depth (deepest first) for proper cleanup
  old_dirs.sort_by { |d| -d.to_s.count("/") }.each do |dir|
    cleanup_directory(dir)
  end

  # Try to remove the root directory if empty
  root_dir = Rails.root.join("app", @source_root_dir)
  if File.directory?(root_dir)
    cleanup_directory(root_dir)
  end
end

#create_new_directoriesObject



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/generators/ruby_llm_agents/migrate_structure_generator.rb', line 152

def create_new_directories
  return if @files_to_migrate.nil? || @files_to_migrate.empty?

  say_status :create, "Creating new directory structure", :green

  directories = @files_to_migrate.map { |f| File.dirname(f[:new]) }.uniq

  directories.each do |dir|
    if options[:dry_run]
      say_status :dry_run, "Would create #{dir.sub(Rails.root.to_s + "/", "")}", :yellow
    else
      FileUtils.mkdir_p(dir)
      say_status :mkdir, dir.sub(Rails.root.to_s + "/", ""), :green
    end
  end
end

#move_filesObject



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/generators/ruby_llm_agents/migrate_structure_generator.rb', line 169

def move_files
  return if @files_to_migrate.nil? || @files_to_migrate.empty?

  say ""
  say_status :move, "Moving files to new locations", :green

  @files_to_migrate.each do |f|
    if options[:dry_run]
      say_status :dry_run, "Would move #{f[:old_display]} -> #{f[:new_display]}", :yellow
    else
      move_file(f[:old], f[:new])
      say_status :moved, f[:new_display], :green
    end
  end
end

#show_completion_messageObject



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
# File 'lib/generators/ruby_llm_agents/migrate_structure_generator.rb', line 246

def show_completion_message
  say ""
  say "=" * 60
  say ""

  if options[:dry_run]
    say "Dry run complete! No changes were made.", :yellow
    say ""
    say "To perform the actual migration, run:"
    say "  rails generate ruby_llm_agents:migrate_structure"
  else
    say "Migration complete!", :green
    say ""
    say "Your app now uses the new directory structure:"
    say ""
    say "  app/"
    say "  ├── agents/"
    say "  │   ├── application_agent.rb"
    say "  │   ├── your_agent.rb"
    say "  │   ├── images/"
    say "  │   ├── audio/"
    say "  │   ├── embedders/"
    say "  │   └── moderators/"
    say ""
    say "Next steps:"
    say "  1. Update class references in your code:"
    say "     - Llm::Image::ProductGenerator -> Images::ProductGenerator"
    say "     - Llm::Audio::MeetingTranscriber -> Audio::MeetingTranscriber"
    say "     - Llm::Text::SemanticEmbedder -> Embedders::SemanticEmbedder"
    say "  2. Run your test suite: bundle exec rspec"
    say "  3. Commit the changes: git add -A && git commit -m 'Migrate to new agent structure'"
  end

  say ""
  say "=" * 60
end

#show_migration_planObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
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
# File 'lib/generators/ruby_llm_agents/migrate_structure_generator.rb', line 108

def show_migration_plan
  say ""
  say "=" * 60
  say "Migration Plan", :bold
  say "=" * 60
  say ""
  say "Source: app/#{@source_root_dir}/"
  say "Target: app/agents/"
  say ""

  @files_to_migrate = []

  PATH_MAPPING.each do |old_subpath, new_path|
    old_full_path = Rails.root.join("app", @source_root_dir, old_subpath)
    next unless File.directory?(old_full_path)

    files = Dir.glob("#{old_full_path}/**/*.rb")
    next if files.empty?

    files.each do |file|
      relative = file.sub("#{old_full_path}/", "")
      new_file = Rails.root.join("app", new_path, relative)
      @files_to_migrate << {
        old: file,
        new: new_file.to_s,
        old_display: "app/#{@source_root_dir}/#{old_subpath}/#{relative}",
        new_display: "app/#{new_path}/#{relative}"
      }
    end
  end

  if @files_to_migrate.empty?
    say_status :skip, "No Ruby files found to migrate", :yellow
    return
  end

  say "Files to migrate (#{@files_to_migrate.size}):"
  @files_to_migrate.each do |f|
    say "  #{f[:old_display]}"
    say "    -> #{f[:new_display]}", :green
  end
  say ""
end

#update_base_classesObject



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/generators/ruby_llm_agents/migrate_structure_generator.rb', line 204

def update_base_classes
  return if options[:skip_namespace_update]
  return if @files_to_migrate.nil? || @files_to_migrate.empty?

  say ""
  say_status :update, "Updating base class references", :green

  @files_to_migrate.each do |f|
    file_path = options[:dry_run] ? f[:old] : f[:new]
    next unless File.exist?(file_path)

    if options[:dry_run]
      say_status :dry_run, "Would update base classes in #{f[:new_display]}", :yellow
    else
      update_file_base_classes(f[:new])
    end
  end
end

#update_namespacesObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/generators/ruby_llm_agents/migrate_structure_generator.rb', line 185

def update_namespaces
  return if options[:skip_namespace_update]
  return if @files_to_migrate.nil? || @files_to_migrate.empty?

  say ""
  say_status :update, "Updating namespaces in Ruby files", :green

  @files_to_migrate.each do |f|
    file_path = options[:dry_run] ? f[:old] : f[:new]
    next unless File.exist?(file_path)

    if options[:dry_run]
      say_status :dry_run, "Would update namespaces in #{f[:new_display]}", :yellow
    else
      update_file_namespaces(f[:new])
    end
  end
end