Class: Vercon::Commands::Generate

Inherits:
Dry::CLI::Command
  • Object
show all
Defined in:
lib/vercon/commands/generate.rb

Constant Summary collapse

AUTOFIXERS =
{
  standard: "bundle exec standardrb --fix %{file} > /dev/null 2>&1",
  rubocop: "bundle exec rubocop -a %{file} > /dev/null 2>&1"
}

Instance Method Summary collapse

Constructor Details

#initializeGenerate

Returns a new instance of Generate.



32
33
34
35
36
37
38
# File 'lib/vercon/commands/generate.rb', line 32

def initialize
  @config = Vercon::Config.new
  @stdout = Vercon::Stdout.new
  @files = Dry::Files.new

  super
end

Instance Method Details

#call(path: nil, **opts) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vercon/commands/generate.rb', line 40

def call(path: nil, **opts)
  return unless can_generate?(path, opts)

  output_path = opts[:output_path] || generate_test_file_path(path, opts)
  return if output_path.nil?

  current_test = files.exist?(output_path) ? files.read(output_path) : nil

  result = generate_test_file(path, opts, current_test)
  return if result.nil?

  result = run_autofixes(result)

  if opts[:stdout]
    formatter = Rouge::Formatters::Terminal256.new(Rouge::Themes::Base16::Monokai.new)
    lexer = Rouge::Lexers::Ruby.new

    stdout.puts(formatter.format(lexer.lex(result)))
    return
  end

  if !opts[:force] && files.exist?(output_path) && stdout.no?("File already exists at \"#{output_path}\". Overwrite?")
    return
  end

  files.write(output_path, result)

  stdout.ok("Test file saved at \"#{output_path}\" 🥳")

  if opts[:open] == true || (opts[:open].nil? && config.open_by_default?)
    TTY::Editor.new(raise_on_failure: true).open(output_path)
  end
end