module Distribution.InstalledPackageInfo (
InstalledPackageInfo(..),
installedPackageId,
installedComponentId,
installedOpenUnitId,
sourceComponentName,
requiredSignatures,
ExposedModule(..),
AbiDependency(..),
ParseResult(..), PError(..), PWarning,
emptyInstalledPackageInfo,
parseInstalledPackageInfo,
showInstalledPackageInfo,
showFullInstalledPackageInfo,
showInstalledPackageInfoField,
showSimpleInstalledPackageInfoField,
) where
import Distribution.Compat.Prelude
import Prelude ()
import Data.Set (Set)
import Distribution.Backpack
import Distribution.CabalSpecVersion (cabalSpecLatest)
import Distribution.FieldGrammar
import Distribution.FieldGrammar.FieldDescrs
import Distribution.ModuleName
import Distribution.Package hiding (installedPackageId, installedUnitId)
import Distribution.ParseUtils
import Distribution.Types.ComponentName
import Distribution.Utils.Generic (toUTF8BS)
import qualified Data.Map as Map
import qualified Distribution.Parsec.Common as P
import qualified Distribution.Parsec.Parser as P
import qualified Distribution.Parsec.ParseResult as P
import qualified Text.Parsec.Error as Parsec
import qualified Text.Parsec.Pos as Parsec
import qualified Text.PrettyPrint as Disp
import Distribution.Types.InstalledPackageInfo
import Distribution.Types.InstalledPackageInfo.FieldGrammar
installedComponentId :: InstalledPackageInfo -> ComponentId
installedComponentId ipi =
case unComponentId (installedComponentId_ ipi) of
"" -> mkComponentId (unUnitId (installedUnitId ipi))
_ -> installedComponentId_ ipi
installedOpenUnitId :: InstalledPackageInfo -> OpenUnitId
installedOpenUnitId ipi
= mkOpenUnitId (installedUnitId ipi) (installedComponentId ipi) (Map.fromList (instantiatedWith ipi))
requiredSignatures :: InstalledPackageInfo -> Set ModuleName
requiredSignatures ipi = openModuleSubstFreeHoles (Map.fromList (instantiatedWith ipi))
installedPackageId :: InstalledPackageInfo -> UnitId
installedPackageId = installedUnitId
sourceComponentName :: InstalledPackageInfo -> ComponentName
sourceComponentName ipi =
case sourceLibName ipi of
Nothing -> CLibName
Just qn -> CSubLibName qn
parseInstalledPackageInfo :: String -> ParseResult InstalledPackageInfo
parseInstalledPackageInfo s = case P.readFields (toUTF8BS s) of
Left err -> ParseFailed (NoParse (show err) $ Parsec.sourceLine $ Parsec.errorPos err)
Right fs -> case partitionFields fs of
(fs', _) -> case P.runParseResult $ parseFieldGrammar cabalSpecLatest fs' ipiFieldGrammar of
(ws, Right x) -> ParseOk ws' x where
ws' = map (PWarning . P.showPWarning "") ws
(_, Left (_, errs)) -> ParseFailed (NoParse errs' 0) where
errs' = intercalate "; " $ map (\(P.PError _ msg) -> msg) errs
showInstalledPackageInfo :: InstalledPackageInfo -> String
showInstalledPackageInfo ipi =
showFullInstalledPackageInfo ipi { pkgRoot = Nothing }
showFullInstalledPackageInfo :: InstalledPackageInfo -> String
showFullInstalledPackageInfo = Disp.render . (Disp.$+$ Disp.text "") . prettyFieldGrammar ipiFieldGrammar
showInstalledPackageInfoField :: String -> Maybe (InstalledPackageInfo -> String)
showInstalledPackageInfoField fn =
fmap (\g -> Disp.render . ppField fn . g) $ fieldDescrPretty ipiFieldGrammar fn
showSimpleInstalledPackageInfoField :: String -> Maybe (InstalledPackageInfo -> String)
showSimpleInstalledPackageInfoField fn =
fmap (Disp.renderStyle myStyle .) $ fieldDescrPretty ipiFieldGrammar fn
where
myStyle = Disp.style { Disp.mode = Disp.LeftMode }