Permalink
Browse files

Making parsers auto compile on spec suite and load in production code

  • Loading branch information...
1 parent 60950a9 commit 56c3e4654e2abd192818b3dee7e2b6181cd420ab @mikel committed Nov 18, 2012
Showing with 45 additions and 18 deletions.
  1. +41 −0 lib/load_parsers.rb
  2. +1 −17 lib/mail.rb
  3. +3 −1 spec/spec_helper.rb
View
@@ -0,0 +1,41 @@
+# encoding: utf-8
+# This file loads up the parsers for mail to use. It also will attempt to compile parsers
+# if they don't exist.
+#
+# It also only uses the compiler if we are running the SPEC suite
+module Mail # :doc:
+ require 'treetop/runtime'
+
+ def self.compile_parser(parser)
+ require 'treetop/compiler'
+ STDOUT.puts "Compiling parser #{parser} from treetop source"
+ Treetop.load(File.join(File.dirname(__FILE__)) + "/mail/parsers/#{parser}")
+ end
+
+ parsers = %w[ rfc2822_obsolete rfc2822 address_lists phrase_lists
+ date_time received message_ids envelope_from rfc2045
+ mime_version content_type content_disposition
+ content_transfer_encoding content_location ]
+
+ if defined?(MAIL_SPEC_SUITE_RUNNING)
+ STDOUT.puts "Compiling all parsers from treetop source"
+
+ parsers.each do |parser|
+ compile_parser(parser)
+ end
+
+ else
+ STDOUT.puts "Loading precompiled parsers from ruby source"
+
+ parsers.each do |parser|
+ begin
+ require "mail/parsers/#{parser}"
+ rescue LoadError
+ STDOUT.puts "Couldn't load parser #{parser} from ruby source"
+ compile_parser(parser)
+ end
+ end
+
+ end
+
+end
View
@@ -77,23 +77,7 @@ def self.eager_autoload!
require 'mail/envelope'
- parsers = %w[ rfc2822_obsolete rfc2822 address_lists phrase_lists
- date_time received message_ids envelope_from rfc2045
- mime_version content_type content_disposition
- content_transfer_encoding content_location ]
-
- parsers.each do |parser|
- begin
- # Try requiring the pre-compiled ruby version first
- require 'treetop/runtime'
- require "mail/parsers/#{parser}"
- rescue LoadError
- # Otherwise, get treetop to compile and load it
- require 'treetop/runtime'
- require 'treetop/compiler'
- Treetop.load(File.join(File.dirname(__FILE__)) + "/mail/parsers/#{parser}")
- end
- end
+ require 'load_parsers'
# Autoload header field elements and transfer encodings.
require 'mail/elements'
View
@@ -13,6 +13,9 @@
require 'rspec'
require File.join(File.dirname(__FILE__), 'matchers', 'break_down_to')
+# Used to force compile all the parsers on each spec suite load
+MAIL_SPEC_SUITE_RUNNING = true
+
require 'mail'
STDERR.puts("Running Specs for Mail Version #{Mail::VERSION::STRING}")
@@ -266,4 +269,3 @@ def self.new(*args)
MockIMAP.new
end
end
-

0 comments on commit 56c3e46

Please sign in to comment.