forked from benbalter/count-org-loc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loc.rb
61 lines (47 loc) · 1.65 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
55
56
57
58
59
60
61
# frozen_string_literal: true
require 'octokit'
require 'open3'
require 'cliver'
require 'fileutils'
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
begin
repos = client.organization_repositories(ARGV[0].strip, type: 'sources')
rescue StandardError
repos = client.repositories(ARGV[0].strip, type: 'sources')
end
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.zero?
_output, _status = cloc destination, '--quiet', "--report-file=#{report_file}"
reports.push(report_file) if File.exist?(report_file) && status.exitstatus.zero?
end
puts 'Done. Summing...'
output, _status = cloc '--sum-reports', *reports
puts output.gsub(%r{^#{Regexp.escape tmp_dir}/(.*)\.txt}) { Regexp.last_match(1) + ' ' * (tmp_dir.length + 5) }