Simplify shebang detection

This commit is contained in:
Brandon Keepers
2014-11-27 12:44:55 -05:00
parent fd85f7f112
commit cd3defda42

View File

@@ -5,27 +5,17 @@ module Linguist
Language.find_by_interpreter(new(blob.data).interpreter)
end
attr_reader :data
def initialize(data)
@data = data
@lines = data.lines
end
def interpreter
lines = data.lines.to_a
return unless match = /^#! ?(.*)$/.match(@lines.first)
if lines.any? && (match = lines[0].match(/(.+)\n?/)) && (bang = match[0]) =~ /^#!/
bang.sub!(/^#! /, '#!')
tokens = bang.split(' ')
pieces = tokens.first.split('/')
tokens = match[0].split(' ')
script = tokens.first.split('/').last
if pieces.size > 1
script = pieces.last
else
script = pieces.first.sub('#!', '')
end
script = script == 'env' ? tokens[1] : script
script = tokens[1] if script == 'env'
# If script has an invalid shebang, we might get here
return unless script
@@ -35,15 +25,11 @@ module Linguist
# Check for multiline shebang hacks that call `exec`
if script == 'sh' &&
lines[0...5].any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
@lines[0...5].any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
script = $1
end
File.basename(script)
else
nil
end
end
end
end