forked from Revolutionary-Games/Thrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_formatting.rb
executable file
·213 lines (176 loc) · 4.66 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
#!/usr/bin/env ruby
# 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_relative 'bootstrap_rubysetupsystem'
require_relative 'RubySetupSystem/RubyCommon'
require_relative 'scripts/fast_build/toggle_analysis_lib'
MAX_LINE_LENGTH = 120
VALID_CHECKS = %w[compile files].freeze
OUTPUT_MUTEX = Mutex.new
@options = {
checks: VALID_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
# Skip some files that would otherwise be processed
def skip_file?(path)
path =~ %r{/ThirdParty/}i || path =~ %r{^\.\/\.\/} || path =~ /GlobalSuppressions.cs/ ||
path =~ %r{/RubySetupSystem/} || path =~ %r{/\.mono/}
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
# 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
# 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
# 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
if path =~ /\.gd$/
handle_gd_file path
elsif path =~ /\.cs$/
handle_cs_file path
elsif path =~ %r{simulation_parameters/.*\.json$}
handle_json_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
run_check = proc { |check|
if check == 'compile'
run_compile
elsif check == 'files'
run_files
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