-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.InstalledPackageInfo
-- Copyright   :  (c) The University of Glasgow 2004
--
-- Maintainer  :  libraries@haskell.org
-- Portability :  portable
--
-- This is the information about an /installed/ package that
-- is communicated to the @ghc-pkg@ program in order to register
-- a package.  @ghc-pkg@ now consumes this package format (as of version
-- 6.4). This is specific to GHC at the moment.
--
-- The @.cabal@ file format is for describing a package that is not yet
-- installed. It has a lot of flexibility, like conditionals and dependency
-- ranges. As such, that format is not at all suitable for describing a package
-- that has already been built and installed. By the time we get to that stage,
-- we have resolved all conditionals and resolved dependency version
-- constraints to exact versions of dependent packages. So, this module defines
-- the 'InstalledPackageInfo' data structure that contains all the info we keep
-- about an installed package. There is a parser and pretty printer. The
-- textual format is rather simpler than the @.cabal@ format: there are no
-- sections, for example.

{- All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.

    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.

    * Neither the name of the University nor the names of other
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}

-- This module is meant to be local-only to Distribution...

module Distribution.InstalledPackageInfo (
        InstalledPackageInfo_(..), InstalledPackageInfo,
        ParseResult(..), PError(..), PWarning,
        emptyInstalledPackageInfo,
        parseInstalledPackageInfo,
        showInstalledPackageInfo,
        showInstalledPackageInfoField,
        fieldsInstalledPackageInfo,
  ) where

import Distribution.ParseUtils
         ( FieldDescr(..), ParseResult(..), PError(..), PWarning
         , simpleField, listField, parseLicenseQ
         , showFields, showSingleNamedField, parseFieldsFlat
         , parseFilePathQ, parseTokenQ, parseModuleNameQ, parsePackageNameQ
         , showFilePath, showToken, boolField, parseOptVersion
         , parseFreeText, showFreeText )
import Distribution.License     ( License(..) )
import Distribution.Package
         ( PackageName(..), PackageIdentifier(..), PackageId, InstalledPackageId(..)
         , packageName, packageVersion )
import qualified Distribution.Package as Package
         ( Package(..) )
import Distribution.ModuleName
         ( ModuleName )
import Distribution.Version
         ( Version(..) )
import Distribution.Text
         ( Text(disp, parse) )

-- -----------------------------------------------------------------------------
-- The InstalledPackageInfo type

data installedPackageId :: InstalledPackageIdInstalledPackageInfo_ m
   = InstalledPackageInfo {
        -- these parts are exactly the same as PackageDescription
        installedPackageId :: InstalledPackageId,
        sourcePackageId    :: PackageId,
        license           :: License,
        copyright         :: String,
        maintainer        :: String,
        author            :: String,
        stability         :: String,
        homepage          :: String,
        pkgUrl            :: String,
        synopsis          :: String,
        description       :: String,
        category          :: String,
        -- these parts are required by an installed package only:
        exposed           :: Bool,
        exposedModules    :: [m],
        hiddenModules     :: [m],
        importDirs        :: [FilePath],  -- contain sources in case of Hugs
        libraryDirs       :: [FilePath],
        hsLibraries       :: [String],
        extraLibraries    :: [String],
        extraGHCiLibraries:: [String],    -- overrides extraLibraries for GHCi
        includeDirs       :: [FilePath],
        includes          :: [String],
        depends           :: [InstalledPackageId],
        hugsOptions       :: [String],
        ccOptions         :: [String],
        ldOptions         :: [String],
        frameworkDirs     :: [FilePath],
        frameworks        :: [String],
        haddockInterfaces :: [FilePath],
        haddockHTMLs      :: [FilePath]
    }
    deriving (D:Read ::
  (Int -> ReadS a)
  -> ReadS [a]
  -> ReadPrec a
  -> ReadPrec [a]
  -> T:Read aRead, D:Show ::
  (Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> T:Show aShow)

instance ($cpackageId) :: InstalledPackageInfo_ str -> PackageIdentifierPackage.Package          (InstalledPackageInfo_ str) where
   packageId = sourcePackageId :: InstalledPackageInfo_ m -> PackageIdsourcePackageId

type InstalledPackageInfo = InstalledPackageInfo_ ModuleName

emptyInstalledPackageInfo :: InstalledPackageInfo_ m
emptyInstalledPackageInfo
   = InstalledPackageInfo {
        installedPackageId = InstalledPackageId :: String -> InstalledPackageIdInstalledPackageId "",
        sourcePackageId    = PackageIdentifier :: PackageName -> Version -> PackageIdentifierPackageIdentifier (PackageName :: String -> PackageNamePackageName "") noVersion :: VersionnoVersion,
        license           = AllRightsReserved :: LicenseAllRightsReserved,
        copyright         = "",
        maintainer        = "",
        author            = "",
        stability         = "",
        homepage          = "",
        pkgUrl            = "",
        synopsis          = "",
        description       = "",
        category          = "",
        exposed           = False :: BoolFalse,
        exposedModules    = [] :: [a][],
        hiddenModules     = [] :: [a][],
        importDirs        = [] :: [a][],
        libraryDirs       = [] :: [a][],
        hsLibraries       = [] :: [a][],
        extraLibraries    = [] :: [a][],
        extraGHCiLibraries= [] :: [a][],
        includeDirs       = [] :: [a][],
        includes          = [] :: [a][],
        depends           = [] :: [a][],
        hugsOptions       = [] :: [a][],
        ccOptions         = [] :: [a][],
        ldOptions         = [] :: [a][],
        frameworkDirs     = [] :: [a][],
        frameworks        = [] :: [a][],
        haddockInterfaces = [] :: [a][],
        haddockHTMLs      = [] :: [a][]
    }

noVersion :: Version
noVersion = Version{ versionBranch=[] :: [a][], versionTags=[] :: [a][] }

-- -----------------------------------------------------------------------------
-- Parsing

parseInstalledPackageInfo :: String -> ParseResult InstalledPackageInfo
parseInstalledPackageInfo =
    parseFieldsFlat :: [FieldDescr a] -> a -> String -> ParseResult aparseFieldsFlat fieldsInstalledPackageInfo :: [FieldDescr InstalledPackageInfo]fieldsInstalledPackageInfo emptyInstalledPackageInfo :: InstalledPackageInfo_ memptyInstalledPackageInfo

-- -----------------------------------------------------------------------------
-- Pretty-printing

showInstalledPackageInfo :: InstalledPackageInfo -> String
showInstalledPackageInfo = showFields :: [FieldDescr a] -> a -> StringshowFields fieldsInstalledPackageInfo :: [FieldDescr InstalledPackageInfo]fieldsInstalledPackageInfo

showInstalledPackageInfoField :: String -> Maybe (InstalledPackageInfo -> String)
showInstalledPackageInfoField = showSingleNamedField ::
  [FieldDescr a] -> String -> Maybe (a -> String)showSingleNamedField fieldsInstalledPackageInfo :: [FieldDescr InstalledPackageInfo]fieldsInstalledPackageInfo

-- -----------------------------------------------------------------------------
-- Description of the fields, for parsing/printing

fieldsInstalledPackageInfo :: [FieldDescr InstalledPackageInfo]
fieldsInstalledPackageInfo = basicFieldDescrs :: [FieldDescr InstalledPackageInfo]basicFieldDescrs (++) :: [a] -> [a] -> [a]++ installedFieldDescrs :: [FieldDescr InstalledPackageInfo]installedFieldDescrs

basicFieldDescrs :: [FieldDescr InstalledPackageInfo]
basicFieldDescrs =
 [ simpleField ::
  String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr bsimpleField "name"
                           disp :: Text a => a -> Docdisp                   parsePackageNameQ :: ReadP r PackageNameparsePackageNameQ
                           packageName :: Package pkg => pkg -> PackageNamepackageName            (\name pkg -> pkg :: InstalledPackageInfopkg{sourcePackageId=(sourcePackageId :: InstalledPackageInfo_ m -> PackageIdsourcePackageId pkg :: InstalledPackageInfopkg){pkgName=name :: PackageNamename}})
 , simpleField ::
  String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr bsimpleField "version"
                           disp :: Text a => a -> Docdisp                   parseOptVersion :: ReadP r VersionparseOptVersion
                           packageVersion :: Package pkg => pkg -> VersionpackageVersion         (\ver pkg -> pkg :: InstalledPackageInfopkg{sourcePackageId=(sourcePackageId :: InstalledPackageInfo_ m -> PackageIdsourcePackageId pkg :: InstalledPackageInfopkg){pkgVersion=ver :: Versionver}})
 , simpleField ::
  String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr bsimpleField "id"
                           disp :: Text a => a -> Docdisp                   parse :: Text a => forall r. ReadP r aparse
                           installedPackageId :: InstalledPackageInfo_ m -> InstalledPackageIdinstalledPackageId     (\ipid pkg -> pkg :: InstalledPackageInfopkg{installedPackageId=ipid :: InstalledPackageIdipid})
 , simpleField ::
  String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr bsimpleField "license"
                           disp :: Text a => a -> Docdisp                   parseLicenseQ :: ReadP r LicenseparseLicenseQ
                           license :: InstalledPackageInfo_ m -> Licenselicense                (\l pkg -> pkg :: InstalledPackageInfopkg{license=l :: Licensel})
 , simpleField ::
  String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr bsimpleField "copyright"
                           showFreeText :: String -> DocshowFreeText           parseFreeText :: ReadP s StringparseFreeText
                           copyright :: InstalledPackageInfo_ m -> Stringcopyright              (\val pkg -> pkg :: InstalledPackageInfopkg{copyright=val :: Stringval})
 , simpleField ::
  String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr bsimpleField "maintainer"
                           showFreeText :: String -> DocshowFreeText           parseFreeText :: ReadP s StringparseFreeText
                           maintainer :: InstalledPackageInfo_ m -> Stringmaintainer             (\val pkg -> pkg :: InstalledPackageInfopkg{maintainer=val :: Stringval})
 , simpleField ::
  String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr bsimpleField "stability"
                           showFreeText :: String -> DocshowFreeText           parseFreeText :: ReadP s StringparseFreeText
                           stability :: InstalledPackageInfo_ m -> Stringstability              (\val pkg -> pkg :: InstalledPackageInfopkg{stability=val :: Stringval})
 , simpleField ::
  String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr bsimpleField "homepage"
                           showFreeText :: String -> DocshowFreeText           parseFreeText :: ReadP s StringparseFreeText
                           homepage :: InstalledPackageInfo_ m -> Stringhomepage               (\val pkg -> pkg :: InstalledPackageInfopkg{homepage=val :: Stringval})
 , simpleField ::
  String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr bsimpleField "package-url"
                           showFreeText :: String -> DocshowFreeText           parseFreeText :: ReadP s StringparseFreeText
                           pkgUrl :: InstalledPackageInfo_ m -> StringpkgUrl                 (\val pkg -> pkg :: InstalledPackageInfopkg{pkgUrl=val :: Stringval})
 , simpleField ::
  String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr bsimpleField "synopsis"
                           showFreeText :: String -> DocshowFreeText           parseFreeText :: ReadP s StringparseFreeText
                           synopsis :: InstalledPackageInfo_ m -> Stringsynopsis               (\val pkg -> pkg :: InstalledPackageInfopkg{synopsis=val :: Stringval})
 , simpleField ::
  String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr bsimpleField "description"
                           showFreeText :: String -> DocshowFreeText           parseFreeText :: ReadP s StringparseFreeText
                           description :: InstalledPackageInfo_ m -> Stringdescription            (\val pkg -> pkg :: InstalledPackageInfopkg{description=val :: Stringval})
 , simpleField ::
  String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr bsimpleField "category"
                           showFreeText :: String -> DocshowFreeText           parseFreeText :: ReadP s StringparseFreeText
                           category :: InstalledPackageInfo_ m -> Stringcategory               (\val pkg -> pkg :: InstalledPackageInfopkg{category=val :: Stringval})
 , simpleField ::
  String
  -> (a -> Doc)
  -> ReadP a a
  -> (b -> a)
  -> (a -> b -> b)
  -> FieldDescr bsimpleField "author"
                           showFreeText :: String -> DocshowFreeText           parseFreeText :: ReadP s StringparseFreeText
                           author :: InstalledPackageInfo_ m -> Stringauthor                 (\val pkg -> pkg :: InstalledPackageInfopkg{author=val :: Stringval})
 ]

installedFieldDescrs :: [FieldDescr InstalledPackageInfo]
installedFieldDescrs = [
   boolField ::
  String -> (b -> Bool) -> (Bool -> b -> b) -> FieldDescr bboolField "exposed"
        exposed :: InstalledPackageInfo_ m -> Boolexposed            (\val pkg -> pkg :: InstalledPackageInfopkg{exposed=val :: Stringval})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "exposed-modules"
        disp :: Text a => a -> Docdisp               parseModuleNameQ :: ReadP r ModuleNameparseModuleNameQ
        exposedModules :: InstalledPackageInfo_ m -> [m]exposedModules     (\xs    pkg -> pkg :: InstalledPackageInfopkg{exposedModules=xs :: [FilePath]xs})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "hidden-modules"
        disp :: Text a => a -> Docdisp               parseModuleNameQ :: ReadP r ModuleNameparseModuleNameQ
        hiddenModules :: InstalledPackageInfo_ m -> [m]hiddenModules      (\xs    pkg -> pkg :: InstalledPackageInfopkg{hiddenModules=xs :: [FilePath]xs})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "import-dirs"
        showFilePath :: FilePath -> DocshowFilePath       parseFilePathQ :: ReadP r FilePathparseFilePathQ
        importDirs :: InstalledPackageInfo_ m -> [FilePath]importDirs         (\xs pkg -> pkg :: InstalledPackageInfopkg{importDirs=xs :: [FilePath]xs})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "library-dirs"
        showFilePath :: FilePath -> DocshowFilePath       parseFilePathQ :: ReadP r FilePathparseFilePathQ
        libraryDirs :: InstalledPackageInfo_ m -> [FilePath]libraryDirs        (\xs pkg -> pkg :: InstalledPackageInfopkg{libraryDirs=xs :: [FilePath]xs})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "hs-libraries"
        showFilePath :: FilePath -> DocshowFilePath       parseTokenQ :: ReadP r StringparseTokenQ
        hsLibraries :: InstalledPackageInfo_ m -> [String]hsLibraries        (\xs pkg -> pkg :: InstalledPackageInfopkg{hsLibraries=xs :: [FilePath]xs})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "extra-libraries"
        showToken :: String -> DocshowToken          parseTokenQ :: ReadP r StringparseTokenQ
        extraLibraries :: InstalledPackageInfo_ m -> [String]extraLibraries     (\xs pkg -> pkg :: InstalledPackageInfopkg{extraLibraries=xs :: [FilePath]xs})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "extra-ghci-libraries"
        showToken :: String -> DocshowToken          parseTokenQ :: ReadP r StringparseTokenQ
        extraGHCiLibraries :: InstalledPackageInfo_ m -> [String]extraGHCiLibraries (\xs pkg -> pkg :: InstalledPackageInfopkg{extraGHCiLibraries=xs :: [FilePath]xs})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "include-dirs"
        showFilePath :: FilePath -> DocshowFilePath       parseFilePathQ :: ReadP r FilePathparseFilePathQ
        includeDirs :: InstalledPackageInfo_ m -> [FilePath]includeDirs        (\xs pkg -> pkg :: InstalledPackageInfopkg{includeDirs=xs :: [FilePath]xs})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "includes"
        showFilePath :: FilePath -> DocshowFilePath       parseFilePathQ :: ReadP r FilePathparseFilePathQ
        includes :: InstalledPackageInfo_ m -> [String]includes           (\xs pkg -> pkg :: InstalledPackageInfopkg{includes=xs :: [FilePath]xs})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "depends"
        disp :: Text a => a -> Docdisp               parse :: Text a => forall r. ReadP r aparse
        depends :: InstalledPackageInfo_ m -> [InstalledPackageId]depends            (\xs pkg -> pkg :: InstalledPackageInfopkg{depends=xs :: [FilePath]xs})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "hugs-options"
        showToken :: String -> DocshowToken          parseTokenQ :: ReadP r StringparseTokenQ
        hugsOptions :: InstalledPackageInfo_ m -> [String]hugsOptions        (\path  pkg -> pkg :: InstalledPackageInfopkg{hugsOptions=path :: [String]path})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "cc-options"
        showToken :: String -> DocshowToken          parseTokenQ :: ReadP r StringparseTokenQ
        ccOptions :: InstalledPackageInfo_ m -> [String]ccOptions          (\path  pkg -> pkg :: InstalledPackageInfopkg{ccOptions=path :: [String]path})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "ld-options"
        showToken :: String -> DocshowToken          parseTokenQ :: ReadP r StringparseTokenQ
        ldOptions :: InstalledPackageInfo_ m -> [String]ldOptions          (\path  pkg -> pkg :: InstalledPackageInfopkg{ldOptions=path :: [String]path})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "framework-dirs"
        showFilePath :: FilePath -> DocshowFilePath       parseFilePathQ :: ReadP r FilePathparseFilePathQ
        frameworkDirs :: InstalledPackageInfo_ m -> [FilePath]frameworkDirs      (\xs pkg -> pkg :: InstalledPackageInfopkg{frameworkDirs=xs :: [FilePath]xs})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "frameworks"
        showToken :: String -> DocshowToken          parseTokenQ :: ReadP r StringparseTokenQ
        frameworks :: InstalledPackageInfo_ m -> [String]frameworks         (\xs pkg -> pkg :: InstalledPackageInfopkg{frameworks=xs :: [FilePath]xs})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "haddock-interfaces"
        showFilePath :: FilePath -> DocshowFilePath       parseFilePathQ :: ReadP r FilePathparseFilePathQ
        haddockInterfaces :: InstalledPackageInfo_ m -> [FilePath]haddockInterfaces  (\xs pkg -> pkg :: InstalledPackageInfopkg{haddockInterfaces=xs :: [FilePath]xs})
 , listField ::
  String
  -> (a -> Doc)
  -> ReadP [a] a
  -> (b -> [a])
  -> ([a] -> b -> b)
  -> FieldDescr blistField   "haddock-html"
        showFilePath :: FilePath -> DocshowFilePath       parseFilePathQ :: ReadP r FilePathparseFilePathQ
        haddockHTMLs :: InstalledPackageInfo_ m -> [FilePath]haddockHTMLs       (\xs pkg -> pkg :: InstalledPackageInfopkg{haddockHTMLs=xs :: [FilePath]xs})
 ]