Files
linguist/bin/linguist
Paul Chaignon 4e0b5f02aa Fix usage line in binary (#3564)
Linguist cannot work on any directory; it needs to be a Git
repository.
2017-04-20 10:18:03 +01:00

85 lines
2.0 KiB
Ruby
Executable File

#!/usr/bin/env ruby
$LOAD_PATH[0, 0] = File.join(File.dirname(__FILE__), '..', 'lib')
require 'linguist'
require 'rugged'
require 'json'
require 'optparse'
path = ARGV[0] || Dir.pwd
# special case if not given a directory
# but still given the --breakdown or --json options/
if path == "--breakdown"
path = Dir.pwd
breakdown = true
elsif path == "--json"
path = Dir.pwd
json_breakdown = true
end
ARGV.shift
breakdown = true if ARGV[0] == "--breakdown"
json_breakdown = true if ARGV[0] == "--json"
if File.directory?(path)
rugged = Rugged::Repository.new(path)
repo = Linguist::Repository.new(rugged, rugged.head.target_id)
if !json_breakdown
repo.languages.sort_by { |_, size| size }.reverse.each do |language, size|
percentage = ((size / repo.size.to_f) * 100)
percentage = sprintf '%.2f' % percentage
puts "%-7s %s" % ["#{percentage}%", language]
end
end
if 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>
linguist <path> [--breakdown] [--json]
linguist [--breakdown] [--json]
HELP
end