on bf52f1d help sign in
stackage/Stackage/GhcPkg.hs
37 lines 1.4 kB history raw
1module Stackage.GhcPkg where
3import Stackage.Types
4import System.Process
5import Distribution.Text (simpleParse)
6import Distribution.Version (Version (Version))
7import Data.Char (isSpace)
8import qualified Data.Set as Set
10getGlobalPackages :: GhcMajorVersion -> IO (Set PackageIdentifier)
11getGlobalPackages version = do
12 output <- readProcess "ghc-pkg" [arg, "list"] ""
13 fmap Set.unions $ mapM parse $ drop 1 $ lines output
14 where
15 -- Account for a change in command line option name
16 arg
17 | version >= GhcMajorVersion 7 6 = "--no-user-package-db"
18 | otherwise = "--no-user-package-conf"
19 parse s =
20 case clean s of
21 "" -> return Set.empty
22 s' ->
23 case simpleParse s' of
24 Just x -> return $ Set.singleton x
25 Nothing -> error $ "Could not parse ghc-pkg output: " ++ show s
26 clean = stripParens . dropWhile isSpace . reverse . dropWhile isSpace . reverse
27 stripParens x@('(':_:_)
28 | last x == ')' = tail $ init $ x
29 stripParens x = x
31getGhcVersion :: IO GhcMajorVersion
32getGhcVersion = do
33 versionOutput <- readProcess "ghc-pkg" ["--version"] ""
34 maybe (error $ "Invalid version output: " ++ show versionOutput) return $ do
35 verS:_ <- Just $ reverse $ words versionOutput
36 Version (x:y:_) _ <- simpleParse verS
37 return $ GhcMajorVersion x y