forked from Revolutionary-Games/Thrive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunCodeFormatting.rb
executable file
·99 lines (78 loc) · 2.59 KB
/
RunCodeFormatting.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
#!/usr/bin/env ruby
# This script goes through all the thrive source files and makes sure that they are formatted
# correctly
# TODO: also add syntax based checks for angelscript
require 'find'
require 'optparse'
options = {}
OptionParser.new do |opts|
opts.on("--only-js", "Only checks that javascript (and CSS) files are good") do |b|
options[:onlyJS] = b
end
end.parse!
if !ARGV.empty?
puts "Invalid parameters given: unparsed: #{ARGV}"
exit 2
end
if !options[:onlyJS]
file_paths = []
Find.find('.') do |path|
if path =~ /\/ThirdParty\//i
next
end
begin
if path !~ /\.h(pp)?$/ && path !~ /\.cpp$/
if path =~ /\.as$/ && path !~ /^\.\/build/
# Check that angelscript doesn't have lines starting with tabs
begin
original = File.read(path)
fixedASCode = ""
original.each_line{|line|
if line.empty?
fixedASCode += "\n"
else
# Remove trailing whitespace
line.rstrip!
line += "\n"
replaced = line.gsub(/^(?:(?!\t)\s)*\t/, " ")
# Run while it matches
while replaced != line
line = replaced
replaced = line.gsub(/^(?:(?!\t)\s)*\t/, " ")
end
fixedASCode += replaced
end
}
if original != fixedASCode
puts "AngelScript file fixed: #{path}"
File.write(path, fixedASCode)
end
rescue ArgumentError => e
abort "AngelScript file isn't utf8 encoded: " + path + ", e: #{e}"
end
end
next
end
rescue ArgumentError => e
puts "Failed to handle path: " + path
puts "Error: " + e.message
raise e
end
if path !~ /\/src\//i && path !~ /\/test\//i ||
# Generated files
path =~ /\/generated\//i || path =~ /\/src\/main.cpp/i ||
path =~ /\/src\/thrive_version.h/i || path =~ /\/src\/server\/main.cpp/i ||
# ignore catch
path =~ /catch.hpp$/i
next
end
system "clang-format", "-i", path
abort("\n\nFAILED to format file: " + path) if $?.exitstatus != 0
end
end
# JavaScript linting
system(%{eslint "scripts/gui/**/*.*js" "scripts/gui/**/*.html" --fix})
abort("\nJavaScript style errors were found.") if $?.exitstatus != 0
# Stylelint for css
system(%{stylelint scripts/gui/**/*.css scripts/gui/**/*.html --fix})
abort("\nCSS style errors were found.") if $?.exitstatus != 0