Add --json option to bin/linguist

This commit is contained in:
Vadim Markovtsev
2016-06-15 18:47:39 +02:00
committed by Brandon Black
parent 210cd19876
commit e9ac71590f

View File

@@ -5,6 +5,7 @@
# #
require 'linguist' require 'linguist'
require 'rugged' require 'rugged'
require 'json'
path = ARGV[0] || Dir.pwd path = ARGV[0] || Dir.pwd
@@ -16,14 +17,17 @@ end
ARGV.shift ARGV.shift
breakdown = true if ARGV[0] == "--breakdown" breakdown = true if ARGV[0] == "--breakdown"
json_breakdown = true if ARGV[0] == "--json"
if File.directory?(path) if File.directory?(path)
rugged = Rugged::Repository.new(path) rugged = Rugged::Repository.new(path)
repo = Linguist::Repository.new(rugged, rugged.head.target_id) repo = Linguist::Repository.new(rugged, rugged.head.target_id)
repo.languages.sort_by { |_, size| size }.reverse.each do |language, size| if !json_breakdown
percentage = ((size / repo.size.to_f) * 100) repo.languages.sort_by { |_, size| size }.reverse.each do |language, size|
percentage = sprintf '%.2f' % percentage percentage = ((size / repo.size.to_f) * 100)
puts "%-7s %s" % ["#{percentage}%", language] percentage = sprintf '%.2f' % percentage
puts "%-7s %s" % ["#{percentage}%", language]
end
end end
if breakdown if breakdown
puts puts
@@ -35,6 +39,8 @@ if File.directory?(path)
end end
puts puts
end end
elsif json_breakdown
puts JSON.dump(repo.breakdown_by_file)
end end
elsif File.file?(path) elsif File.file?(path)
blob = Linguist::FileBlob.new(path, Dir.pwd) blob = Linguist::FileBlob.new(path, Dir.pwd)