forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_format.py
149 lines (130 loc) · 5.05 KB
/
run_format.py
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
import os.path
import re
path = os.getcwd()
def prettier_format():
"""Format code with prettier"""
# before prettier, add `<?php` to php files
for root, _, files in os.walk(path):
for name in files:
if name.endswith('.php'):
p = root + '/' + name
with open(p, 'r', encoding='utf-8') as f:
content = f.read()
content = '<?php\n' + content
with open(p, 'w', encoding='utf-8') as f:
f.write(content)
# start formatting with prettier
for suffix in ['md', 'js', 'ts', 'php']:
command = f'npx prettier --write "**/*.{suffix}"'
os.system(command)
# after prettier, remove `<?php` from php files
for root, _, files in os.walk(path):
for name in files:
if name.endswith('.php'):
p = root + '/' + name
with open(p, 'r', encoding='utf-8') as f:
content = f.read()
content = content.replace('<?php\n', '')
with open(p, 'w', encoding='utf-8') as f:
f.write(content)
def clang_format():
"""Format code with clang-format"""
suffix = ['c', 'cpp', 'java']
for root, _, files in os.walk(path):
for name in files:
for suf in suffix:
if name.endswith('.' + suf):
local_path = root + '/' + name
command = f'clang-format -i --style=file "{local_path}"'
print(command)
os.system(command)
for root, _, files in os.walk(path):
for name in files:
if name.endswith('.md'):
p = root + '/' + name
with open(p, 'r', encoding='utf-8') as f:
content = f.read()
x = content
print(p)
for suf in suffix:
res = re.findall(f'```{suf}\n(.*?)```', content, re.S)
if not res:
continue
for v in res:
p2 = f'{root}/Solution2.{suf}'
with open(p2, 'w', encoding='utf-8') as f:
f.write(v)
command = f'clang-format -i --style=file "{p2}"'
os.system(command)
with open(p2, 'r', encoding='utf-8') as f:
content = f.read()
content = x.replace(v, content)
with open(p, 'w', encoding='utf-8') as f:
f.write(content)
os.remove(p2)
def black_format():
"""Format python code with black"""
command = 'black -S .'
os.system(command)
for root, _, files in os.walk(path):
for name in files:
if name.endswith('.md'):
p1 = root + '/' + name
with open(p1, 'r', encoding='utf-8') as f:
content = f.read()
x = content
res = re.findall('```python\n(.*?)```', content, re.S)
if not res:
continue
print(p1)
for v in res:
p2 = root + "/tmp.py"
with open(p2, 'w', encoding='utf-8') as f:
f.write(v)
os.system(f'black -S "{p2}"')
with open(p2, 'r', encoding='utf-8') as f:
content = f.read()
content = x.replace(v, content)
with open(p1, 'w', encoding='utf-8') as f:
f.write(content)
os.remove(p2)
def golang_format():
"""Format golang code with gofmt"""
for root, _, files in os.walk(path):
for name in files:
if name.endswith('.go'):
p = root + '/' + name
with open(p, 'r', encoding='utf-8') as f:
content = f.read()
if content.startswith('package '):
continue
content = 'package main\n' + content
with open(p, 'w', encoding='utf-8') as f:
f.write(content)
command = 'gofmt -w .'
os.system(command)
for root, _, files in os.walk(path):
for name in files:
if name.endswith('.go'):
p = root + '/' + name
if 'sorting' in p:
continue
with open(p, 'r', encoding='utf-8') as f:
content = f.read()
content = (
content.replace('package main\n\n', '')
.replace('package main\n', '')
.rstrip()
)
with open(p, 'w', encoding='utf-8') as f:
f.write(content)
def git_add():
"""Git add all files"""
command = 'git add .'
os.system(command)
if __name__ == '__main__':
# prettier_format()
# black_format()
# clang_format()
golang_format()
git_add()