-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit_stats.rb
More file actions
80 lines (68 loc) · 2 KB
/
git_stats.rb
File metadata and controls
80 lines (68 loc) · 2 KB
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
class GitStats
def self.get_file_stats(file_path, file_stats)
Dir.foreach(file_path) do |entry|
next if nativation_element?(entry)
extenstion = get_extension(entry)
if File.directory?(file_path + entry)
next if directory_to_ignore?(entry)
get_file_stats(file_path + entry + "/")
elsif extenstion_valid?(extenstion)
stats = compile_statistics(file_path, entry)
assign_statistics(file_stats, extenstion, stats)
end
end
file_stats
end
def self.nativation_element?(dir)
(dir == '.' || dir == '..' || dir[0] == '.')
end
def self.empty?(line)
!(line =~ /[a-z]/)
end
def self.directory_to_ignore?(directory)
["lib"].include?(directory?)
end
def self.ignore_extenstion?(extenstion)
['log'].include?(extenstion)
end
def self.get_extension(entry)
return 'none' if !entry.include?('.')
index = entry.size - entry.reverse.index('.')
entry[index, entry.size]
end
def self.extenstion_valid?(ext)
ext != nil && !ignore_extenstion?(ext)
end
def self.compile_statistics(file_path, entry)
begin
count_lines_of_code(file_path + entry)
rescue
return nil
end
end
def self.count_lines_of_code(file_name)
empty, code, comments = 0, 0, 0
puts file_name
File.open(file_name).each do |line|
if empty?(line)
empty += 1
elsif line.strip[0] == '#'
comments += 1
else
code += 1
end
end
{ :empty => empty, :code => code, :comments => comments }
end
def self.assign_statistics(file_stats, extenstion, loc)
return file_stats if loc == nil
if file_stats[extenstion] == nil
file_stats[extenstion] = loc
else
file_stats[extenstion] = { :empty => file_stats[extenstion][:empty] + loc[:empty],
:code => file_stats[extenstion][:code] + loc[:code],
:comments => file_stats[extenstion][:comments] + loc[:comments] }
end
file_stats
end
end