Class: RSpec::Scaffold::Cli

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/scaffold/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(boot_path) ⇒ Cli

Returns a new instance of Cli.

Parameters:

  • boot_path (Pathname)


12
13
14
15
# File 'lib/rspec/scaffold/cli.rb', line 12

def initialize(boot_path)
  @boot_path = boot_path
  @command_line_arguments = send(:class).command_line_arguments
end

Class Method Details

.command_line_argumentsObject

this method allows simple testing since it, unlike ARGV constant, can be mocked.



7
8
9
# File 'lib/rspec/scaffold/cli.rb', line 7

def self.command_line_arguments
  return ARGV # => ["-t", "-y", "/some/path"] or ["-ty", "/some/path"]
end

Instance Method Details

#startObject

This method will be run as a script and uses on ARGV hash contents



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
# File 'lib/rspec/scaffold/cli.rb', line 18

def start
  # fallback to help text output if needed args are missing
  (puts help_string; abort) if path_argument.nil? || options.include?("h")

  # 0 see if path argument is not absolute
  (puts absolute_path_warning; abort) if path_argument[%r'\A[\/]']

  # 0. see if passed file/dir exists and is in boot_path tree.
  (puts not_found_warning_string; abort) if !file_or_dir.exist?

  # 1. must process path argument to determine whether it is a single file or a dir.
  @processable_files = if file_or_dir.directory?
    RSpec::Scaffold::DirExpander.new(file_or_dir).expand_ruby_files
  else
    # file_or_dir.file?
    [file_or_dir]
  end

  # 2. verify processing if there are numerous files and -y is not given
  if !many_processable_file_danger? || argumented_agreeing? || output_to_console?
    # noop, alls well
  else
    puts "#{@processable_files.size} files are about to be processed. Are you sure? [Y/n]"
    if STDIN.gets.strip[%r'\Ay'i]
      green("-> Proceeding with scaffold build!")
    else
      abort("Nothing was done!")
    end
  end

  # 2. once path processing is done, array of files (sometimes and array of one file) is looped over
  @processable_files.each do |processable_file|
    # all is set for processing
    if output_to_console?
      test_scaffold = RSpec::Scaffold.testify_file(processable_file, :to_text)

      puts("#== #{processable_file} ==")
      puts("")
      puts(test_scaffold)
      puts("")
    else
      # build found file's spec file location
      spec_file_location = RSpec::Scaffold::SpecLocationBuilder.new(@boot_path, processable_file).spec_location

      # output to spec files, core behavior
      RSpec::Scaffold.testify_file(processable_file, out: spec_file_location)
    end
  end

  puts "-> All done!"

  return true
end