mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Don't bother creating an instance
This commit is contained in:
@@ -117,7 +117,7 @@ module Linguist
|
||||
|
||||
# Used to retrieve the interpreter from the shebang line of a file's data.
|
||||
def self.interpreter_from_shebang(data)
|
||||
Shebang.new(data).interpreter
|
||||
Shebang.interpreter(data)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
@@ -2,15 +2,12 @@ module Linguist
|
||||
# Check if there's a shebang line and use that as authoritative
|
||||
class Shebang
|
||||
def self.call(blob, _)
|
||||
Language.find_by_interpreter(new(blob.data).interpreter)
|
||||
Language.find_by_interpreter interpreter(blob.data)
|
||||
end
|
||||
|
||||
def initialize(data)
|
||||
@lines = data.lines
|
||||
end
|
||||
|
||||
def interpreter
|
||||
return unless match = /^#! ?(.*)$/.match(@lines.first)
|
||||
def self.interpreter(data)
|
||||
lines = data.lines
|
||||
return unless match = /^#! ?(.*)$/.match(lines.first)
|
||||
|
||||
tokens = match[0].split(' ')
|
||||
script = tokens.first.split('/').last
|
||||
@@ -25,7 +22,7 @@ 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
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ class TestShebang < Test::Unit::TestCase
|
||||
include Linguist
|
||||
|
||||
def assert_interpreter(interpreter, body)
|
||||
assert_equal interpreter, Shebang.new(body).interpreter
|
||||
assert_equal interpreter, Shebang.interpreter(body)
|
||||
end
|
||||
|
||||
def test_shebangs
|
||||
@@ -17,14 +17,14 @@ class TestShebang < Test::Unit::TestCase
|
||||
assert_interpreter nil, " #!/usr/sbin/ruby"
|
||||
assert_interpreter nil, "\n#!/usr/sbin/ruby"
|
||||
|
||||
assert_interpreter "ruby", "#!/usr/sbin/ruby\n# bar",
|
||||
assert_interpreter "ruby", "#!/usr/bin/ruby\n# foo",
|
||||
assert_interpreter "ruby", "#!/usr/sbin/ruby",
|
||||
assert_interpreter "ruby", "#!/usr/sbin/ruby\n# bar"
|
||||
assert_interpreter "ruby", "#!/usr/bin/ruby\n# foo"
|
||||
assert_interpreter "ruby", "#!/usr/sbin/ruby"
|
||||
assert_interpreter "ruby", "#!/usr/sbin/ruby foo bar baz\n"
|
||||
|
||||
assert_interpreter "Rscript", "#!/usr/bin/env Rscript\n# example R script\n#\n"
|
||||
assert_interpreter "crystal", "#!/usr/bin/env bin/crystal"
|
||||
assert_interpreter "ruby", "#!/usr/bin/env ruby\n# baz",
|
||||
assert_interpreter "ruby", "#!/usr/bin/env ruby\n# baz"
|
||||
|
||||
assert_interpreter "bash", "#!/usr/bin/bash\n"
|
||||
assert_interpreter "sh", "#!/bin/sh"
|
||||
|
||||
Reference in New Issue
Block a user