1+ {-# LANGUAGE MultiWayIf #-}
12{-# LANGUAGE FlexibleContexts #-}
23{-# LANGUAGE LambdaCase #-}
34{-# LANGUAGE OverloadedStrings #-}
@@ -37,7 +38,7 @@ import Nix.Utils
3738import Nix.Var
3839import Nix.Value.Monad
3940import Options.Applicative hiding ( ParserResult (.. ) )
40- import Prettyprinter
41+ import Prettyprinter hiding ( list )
4142import Prettyprinter.Render.Text
4243import qualified Repl
4344import System.FilePath
@@ -48,25 +49,42 @@ main :: IO ()
4849main = do
4950 time <- getCurrentTime
5051 opts <- execParser (nixOptionsInfo time)
51- runWithBasicEffectsIO opts $
52- case readFrom opts of
53- Nothing -> case expression opts of
54- Nothing -> case fromFile opts of
55- Nothing -> case filePaths opts of
56- [] -> withNixContext mempty Repl. main
57- [" -" ] ->
58- handleResult opts mempty
59- . parseNixTextLoc
60- =<< liftIO Text. getContents
61- paths -> mapM_ (processFile opts) paths
62- Just " -" -> mapM_ (processFile opts) . lines =<< liftIO getContents
63- Just path ->
64- mapM_ (processFile opts) . lines =<< liftIO (readFile path)
65- Just s -> handleResult opts mempty (parseNixTextLoc s)
66- Just path -> do
67- let file = addExtension (dropExtension path) " nixc"
68- process opts (pure file) =<< liftIO (readCache path)
52+
53+ runWithBasicEffectsIO opts $ execContentsFilesOrRepl opts
54+
6955 where
56+ execContentsFilesOrRepl opts =
57+ maybe
58+ (maybe
59+ (maybe
60+ (list
61+ (withNixContext mempty Repl. main) -- run REPL
62+ (\ case
63+ [" -" ] -> handleResult opts mempty . parseNixTextLoc =<< liftIO Text. getContents
64+ _paths -> traverse_ (processFile opts) _paths
65+ )
66+ (filePaths opts)
67+ )
68+ (\ x ->
69+ -- We can start use Text as in the base case, requires changing FilePath -> Text
70+ traverse_ (processFile opts) . lines =<< liftIO
71+ (case x of
72+ " -" -> getContents -- get user input
73+ _path -> readFile _path
74+ )
75+ )
76+ (fromFile opts)
77+ )
78+ (handleResult opts mempty . parseNixTextLoc)
79+ (expression opts)
80+ )
81+ (\ path ->
82+ do
83+ let file = addExtension (dropExtension path) " nixc"
84+ process opts (pure file) =<< liftIO (readCache path)
85+ )
86+ (readFrom opts)
87+
7088 processFile opts path = do
7189 eres <- parseNixFileLoc path
7290 handleResult opts (pure path) eres
@@ -111,57 +129,36 @@ main = do
111129 (evaluate opts)
112130
113131 process opts mpath expr
114- | evaluate opts
115- , tracing opts
116- = evaluateExpression mpath Nix. nixTracingEvalExprLoc printer expr
117- | evaluate opts
118- , Just path <- reduce opts
119- = evaluateExpression mpath (reduction path) printer expr
120- | evaluate opts
121- , not (null (arg opts) && null (argstr opts))
122- = evaluateExpression mpath Nix. nixEvalExprLoc printer expr
123- | evaluate opts
124- = processResult printer =<< Nix. nixEvalExprLoc mpath expr
125- | xml opts
126- = error " Rendering expression trees to XML is not yet implemented"
127- | json opts
128- = error " Rendering expression trees to JSON is not implemented"
129- | verbose opts >= DebugInfo
130- = liftIO $ putStr $ PS. ppShow $ stripAnnotation expr
132+ | evaluate opts =
133+ if
134+ | tracing opts -> evaluateExpression mpath Nix. nixTracingEvalExprLoc printer expr
135+ | Just path <- reduce opts -> evaluateExpression mpath (reduction path) printer expr
136+ | not ( null (arg opts)
137+ && null (argstr opts)
138+ ) -> evaluateExpression mpath Nix. nixEvalExprLoc printer expr
139+ | otherwise -> processResult printer =<< Nix. nixEvalExprLoc mpath expr
140+ | xml opts = error " Rendering expression trees to XML is not yet implemented"
141+ | json opts = error " Rendering expression trees to JSON is not implemented"
142+ | verbose opts >= DebugInfo = liftIO $ putStr $ PS. ppShow $ stripAnnotation expr
131143 | cache opts
132- , Just path <- mpath
133- = liftIO $ writeCache (addExtension (dropExtension path) " nixc" ) expr
134- | parseOnly opts
135- = void $ liftIO $ Exc. evaluate $ Deep. force expr
136- | otherwise
137- = liftIO
138- $ renderIO stdout
139- . layoutPretty (LayoutOptions $ AvailablePerLine 80 0.4 )
140- . prettyNix
141- . stripAnnotation
142- $ expr
144+ , Just path <- mpath = liftIO $ writeCache (addExtension (dropExtension path) " nixc" ) expr
145+ | parseOnly opts = void $ liftIO $ Exc. evaluate $ Deep. force expr
146+ | otherwise =
147+ liftIO $
148+ renderIO
149+ stdout
150+ . layoutPretty (LayoutOptions $ AvailablePerLine 80 0.4 )
151+ . prettyNix
152+ . stripAnnotation $
153+ expr
143154 where
144155 printer
145- | finder opts
146- = fromValue @ (AttrSet (StdValue (StandardT (StdIdT IO )))) >=> findAttrs
147- | xml opts
148- = liftIO
149- . putStrLn
150- . Text. unpack
151- . stringIgnoreContext
152- . toXML
153- <=< normalForm
154- | json opts
155- = liftIO
156- . Text. putStrLn
157- . stringIgnoreContext
158- <=< nvalueToJSONNixString
159- | strict opts
160- = liftIO . print . prettyNValue <=< normalForm
161- | values opts
162- = liftIO . print . prettyNValueProv <=< removeEffects
163- | otherwise
164- = liftIO . print . prettyNValue <=< removeEffects
156+ | finder opts = findAttrs <=< fromValue @ (AttrSet (StdValue (StandardT (StdIdT IO ))))
157+ | xml opts = liftIO . putStrLn . Text. unpack . stringIgnoreContext . toXML <=< normalForm
158+ | json opts = liftIO . Text. putStrLn . stringIgnoreContext <=< nvalueToJSONNixString
159+ | strict opts = liftIO . print . prettyNValue <=< normalForm
160+ | values opts = liftIO . print . prettyNValueProv <=< removeEffects
161+ | otherwise = liftIO . print . prettyNValue <=< removeEffects
165162 where
166163 findAttrs
167164 :: AttrSet (StdValue (StandardT (StdIdT IO )))
@@ -182,7 +179,7 @@ main = do
182179 )
183180 (\ v -> pure (k, pure (Free v)))
184181 nv
185- forM_ xs $ \ (k, mv) -> do
182+ for_ xs $ \ (k, mv) -> do
186183 let path = prefix <> Text. unpack k
187184 (report, descend) = filterEntry path k
188185 when report $ do
0 commit comments