Class: ParserGauntlet

Inherits:
Gauntlet
  • Object
show all
Defined in:
lib/gauntlet_parser.rb

Constant Summary collapse

RUBY20 =
'ruby'
RUBY19 =
'ruby1.9.1'
RUBY18 =
'/opt/rubies/ruby-1.8.7-p370/bin/ruby'

Instance Method Summary collapse

Instance Method Details

#load_yamlObject

[View source]

96
97
98
99
100
101
# File 'lib/gauntlet_parser.rb', line 96

def load_yaml(*)
  data = super
  @was_errors = data.count { |_name, errs| errs != {} }

  data
end

#parse(name) ⇒ Object

[View source]

57
58
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
# File 'lib/gauntlet_parser.rb', line 57

def parse(name)
  puts "GEM: #{name}"

  @result = {}

  if ENV.include?('FAST')
    total_size = Dir["**/*.rb"].map(&File.method(:size)).reduce(:+)
    if total_size > 300_000
      puts "Skip."
      return
    end
  end

  Dir["**/*.rb"].each do |file|
    next if File.directory? file

    try(Parser::Ruby20, RUBY20, file) do
      puts "Trying 1.9:"
      try(Parser::Ruby19, RUBY19, file, show_ok: true) do
        puts "Trying 1.8:"
        try(Parser::Ruby18, RUBY18, file, show_ok: true) do
          puts "Invalid syntax."
        end
      end
    end
  end

  @result
end

#run(name) ⇒ Object

[View source]

87
88
89
90
# File 'lib/gauntlet_parser.rb', line 87

def run(name)
  data[name] = parse(name)
  self.dirty = true
end

#should_skip?(name) ⇒ Boolean

Returns:

  • (Boolean)
[View source]

92
93
94
# File 'lib/gauntlet_parser.rb', line 92

def should_skip?(name)
  data[name] == {}
end

#shutdownObject

[View source]

103
104
105
106
107
108
109
110
# File 'lib/gauntlet_parser.rb', line 103

def shutdown
  super

  errors  = data.count { |_name, errs| errs != {} }
  total   = data.count
  percent = "%.5f" % [100 - errors.to_f / total * 100]
  puts "!!! was: #{@was_errors} now: #{errors} total: #{total} frac: #{percent}%"
end

#try(parser, ruby, file, show_ok: false) ⇒ Object

[View source]

12
13
14
15
16
17
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
# File 'lib/gauntlet_parser.rb', line 12

def try(parser, ruby, file, show_ok: false)
  try_ruby = lambda do |e|
    Process.spawn(%{#{ruby} -c #{Shellwords.escape file}},
                  :err => '/dev/null', :out => '/dev/null')
    _, status = Process.wait2

    if status.success?
      # Bug in Parser.
      puts "Parser bug."
      @result[file] = { parser.to_s => "#{e.class}: #{e.to_s}" }
    else
      # No, this file is not Ruby.
      yield if block_given?
    end
  end

  begin
    parser.parse_file(file)

  rescue Parser::SyntaxError => e
    if e.diagnostic.location.resize(2).is?('<%')
      puts "ERb."
      return
    end

    try_ruby.call(e)

  rescue ArgumentError, RegexpError,
         Encoding::UndefinedConversionError => e
    puts "#{file}: #{e.class}: #{e.to_s}"

    try_ruby.call(e)

  rescue Interrupt
    raise

  rescue Exception => e
    puts "Parser bug: #{file} #{e.class}: #{e.to_s}"
    @result[file] = { parser.to_s => "#{e.class}: #{e.to_s}" }

  else
    puts "Ok." if show_ok
  end
end