mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Simplify shebang detection
This commit is contained in:
@@ -5,45 +5,31 @@ 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 = tokens[1] if script == 'env'
|
||||
|
||||
script = script == 'env' ? tokens[1] : script
|
||||
# If script has an invalid shebang, we might get here
|
||||
return unless script
|
||||
|
||||
# If script has an invalid shebang, we might get here
|
||||
return unless script
|
||||
# "python2.6" -> "python2"
|
||||
script.sub! $1, '' if script =~ /(\.\d+)$/
|
||||
|
||||
# "python2.6" -> "python2"
|
||||
script.sub! $1, '' if script =~ /(\.\d+)$/
|
||||
|
||||
# Check for multiline shebang hacks that call `exec`
|
||||
if script == 'sh' &&
|
||||
lines[0...5].any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
|
||||
script = $1
|
||||
end
|
||||
|
||||
File.basename(script)
|
||||
else
|
||||
nil
|
||||
# Check for multiline shebang hacks that call `exec`
|
||||
if script == 'sh' &&
|
||||
@lines[0...5].any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
|
||||
script = $1
|
||||
end
|
||||
|
||||
File.basename(script)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user