git.iko.soy/typeable/cabal2nix
on master help sign in
cabal2nix: switch command line parsing to optparse-applicative
commit513a5fce6cfabe0b062424f6deb191a12f7e2187 authorPeter Simons <simons@cryp.to> dateSun 31 May 2015, 13:19 (11 years ago) parente03009b
3 files changed+58−78
generate-cabal-file.hs+2 release.nix+1−1 src/cabal2nix.hs+55−77
generate-cabal-file.hs+2
@@ -60,6 +60,7 @@ commonBuildDepends =
6060 [ buildDepends
6161 [ package "base" (lt [5])
6262 , unconstrained "aeson"
63+ , unconstrained "ansi-wl-pprint"
6364 , unconstrained "bytestring"
6465 , package "Cabal" (gtEq [1,22,2])
6566 , unconstrained "containers"
@@ -71,6 +72,7 @@ commonBuildDepends =
7172 , unconstrained "monad-par"
7273 , unconstrained "monad-par-extras"
7374 , unconstrained "mtl"
75+ , unconstrained "optparse-applicative"
7476 , package "pretty" (gtEq [1,1,2])
7577 , unconstrained "process"
7678 , unconstrained "regex-posix"
release.nix+1−1
@@ -27,7 +27,7 @@ in
2727 aeson base bytestring Cabal containers deepseq deepseq-generics
2828 directory filepath hackage-db monad-par monad-par-extras mtl pretty
2929 process QuickCheck regex-posix SHA split transformers
30− utf8-string lens
30+ utf8-string lens optparse-applicative
3131 ];
3232 testDepends = with haskellPackages; [
3333 aeson base bytestring Cabal containers deepseq deepseq-generics
src/cabal2nix.hs+55−77
@@ -5,102 +5,80 @@ import Cabal2Nix.Normalize ( normalize )
55import Cabal2Nix.Package
66import Cabal2Nix.Version
77import Control.Exception ( bracket )
8−import Control.Monad ( when )
9−import Distribution.Nixpkgs.Haskell hiding ( version )
8+import Data.Maybe ( fromMaybe )
9+import qualified Data.Set as Set
1010import Distribution.Nixpkgs.Fetch
11−import Distribution.Nixpkgs.Util.PrettyPrinting
11+import Distribution.Nixpkgs.Haskell hiding ( version )
12+import Distribution.Nixpkgs.Util.PrettyPrinting hiding ( (<>) )
1213import Distribution.PackageDescription ( FlagName(..), FlagAssignment )
13−import System.Console.GetOpt ( OptDescr(..), ArgDescr(..), ArgOrder(..), usageInfo, getOpt )
14−import System.Environment ( getArgs )
15−import System.Exit ( exitFailure, exitSuccess )
16−import System.IO ( hPutStrLn, hFlush, stdout, stderr )
1714import Distribution.Simple.Utils ( lowercase )
18−import qualified Data.Set as Set
15+import Options.Applicative
16+import System.IO ( hFlush, stdout, stderr )
17+import qualified Text.PrettyPrint.ANSI.Leijen as P2 hiding ( (<$>), (<>) )
1918
20−data Configuration = Configuration
21− { optPrintHelp :: Bool
22− , optPrintVersion :: Bool
23− , optSha256 :: Maybe String
19+data Options = Options
20+ { optSha256 :: Maybe String
2421 , optMaintainer :: [String]
2522 , optPlatform :: [String]
2623 , optHaddock :: Bool
2724 , optDoCheck :: Bool
2825 , optJailbreak :: Bool
29− , optRevision :: String
26+ , optRevision :: Maybe String
3027 , optHyperlinkSource :: Bool
3128 , optHackageDb :: Maybe FilePath
3229 , optNixShellOutput :: Bool
3330 , optFlags :: [String]
31+ , optUrl :: String
3432 }
3533 deriving (Show)
3634
37−defaultConfiguration :: Configuration
38−defaultConfiguration = Configuration
39− { optPrintHelp = False
40− , optPrintVersion = False
41− , optSha256 = Nothing
42− , optMaintainer = []
43− , optPlatform = []
44− , optHaddock = True
45− , optDoCheck = True
46− , optJailbreak = False
47− , optRevision = ""
48− , optHyperlinkSource = True
49− , optHackageDb = Nothing
50− , optNixShellOutput = False
51− , optFlags = []
52− }
53−
54−options :: [OptDescr (Configuration -> Configuration)]
55−options =
56− [ Option "h" ["help"] (NoArg (\o -> o { optPrintHelp = True })) "show this help text"
57− , Option "v" ["version"] (NoArg (\o -> o { optPrintVersion = True })) "print version"
58− , Option "" ["hackage-db"] (ReqArg (\x o -> o { optHackageDb = Just x }) "FILEPATH") "path to the local hackage db in tar format"
59− , Option "" ["sha256"] (ReqArg (\x o -> o { optSha256 = Just x }) "HASH") "sha256 hash of source tarball"
60− , Option "m" ["maintainer"] (ReqArg (\x o -> o { optMaintainer = x : optMaintainer o }) "MAINTAINER") "maintainer of this package (may be specified multiple times)"
61− , Option "p" ["platform"] (ReqArg (\x o -> o { optPlatform = x : optPlatform o }) "PLATFORM") "supported build platforms (may be specified multiple times)"
62− , Option "f" ["flag"] (ReqArg (\x o -> o { optFlags = x : optFlags o }) "FLAG") "Cabal flag (may be specified multiple times)"
63− , Option "" ["jailbreak"] (NoArg (\o -> o { optJailbreak = True })) "don't honor version restrictions on build inputs"
64− , Option "" ["no-haddock"] (NoArg (\o -> o { optHaddock = False })) "don't run Haddock when building this package"
65− , Option "" ["no-check"] (NoArg (\o -> o { optDoCheck = False })) "don't run regression test suites of this package"
66− , Option "" ["rev"] (ReqArg (\x o -> o { optRevision = x }) "REVISION") "revision, only used when fetching from VCS"
67− , Option "" ["no-hyperlink-source"] (NoArg (\o -> o { optHyperlinkSource = False })) "don't add pretty-printed source code to the documentation"
68− , Option "" ["shell"] (NoArg (\o -> o { optNixShellOutput = True })) "generate output suitable for nix-shell"
69− ]
70−
71−usage :: String
72−usage = usageInfo "Usage: cabal2nix [options] URI" options ++ unlines
73− [ ""
74− , "Recognized URI schemes:"
75− , ""
76− , " cabal://pkgname-pkgversion download the specified package from Hackage"
77− , " cabal://pkgname download latest version of the specified package from Hackage"
78− , " file:///local/path load the Cabal file from the local disk"
79− , " /local/path abbreviated version of file URI"
80− , " <git/svn/bzr/hg URL> download the source from the specified repository"
81− , ""
82− , "If the URI refers to a cabal file, information for building the package will be retrieved from that file, but "
83− , "hackage will be used as a source for the derivation."
84− , "Otherwise, the supplied URI will be used to as the source for the derivation and the information is taken"
85− , "from the cabal file at the root of the downloaded source."
86− ]
87−
88−cmdlineError :: String -> IO a
89−cmdlineError "" = hPutStrLn stderr usage >> exitFailure
90−cmdlineError errMsg = hPutStrLn stderr errMsg >> cmdlineError ""
35+options :: Parser Options
36+options = Options
37+ <$> optional (strOption $ long "sha256" <> metavar "HASH" <> help "sha256 hash of source tarball")
38+ <*> many (strOption $ long "maintainer" <> metavar "MAINTAINER" <> help "maintainer of this package (may be specified multiple times)")
39+ <*> many (strOption $ long "platform" <> metavar "PLATFORM" <> help "supported build platforms (may be specified multiple times)")
40+ <*> flag True False (long "no-haddock" <> help "don't run Haddock when building this package")
41+ <*> flag True False (long "no-check" <> help "don't run regression test suites of this package")
42+ <*> switch (long "jailbreak" <> help "disregard version restrictions on build inputs")
43+ <*> optional (strOption $ long "revision" <> help "revision to use when fetching from VCS")
44+ <*> flag True False (long "no-hyperlink-source" <> help "don't generate pretty-printed source code for the documentation")
45+ <*> optional (strOption $ long "hackage-db" <> metavar "PATH" <> help "path to the local hackage db in tar format")
46+ <*> switch (long "shell" <> help "generate output suitable for nix-shell")
47+ <*> many (strOption $ short 'f' <> long "flag" <> help "Cabal flag (may be specified multiple times)")
48+ <*> strArgument (metavar "URI")
49+
50+pinfo :: ParserInfo Options
51+pinfo = info
52+ ( helper
53+ <*> infoOption ("cabal2nix " ++ version) (long "version" <> help "Show version number")
54+ <*> options
55+ )
56+ ( fullDesc
57+ <> header "cabal2nix converts Cabal files into build instructions for Nix."
58+ <> progDescDoc (Just (P2.vcat
59+ [ P2.text ""
60+ , P2.text "Recognized URI schemes:"
61+ , P2.text ""
62+ , P2.text " cabal://pkgname-pkgversion download the specified package from Hackage"
63+ , P2.text " cabal://pkgname download latest version of this package from Hackage"
64+ , P2.text " file:///local/path load the Cabal file from the local disk"
65+ , P2.text " /local/path abbreviated version of file URI"
66+ , P2.text " <git/svn/bzr/hg URL> download the source from the specified repository"
67+ , P2.text ""
68+ , P2.fillSep (map P2.text (words ( "If the URI refers to a cabal file, information for building the package "
69+ ++ "will be retrieved from that file, but hackage will be used as a source "
70+ ++ "for the derivation. Otherwise, the supplied URI will be used to as the "
71+ ++ "souce for the derivation and the information is taken from the cabal file "
72+ ++ "at the root of the downloaded source."
73+ )))
74+ ]))
75+ )
9176
9277main :: IO ()
9378main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
94− args' <- getArgs
95− (cfg,args) <- case getOpt Permute options args' of
96− (o,n,[] ) -> return (foldl (flip ($)) defaultConfiguration o,n)
97− (_,_,errs) -> cmdlineError (concatMap ("*** "++) errs)
98−
99− when (optPrintHelp cfg) (putStr usage >> exitSuccess)
100− when (optPrintVersion cfg) (putStrLn version >> exitSuccess)
101− when (length args /= 1) (cmdlineError "*** exactly one url-to-cabal-file must be specified\n")
79+ cfg <- execParser pinfo
10280
103− pkg <- getPackage (optHackageDb cfg) $ Source (head args) (optRevision cfg) (optSha256 cfg)
81+ pkg <- getPackage (optHackageDb cfg) $ Source (optUrl cfg) (fromMaybe "" (optRevision cfg)) (optSha256 cfg)
10482
10583 let flags = readFlagList (optFlags cfg)
10684
@@ -128,7 +106,7 @@ main = bracket (return ()) (\() -> hFlush stdout >> hFlush stderr) $ \() -> do
128106 , text ""
129107 , text " inherit (nixpkgs) pkgs;"
130108 , text ""
131− , text " f = " <> expr <> semi
109+ , hcat [ text " f = ", expr, semi ]
132110 , text ""
133111 , text " drv = pkgs.haskell.packages.${compiler}.callPackage f {};"
134112 , text ""