bookshelf-doc/DocGen4/Load.lean

57 lines
2.1 KiB
Plaintext
Raw Normal View History

2021-12-10 12:29:04 +00:00
/-
Copyright (c) 2021 Henrik Böving. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Henrik Böving
-/
2021-11-27 15:19:56 +00:00
import Lean
import DocGen4.Process
import Std.Data.HashMap
2021-11-27 15:19:56 +00:00
namespace DocGen4
open Lean System Std IO
2021-11-27 15:19:56 +00:00
def getLakePath : IO FilePath := do
match (← IO.getEnv "LAKE") with
| some path => System.FilePath.mk path
| none =>
let lakePath := (←findSysroot?) / "bin" / "lake"
lakePath.withExtension System.FilePath.exeExtension
2021-11-27 15:19:56 +00:00
-- Modified from the LSP Server
def lakeSetupSearchPath (lakePath : System.FilePath) (imports : Array String) : IO Lean.SearchPath := do
let args := #["print-paths"] ++ imports
let cmdStr := " ".intercalate (toString lakePath :: args.toList)
let lakeProc ← Process.spawn {
stdin := Process.Stdio.null
stdout := Process.Stdio.piped
stderr := Process.Stdio.piped
cmd := lakePath.toString
args
}
let stdout := String.trim (← lakeProc.stdout.readToEnd)
let stderr := String.trim (← lakeProc.stderr.readToEnd)
match (← lakeProc.wait) with
| 0 =>
let stdout := stdout.split (· == '\n') |>.getLast!
let Except.ok (paths : LeanPaths) ← pure (Json.parse stdout >>= fromJson?)
| throw $ userError s!"invalid output from `{cmdStr}`:\n{stdout}\nstderr:\n{stderr}"
initSearchPath (← findSysroot?) paths.oleanPath
paths.oleanPath.mapM realPathNormalized
| 2 => pure [] -- no lakefile.lean
| _ => throw $ userError s!"`{cmdStr}` failed:\n{stdout}\nstderr:\n{stderr}"
2021-11-27 15:19:56 +00:00
def load (imports : List Name) : IO (HashMap Name Module) := do
2021-11-27 15:19:56 +00:00
let env ← importModules (List.map (Import.mk · false) imports) Options.empty
-- TODO parameterize maxHeartbeats
let doc ← Prod.fst <$> (Meta.MetaM.toIO process { maxHeartbeats := 100000000} { env := env} {} {})
for (_, module) in doc.toList do
let s ← Core.CoreM.toIO module.prettyPrint {} { env := env }
2021-11-27 15:19:56 +00:00
IO.println s.fst
IO.println s!"Processed {List.foldl (λ a (_, b) => a + b.members.size) 0 doc.toList} declarations"
IO.println s!"Processed {doc.size} modules"
2021-11-27 15:19:56 +00:00
return doc
end DocGen4