2018-04-06 06:10:06 +02:00
|
|
|
{-# LANGUAGE LambdaCase #-}
|
2018-03-30 00:35:12 +02:00
|
|
|
{-# LANGUAGE MultiWayIf #-}
|
2018-04-04 02:15:54 +02:00
|
|
|
-- {-# LANGUAGE QuasiQuotes #-}
|
2018-03-30 00:35:12 +02:00
|
|
|
|
2015-06-23 23:53:07 +02:00
|
|
|
module Main where
|
|
|
|
|
2018-04-12 05:46:52 +02:00
|
|
|
import Control.Arrow (second)
|
2018-04-10 17:34:21 +02:00
|
|
|
import Control.DeepSeq
|
|
|
|
import qualified Control.Exception as Exc
|
2018-04-03 23:21:33 +02:00
|
|
|
import Control.Monad
|
2018-04-04 22:33:53 +02:00
|
|
|
import Control.Monad.ST
|
2018-04-12 05:46:52 +02:00
|
|
|
import Data.Fix
|
|
|
|
import qualified Data.HashMap.Lazy as M
|
2018-04-10 02:12:16 +02:00
|
|
|
import qualified Data.Text.IO as Text
|
2018-04-03 23:21:33 +02:00
|
|
|
import qualified Nix
|
2018-04-12 05:46:52 +02:00
|
|
|
import Nix.Cache
|
2018-04-12 05:53:38 +02:00
|
|
|
import Nix.Exec (Lazy, runLazyM)
|
2018-04-11 06:29:48 +02:00
|
|
|
import Nix.Expr
|
2018-04-03 23:21:33 +02:00
|
|
|
import Nix.Lint
|
2018-04-12 05:46:52 +02:00
|
|
|
import Nix.Normal
|
2018-04-12 05:53:38 +02:00
|
|
|
import Nix.Options
|
2018-04-03 23:21:33 +02:00
|
|
|
import Nix.Parser
|
|
|
|
import Nix.Pretty
|
2018-04-10 21:25:34 +02:00
|
|
|
import Nix.Stack (NixException(..))
|
2018-04-12 05:46:52 +02:00
|
|
|
import qualified Nix.Value as V
|
2018-04-03 23:40:49 +02:00
|
|
|
-- import Nix.TH
|
2018-04-03 23:21:33 +02:00
|
|
|
import Options.Applicative hiding (ParserResult(..))
|
|
|
|
import System.IO
|
2018-04-11 23:35:17 +02:00
|
|
|
import System.FilePath
|
2018-04-03 23:21:33 +02:00
|
|
|
import Text.PrettyPrint.ANSI.Leijen hiding ((<$>))
|
2015-06-23 23:53:07 +02:00
|
|
|
|
|
|
|
main :: IO ()
|
|
|
|
main = do
|
2018-04-12 05:53:38 +02:00
|
|
|
opts <- execParser nixOptionsInfo
|
2018-04-11 23:35:17 +02:00
|
|
|
case readFrom opts of
|
|
|
|
Just path -> do
|
2018-04-12 05:19:59 +02:00
|
|
|
let file = addExtension (dropExtension path) "nix"
|
|
|
|
process opts (Just file) =<< readCache path
|
2018-04-11 23:35:17 +02:00
|
|
|
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
|
|
|
|
[] ->
|
|
|
|
handleResult opts Nothing . parseNixTextLoc
|
|
|
|
=<< Text.getContents
|
|
|
|
["-"] ->
|
|
|
|
handleResult opts Nothing . parseNixTextLoc
|
|
|
|
=<< Text.getContents
|
|
|
|
paths ->
|
|
|
|
mapM_ (processFile opts) paths
|
2018-04-06 05:43:13 +02:00
|
|
|
where
|
|
|
|
processFile opts path = do
|
|
|
|
eres <- parseNixFileLoc path
|
|
|
|
handleResult opts (Just path) eres
|
2018-03-28 06:59:27 +02:00
|
|
|
|
2018-04-03 23:24:16 +02:00
|
|
|
-- print . printNix =<< Nix.eval [nix|1 + 3|]
|
2018-04-03 23:21:33 +02:00
|
|
|
|
2018-04-06 05:43:13 +02:00
|
|
|
handleResult opts mpath = \case
|
2018-04-10 06:35:46 +02:00
|
|
|
Failure err ->
|
2018-04-10 18:02:02 +02:00
|
|
|
(if ignoreErrors opts
|
|
|
|
then hPutStrLn stderr
|
|
|
|
else errorWithoutStackTrace) $ "Parse failed: " ++ show err
|
2018-04-10 06:35:46 +02:00
|
|
|
|
2018-04-11 23:35:17 +02:00
|
|
|
Success expr -> Exc.catch (process opts mpath expr) $ \case
|
2018-04-10 21:25:34 +02:00
|
|
|
NixEvalException msg -> errorWithoutStackTrace msg
|
2018-04-09 11:07:40 +02:00
|
|
|
|
2018-04-11 23:35:17 +02:00
|
|
|
process opts mpath expr = do
|
|
|
|
when (check opts) $
|
|
|
|
putStrLn $ runST $ Nix.runLintM . renderSymbolic
|
|
|
|
=<< Nix.lint expr
|
2018-04-04 22:33:53 +02:00
|
|
|
|
2018-04-12 05:46:52 +02:00
|
|
|
let parseArg s = case parseNixText s of
|
|
|
|
Success x -> x
|
|
|
|
Failure err -> errorWithoutStackTrace (show err)
|
|
|
|
|
|
|
|
args <- traverse (traverse (Nix.eval Nothing)) $
|
|
|
|
map (second parseArg) (arg opts) ++
|
|
|
|
map (second mkStr) (argstr opts)
|
|
|
|
|
|
|
|
let argmap :: Lazy IO (V.NValue (Lazy IO))
|
|
|
|
argmap = embed $ Fix $ V.NVSet (M.fromList args) mempty
|
|
|
|
|
|
|
|
compute ev x p = do
|
|
|
|
f <- ev mpath x
|
|
|
|
p =<< case f of
|
|
|
|
Fix (V.NVClosure _ g) ->
|
|
|
|
runLazyM $ normalForm =<< g argmap
|
|
|
|
_ -> pure f
|
2018-04-11 06:29:48 +02:00
|
|
|
|
2018-04-11 23:35:17 +02:00
|
|
|
if | evaluate opts, debug opts ->
|
2018-04-12 05:46:52 +02:00
|
|
|
compute Nix.tracingEvalLoc expr print
|
|
|
|
| evaluate opts, not (null args) ->
|
|
|
|
compute Nix.evalLoc expr (putStrLn . printNix)
|
2018-04-11 23:35:17 +02:00
|
|
|
| evaluate opts ->
|
|
|
|
putStrLn . printNix =<< Nix.evalLoc mpath expr
|
|
|
|
| debug opts ->
|
|
|
|
print $ stripAnnotation expr
|
2018-04-12 05:19:59 +02:00
|
|
|
| cache opts, Just path <- mpath -> do
|
|
|
|
let file = addExtension (dropExtension path) "nixc"
|
|
|
|
writeCache file expr
|
2018-04-11 23:35:17 +02:00
|
|
|
| parseOnly opts ->
|
|
|
|
void $ Exc.evaluate $ force expr
|
|
|
|
| otherwise ->
|
|
|
|
displayIO stdout
|
|
|
|
. renderPretty 0.4 80
|
|
|
|
. prettyNix
|
|
|
|
. stripAnnotation $ expr
|