1
1
mirror of https://github.com/anoma/juvix.git synced 2024-12-12 14:28:08 +03:00
juvix/app/Main.hs

52 lines
1.5 KiB
Haskell
Raw Normal View History

2022-01-18 14:25:42 +03:00
module Main (main) where
import App
2022-09-14 17:16:15 +03:00
import CommonOptions
2022-04-05 20:57:21 +03:00
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
2022-09-14 17:16:15 +03:00
import TopCommand
import TopCommand.Options
2022-09-14 17:16:15 +03:00
main :: IO ()
main = do
let p = prefs showHelpOnEmpty
(global, cli) <- customExecParser p descr >>= secondM makeAbsPaths
(root, pkg) <- findRoot cli
runM (runAppIO global root pkg (runTopCommand cli))
findRoot :: TopCommand -> IO (FilePath, Package)
findRoot cli = do
let dir :: Maybe FilePath
2022-09-14 17:16:15 +03:00
dir = takeDirectory <$> topCommandInputFile cli
whenJust dir 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
2022-04-05 20:57:21 +03:00
possiblePaths :: FilePath -> [FilePath]
possiblePaths start = takeWhile (/= "/") (aux start)
where
aux f = f : aux (takeDirectory f)
go :: IO (FilePath, Package)
2022-04-05 20:57:21 +03:00
go = do
c <- getCurrentDirectory
l <- findFile (possiblePaths c) Paths.juvixYamlFile
2022-04-05 20:57:21 +03:00
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)