on bf47ded help sign in
stackage/Stackage2/CorePackages.hs
33 lines 1.2 kB history raw
1{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Stackage2.CorePackages
4 ( getCorePackages
5 ) where
7import qualified Data.Text as T
8import Stackage2.Prelude
10-- | Get a @Map@ of all of the core packages. Core packages are defined as
11-- packages which ship with GHC itself.
12--
13-- Precondition: GHC global package database has only core packages, and GHC
14-- ships with just a single version of each packages.
15getCorePackages :: IO (Map PackageName Version)
16getCorePackages =
17 withCheckedProcess cp $ \ClosedStream src Inherited ->
18 src $$ decodeUtf8C =$ linesUnboundedC =$ foldMapMC parsePackage
19 where
20 cp = proc "ghc-pkg" ["--no-user-package-conf", "list"]
21 parsePackage t
22 | ":" `isInfixOf` t = return mempty
23 | Just p <- stripSuffix "-" p' = singletonMap
24 <$> simpleParse p
25 <*> simpleParse v
26 | otherwise = return mempty
27 where
28 (p', v) = T.breakOnEnd "-" $ dropParens $ T.strip t
30 dropParens s
31 | length s > 2 && headEx s == '(' && lastEx s == ')' =
32 initEx $ tailEx s
33 | otherwise = s