Class: FactorySeeder::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/factory_seeder/cli.rb

Instance Method Summary collapse

Instance Method Details

#generate(factory_name = nil) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/factory_seeder/cli.rb', line 14

def generate(factory_name = nil)
  if factory_name
    generate_single_factory(factory_name)
  else
    interactive_generate
  end
end

#initObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
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/factory_seeder/cli.rb', line 83

def init
  puts '🚀 Initializing FactorySeeder...'

  seeds_file = Rails.root.join('db/seeds_factory_seeder.rb')
  unless File.exist?(seeds_file)
    File.write(seeds_file, <<~RUBY)
      # FactorySeeder Seeds
      # This file is automatically generated by FactorySeeder

      FactorySeeder.generate do |seeder|
        # Add your custom seeds here
      end
    RUBY
    puts "✅ Seeds file created: #{seeds_file}"
  end

  initializer_file = Rails.root.join('config/initializers/factory_seeder.rb')
  unless File.exist?(initializer_file)
    File.write(initializer_file, <<~RUBY)
      # frozen_string_literal: true
      # FactorySeeder initializer
      # This file is generated by FactorySeeder

      FactorySeeder.configure do |config|
        config.factory_paths << Rails.root.join('spec/factories').to_s if Dir.exist?('spec/factories')
        config.factory_paths << Rails.root.join('test/factories').to_s if Dir.exist?('test/factories')
        config.verbose = true
      end

      # Load custom seeds so they are available at boot
      seeds_file = Rails.root.join('db/seeds_factory_seeder.rb')
      load seeds_file if File.exist?(seeds_file)
    RUBY

    puts "✅ Initializer created: #{initializer_file}"
  end

  puts '✅ FactorySeeder initialized!'
  puts "  Factory paths: #{FactorySeeder.configuration.factory_paths.join(', ')}"
  puts "  Verbose mode: #{FactorySeeder.configuration.verbose}"
end

#listObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/factory_seeder/cli.rb', line 24

def list
  FactorySeeder.configuration.verbose = options[:verbose] if options[:verbose]

  factories = FactorySeeder.scan_factories

  if factories.empty?
    puts '❌ No factories found. Make sure you have FactoryBot factories in spec/factories/ or test/factories/'
    return
  end

  puts "📋 Found #{factories.count} factories:\n\n"

  factories.sort.each do |name, info|
    puts "🏭 #{name} (#{info[:class_name] || 'Unknown class'})"
    puts "   Traits: #{formatted_traits(info[:traits])}"
    puts "   Associations: #{formatted_associations(info[:associations])}"
    puts "   Attributes: #{formatted_attributes(info[:attributes])}"
    puts
  end
end

#preview(factory_name) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/factory_seeder/cli.rb', line 49

def preview(factory_name)
  FactorySeeder.configuration.verbose = true

  # Vérifier si la factory existe
  factory_names = FactorySeeder.list_factory_names
  unless factory_names.include?(factory_name)
    puts "❌ Factory '#{factory_name}' not found"
    puts "Available factories: #{factory_names.first(10).join(', ')}"
    return
  end

  traits = options[:traits]&.split(',')&.map(&:strip) || []
  attributes = parse_attributes(options[:attributes])

  generator = SeedGenerator.new
  preview_data = generator.preview(factory_name, resolved_count, traits, attributes)

  puts "🔍 Preview for #{factory_name}:"
  puts JSON.pretty_generate(preview_data)
end

#seeds(seed_name = nil) ⇒ Object



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
# File 'lib/factory_seeder/cli.rb', line 129

def seeds(seed_name = nil)
  seeds_file = 'db/seeds_factory_seeder.rb'

  unless File.exist?(seeds_file)
    puts "❌ Seeds file not found: #{seeds_file}"
    puts "💡 Run 'factory_seeder init' to create the default seeds file"
    return
  end

  # Charger le fichier de seeds pour avoir accès aux seeds définis
  load seeds_file

  if options[:list]
    list_available_seeds
    return
  end

  if options[:all]
    run_all_seeds
    return
  end

  if seed_name.nil?
    puts '❌ Please specify a seed name, use --list to see available seeds, or --all to run all seeds'
    puts '💡 Example: factory_seeder seeds development'
    return
  end

  run_specific_seed(seed_name)
end

#webObject



73
74
75
76
77
78
79
80
# File 'lib/factory_seeder/cli.rb', line 73

def web
  puts "🌐 Starting FactorySeeder web interface on http://#{options[:host]}:#{options[:port]}"
  puts 'Press Ctrl+C to stop'

  WebInterface.set :port, options[:port]
  WebInterface.set :bind, options[:host]
  WebInterface.run!
end