runnableExamples: compile and test them as a single file; fixes #9300
This commit is contained in:
parent
77166ba795
commit
086229459a
1 changed files with 10 additions and 2 deletions
|
|
@ -40,6 +40,7 @@ type
|
||||||
# already. See bug #3655
|
# already. See bug #3655
|
||||||
destFile*: AbsoluteFile
|
destFile*: AbsoluteFile
|
||||||
thisDir*: AbsoluteDir
|
thisDir*: AbsoluteDir
|
||||||
|
examples: string
|
||||||
|
|
||||||
PDoc* = ref TDocumentor ## Alias to type less.
|
PDoc* = ref TDocumentor ## Alias to type less.
|
||||||
|
|
||||||
|
|
@ -378,6 +379,13 @@ proc testExample(d: PDoc; ex: PNode) =
|
||||||
"_examples" & $d.exampleCounter & ".nim"))
|
"_examples" & $d.exampleCounter & ".nim"))
|
||||||
#let nimcache = outp.changeFileExt"" & "_nimcache"
|
#let nimcache = outp.changeFileExt"" & "_nimcache"
|
||||||
renderModule(ex, d.filename, outp.string, conf = d.conf)
|
renderModule(ex, d.filename, outp.string, conf = d.conf)
|
||||||
|
d.examples.add "import r\"" & outp.string & "\"\n"
|
||||||
|
|
||||||
|
proc runAllExamples(d: PDoc) =
|
||||||
|
let outputDir = d.conf.getNimcacheDir / RelativeDir"runnableExamples"
|
||||||
|
let outp = outputDir / RelativeFile(extractFilename(d.filename.changeFileExt"" &
|
||||||
|
"_examples.nim"))
|
||||||
|
writeFile(outp, d.examples)
|
||||||
let backend = if isDefined(d.conf, "js"): "js"
|
let backend = if isDefined(d.conf, "js"): "js"
|
||||||
elif isDefined(d.conf, "cpp"): "cpp"
|
elif isDefined(d.conf, "cpp"): "cpp"
|
||||||
elif isDefined(d.conf, "objc"): "objc"
|
elif isDefined(d.conf, "objc"): "objc"
|
||||||
|
|
@ -400,11 +408,9 @@ proc extractImports(n: PNode; result: PNode) =
|
||||||
for i in 0..<n.safeLen: extractImports(n[i], result)
|
for i in 0..<n.safeLen: extractImports(n[i], result)
|
||||||
|
|
||||||
proc prepareExamples(d: PDoc; n: PNode) =
|
proc prepareExamples(d: PDoc; n: PNode) =
|
||||||
|
|
||||||
var docComment = newTree(nkCommentStmt)
|
var docComment = newTree(nkCommentStmt)
|
||||||
let loc = d.conf.toFileLineCol(n.info)
|
let loc = d.conf.toFileLineCol(n.info)
|
||||||
docComment.comment = "autogenerated by docgen from " & loc
|
docComment.comment = "autogenerated by docgen from " & loc
|
||||||
|
|
||||||
var runnableExamples = newTree(nkStmtList,
|
var runnableExamples = newTree(nkStmtList,
|
||||||
docComment,
|
docComment,
|
||||||
newTree(nkImportStmt, newStrNode(nkStrLit, d.filename)))
|
newTree(nkImportStmt, newStrNode(nkStrLit, d.filename)))
|
||||||
|
|
@ -935,6 +941,7 @@ proc generateIndex*(d: PDoc) =
|
||||||
writeIndexFile(d[], dest.string)
|
writeIndexFile(d[], dest.string)
|
||||||
|
|
||||||
proc writeOutput*(d: PDoc, useWarning = false) =
|
proc writeOutput*(d: PDoc, useWarning = false) =
|
||||||
|
runAllExamples(d)
|
||||||
var content = genOutFile(d)
|
var content = genOutFile(d)
|
||||||
if optStdout in d.conf.globalOptions:
|
if optStdout in d.conf.globalOptions:
|
||||||
writeRope(stdout, content)
|
writeRope(stdout, content)
|
||||||
|
|
@ -947,6 +954,7 @@ proc writeOutput*(d: PDoc, useWarning = false) =
|
||||||
outfile.string)
|
outfile.string)
|
||||||
|
|
||||||
proc writeOutputJson*(d: PDoc, useWarning = false) =
|
proc writeOutputJson*(d: PDoc, useWarning = false) =
|
||||||
|
runAllExamples(d)
|
||||||
var modDesc: string
|
var modDesc: string
|
||||||
for desc in d.modDesc:
|
for desc in d.modDesc:
|
||||||
modDesc &= desc
|
modDesc &= desc
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue