-
Notifications
You must be signed in to change notification settings - Fork 12
/
nakefile.nim
168 lines (133 loc) · 4.7 KB
/
nakefile.nim
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
import nake
import sequtils
proc mvFile(`from`,to: string) =
moveFile(`from`,to)
echo "Moved file"
when defined(Linux):
proc symlinkFile (file, to: string) =
removeFile(to)
direShell("ln -s", file.expandFileName, to)
echo "Symlinked file"
proc buildDocs() =
for name in ["nake", "nakelib"]:
let
dest = name & ".html"
src = name & ".nim"
if dest.needsRefresh(src):
direSilentShell(src & " -> " & dest,
nimExe, "doc2", "--verbosity:0", "--index:on", src)
for rstSrc in walkFiles("*.rst"):
let rstDest = rstSrc.changeFileExt(".html")
if not rstDest.needsRefresh(rstSrc): continue
direSilentShell(rstSrc & " -> " & rstDest,
nimExe & " rst2html --verbosity:0 --index:on -o:" &
rstDest & " " & rstSrc)
direSilentShell("Building theindex.html", nimExe, "buildIndex .")
proc runTests() =
var testResults: seq[bool] = @[]
withDir "tests":
for nakeFile in walkFiles "*.nim":
let nakeExe = nakeFile.changeFileExt(ExeExt)
if not shell(nimExe, "c",
"--noNimblePath --verbosity:0 -d:debug -r", nakeFile):
testResults.add(false)
continue
# Repeat compilation in release mode.
if not shell(nimExe, "c",
"--noNimblePath --verbosity:0 -d:release -r", nakeFile):
testResults.add(false)
continue
testResults.add(true)
echo "" # prettify the output
let
total = testResults.len
successes = filter(testResults, proc(x:bool): bool = x).len
echo ("Tests Complete: ", total, " test files run, ",
successes, " test files succeeded.")
proc installNake() =
direSilentShell("Compiling nake...", nimExe, "c", "nake")
var
installMethod: proc(src,dest:string)# = mvFile
when defined(Linux):
echo "How to install the nake binary?\L",
" * [M]ove file\L",
" [S]ymlink file"
case stdin.readLine.toLowerAscii
of "m","move": installMethod = mvFile
of "s","symlink": installMethod = symlinkFile
else:
installMethod = mvFile
let path = getEnv("PATH").split(PathSep)
echo "Your $PATH:"
for index, dir in pairs(path):
echo " ", index, ". ", dir
echo "Where to install nake binary? (quit with ^C or quit or exit)"
let ans = stdin.readLine().toLowerAscii
var index = 0
case ans
of "q", "quit", "x", "exit":
quit 0
else:
index = parseInt(ans)
if index notin 0 ..< path.len:
echo "Invalid index."
quit 1
installMethod "nake", path[index]/"nake"
proc switchToGhPages(iniPathOrDir = ".") =
## Forces changing git branch to ``gh-pages`` and running
## ``gh_nimrod_doc_pages``.
##
## **This is a potentially destructive action!**. Pass the directory where
## the ``gh_nimrod_doc_pages.ini`` file lives, or the path to the specific
## file if you renamed it.
assert(iniPathOrDir.len != 0)
let
ghExe = findExe("gh_nimrod_doc_pages")
gitExe = findExe("git")
if ghExe.len < 1:
quit("""Could not find gh_nimrod_doc_pages binary in $PATH, aborting.
You may find it at https://github.com/gradha/gh_nimrod_doc_pages.
Or maybe run 'nimble install gh_nimrod_doc_pages'.
""")
if gitExe.len < 1:
quit("Could not find git binary in $PATH, aborting")
echo "Changing branches to render gh-pages…"
let
nakeExe = "nakefile".addFileExt(ExeExt)
ourselves = readFile(nakeExe)
direShell gitExe & " checkout gh-pages"
# Keep ingored files http://stackoverflow.com/a/3801554/172690.
when defined(posix):
shell "rm -Rf `git ls-files --others --exclude-standard`"
removeDir("gh_docs")
direShell ghExe & " -c " & iniPathOrDir
writeFile(nakeExe, ourselves)
direShell "chmod 775 nakefile"
echo "All commands run, now check the output and commit to git."
when defined(macosx):
shell "open index.html"
echo "Wen you are done come back with './" & nakeExe & " postweb'."
proc switchBackFromGhPages() =
## Counterpart of ``switchToGhPages``.
let gitExe = findExe("git")
if gitExe.len < 1:
quit("Could not find git binary in $PATH, aborting")
echo "Forcing changes back to master."
direShell gitExe & " checkout -f @{-1}"
echo "Updating submodules just in case."
direShell gitExe & " submodule update"
removeDir("gh_docs")
task "docs", "generate user documentation for nake API and local rst files":
buildDocs()
echo "Finished generating docs"
task "test", "runs any tests in the `./tests` directory":
runTests()
task "install", "compile and install nake binary":
installNake()
echo "Great success!"
task "web", "switches to gh-pages branch and runs gh_nimrod_doc_pages":
switchToGhPages("web.ini")
echo "Now you can run 'git add .' if everything looks good."
task "postweb", "switches back from gh-pages":
switchBackFromGhPages()
echo "Back from gh-pages"