This repository has been archived by the owner on Aug 12, 2019. It is now read-only.
forked from benbalter/count-org-loc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathloc.rb
54 lines (41 loc) · 1.49 KB
/
loc.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
require 'octokit'
require 'open3'
require 'cliver'
require 'dotenv'
if ARGV.count != 1
puts "Usage: script/count [ORG NAME]"
exit 1
end
Dotenv.load
def cloc(*args)
cloc_path = Cliver.detect! 'cloc'
Open3.capture2e(cloc_path, *args)
end
tmp_dir = File.expand_path "./tmp", File.dirname(__FILE__)
FileUtils.rm_rf tmp_dir
FileUtils.mkdir_p tmp_dir
# Enabling support for GitHub Enterprise
unless ENV["GITHUB_ENTERPRISE_URL"].nil?
Octokit.configure do |c|
c.api_endpoint = ENV["GITHUB_ENTERPRISE_URL"]
end
end
client = Octokit::Client.new access_token: ENV["GITHUB_TOKEN"]
client.auto_paginate = true
repos = client.organization_repositories(ARGV[0].strip, type: 'sources')
puts "Found #{repos.count} repos. Counting..."
reports = []
repos.each do |repo|
puts "Counting #{repo.name}..."
destination = File.expand_path repo.name, tmp_dir
report_file = File.expand_path "#{repo.name}.txt", tmp_dir
clone_url = repo.clone_url
clone_url = clone_url.sub "//", "//#{ENV["GITHUB_TOKEN"]}:x-oauth-basic@" if ENV["GITHUB_TOKEN"]
output, status = Open3.capture2e "git", "clone", "--depth", "1", "--quiet", clone_url, destination
next unless status.exitstatus == 0
output, status = cloc destination, "--quiet", "--report-file=#{report_file}"
reports.push(report_file) if File.exists?(report_file) && status.exitstatus == 0
end
puts "Done. Summing..."
output, status = cloc "--sum-reports", *reports
puts output.gsub(/^#{Regexp.escape tmp_dir}\/(.*)\.txt/) { $1 + " " * (tmp_dir.length + 5) }