forked from Revolutionary-Games/Thrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_formatting.rb
executable file
·423 lines (333 loc) · 9.03 KB
/
check_formatting.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/env ruby
# frozen_string_literal: true
# This script first builds using msbuild treating warnings as errors
# and then runs some custom line length checks
require 'optparse'
require 'find'
require 'digest'
require 'nokogiri'
require_relative 'bootstrap_rubysetupsystem'
require_relative 'RubySetupSystem/RubyCommon'
require_relative 'scripts/fast_build/toggle_analysis_lib'
MAX_LINE_LENGTH = 120
VALID_CHECKS = %w[compile files inspectcode cleanupcode].freeze
DEFAULT_CHECKS = %w[compile files inspectcode cleanupcode].freeze
ONLY_FILE_LIST = 'files_to_check.txt'
OUTPUT_MUTEX = Mutex.new
@options = {
checks: DEFAULT_CHECKS,
skip_file_types: [],
parallel: true
}
OptionParser.new do |opts|
opts.banner = "Usage: #{$PROGRAM_NAME} [options]"
opts.on('-c', '--checks check1,check2', Array,
'Select checks to do. Default is all') do |checks|
@options[:checks] = checks
end
opts.on('-s', '--skip filetype1,filetype2', Array,
'Skips files checks on the specified types') do |skip|
@options[:skip_file_types] = skip
end
opts.on('-p', '--[no-]parallel', 'Run different checks in parallel (default)') do |b|
@options[:parallel] = b
end
end.parse!
onError "Unhandled parameters: #{ARGV}" unless ARGV.empty?
info "Starting formatting checks with the following checks: #{@options[:checks]}"
# Helper functions
def ide_file?(path)
path =~ %r{/\.vs/} || path =~ %r{/\.idea/}
end
def explicitly_ignored?(path)
path =~ %r{/ThirdParty/}i || path =~ /GlobalSuppressions.cs/ || path =~ %r{/RubySetupSystem/}
end
def cache?(path)
path =~ %r{/\.mono/} || path =~ %r{/\.import/} || path =~ %r{/builds/} || path =~ %r{/\.git/}
end
# Skip some files that would otherwise be processed
def skip_file?(path)
explicitly_ignored?(path) || path =~ %r{^\.\/\.\/} || cache?(path) || ide_file?(path)
end
def file_type_skipped?(path)
if @options[:skip_file_types].include? File.extname(path)[1..-1]
OUTPUT_MUTEX.synchronize do
puts "Skipping file '#{path}'"
end
true
else
false
end
end
# Detects if there is a file telling which files to check. Returns nil otherwise
def files_to_include
return nil unless File.exist? ONLY_FILE_LIST
includes = []
File.foreach(ONLY_FILE_LIST).with_index do |line, _num|
next unless line
file = line.strip
next if file.empty?
includes.append file
end
includes
end
@includes = files_to_include
def includes_changes_to(type)
return false if @includes.nil?
@includes.each do |file|
return true if file.end_with? type
end
false
end
def process_file?(filepath)
if !@includes
true
else
filepath = filepath.sub './', ''
@includes.each do |file|
return true if filepath.end_with? file
end
false
end
end
# Different handle functions for file checks
def handle_gd_file(_path)
OUTPUT_MUTEX.synchronize do
error 'GD scripts should not exist'
end
true
end
def handle_cs_file(path)
errors = false
original = File.read(path)
line_number = 0
OUTPUT_MUTEX.synchronize do
original.each_line do |line|
line_number += 1
if line.include? "\t"
error "Line #{line_number} contains a tab"
errors = true
end
if !OS.windows? && line.include?("\r\n")
error "Line #{line_number} contains a windows style line ending (CR LF)"
errors = true
end
# For some reason this reports 1 too high
length = line.length - 1
if length > MAX_LINE_LENGTH
error "Line #{line_number} is too long. #{length} > #{MAX_LINE_LENGTH}"
errors = true
end
end
end
errors
end
def handle_json_file(path)
digest_before = Digest::MD5.hexdigest File.read(path)
if runSystemSafe('jsonlint', '-i', path, '--indent', ' ') != 0
OUTPUT_MUTEX.synchronize do
error 'JSONLint failed on file'
end
return true
end
digest_after = Digest::MD5.hexdigest File.read(path)
if digest_before != digest_after
OUTPUT_MUTEX.synchronize do
error 'JSONLint made formatting changes'
end
true
else
false
end
end
def handle_shader_file(path)
errors = false
File.foreach(path).with_index do |line, line_number|
if line.include? "\t"
OUTPUT_MUTEX.synchronize do
error "Line #{line_number + 1} contains a tab"
errors = true
end
end
# For some reason this reports 1 too high
length = line.length - 1
if length > MAX_LINE_LENGTH
OUTPUT_MUTEX.synchronize do
error "Line #{line_number + 1} is too long. #{length} > #{MAX_LINE_LENGTH}"
errors = true
end
end
end
errors
end
def handle_csproj_file(path)
errors = false
data = File.read(path, encoding: 'utf-8')
unless data.start_with? '<?xml'
OUTPUT_MUTEX.synchronize do
error "File doesn't start with '<?xml' likely due to added BOM"
errors = true
end
end
# This next check is a bit problematic on Windows so it is skipped
return errors if OS.windows?
unless data.end_with? "\n"
OUTPUT_MUTEX.synchronize do
error "File doesn't end with a new line"
errors = true
end
end
errors
end
# Forwards the file handling to a specific handler function if
# something should be done with the file type
def handle_file(path)
return false if file_type_skipped?(path) || !process_file?(path)
if path =~ /\.gd$/
handle_gd_file path
elsif path =~ /\.cs$/
handle_cs_file path
elsif path =~ %r{simulation_parameters/.*\.json$}
handle_json_file path
elsif path =~ /\.shader$/
handle_shader_file path
elsif path =~ /\.csproj$/
handle_csproj_file path
else
false
end
end
# Run functions for the specific checks
def run_compile
# Make sure in analysis mode before running build
perform_analysis_mode_check true, quiet: true
status, output = runOpen3CaptureOutput('msbuild', 'Thrive.sln', '/t:Clean,Build',
'/warnaserror')
if status != 0
OUTPUT_MUTEX.synchronize do
info 'Build output from msbuild:'
puts output
error "\nBuild generated warnings or errors."
end
exit 1
end
end
def run_files
issues_found = false
Find.find('.') do |path|
# path = path[2..-1]
next if skip_file? path
begin
if handle_file path
OUTPUT_MUTEX.synchronize do
puts 'Problems found in file (see above): ' + path
puts ''
end
issues_found = true
end
rescue StandardError => e
OUTPUT_MUTEX.synchronize do
puts 'Failed to handle path: ' + path
puts 'Error: ' + e.message
end
raise e
end
end
return unless issues_found
OUTPUT_MUTEX.synchronize do
error 'Code format issues detected'
end
exit 2
end
def inspect_code_executable
# TODO: 32 bit support if needed
if OS.windows?
'inspectcode.exe'
else
'inspectcode.sh'
end
end
def skip_jetbrains?
if @includes && !includes_changes_to('.cs')
OUTPUT_MUTEX.synchronize do
info 'No changes to be checked for .cs files'
end
return true
end
false
end
def run_inspect_code
return if skip_jetbrains?
params = [inspect_code_executable, 'Thrive.sln', '-o=inspect_results.xml']
params.append "--include=#{@includes.join(';')}" if @includes
runOpen3Checked(*params)
issues_found = false
doc = Nokogiri::XML(File.open('inspect_results.xml'), &:norecover)
issue_types = {}
doc.xpath('//IssueType').each do |node|
issue_types[node['Id']] = node
end
doc.xpath('//Issue').each do |issue|
type = issue_types[issue['TypeId']]
next if type['Severity'] == 'SUGGESTION'
issues_found = true
OUTPUT_MUTEX.synchronize do
error "#{issue['File']}:#{issue['Line']} #{issue['Message']} type: #{issue['TypeId']}"
end
end
return unless issues_found
OUTPUT_MUTEX.synchronize do
error 'Code inspection detected issues, see inspect_results.xml'
end
exit 2
end
def cleanup_code_executable
# TODO: 32 bit support if needed
if OS.windows?
'cleanupcode.exe'
else
'cleanupcode.sh'
end
end
def run_cleanup_code
return if skip_jetbrains?
old_diff = runOpen3CaptureOutput 'git', 'diff', '--stat'
params = [cleanup_code_executable, 'Thrive.sln', '--profile=full_no_xml']
params.append "--include=#{@includes.join(';')}" if @includes
runOpen3Checked(*params)
new_diff = runOpen3CaptureOutput 'git', 'diff', '--stat'
return if new_diff == old_diff
OUTPUT_MUTEX.synchronize do
error 'Code cleanup performed changes, please stage / check them before committing'
end
exit 2
end
run_check = proc { |check|
if check == 'compile'
run_compile
elsif check == 'files'
run_files
elsif check == 'inspectcode'
run_inspect_code
elsif check == 'cleanupcode'
run_cleanup_code
else
OUTPUT_MUTEX.synchronize do
onError "Unknown check type: #{check}"
end
end
}
if @options[:parallel]
threads = @options[:checks].map do |check|
Thread.new do
run_check.call check
end
end
threads.map(&:join)
else
@options[:checks].each do |check|
run_check.call check
end
end
success 'No code format issues found'
exit 0