feat(library/init/lean): add lean/path.lean
This commit is contained in:
parent
798bbc0662
commit
5a1380ec6a
3 changed files with 71 additions and 2 deletions
|
|
@ -4,6 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
|
|||
Authors: Leonardo de Moura
|
||||
-/
|
||||
prelude
|
||||
import init.lean.path
|
||||
import init.lean.compiler
|
||||
import init.lean.environment
|
||||
import init.lean.modifiers
|
||||
|
|
|
|||
|
|
@ -343,10 +343,10 @@ catch
|
|||
|
||||
/- TODO: support for non-standard search path for web interface -/
|
||||
@[extern 1 "lean_set_search_path"]
|
||||
constant setSearchPath : IO Unit := default _
|
||||
constant setSearchPathOld : IO Unit := default _
|
||||
|
||||
def testFrontend (input : String) (fileName : Option String := none) : IO (Environment × MessageLog) :=
|
||||
do setSearchPath;
|
||||
do setSearchPathOld;
|
||||
env ← mkEmptyEnvironment;
|
||||
let fileName := fileName.getOrElse "<input>";
|
||||
let ctx := Parser.mkParserContextCore env input fileName;
|
||||
|
|
|
|||
68
library/init/lean/path.lean
Normal file
68
library/init/lean/path.lean
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/-
|
||||
Copyright (c) 2019 Microsoft Corporation. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
Authors: Leonardo de Moura
|
||||
-/
|
||||
prelude
|
||||
import init.system.io
|
||||
import init.system.filepath
|
||||
import init.data.array
|
||||
|
||||
namespace Lean
|
||||
open System.FilePath (pathSeparator searchPathSeparator)
|
||||
private def pathSep : String := toString pathSeparator
|
||||
private def searchPathSep : String := toString searchPathSeparator
|
||||
|
||||
def mkSearchPathRef : IO (IO.Ref (Array String)) :=
|
||||
do curr ← IO.realPath ".";
|
||||
IO.mkRef (Array.singleton curr)
|
||||
|
||||
@[init mkSearchPathRef]
|
||||
constant searchPathRef : IO.Ref (Array String) := default _
|
||||
|
||||
def setSearchPath (s : String) : IO Unit :=
|
||||
searchPathRef.set (s.split searchPathSep).toArray
|
||||
|
||||
def getBuiltinSearchPath : IO String :=
|
||||
do appDir ← IO.appDir;
|
||||
let libDir := appDir ++ pathSep ++ ".." ++ pathSep ++ "library";
|
||||
IO.println libDir;
|
||||
libDirExists ← IO.isDir libDir;
|
||||
if libDirExists then
|
||||
IO.realPath libDir
|
||||
else do
|
||||
let installedLibDir := appDir ++ pathSep ++ ".." ++ pathSep ++ "lib" ++ pathSep ++ "lean" ++ pathSep ++ "library";
|
||||
installedLibDirExists ← IO.isDir installedLibDir;
|
||||
IO.println installedLibDir;
|
||||
if installedLibDirExists then do
|
||||
IO.realPath installedLibDir
|
||||
else
|
||||
throw (IO.userError "failed to locate builtin search path, please set LEAN_PATH")
|
||||
|
||||
def getSearchPathFromEnv : IO (Option (List String)) :=
|
||||
do val ← IO.getEnv "LEAN_PATH";
|
||||
match val with
|
||||
| none => pure none
|
||||
| some val =>
|
||||
pure $ val.split searchPathSep
|
||||
|
||||
def initSearchPath (path : Option String := "") : IO Unit :=
|
||||
match path with
|
||||
| some path => setSearchPath path
|
||||
| none => do
|
||||
path ← getSearchPathFromEnv;
|
||||
match path with
|
||||
| some path => searchPathRef.set path.toArray
|
||||
| none => do
|
||||
path ← getBuiltinSearchPath;
|
||||
curr ← IO.realPath ".";
|
||||
searchPathRef.set [path, curr].toArray
|
||||
|
||||
def findFile (fname : String) : IO (Option String) :=
|
||||
do paths ← searchPathRef.get;
|
||||
paths.mfind $ fun path => do
|
||||
let curr := path ++ pathSep ++ fname;
|
||||
ex ← IO.fileExists curr;
|
||||
if ex then pure (some curr) else pure none
|
||||
|
||||
end Lean
|
||||
Loading…
Add table
Reference in a new issue