mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Detect language from shebang script
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
require 'linguist/language'
|
||||||
require 'linguist/mime'
|
require 'linguist/mime'
|
||||||
require 'linguist/pathname'
|
require 'linguist/pathname'
|
||||||
|
|
||||||
@@ -77,5 +78,65 @@ module Linguist
|
|||||||
def viewable?
|
def viewable?
|
||||||
!file? && !large?
|
!file? && !large?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def language
|
||||||
|
if text?
|
||||||
|
shebang_language || name.language
|
||||||
|
else
|
||||||
|
Language['Text']
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def lexer
|
||||||
|
language.lexer
|
||||||
|
end
|
||||||
|
|
||||||
|
def shebang_script
|
||||||
|
return if !text? || large?
|
||||||
|
|
||||||
|
if (match = data.match(/(.+)\n?/)) && (bang = match[0]) =~ /^#!/
|
||||||
|
bang.sub!(/^#! /, '#!')
|
||||||
|
tokens = bang.split(' ')
|
||||||
|
pieces = tokens.first.split('/')
|
||||||
|
if pieces.size > 1
|
||||||
|
script = pieces.last
|
||||||
|
else
|
||||||
|
script = pieces.first.sub('#!', '')
|
||||||
|
end
|
||||||
|
|
||||||
|
script = script == 'env' ? tokens[1] : script
|
||||||
|
|
||||||
|
# python2.4 => python
|
||||||
|
if script =~ /((?:\d+\.?)+)/
|
||||||
|
script.sub! $1, ''
|
||||||
|
end
|
||||||
|
|
||||||
|
script
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def shebang_language
|
||||||
|
if script = shebang_script
|
||||||
|
case script
|
||||||
|
when 'bash'
|
||||||
|
Language['Shell']
|
||||||
|
when 'groovy'
|
||||||
|
Language['Java']
|
||||||
|
when 'macruby'
|
||||||
|
Language['Ruby']
|
||||||
|
when 'node'
|
||||||
|
Language['JavaScript']
|
||||||
|
when 'rake'
|
||||||
|
Language['Ruby']
|
||||||
|
when 'sh'
|
||||||
|
Language['Shell']
|
||||||
|
when 'zsh'
|
||||||
|
Language['Shell']
|
||||||
|
else
|
||||||
|
lang = Language.find_by_lexer(shebang_script)
|
||||||
|
lang != Language['Text'] ? lang : nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
3
test/fixtures/blob/Capfile
vendored
Normal file
3
test/fixtures/blob/Capfile
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
load 'deploy'
|
||||||
|
Dir['vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
|
||||||
|
load 'config/deploy'
|
||||||
2
test/fixtures/blob/README
vendored
Normal file
2
test/fixtures/blob/README
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
README
|
||||||
|
======
|
||||||
3
test/fixtures/blob/Rakefile
vendored
Normal file
3
test/fixtures/blob/Rakefile
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
task :default do
|
||||||
|
puts "Rake"
|
||||||
|
end
|
||||||
4
test/fixtures/blob/dude-thing-okay--001.patch
vendored
Normal file
4
test/fixtures/blob/dude-thing-okay--001.patch
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
diff --git a/lib/linguist.rb b/lib/linguist.rb
|
||||||
|
index d472341..8ad9ffb 100644
|
||||||
|
--- a/lib/linguist.rb
|
||||||
|
+++ b/lib/linguist.rb
|
||||||
1
test/fixtures/blob/dude.el
vendored
Normal file
1
test/fixtures/blob/dude.el
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
(print "Dude!")
|
||||||
1
test/fixtures/blob/dude.js
vendored
Normal file
1
test/fixtures/blob/dude.js
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
alert("dude!")
|
||||||
2
test/fixtures/blob/grit.rb
vendored
Normal file
2
test/fixtures/blob/grit.rb
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
module Grit
|
||||||
|
end
|
||||||
2
test/fixtures/blob/script.bash
vendored
Normal file
2
test/fixtures/blob/script.bash
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
echo "bash"
|
||||||
2
test/fixtures/blob/script.foo
vendored
Normal file
2
test/fixtures/blob/script.foo
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/foo
|
||||||
|
???
|
||||||
2
test/fixtures/blob/script.groovy
vendored
Normal file
2
test/fixtures/blob/script.groovy
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#!/usr/bin/env groovy
|
||||||
|
println "Groovy!"
|
||||||
2
test/fixtures/blob/script.js
vendored
Normal file
2
test/fixtures/blob/script.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
console.log("Node")
|
||||||
2
test/fixtures/blob/script.mrb
vendored
Normal file
2
test/fixtures/blob/script.mrb
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#!/usr/bin/env macruby
|
||||||
|
puts "MacRuby"
|
||||||
2
test/fixtures/blob/script.pl
vendored
Normal file
2
test/fixtures/blob/script.pl
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#!/usr/local/bin/perl
|
||||||
|
print "Perl\n"
|
||||||
2
test/fixtures/blob/script.py
vendored
Normal file
2
test/fixtures/blob/script.py
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#!/usr/bin/env python2.4
|
||||||
|
print "Python"
|
||||||
4
test/fixtures/blob/script.rake
vendored
Normal file
4
test/fixtures/blob/script.rake
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/usr/bin/env rake
|
||||||
|
task :default do
|
||||||
|
puts "Rake"
|
||||||
|
end
|
||||||
2
test/fixtures/blob/script.rb
vendored
Normal file
2
test/fixtures/blob/script.rb
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#!/usr/bin/env ruby
|
||||||
|
puts "Ruby"
|
||||||
2
test/fixtures/blob/script.sh
vendored
Normal file
2
test/fixtures/blob/script.sh
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
echo "sh"
|
||||||
2
test/fixtures/blob/script.zsh
vendored
Normal file
2
test/fixtures/blob/script.zsh
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/zsh
|
||||||
|
echo "zsh"
|
||||||
2
test/fixtures/blob/script2.rb
vendored
Normal file
2
test/fixtures/blob/script2.rb
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
#! /usr/bin/env ruby -w -Ilib:test
|
||||||
|
echo "Ruby"
|
||||||
3
test/fixtures/blob/subdir/Rakefile
vendored
Normal file
3
test/fixtures/blob/subdir/Rakefile
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
task :default do
|
||||||
|
puts "Rake (subdir)"
|
||||||
|
end
|
||||||
@@ -92,4 +92,53 @@ class TestBlob < Test::Unit::TestCase
|
|||||||
assert blob("octocat.gif").image?
|
assert blob("octocat.gif").image?
|
||||||
assert !blob("octocat.psd").image?
|
assert !blob("octocat.psd").image?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_language
|
||||||
|
assert_equal Language['Ruby'], blob("foo.rb").language
|
||||||
|
assert_equal Language['Ruby'], blob("script.rb").language
|
||||||
|
assert_equal Language['Text'], blob("octocat.png").language
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_lexer
|
||||||
|
assert_equal 'ruby', blob("grit.rb").lexer
|
||||||
|
assert_equal 'text', blob("README").lexer
|
||||||
|
assert_equal 'diff', blob("dude-thing-okay--001.patch").lexer
|
||||||
|
assert_equal 'scheme', blob("dude.el").lexer
|
||||||
|
assert_equal 'javascript', blob("dude.js").lexer
|
||||||
|
assert_equal 'ruby', blob("Capfile").lexer
|
||||||
|
|
||||||
|
assert_equal 'ruby', blob("Rakefile").lexer
|
||||||
|
assert_equal 'ruby', blob("subdir/Rakefile").lexer
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_shebang_script
|
||||||
|
assert_equal 'sh', blob("script.sh").shebang_script
|
||||||
|
assert_equal 'bash', blob("script.bash").shebang_script
|
||||||
|
assert_equal 'zsh', blob("script.zsh").shebang_script
|
||||||
|
assert_equal 'perl', blob("script.pl").shebang_script
|
||||||
|
assert_equal 'ruby', blob("script.rb").shebang_script
|
||||||
|
assert_equal 'ruby', blob("script2.rb").shebang_script
|
||||||
|
assert_equal 'python', blob("script.py").shebang_script
|
||||||
|
assert_equal 'node', blob("script.js").shebang_script
|
||||||
|
assert_equal 'groovy', blob("script.groovy").shebang_script
|
||||||
|
assert_equal 'macruby', blob("script.mrb").shebang_script
|
||||||
|
assert_equal 'rake', blob("script.rake").shebang_script
|
||||||
|
assert_equal 'foo', blob("script.foo").shebang_script
|
||||||
|
assert_equal nil, blob("foo.rb").shebang_script
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_shebang_language
|
||||||
|
assert_equal Language['Shell'], blob("script.sh").shebang_language
|
||||||
|
assert_equal Language['Shell'], blob("script.bash").shebang_language
|
||||||
|
assert_equal Language['Shell'], blob("script.zsh").shebang_language
|
||||||
|
assert_equal Language['Perl'], blob("script.pl").shebang_language
|
||||||
|
assert_equal Language['Ruby'], blob("script.rb").shebang_language
|
||||||
|
assert_equal Language['Python'], blob("script.py").shebang_language
|
||||||
|
assert_equal Language['JavaScript'], blob("script.js").shebang_language
|
||||||
|
assert_equal Language['Java'], blob("script.groovy").shebang_language
|
||||||
|
assert_equal Language['Ruby'], blob("script.mrb").shebang_language
|
||||||
|
assert_equal Language['Ruby'], blob("script.rake").shebang_language
|
||||||
|
assert_equal nil, blob("script.foo").shebang_language
|
||||||
|
assert_equal nil, blob("foo.rb").shebang_language
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user