1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-13 19:49:20 +03:00
juvix/app/Root.hs
Paul Cadman df4036d600
Compute new entrypoint root when loading a file in the REPL (#1615)
* Add shelltests for `juvix repl`

* repl: Compute entrypoint root when loading a file

Previously the REPL would use app root (which could be the current
directory or the project root of the initially loaded file). Thus files
in a different project could not be loaded.

The entrypoint root must be computed each time a new file is `:load`ed.

Adds shell-tests for REPL commands

* Move preludePath to Juvix.Extra.Paths
2022-11-10 11:26:38 +00:00

40 lines
1.2 KiB
Haskell

module Root where
import Control.Exception qualified as IO
import Data.ByteString qualified as ByteString
import Data.Yaml
import Juvix.Compiler.Pipeline
import Juvix.Extra.Paths qualified as Paths
import Juvix.Prelude
findRoot :: Maybe FilePath -> IO (FilePath, Package)
findRoot minputFile = do
whenJust (takeDirectory <$> minputFile) setCurrentDirectory
r <- IO.try go
case r of
Left (err :: IO.SomeException) -> do
putStrLn "Something went wrong when figuring out the root of the project."
putStrLn (pack (IO.displayException err))
exitFailure
Right root -> return root
where
possiblePaths :: FilePath -> [FilePath]
possiblePaths start = takeWhile (/= "/") (aux start)
where
aux f = f : aux (takeDirectory f)
go :: IO (FilePath, Package)
go = do
c <- getCurrentDirectory
l <- findFile (possiblePaths c) Paths.juvixYamlFile
case l of
Nothing -> return (c, emptyPackage)
Just yaml -> do
bs <- ByteString.readFile yaml
let isEmpty = ByteString.null bs
pkg <-
if
| isEmpty -> return emptyPackage
| otherwise -> decodeThrow bs
return (takeDirectory yaml, pkg)