-
-
Notifications
You must be signed in to change notification settings - Fork 265
/
Copy pathlinguist.rb
103 lines (94 loc) · 2.38 KB
/
linguist.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env ruby
$LOAD_PATH[0, 0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'linguist'
require 'rugged'
require 'json'
require 'optparse'
path = "."
breakdown = false
json_breakdown = false
target_languages = []
if ARGV[0] == "--breakdown" || ARGV[0] == "--json"
path = Dir.pwd
else
path = ARGV[0] || "."
ARGV.shift
end
if ARGV[0] == "--breakdown"
breakdown = true
ARGV.shift
end
if ARGV[0] == "--json"
json_breakdown = true
ARGV.shift
end
if ARGV[0]
if ARGV[1]
target_languages = ARGV
else
target_languages = ARGV[0].split(/[ ,]/)
end
target_languages.map! &:downcase
end
if File.directory?(path)
rugged = Rugged::Repository.new(path)
repo = Linguist::Repository.new(rugged, rugged.head.target_id)
if !json_breakdown && target_languages.empty?
repo.languages.sort_by { |_, size| size }.reverse.each do |language, size|
percentage = ((size / repo.size.to_f) * 100)
percentage = sprintf '%.2f' % percentage
puts "%-7s %9s %s" % ["#{percentage}%", "#{size}B", language]
end
end
if target_languages.any?
repo.breakdown_by_file.each do |lang, files|
if target_languages.include? lang.downcase
files.each do |file|
puts file
end
end
end
elsif breakdown
puts
file_breakdown = repo.breakdown_by_file
file_breakdown.each do |lang, files|
puts "#{lang}:"
files.each do |file|
puts file
end
puts
end
elsif json_breakdown
puts JSON.dump(repo.breakdown_by_file)
end
elsif File.file?(path)
blob = Linguist::FileBlob.new(path, Dir.pwd)
type = if blob.text?
'Text'
elsif blob.image?
'Image'
else
'Binary'
end
puts "#{blob.name}: #{blob.loc} lines (#{blob.sloc} sloc)"
puts " type: #{type}"
puts " mime type: #{blob.mime_type}"
puts " language: #{blob.language}"
if blob.large?
puts " blob is too large to be shown"
end
if blob.generated?
puts " appears to be generated source code"
end
if blob.vendored?
puts " appears to be a vendored file"
end
else
abort <<-HELP
Linguist v#{Linguist::VERSION}
Detect language type for a file, or, given a repository, determine language breakdown.
Usage: linguist <path> [...target language list]
linguist <path> [--breakdown] [--json] [...target language list]
linguist [--breakdown] [--json] [...target language list]
HELP
end