on a072dcc help sign in
stackage/Stackage2/CorePackages.hs
53 lines 1.9 kB history raw
1{-# LANGUAGE NoImplicitPrelude #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Stackage2.CorePackages
4 ( getCorePackages
5 , getCoreExecutables
6 , getGhcVersion
7 ) where
9import qualified Data.Text as T
10import Filesystem (listDirectory)
11import Stackage2.Prelude
12import System.Directory (findExecutable)
14-- | Get a @Map@ of all of the core packages. Core packages are defined as
15-- packages which ship with GHC itself.
16--
17-- Precondition: GHC global package database has only core packages, and GHC
18-- ships with just a single version of each packages.
19getCorePackages :: IO (Map PackageName Version)
20getCorePackages =
21 withCheckedProcess cp $ \ClosedStream src Inherited ->
22 src $$ decodeUtf8C =$ linesUnboundedC =$ foldMapMC parsePackage
23 where
24 cp = proc "ghc-pkg" ["--no-user-package-conf", "list"]
25 parsePackage t
26 | ":" `isInfixOf` t = return mempty
27 | Just p <- stripSuffix "-" p' = singletonMap
28 <$> simpleParse p
29 <*> simpleParse v
30 | otherwise = return mempty
31 where
32 (p', v) = T.breakOnEnd "-" $ dropParens $ T.strip t
34 dropParens s
35 | length s > 2 && headEx s == '(' && lastEx s == ')' =
36 initEx $ tailEx s
37 | otherwise = s
39-- | A list of executables that are shipped with GHC.
40getCoreExecutables :: IO (Set ExeName)
41getCoreExecutables = do
42 mfp <- findExecutable "ghc"
43 dir <-
44 case mfp of
45 Nothing -> error "No ghc executable found on PATH"
46 Just fp -> return $ directory $ fpFromString fp
47 (setFromList . map (ExeName . fpToText . filename)) <$> listDirectory dir
49getGhcVersion :: IO Version
50getGhcVersion = do
51 withCheckedProcess (proc "ghc" ["--numeric-version"]) $
52 \ClosedStream src Inherited ->
53 (src $$ decodeUtf8C =$ foldC) >>= simpleParse