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', '--warnings')

Class Method Summary collapse

Class Method Details

.fetch_file_paths(file_prefix, file_suffix, *args) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/fix/command.rb', line 133

def self.fetch_file_paths(file_prefix, file_suffix, *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, '**', "#{file_prefix}*#{file_suffix}.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



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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/fix/command.rb', line 74

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

  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.on('--prefix=[PREFIX]', String, 'Prefix of the spec files') do |s|
      options[:prefix] = s
    end

    opts.on('--suffix=[SUFFIX]', String, 'Suffix of the spec files') do |s|
      options[:suffix] = s
    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

  %w(~ .).each do |c|
    config_path = File.join(File.expand_path(c), COMMAND_LINE_OPTIONS_FILE)
    opt_parser.load(config_path)
  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', '--warnings')

Parameters:

  • args (Array)

    A list of files and options.

Raises:

  • (SystemExit)

    The result of the tests.



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

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

  file_paths = fetch_file_paths(
    config.fetch(:prefix),
    config.fetch(:suffix), *args
  )

  str  = "\e[37m"
  str += '> fix'
  str += ' --debug'          if $DEBUG
  str += ' --warnings'       if $VERBOSE
  str += ' --prefix'         if config.fetch(:prefix)
  str += ' --suffix'         if config.fetch(:suffix)

  puts file_paths.to_a.join(' ') + ' ' + str + "\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