-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathnph.nimble
120 lines (99 loc) · 3.25 KB
/
nph.nimble
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
# Package
version = "0.6.1"
author = "Jacek Sieka"
description = "Nim code formatter"
license = "MIT"
srcDir = "src"
bin = @["nph"]
# Dependencies
# TODO https://github.com/nim-lang/nimble/issues/1166
# Using exact version here and adding path manually :facepalm:
# run `nimble setup -l` to hopefully make it work
# Note that the parser used by the formatter and the parser used by the verifier
# might disagree if syntax changes are included in a Nim patch version.
# TODO: nph should learn how to check formatting with any nim version
requires "nim >= 2.0.0 & < 2.1", "compiler >= 2.0.0 & < 2.1"
proc build() =
exec "nim c --debuginfo -o:nph src/nph"
task self, "Format nph itself":
build()
# Require there are no changes before self-formatting! Can stage if needed
exec "git diff --no-ext-diff --quiet --exit-code"
for file in listFiles("src"):
if file.len > 4 and file[^4..^1] == ".nim":
echo file
exec "./nph " & file
import std/algorithm
task f, "Format":
build()
cd "tests/before"
# Sort tests so that 00_empty always is first, which makes it a convenient
# experimentation ground :)
for file in sorted(listFiles(".")):
if file.len > 4 and file[^4..^1] == ".nim":
echo file
exec "../../nph " & file & " --outDir:../after --debug"
proc formatProject(
name, url, branch: string, dirs: openArray[string]
) =
if not dirExists("playground"):
mkdir("playground")
cd "playground/"
if not dirExists(name):
exec "git clone --single-branch --branch " & branch & " " & url & " " & name
cd name
for dir in dirs:
if dir.len > 0:
cd dir
exec "git checkout " & branch & " -- ."
exec "git restore --staged ."
try:
exec "git ls-files | grep .nim$ | xargs nph"
exec "git diff"
except: discard
if dir.len > 0:
cd ".."
cd "../.."
proc againProject(
name, url, branch: string, dirs: openArray[string]
) =
if not dirExists("playground"):
mkdir("playground")
cd "playground/"
cd name
for dir in dirs:
if dir.len > 0:
cd dir
try:
exec "git ls-files | grep .nim$ | xargs nph"
exec "git diff"
except: discard
if dir.len > 0:
cd ".."
cd "../.."
proc commitProject(
name, url, branch: string, dirs: openArray[string]
) =
formatProject(name, url, branch, dirs)
cd "playground/" & name
try:
exec "git checkout -b nph"
exec "git commit -am \"Formatted with nph $(nph --version)\" --author \"nph <[email protected]>\""
except:
exec "git checkout nph"
exec "git commit -am \"Formatted with nph $(nph --version)\" --amend --author \"nph <[email protected]>\""
cd "../.."
const projects = [
("Nim", "https://github.com/arnetheduck/Nim.git", "version-2-0", @["compiler", "lib"]),
("nim-results", "https://github.com/arnetheduck/nim-results.git", "master", @[""]),
("nimbus-eth2", "https://github.com/status-im/nimbus-eth2.git", "unstable", @[""]),
]
task play, "Format several popular projects":
for p in projects:
formatProject(p[0], p[1], p[2], p[3])
task again, "Format code formatted by replay (instead of raw code)":
for p in projects:
againProject(p[0], p[1], p[2], p[3])
task replay, "Commit formatted sources":
for p in projects:
commitProject(p[0], p[1], p[2], p[3])