Permalink
Browse files

Add a gem analyzer to fix GH-1

  • Loading branch information...
1 parent 62eb1b8 commit e2dc8773446b63ecf7e635fa0c69036141b1ded6 @ferrous26 committed Oct 15, 2011
Showing with 41 additions and 1 deletion.
  1. +14 −0 lib/rubygems-compile/analyzer.rb
  2. +13 −1 lib/rubygems-compile/compiler.rb
  3. +1 −0 lib/rubygems_plugin.rb
  4. +13 −0 test/test_analyzer.rb
@@ -0,0 +1,14 @@
+require 'ripper'
+
+class Gem::Analyzer < Ripper::SexpBuilder
+
+ class Warning < Exception
+ end
+
+ def on_kw token
+ if token == '__FILE__'
+ raise Warning, 'The __FILE__ keyword is used in this file'
+ end
+ end
+
+end
@@ -17,6 +17,8 @@ def call gem
say compilation_message if @config.verbose
gem_files.each do |file|
+ next if unsafe?(file)
+
say compile_file_msg(file) if @config.really_verbose
absolute_file_path = File.join(@spec.full_gem_path, file)
MacRuby::Compiler.new(
@@ -26,9 +28,19 @@ def call gem
).run
end
end
-
alias_method :compile, :call
+ ##
+ # Uses the GemAnalyzer class to determine if a given file might have
+ # any potential issues when compiled.
+
+ def unsafe? file
+ Gem::Analyzer.new(File.read(file)).parse
+ false
+ rescue Gem::Analyzer::Warning
+ true
+ end
+
def compilation_message
slash = @config.really_verbose ? '/' : ''
"Compiling #{@spec.full_name}#{slash}"
View
@@ -15,6 +15,7 @@
end
require 'rubygems-compile/common_methods'
+ require 'rubygems-compile/analyzer'
require 'rubygems-compile/compiler'
require 'rubygems-compile/uncompiler'
require 'rubygems-compile/commands'
View
@@ -0,0 +1,13 @@
+class TestGemAnalyzer < MiniTest::Unit::TestCase
+
+ def test_finds_usage_of___FILE__
+ assert_raises Gem::Analyzer::Warning do
+ Gem::Analyzer.new("__FILE__").parse
+ end
+
+ assert_block do
+ Gem::Analyzer.new("puts '__FILE__'").parse
+ end
+ end
+
+end

0 comments on commit e2dc877

Please sign in to comment.