bookshelf-doc/DocGen4/Load.lean

52 lines
1.9 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
2022-02-12 14:09:13 +00:00
| some path => pure $ System.FilePath.mk path
| none =>
let lakePath := (←findSysroot?) / "bin" / "lake"
2022-02-12 14:09:13 +00:00
pure $ lakePath.withExtension System.FilePath.exeExtension
2021-11-27 15:19:56 +00:00
-- Modified from the LSP Server
2022-02-23 21:54:10 +00:00
def lakeSetupSearchPath (lakePath : System.FilePath) (imports : List String) : IO Lean.SearchPath := do
let args := "print-paths" :: imports
let cmdStr := " ".intercalate (toString lakePath :: args)
let lakeProc ← Process.spawn {
stdin := Process.Stdio.null
stdout := Process.Stdio.piped
stderr := Process.Stdio.piped
cmd := lakePath.toString
2022-02-23 21:54:10 +00:00
args := args.toArray
}
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
2021-12-12 12:21:53 +00:00
def load (imports : List Name) : IO AnalyzerResult := do
2021-11-27 15:19:56 +00:00
let env ← importModules (List.map (Import.mk · false) imports) Options.empty
-- TODO parameterize maxHeartbeats
2021-12-15 11:02:05 +00:00
IO.println "Processing modules"
Prod.fst <$> (Meta.MetaM.toIO process { maxHeartbeats := 100000000, options := ⟨[(`pp.tagAppFns, true)]⟩ } { env := env} {} {})
2021-11-27 15:19:56 +00:00
end DocGen4