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