git.iko.soy/typeable/cabal2nix
on master help sign in
Decouple option parsing from core of the library
5fb2489Domen Kožar
src/Cabal2nix.hs+12−10
@@ -3,7 +3,8 @@
3{-# LANGUAGE RecordWildCards #-}
3{-# LANGUAGE RecordWildCards #-}
4
4
5module Cabal2nix
5module Cabal2nix
6− ( main, cabal2nix, cabal2nix', cabal2nixWithDB
6+ ( main, cabal2nix, cabal2nix', cabal2nixWithDB, parseArgs
7+ , Options(..)
7 )
8 )
8 where
9 where
9
10
@@ -183,19 +184,15 @@ hpackOverrides :: Derivation -> Derivation
183hpackOverrides = over phaseOverrides (++ "preConfigure = \"hpack\";")
184hpackOverrides = over phaseOverrides (++ "preConfigure = \"hpack\";")
184 . set (libraryDepends . tool . contains (PP.pkg "hpack")) True
185 . set (libraryDepends . tool . contains (PP.pkg "hpack")) True
185
186
186−cabal2nix' :: [String] -> IO (Either Doc Derivation)
187+cabal2nix' :: Options -> IO (Either Doc Derivation)
187−cabal2nix' args = do
188+cabal2nix' opts@Options{..} = do
188− opts@Options {..} <- handleParseResult $ execParserPure defaultPrefs pinfo args
189−
190 pkg <- getPackage optHpack optHackageDb optHackageSnapshot $ Source optUrl (fromMaybe "" optRevision) (maybe UnknownHash Guess optSha256) (fromMaybe "" optSubpath)
189 pkg <- getPackage optHpack optHackageDb optHackageSnapshot $ Source optUrl (fromMaybe "" optRevision) (maybe UnknownHash Guess optSha256) (fromMaybe "" optSubpath)
191 processPackage opts pkg
190 processPackage opts pkg
192
191
193−cabal2nixWithDB :: DB.HackageDB -> [String] -> IO (Either Doc Derivation)
192+cabal2nixWithDB :: DB.HackageDB -> Options -> IO (Either Doc Derivation)
194−cabal2nixWithDB db args = do
193+cabal2nixWithDB db opts@Options{..} = do
195− opts@Options {..} <- handleParseResult $ execParserPure defaultPrefs pinfo args
196 when (isJust optHackageDb) $ hPutStrLn stderr "WARN: HackageDB provided directly; ignoring --hackage-db"
194 when (isJust optHackageDb) $ hPutStrLn stderr "WARN: HackageDB provided directly; ignoring --hackage-db"
197 when (isJust optHackageSnapshot) $ hPutStrLn stderr "WARN: HackageDB provided directly; ignoring --hackage-snapshot"
195 when (isJust optHackageSnapshot) $ hPutStrLn stderr "WARN: HackageDB provided directly; ignoring --hackage-snapshot"
198−
199 pkg <- getPackage' optHpack (return db) $ Source optUrl (fromMaybe "" optRevision) (maybe UnknownHash Guess optSha256) (fromMaybe "" optSubpath)
196 pkg <- getPackage' optHpack (return db) $ Source optUrl (fromMaybe "" optRevision) (maybe UnknownHash Guess optSha256) (fromMaybe "" optSubpath)
200 processPackage opts pkg
197 processPackage opts pkg
201
198
@@ -253,7 +250,12 @@ processPackage Options{..} pkg = do
253 pure $ if optNixShellOutput then Left shell else Right deriv
250 pure $ if optNixShellOutput then Left shell else Right deriv
254
251
255cabal2nix :: [String] -> IO ()
252cabal2nix :: [String] -> IO ()
256−cabal2nix = cabal2nix' >=> putStrLn . either render prettyShow
253+cabal2nix = parseArgs >=> cabal2nix' >=> putStrLn . either render prettyShow
254+
255+parseArgs :: [String] -> IO Options
256+parseArgs = handleParseResult . execParserPure defaultPrefs pinfo
257+
258+-- Utils
257
259
258readFlagList :: [String] -> FlagAssignment
260readFlagList :: [String] -> FlagAssignment
259readFlagList = mkFlagAssignment . map tagWithValue
261readFlagList = mkFlagAssignment . map tagWithValue