-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathfmt
More file actions
executable file
·189 lines (160 loc) · 5.86 KB
/
Copy pathfmt
File metadata and controls
executable file
·189 lines (160 loc) · 5.86 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
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
#!/usr/bin/env ruby
LANG_MASKS = {
'c' => ['./c', ['.c', '.h'], ['target', 'deps'], 'clang-format -i --style="{InsertNewlineAtEOF: true}" $@'],
'c3' => ['./c3', ['.c3'], ['build'], 'c3fmt -i $@'],
'cpp' => ['./cpp', ['.cpp', '.hpp', '.h', '.cc', '.cxx'], ['target', 'deps'], "clang-format -i $@"],
'golang' => ['./golang', ['.go'], ['target'], "go fmt $@"],
'crystal' => ['./crystal', ['.cr'], ['target', 'lib'], "crystal tool format $@"],
'ruby' => ['./ruby', ['.rb'], [], "rubyfmt -i $@"],
'rust' => ['./rust', ['.rs'], ['target'], "cargo fmt"],
'csharp' => ['./csharp', ['.cs'], ['obj', 'bin'], "dotnet format"],
'swift' => ['./swift', ['.swift'], ['.build', 'Package.swift'], "swift format format --in-place $@"],
'java' => ['./java', ['.java'], ['target'], "astyle -n --mode=java $@"],
'kotlin' => ['./kotlin', ['.kt'], ['build', '.gradle', 'gradle'], "ktlint -F src/**/*.kt"],
'typescript' => ['./typescript', ['.ts', '.tsx'], ['node_modules', 'target'], "npx oxfmt $@ < /dev/null"],
'zig' => ['./zig', ['.zig'], ['.zig-cache', 'zig-pkg'], "zig fmt $@"],
'd' => ['./d', ['.d'], [], "dfmt --inplace $@"],
'v' => ['./v', ['.v'], ['target'], "v fmt -w $@"],
'julia' => ['./julia', ['.jl'], ['target'], "julia -e 'using Pkg; Pkg.add(\"JuliaFormatter\"); using JuliaFormatter; format(\".\")'"],
'nim' => ['./nim', ['.nim'], ['target'], "nimpretty $@"],
'fsharp' => ['./fsharp', ['.fs'], ['bin', 'obj'], "dotnet fantomas *.fs"],
'dart' => ['./dart', ['.dart'], ['target'], "dart format $@"],
'python' => ['./python', ['.py'], ['__pycache__'], "yapf --in-place --style=google $@"],
'odin' => ['./odin', ['.odin'], ['target'], "odinfmt -w $@"],
'scala' => ['./scala', ['.scala'], ['target', 'project'], "\"/Users/kostya/Library/Application Support/Coursier/bin/scalafmt\" -i src/"],
'mojo' => ['./mojo', ['.mojo'], ['target', '.venv', '.pixi'], 'pixi run mojo format $@'],
'php' => ['./php', ['.php'], [], 'pretty-php $@'],
'gossamer' => ['./gossamer', ['.gos'], ['target', '.gos-cache'], 'gos fmt $@'],
'javascript' => ['./javascript', ['.js'], [], 'npx oxfmt $@ < /dev/null'],
}
def remove_comments(filepath, lang)
content = File.read(filepath, encoding: 'utf-8')
case lang
when 'c', 'cpp', 'golang', 'rust', 'csharp', 'swift', 'java', 'kotlin', 'd', 'v',
'fsharp', 'dart', 'zig', 'odin', 'scala', 'c3', 'php', 'gossamer', 'javascript', 'typescript'
lines = content.lines.map do |line|
if (line =~ %r{//\s*@ts-ignore}) || (line =~ %r{/[^/\n]*\\/[^/\n]*/})
puts "skipping like #{line.inspect}"
next line.rstrip
end
in_string = false
string_char = nil
in_char = false
comment_start = nil
line.chars.each_with_index do |char, i|
if char == '"' || char == '\'' || char == '`'
if !in_string && !in_char
in_string = true
string_char = char
elsif string_char == char && (i == 0 || line[i-1] != '\\')
in_string = false
string_char = nil
end
elsif char == '\'' && !in_string
in_char = !in_char
end
if !in_string && !in_char
if char == '/' && i+1 < line.length && line[i+1] == '/'
comment_start = i
break
elsif char == '/' && i+1 < line.length && line[i+1] == '*'
end
end
end
if comment_start
line[0...comment_start].rstrip
else
line.rstrip
end
end
content = lines.join("\n")
content.gsub!(/\/\*[\s\S]*?\*\//m) do |substr|
if substr.include?("//gi]],") || substr.include?("@ts-ignore")
substr
else
""
end
end
when 'crystal', 'julia', 'nim', 'python', 'ruby', 'mojo'
content.gsub!(/^=begin[\s\S]*?^=end/m, '')
lines = content.lines.map do |line|
comment_pos = -1
in_string = false
string_char = nil
line.chars.each_with_index do |char, i|
if char == '"' || char == "'" || char == '`'
if !in_string
in_string = true
string_char = char
elsif string_char == char && (i == 0 || line[i-1] != '\\')
in_string = false
string_char = nil
end
end
if !in_string && char == '#' && (i == 0 || line[i-1] != '\\')
if i + 1 < line.length && line[i+1] == '{'
next
else
comment_pos = i
break
end
end
end
if comment_pos >= 0
line[0...comment_pos].rstrip
else
line.rstrip
end
end
content = lines.join("\n")
end
content.gsub!(/^[ \t]+$/, '')
while content.gsub!(/\n\n\n+/, "\n\n")
end
content.sub!(/\A\n+/, '')
content.sub!(/\n+\z/, '')
File.write(filepath, content, encoding: 'utf-8')
end
def get_files(lang)
dir, exts, excludes, _ = LANG_MASKS[lang]
pattern = "#{dir}/**/*{#{exts.join(',')}}"
files = Dir.glob(pattern, File::FNM_DOTMATCH)
files.reject! do |file|
excludes.any? { |exclude| file.include?(exclude) }
end
files
end
mask = ARGV[0]
LANG_MASKS.each do |lang, (dir, _, _, format_cmd)|
if mask
unless lang.downcase.include?(mask.downcase)
next
end
end
puts '-' * 100
files = get_files(lang)
puts "Lang: #{lang}, files: #{files.size}"
print "Remove comments: #{lang} "
t = Time.now
files.each do |file|
remove_comments(file, lang)
end
puts "in #{(Time.now - t).to_f.round(3)}s"
unless format_cmd
puts "no fmt command"
next
end
print "Formatting: #{lang} "
Dir.chdir(dir) do
if format_cmd.include?("$@")
files.each do |filename|
cmd = "#{format_cmd.gsub("$@", "'" + filename.gsub("#{dir}/", "") + "'")}"
system cmd
end
else
cmd = "#{format_cmd}"
system cmd
end
end
puts "in #{(Time.now - t).to_f.round(3)}s"
end