mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-12-29 21:31:01 +00:00
Simplify shebang detection
This commit is contained in:
@@ -5,27 +5,17 @@ module Linguist
|
|||||||
Language.find_by_interpreter(new(blob.data).interpreter)
|
Language.find_by_interpreter(new(blob.data).interpreter)
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :data
|
|
||||||
|
|
||||||
def initialize(data)
|
def initialize(data)
|
||||||
@data = data
|
@lines = data.lines
|
||||||
end
|
end
|
||||||
|
|
||||||
def interpreter
|
def interpreter
|
||||||
lines = data.lines.to_a
|
return unless match = /^#! ?(.*)$/.match(@lines.first)
|
||||||
|
|
||||||
if lines.any? && (match = lines[0].match(/(.+)\n?/)) && (bang = match[0]) =~ /^#!/
|
tokens = match[0].split(' ')
|
||||||
bang.sub!(/^#! /, '#!')
|
script = tokens.first.split('/').last
|
||||||
tokens = bang.split(' ')
|
|
||||||
pieces = tokens.first.split('/')
|
|
||||||
|
|
||||||
if pieces.size > 1
|
script = tokens[1] if script == 'env'
|
||||||
script = pieces.last
|
|
||||||
else
|
|
||||||
script = pieces.first.sub('#!', '')
|
|
||||||
end
|
|
||||||
|
|
||||||
script = script == 'env' ? tokens[1] : script
|
|
||||||
|
|
||||||
# If script has an invalid shebang, we might get here
|
# If script has an invalid shebang, we might get here
|
||||||
return unless script
|
return unless script
|
||||||
@@ -35,15 +25,11 @@ module Linguist
|
|||||||
|
|
||||||
# Check for multiline shebang hacks that call `exec`
|
# Check for multiline shebang hacks that call `exec`
|
||||||
if script == 'sh' &&
|
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
|
script = $1
|
||||||
end
|
end
|
||||||
|
|
||||||
File.basename(script)
|
File.basename(script)
|
||||||
else
|
|
||||||
nil
|
|
||||||
end
|
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user