on 2944980 help sign in
stackage/Stackage/GhcPkg.hs
49 lines 1.9 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
10getPackages :: [String] -> GhcMajorVersion -> IO (Set PackageIdentifier)
11getPackages extraArgs version = do
12 output <- readProcess "ghc-pkg" (extraArgs ++ [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
31getGlobalPackages :: GhcMajorVersion -> IO (Set PackageIdentifier)
32getGlobalPackages version = getPackages [] version
34getDBPackages :: [FilePath] -> GhcMajorVersion -> IO (Set PackageIdentifier)
35getDBPackages [] _ = return Set.empty
36getDBPackages packageDirs version =
37 getPackages (map packageDbArg packageDirs) version
38 where
39 packageDbArg db
40 | version >= GhcMajorVersion 7 6 = "--package-db=" ++ db
41 | otherwise = "--package-conf" ++ db
43getGhcVersion :: IO GhcMajorVersion
44getGhcVersion = do
45 versionOutput <- readProcess "ghc-pkg" ["--version"] ""
46 maybe (error $ "Invalid version output: " ++ show versionOutput) return $ do
47 verS:_ <- Just $ reverse $ words versionOutput
48 Version (x:y:_) _ <- simpleParse verS
49 return $ GhcMajorVersion x y