Class: Fix::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/fix/command.rb

Overview

Open the command class.

Examples:

Let’s test a duck’s spec!

Command.run('duck_fix.rb', '-w')

Class Method Summary collapse

Class Method Details

.fetch_file_paths(*args) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/fix/command.rb', line 102

def self.fetch_file_paths(*args)
  absolute_paths = Set.new

  args << '.' if args.empty?
  args.each do |s|
    s = File.absolute_path(s) unless s.start_with?(File::SEPARATOR)

    if File.directory?(s)
      spec_files = File.join(s, '**', '*_fix.rb')
      Dir.glob(spec_files).each { |spec_f| absolute_paths.add(spec_f) }
    else
      absolute_paths.add(s)
    end
  end

  if absolute_paths.empty?
    warn 'Sorry, files or directories not found.'
    exit false
  end

  absolute_paths
end

.process_args(args) ⇒ Object



59
60
61
62
63
64
65
66
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
96
97
98
99
# File 'lib/fix/command.rb', line 59

def self.process_args(args)
  options = {
    debug:          false,
    warnings:       false
  }

  opt_parser = OptionParser.new do |opts|
    opts.banner = 'Usage: fix <files or directories> [options]'

    opts.separator ''
    opts.separator 'Specific options:'

    opts.on('--debug', 'Enable ruby debug') do
      options[:debug] = $DEBUG = true
    end

    opts.on('--warnings', 'Enable ruby warnings') do
      options[:warnings] = $VERBOSE = true
    end

    opts.separator ''
    opts.separator 'Common options:'

    opts.on_tail '--help', 'Show this message' do
      puts opts
      exit
    end

    opts.on_tail '--version', 'Show the version' do
      puts File.read(File.join(File.expand_path(File.dirname(__FILE__)),
                               '..',
                               '..',
                               'VERSION.semver')).chomp

      exit
    end
  end

  opt_parser.parse!(args)
  options
end

.run(*args) ⇒ Object

Handle the parameters passed to fix command from the console.

Examples:

Let’s test a duck’s spec!

run('duck_fix.rb', '-w')

Parameters:

  • args (Array)

    A list of files and options.

Raises:

  • (SystemExit)

    The result of the tests.



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
# File 'lib/fix/command.rb', line 31

def self.run(*args)
  process_args(args)

  file_paths = fetch_file_paths(*args)

  str  = "\e[37m"
  str += '> fix'
  str += ' --debug'          if $DEBUG
  str += ' --warnings'       if $VERBOSE

  puts str + ' ' + file_paths.to_a.join(' ') + "\e[22m"

  status = true

  file_paths.each do |file_path|
    begin
      puts
      print "\e[30m#{file_path}\e[0m "
      require file_path
    rescue SystemExit => e
      status = false unless e.success?
    end
  end

  exit(status)
end