Class: RailsLens::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_lens/commands.rb

Overview

Handles the actual execution of CLI commands

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = $stdout) ⇒ Commands

Returns a new instance of Commands.



10
11
12
# File 'lib/rails_lens/commands.rb', line 10

def initialize(output = $stdout)
  @output = output
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



8
9
10
# File 'lib/rails_lens/commands.rb', line 8

def output
  @output
end

Instance Method Details

#annotate_database_objects(options = {}) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/rails_lens/commands.rb', line 29

def annotate_database_objects(options = {})
  results = Schema::DatabaseAnnotator.annotate_all(options)

  output.say "Annotated #{results[:annotated].length} abstract base classes with database objects", :green
  report_skipped_and_failed(results, 'abstract classes')

  results
end

#annotate_mailers(options = {}) ⇒ Object



42
43
44
# File 'lib/rails_lens/commands.rb', line 42

def annotate_mailers(options = {})
  annotate_files(Mailer::Annotator.new(dry_run: options[:dry_run]), 'mailer files', options)
end

#annotate_models(options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rails_lens/commands.rb', line 14

def annotate_models(options = {})
  results = Schema::AnnotationManager.annotate_all(options)

  output.say "Annotated #{results[:annotated].length} models", :green
  report_skipped_and_failed(results, 'models')

  # Also annotate database-level objects (functions, etc.)
  if options[:include_database_objects]
    db_results = annotate_database_objects(options)
    results.merge!(database_objects: db_results)
  end

  results
end

#annotate_routes(options = {}) ⇒ Object



38
39
40
# File 'lib/rails_lens/commands.rb', line 38

def annotate_routes(options = {})
  annotate_files(Route::Annotator.new(dry_run: options[:dry_run]), 'controller files with routes', options)
end

#check(_options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rails_lens/commands.rb', line 97

def check(_options = {})
  output.say 'Checking Rails Lens configuration validity...', :blue

  # Check if Rails is properly loaded
  unless defined?(Rails)
    output.say 'Rails environment not detected', :red
    return { valid: false, errors: ['Rails environment not detected'] }
  end

  # Check database connections
  errors = []
  begin
    ApplicationRecord.connection.execute('SELECT 1')
    output.say 'Primary database connection: OK', :green
  rescue StandardError => e
    errors << "Primary database connection failed: #{e.message}"
    output.say 'Primary database connection: FAILED', :red
  end

  if errors.empty?
    output.say 'Configuration is valid', :green
    { valid: true, errors: [] }
  else
    output.say 'Configuration has errors', :red
    { valid: false, errors: errors }
  end
end

#config(subcommand, 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
# File 'lib/rails_lens/commands.rb', line 125

def config(subcommand, options = {})
  case subcommand
  when 'show'
    output.say 'Rails Lens Configuration:', :blue
    output.say "  Config file: #{RailsLens.config_file || 'default'}"
    output.say "  Verbose: #{RailsLens.config.verbose || false}"
    output.say "  Debug: #{RailsLens.config.debug || false}"

    if RailsLens.config.respond_to?(:position)
      output.say "  Default position: #{RailsLens.config.position || 'before'}"
    end

  when 'set'
    if options[:key] && options[:value]
      output.say "Setting #{options[:key]} = #{options[:value]}", :green
      # NOTE: This would require implementing config persistence
      output.say 'Note: Configuration changes are not persisted yet', :yellow
    else
      output.say 'Usage: config set --key KEY --value VALUE', :red
    end

  when 'reset'
    output.say 'Resetting configuration to defaults', :yellow
    # NOTE: This would reset to defaults

  else
    output.say "Unknown config subcommand: #{subcommand}", :red
    output.say 'Available: show, set, reset'
  end
end

#generate_erd(options = {}) ⇒ Object



60
61
62
63
64
65
# File 'lib/rails_lens/commands.rb', line 60

def generate_erd(options = {})
  visualizer = ERD::Visualizer.new(options: options)
  filename = visualizer.generate
  output.say "Entity Relationship Diagram generated at #{filename}", :green
  filename
end

#install(options = {}) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
# File 'lib/rails_lens/commands.rb', line 156

def install(options = {})
  output.say 'Installing Rails Lens rake tasks...', :blue

  # Determine Rails root
  rails_root = if defined?(Rails) && Rails.respond_to?(:root) && Rails.root
                 Rails.root.to_s
               else
                 Dir.pwd
               end
  tasks_dir = File.join(rails_root, 'lib', 'tasks')
  rake_file = File.join(tasks_dir, 'rails_lens.rake')

  # Check if file exists
  if File.exist?(rake_file) && !options[:force]
    output.say 'Rails Lens rake task already exists at lib/tasks/rails_lens.rake', :yellow
    output.say 'Use --force to overwrite', :yellow
    return { installed: false, path: rake_file }
  end

  # Create lib/tasks directory if it doesn't exist
  FileUtils.mkdir_p(tasks_dir)

  # Write the rake task
  File.write(rake_file, rake_task_template)

  output.say "Created rake task at #{rake_file}", :green
  output.say ''
  output.say 'The following task has been installed:', :blue
  output.say '  • rails_lens:annotate - Annotate models after migrations', :green
  output.say ''
  output.say 'Configuration options in lib/tasks/rails_lens.rake:', :blue
  output.say '  • AUTO_ANNOTATE (default: true in development)', :cyan
  output.say '  • RAILS_LENS_ENV (default: development)', :cyan
  output.say ''
  output.say 'Disable auto-annotation:', :blue
  output.say '  export AUTO_ANNOTATE=false', :cyan

  { installed: true, path: rake_file }
end

#lint(options = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rails_lens/commands.rb', line 67

def lint(options = {})
  output.say 'Linting Rails Lens configuration and annotations...', :blue

  issues = []
  warnings = []
  domains = options[:domains] || %w[models routes mailers]

  domains.each do |domain|
    case domain
    when 'models'
      # Check for inconsistent model annotations
      output.say '  Checking models domain...', :blue if options[:verbose]
    when 'routes'
      # Check for route annotation consistency
      output.say '  Checking routes domain...', :blue if options[:verbose]
    when 'mailers'
      # Check for mailer annotation consistency
      output.say '  Checking mailers domain...', :blue if options[:verbose]
    end
  end

  if issues.empty? && warnings.empty?
    output.say 'No linting issues found', :green
  else
    output.say "Found #{issues.length} issues and #{warnings.length} warnings", :yellow
  end

  { issues: issues, warnings: warnings }
end

#remove_mailers(options = {}) ⇒ Object



56
57
58
# File 'lib/rails_lens/commands.rb', line 56

def remove_mailers(options = {})
  remove_files(Mailer::Annotator.new(dry_run: options[:dry_run]), 'mailer', 'mailer files')
end

#remove_models(options = {}) ⇒ Object



46
47
48
49
50
# File 'lib/rails_lens/commands.rb', line 46

def remove_models(options = {})
  results = Schema::AnnotationManager.remove_all(options)
  output.say "Removed annotations from #{results[:removed].length} models", :green
  results
end

#remove_routes(options = {}) ⇒ Object



52
53
54
# File 'lib/rails_lens/commands.rb', line 52

def remove_routes(options = {})
  remove_files(Route::Annotator.new(dry_run: options[:dry_run]), 'route', 'controller files')
end