kochdocs.nim 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. ## Part of 'koch' responsible for the documentation generation.
  2. import os, strutils, osproc
  3. const
  4. gaCode* = " --doc.googleAnalytics:UA-48159761-1"
  5. nimArgs = "--hint[Conf]:off --hint[Path]:off --hint[Processing]:off -d:boot --putenv:nimversion=$#" % system.NimVersion
  6. gitUrl = "https://github.com/nim-lang/Nim"
  7. docHtmlOutput = "doc/html"
  8. webUploadOutput = "web/upload"
  9. docHackDir = "tools/dochack"
  10. proc exe*(f: string): string =
  11. result = addFileExt(f, ExeExt)
  12. when defined(windows):
  13. result = result.replace('/','\\')
  14. proc findNim*(): string =
  15. var nim = "nim".exe
  16. result = "bin" / nim
  17. if existsFile(result): return
  18. for dir in split(getEnv("PATH"), PathSep):
  19. if existsFile(dir / nim): return dir / nim
  20. # assume there is a symlink to the exe or something:
  21. return nim
  22. proc exec*(cmd: string, errorcode: int = QuitFailure, additionalPath = "") =
  23. let prevPath = getEnv("PATH")
  24. if additionalPath.len > 0:
  25. var absolute = additionalPATH
  26. if not absolute.isAbsolute:
  27. absolute = getCurrentDir() / absolute
  28. echo("Adding to $PATH: ", absolute)
  29. putEnv("PATH", (if prevPath.len > 0: prevPath & PathSep else: "") & absolute)
  30. echo(cmd)
  31. if execShellCmd(cmd) != 0: quit("FAILURE", errorcode)
  32. putEnv("PATH", prevPath)
  33. proc execCleanPath*(cmd: string,
  34. additionalPath = ""; errorcode: int = QuitFailure) =
  35. # simulate a poor man's virtual environment
  36. let prevPath = getEnv("PATH")
  37. when defined(windows):
  38. let CleanPath = r"$1\system32;$1;$1\System32\Wbem" % getEnv"SYSTEMROOT"
  39. else:
  40. const CleanPath = r"/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin"
  41. putEnv("PATH", CleanPath & PathSep & additionalPath)
  42. echo(cmd)
  43. if execShellCmd(cmd) != 0: quit("FAILURE", errorcode)
  44. putEnv("PATH", prevPath)
  45. proc nimexec*(cmd: string) =
  46. # Consider using `nimCompile` instead
  47. exec findNim() & " " & cmd
  48. proc nimCompile*(input: string, outputDir = "bin", mode = "c", options = "") =
  49. # TODO: simplify pending https://github.com/nim-lang/Nim/issues/9513
  50. var cmd = findNim() & " " & mode
  51. let output = outputDir / input.splitFile.name.exe
  52. cmd.add " -o:" & output
  53. cmd.add " " & options
  54. cmd.add " " & input
  55. exec cmd
  56. const
  57. pdf = """
  58. doc/manual.rst
  59. doc/lib.rst
  60. doc/tut1.rst
  61. doc/tut2.rst
  62. doc/tut3.rst
  63. doc/nimc.rst
  64. doc/niminst.rst
  65. doc/gc.rst
  66. """.splitWhitespace()
  67. rst2html = """
  68. doc/intern.rst
  69. doc/apis.rst
  70. doc/lib.rst
  71. doc/manual.rst
  72. doc/tut1.rst
  73. doc/tut2.rst
  74. doc/tut3.rst
  75. doc/nimc.rst
  76. doc/overview.rst
  77. doc/filters.rst
  78. doc/tools.rst
  79. doc/niminst.rst
  80. doc/nimgrep.rst
  81. doc/gc.rst
  82. doc/estp.rst
  83. doc/idetools.rst
  84. doc/docgen.rst
  85. doc/koch.rst
  86. doc/backends.rst
  87. doc/nimsuggest.rst
  88. doc/nep1.rst
  89. doc/nims.rst
  90. doc/contributing.rst
  91. doc/codeowners.rst
  92. doc/manual/var_t_return.rst
  93. """.splitWhitespace()
  94. doc = """
  95. lib/system.nim
  96. lib/system/nimscript.nim
  97. lib/deprecated/pure/ospaths.nim
  98. lib/pure/parsejson.nim
  99. lib/pure/cstrutils.nim
  100. lib/core/macros.nim
  101. lib/pure/marshal.nim
  102. lib/core/typeinfo.nim
  103. lib/impure/re.nim
  104. lib/pure/typetraits.nim
  105. nimsuggest/sexp.nim
  106. lib/pure/concurrency/threadpool.nim
  107. lib/pure/concurrency/cpuinfo.nim
  108. lib/pure/concurrency/cpuload.nim
  109. lib/js/dom.nim
  110. lib/js/jsffi.nim
  111. lib/js/jsconsole.nim
  112. lib/js/asyncjs.nim
  113. lib/pure/os.nim
  114. lib/pure/strutils.nim
  115. lib/pure/math.nim
  116. lib/pure/matchers.nim
  117. lib/std/editdistance.nim
  118. lib/std/wordwrap.nim
  119. lib/experimental/diff.nim
  120. lib/pure/algorithm.nim
  121. lib/pure/stats.nim
  122. lib/windows/winlean.nim
  123. lib/pure/random.nim
  124. lib/pure/complex.nim
  125. lib/pure/times.nim
  126. lib/pure/osproc.nim
  127. lib/pure/pegs.nim
  128. lib/pure/dynlib.nim
  129. lib/pure/strscans.nim
  130. lib/pure/parseopt.nim
  131. lib/pure/hashes.nim
  132. lib/pure/strtabs.nim
  133. lib/pure/lexbase.nim
  134. lib/pure/parsecfg.nim
  135. lib/pure/parsexml.nim
  136. lib/pure/parsecsv.nim
  137. lib/pure/parsesql.nim
  138. lib/pure/streams.nim
  139. lib/pure/terminal.nim
  140. lib/pure/cgi.nim
  141. lib/pure/unicode.nim
  142. lib/pure/strmisc.nim
  143. lib/pure/htmlgen.nim
  144. lib/pure/parseutils.nim
  145. lib/pure/browsers.nim
  146. lib/impure/db_postgres.nim
  147. lib/impure/db_mysql.nim
  148. lib/impure/db_sqlite.nim
  149. lib/impure/db_odbc.nim
  150. lib/pure/db_common.nim
  151. lib/pure/httpserver.nim
  152. lib/pure/httpclient.nim
  153. lib/pure/smtp.nim
  154. lib/impure/ssl.nim
  155. lib/pure/ropes.nim
  156. lib/pure/unidecode/unidecode.nim
  157. lib/pure/xmlparser.nim
  158. lib/pure/htmlparser.nim
  159. lib/pure/xmltree.nim
  160. lib/pure/colors.nim
  161. lib/pure/mimetypes.nim
  162. lib/pure/json.nim
  163. lib/pure/base64.nim
  164. lib/pure/scgi.nim
  165. lib/impure/nre.nim
  166. lib/impure/nre/private/util.nim
  167. lib/deprecated/pure/sockets.nim
  168. lib/deprecated/pure/asyncio.nim
  169. lib/pure/collections/tables.nim
  170. lib/pure/collections/sets.nim
  171. lib/pure/collections/lists.nim
  172. lib/pure/collections/sharedlist.nim
  173. lib/pure/collections/sharedtables.nim
  174. lib/pure/collections/intsets.nim
  175. lib/pure/collections/queues.nim
  176. lib/pure/collections/deques.nim
  177. lib/pure/encodings.nim
  178. lib/pure/collections/sequtils.nim
  179. lib/pure/collections/rtarrays.nim
  180. lib/pure/cookies.nim
  181. lib/pure/memfiles.nim
  182. lib/pure/subexes.nim
  183. lib/pure/collections/critbits.nim
  184. lib/core/locks.nim
  185. lib/core/rlocks.nim
  186. lib/pure/oids.nim
  187. lib/pure/endians.nim
  188. lib/pure/uri.nim
  189. lib/pure/nimprof.nim
  190. lib/pure/unittest.nim
  191. lib/packages/docutils/highlite.nim
  192. lib/packages/docutils/rst.nim
  193. lib/packages/docutils/rstast.nim
  194. lib/packages/docutils/rstgen.nim
  195. lib/pure/logging.nim
  196. lib/pure/options.nim
  197. lib/pure/asyncdispatch.nim
  198. lib/pure/asyncnet.nim
  199. lib/pure/asyncstreams.nim
  200. lib/pure/asyncfutures.nim
  201. lib/pure/nativesockets.nim
  202. lib/pure/asynchttpserver.nim
  203. lib/pure/net.nim
  204. lib/pure/selectors.nim
  205. lib/pure/sugar.nim
  206. lib/pure/collections/chains.nim
  207. lib/pure/asyncfile.nim
  208. lib/deprecated/pure/ftpclient.nim
  209. lib/pure/asyncftpclient.nim
  210. lib/pure/lenientops.nim
  211. lib/pure/md5.nim
  212. lib/pure/rationals.nim
  213. lib/pure/distros.nim
  214. lib/pure/oswalkdir.nim
  215. lib/pure/collections/heapqueue.nim
  216. lib/pure/fenv.nim
  217. lib/std/sha1.nim
  218. lib/std/varints.nim
  219. lib/impure/rdstdin.nim
  220. lib/wrappers/linenoise/linenoise.nim
  221. lib/pure/strformat.nim
  222. lib/pure/segfaults.nim
  223. lib/pure/mersenne.nim
  224. lib/pure/coro.nim
  225. lib/pure/httpcore.nim
  226. lib/pure/bitops.nim
  227. lib/pure/nimtracker.nim
  228. lib/pure/punycode.nim
  229. lib/pure/volatile.nim
  230. """.splitWhitespace()
  231. doc0 = """
  232. lib/system/threads.nim
  233. lib/system/channels.nim
  234. """.splitWhitespace()
  235. withoutIndex = """
  236. lib/wrappers/mysql.nim
  237. lib/wrappers/iup.nim
  238. lib/wrappers/sqlite3.nim
  239. lib/wrappers/postgres.nim
  240. lib/wrappers/tinyc.nim
  241. lib/wrappers/odbcsql.nim
  242. lib/wrappers/pcre.nim
  243. lib/wrappers/openssl.nim
  244. lib/posix/posix.nim
  245. lib/posix/linux.nim
  246. lib/posix/termios.nim
  247. lib/wrappers/odbcsql.nim
  248. lib/js/jscore.nim
  249. """.splitWhitespace()
  250. proc sexec(cmds: openarray[string]) =
  251. ## Serial queue wrapper around exec.
  252. for cmd in cmds:
  253. echo(cmd)
  254. let (outp, exitCode) = osproc.execCmdEx(cmd)
  255. if exitCode != 0: quit outp
  256. proc mexec(cmds: openarray[string]) =
  257. ## Multiprocessor version of exec
  258. let r = execProcesses(cmds, {poStdErrToStdOut, poParentStreams, poEchoCmd})
  259. if r != 0:
  260. echo "external program failed, retrying serial work queue for logs!"
  261. sexec(cmds)
  262. proc buildDocSamples(nimArgs, destPath: string) =
  263. ## Special case documentation sample proc.
  264. ##
  265. ## TODO: consider integrating into the existing generic documentation builders
  266. ## now that we have a single `doc` command.
  267. exec(findNim() & " doc $# -o:$# $#" %
  268. [nimArgs, destPath / "docgen_sample.html", "doc" / "docgen_sample.nim"])
  269. proc buildDoc(nimArgs, destPath: string) =
  270. # call nim for the documentation:
  271. var
  272. commands = newSeq[string](rst2html.len + len(doc0) + len(doc) + withoutIndex.len)
  273. i = 0
  274. let nim = findNim()
  275. for d in items(rst2html):
  276. commands[i] = nim & " rst2html $# --git.url:$# -o:$# --index:on $#" %
  277. [nimArgs, gitUrl,
  278. destPath / changeFileExt(splitFile(d).name, "html"), d]
  279. i.inc
  280. for d in items(doc0):
  281. commands[i] = nim & " doc0 $# --git.url:$# -o:$# --index:on $#" %
  282. [nimArgs, gitUrl,
  283. destPath / changeFileExt(splitFile(d).name, "html"), d]
  284. i.inc
  285. for d in items(doc):
  286. commands[i] = nim & " doc $# --git.url:$# -o:$# --index:on $#" %
  287. [nimArgs, gitUrl,
  288. destPath / changeFileExt(splitFile(d).name, "html"), d]
  289. i.inc
  290. for d in items(withoutIndex):
  291. commands[i] = nim & " doc2 $# --git.url:$# -o:$# $#" %
  292. [nimArgs, gitUrl,
  293. destPath / changeFileExt(splitFile(d).name, "html"), d]
  294. i.inc
  295. mexec(commands)
  296. exec(nim & " buildIndex -o:$1/theindex.html $1" % [destPath])
  297. proc buildPdfDoc*(nimArgs, destPath: string) =
  298. createDir(destPath)
  299. if os.execShellCmd("pdflatex -version") != 0:
  300. echo "pdflatex not found; no PDF documentation generated"
  301. else:
  302. const pdflatexcmd = "pdflatex -interaction=nonstopmode "
  303. for d in items(pdf):
  304. exec(findNim() & " rst2tex $# $#" % [nimArgs, d])
  305. # call LaTeX twice to get cross references right:
  306. exec(pdflatexcmd & changeFileExt(d, "tex"))
  307. exec(pdflatexcmd & changeFileExt(d, "tex"))
  308. # delete all the crappy temporary files:
  309. let pdf = splitFile(d).name & ".pdf"
  310. let dest = destPath / pdf
  311. removeFile(dest)
  312. moveFile(dest=dest, source=pdf)
  313. removeFile(changeFileExt(pdf, "aux"))
  314. if existsFile(changeFileExt(pdf, "toc")):
  315. removeFile(changeFileExt(pdf, "toc"))
  316. removeFile(changeFileExt(pdf, "log"))
  317. removeFile(changeFileExt(pdf, "out"))
  318. removeFile(changeFileExt(d, "tex"))
  319. proc buildJS() =
  320. exec(findNim() & " js -d:release --out:$1 tools/nimblepkglist.nim" %
  321. [webUploadOutput / "nimblepkglist.js"])
  322. exec(findNim() & " js " & (docHackDir / "dochack.nim"))
  323. proc buildDocs*(args: string) =
  324. let
  325. a = nimArgs & " " & args
  326. docHackJs = "dochack.js"
  327. docHackJsSource = docHackDir / "nimcache" / docHackJs
  328. docHackJsDest = docHtmlOutput / docHackJs
  329. buildJS() # This call generates docHackJsSource
  330. let docup = webUploadOutput / NimVersion
  331. createDir(docup)
  332. buildDocSamples(a, docup)
  333. buildDoc(a, docup)
  334. # 'nimArgs' instead of 'a' is correct here because we don't want
  335. # that the offline docs contain the 'gaCode'!
  336. createDir(docHtmlOutput)
  337. buildDocSamples(nimArgs, docHtmlOutput)
  338. buildDoc(nimArgs, docHtmlOutput)
  339. copyFile(docHackJsSource, docHackJsDest)
  340. copyFile(docHackJsSource, docup / docHackJs)