forked from tir38/android-lint-entropy-reducer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlint-up.rb
179 lines (137 loc) · 5.75 KB
/
lint-up.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
puts "=================== starting Android Lint Entropy Reducer ===================="
# ======================== SETUP ============================
# User name for git commits made by this script.
TRAVIS_GIT_USERNAME = String.new("Travis CI server")
# File name and relative path of generated Lint report. Must match build.gradle file:
# lintOptions {
# htmlOutput file("[FILE_NAME].html")
# }
LINT_REPORT_FILE = String.new("app/lint-report.html")
# File name and relative path of previous results of this script.
PREVIOUS_LINT_RESULTS_FILE=String.new("lint-report/lint-results.txt")
# Flag to evaluate warnings. true = check warnings; false = ignore warnings
CHECK_WARNINGS = true
# File name and relative path to custom lint rules; Can be null or "".
CUSTOM_LINT_FILE = String.new("")
# ================ SETUP DONE; DON'T TOUCH ANYTHING BELOW ================
require 'fileutils'
require 'pathname'
require 'open3'
# since we need the xml-simple gem, and we want this script self-contained, let's grab it just when we need it
begin
gem "xml-simple"
rescue LoadError
system("gem install xml-simple")
Gem.clear_paths
end
require 'xmlsimple'
# add custom Lint jar
if CUSTOM_LINT_FILE != nil &&
CUSTOM_LINT_FILE.length > 0
ENV["ANDROID_LINT_JARS"] = Dir.pwd + "/" + CUSTOM_LINT_FILE
puts "adding custom lint rules to default set: "
puts ENV["ANDROID_LINT_JARS"]
end
# run Lint
puts "running Lint..."
system './gradlew clean lint'
# confirm that Lint ran w/out error
result = $?.to_i
if result != 0
puts "FAIL: failed to run ./gradlew clean lint"
exit 1
end
# find Lint report file
lint_reports = Dir.glob(LINT_REPORT_FILE)
if lint_reports.length == 0
puts "Lint HTML report not found."
exit 1
end
lint_report = String.new(lint_reports[0])
# find error/warning count string in HTML report
error_warning_string = ""
File.open lint_report do |file|
error_warning_string = file.find { |line| line =~ /[0-9]* errors and [0-9]* warnings/ }
end
# find number of errors
error_string = error_warning_string.match(/[0-9]* errors/)[0]
current_error_count = error_string.match(/[0-9]*/)[0].to_i
puts "found errors: " + current_error_count.to_s
# find number of warnings
if CHECK_WARNINGS == true
warning_string = error_warning_string.match(/[0-9]* warnings/)[0]
current_warning_count = warning_string.match(/[0-9]*/)[0].to_i
puts "found warnings: " + current_warning_count.to_s
end
# get previous error and warning counts from last successful build
previous_results = false
previous_lint_reports = Dir.glob(PREVIOUS_LINT_RESULTS_FILE)
if previous_lint_reports == nil ||
previous_lint_reports.length == 0
previous_lint_report = File.new(PREVIOUS_LINT_RESULTS_FILE, "w") # create for writing to later
else
previous_lint_report = String.new(previous_lint_reports[0])
previous_error_warning_string = ""
File.open previous_lint_report do |file|
previous_error_warning_string = file.find { |line| line =~ /[0-9]* errors and [0-9]* warnings/ }
end
unless previous_error_warning_string.nil?
previous_results = true
previous_error_string = previous_error_warning_string.match(/[0-9]* errors/)[0]
previous_error_count = previous_error_string.match(/[0-9]*/)[0].to_i
puts "previous errors: " + previous_error_count.to_s
if CHECK_WARNINGS == true
previous_warning_string = previous_error_warning_string.match(/[0-9]* warnings/)[0]
previous_warning_count = previous_warning_string.match(/[0-9]*/)[0].to_i
puts "previous warnings: " + previous_warning_count.to_s
end
end
end
# compare previous error count with current error count
if previous_results == true &&
current_error_count > previous_error_count
puts "FAIL: error count increased"
exit 1
end
# compare previous warning count with current warning count
if CHECK_WARNINGS == true &&
previous_results == true &&
current_warning_count > previous_warning_count
puts "FAIL: warning count increased"
exit 1
end
# check if warning and error count stayed the same
if previous_results == true &&
current_error_count == previous_error_count &&
current_warning_count == previous_warning_count
puts "SUCCESS: count stayed the same"
exit 0
end
# either error count or warning count DECREASED
# write new results to file (will overwrite existing, or create new)
File.write(previous_lint_report, "DO NOT TOUCH; GENERATED BY TRAVIS\n" + error_warning_string)
# push changes to github (if this script is run locally, we don't want to overwrite git username and email, so save temporarily)
previous_git_username, st = Open3.capture2('git config user.name')
previous_git_username = previous_git_username.strip
previous_git_email, st = Open3.capture3('git config user.email')
previous_git_email = previous_git_email.strip
# update git user name and email for this script
system ("git config --local user.name '" + TRAVIS_GIT_USERNAME + "'")
system ("git config --local user.email '.'") # set email blank
# add previous Lint result file to git
system ('git add ' + PREVIOUS_LINT_RESULTS_FILE)
# Travis has git in a detached head here. Let's get on the right branch.
travis_branch_name = ENV['TRAVIS_BRANCH']
if travis_branch_name != nil
system ('git checkout ' + travis_branch_name)
end
# commit changes; Add "skip ci" so that we don't accidentally trigger another Travis build
system ('git commit -m "Travis: update Lint results to reflect reduced error/warning count [skip ci]" ')
# push to origin
system("git config push.default simple")
system ("git push")
# restore previous git user name and email
system("git config --local user.name '#{previous_git_username}'")
system("git config --local user.email '#{previous_git_email}'")
puts "SUCCESS: count was reduced"
exit 0 # success