hnix/main/Main.hs

92 lines
3.2 KiB
Haskell
Raw Normal View History

{-# LANGUAGE FlexibleContexts #-}
2018-04-06 06:10:06 +02:00
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
-- {-# LANGUAGE QuasiQuotes #-}
2018-04-12 06:31:48 +02:00
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Main where
2018-04-14 18:44:55 +02:00
import qualified Control.DeepSeq as Deep
2018-04-10 17:34:21 +02:00
import qualified Control.Exception as Exc
import Control.Monad
import Control.Monad.IO.Class
import Control.Monad.ST
import qualified Data.Text.IO as Text
2018-04-14 18:44:55 +02:00
import Nix
import qualified Repl
-- import Nix.TH
import Options.Applicative hiding (ParserResult(..))
import System.IO
import System.FilePath
import Text.PrettyPrint.ANSI.Leijen hiding ((<$>))
main :: IO ()
main = do
opts <- execParser nixOptionsInfo
case readFrom opts of
Just path -> do
let file = addExtension (dropExtension path) "nix"
process opts (Just file) =<< readCache path
Nothing -> case expression opts of
Just s -> handleResult opts Nothing (parseNixTextLoc s)
Nothing -> case fromFile opts of
Just "-" ->
mapM_ (processFile opts) =<< (lines <$> getContents)
Just path ->
mapM_ (processFile opts) =<< (lines <$> readFile path)
Nothing -> case filePaths opts of
[] -> Repl.shell (pure ())
["-"] ->
handleResult opts Nothing . parseNixTextLoc
=<< Text.getContents
paths ->
mapM_ (processFile opts) paths
where
processFile opts path = do
eres <- parseNixFileLoc path
handleResult opts (Just path) eres
2018-04-03 23:24:16 +02:00
-- print . printNix =<< Nix.eval [nix|1 + 3|]
handleResult opts mpath = \case
Failure err ->
(if ignoreErrors opts
then hPutStrLn stderr
else errorWithoutStackTrace) $ "Parse failed: " ++ show err
Success expr -> Exc.catch (process opts mpath expr) $ \case
NixEvalException msg -> errorWithoutStackTrace msg
2018-04-09 11:07:40 +02:00
process opts mpath expr = do
when (check opts) $
putStrLn $ runST $ runLintM . renderSymbolic =<< lint expr
if | evaluate opts, debug opts ->
runLazyM $ evaluateExpression opts mpath
Nix.tracingEvalLoc (liftIO . print) expr
2018-04-12 06:31:48 +02:00
| evaluate opts, not (null (arg opts) && null (argstr opts)) ->
runLazyM $ evaluateExpression opts mpath
Nix.evalLoc (liftIO . print) expr
2018-04-12 06:31:48 +02:00
| evaluate opts -> runLazyM $
processResult opts (liftIO . print)
=<< Nix.evalLoc mpath (include opts) expr
2018-04-12 06:31:48 +02:00
| debug opts -> print $ stripAnnotation expr
| cache opts, Just path <- mpath -> do
let file = addExtension (dropExtension path) "nixc"
writeCache file expr
2018-04-12 06:31:48 +02:00
2018-04-14 18:44:55 +02:00
| parseOnly opts -> void $ Exc.evaluate $ Deep.force expr
2018-04-12 06:31:48 +02:00
| otherwise ->
displayIO stdout
. renderPretty 0.4 80
. prettyNix
. stripAnnotation $ expr
when (repl opts) $ Repl.shell (pure ())