-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Package
-- Copyright   :  Isaac Jones 2003-2004
--
-- Maintainer  :  cabal-devel@haskell.org
-- Portability :  portable
--
-- Defines a package identifier along with a parser and pretty printer for it.
-- 'PackageIdentifier's consist of a name and an exact version. It also defines
-- a 'Dependency' data type. A dependency is a package name and a version
-- range, like @\"foo >= 1.2 && < 2\"@.

{- 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 Isaac Jones 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. -}

module Distribution.Package (
        -- * Package ids
        PackageName(..),
        PackageIdentifier(..),
        PackageId,

        -- * Installed package identifiers
        InstalledPackageId(..),

        -- * Package source dependencies
        Dependency(..),
        thisPackageVersion,
        notThisPackageVersion,
        simplifyDependency,

        -- * Package classes
        Package(..), packageName, packageVersion,
        PackageFixedDeps(..),
  ) where

import Distribution.Version
         ( Version(..), VersionRange, anyVersion, thisVersion
         , notThisVersion, simplifyVersionRange )

import Distribution.Text (Text(..))
import qualified Distribution.Compat.ReadP as Parse
import Distribution.Compat.ReadP ((<++))
import qualified Text.PrettyPrint as Disp
import Text.PrettyPrint ((<>), (<+>), text)
import qualified Data.Char as Char ( isDigit, isAlphaNum )
import Data.List ( intersperse )

newtype PackageName = PackageName String
    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, D:Eq :: (a -> a -> Bool) -> (a -> a -> Bool) -> T:Eq aEq, D:Ord ::
  Eq a =>
  (a -> a -> Ordering)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> a)
  -> (a -> a -> a)
  -> T:Ord aOrd)

instance D:Text :: (a -> Doc) -> (forall r. ReadP r a) -> T:Text aText PackageName where
  disp (PackageName n) = text :: String -> DocDisp.text n :: Stringn
  parse = do
    ns <- sepBy1 :: ReadP r a -> ReadP r sep -> ReadP r [a]Parse.sepBy1 component :: Parser r Char Stringcomponent (char :: Char -> ReadP r CharParse.char '-')
    return :: Monad m => forall a. a -> m areturn (PackageName :: String -> PackageNamePackageName (concat :: [[a]] -> [a]concat (intersperse :: a -> [a] -> [a]intersperse "-" ns :: [String]ns)))
    where
      component = do
        cs <- munch1 :: (Char -> Bool) -> ReadP r StringParse.munch1 isAlphaNum :: Char -> BoolChar.isAlphaNum
        if all :: (a -> Bool) -> [a] -> Boolall isDigit :: Char -> BoolChar.isDigit cs :: Stringcs then pfail :: ReadP r aParse.pfail else return :: Monad m => forall a. a -> m areturn cs :: Stringcs
        -- each component must contain an alphabetic character, to avoid
        -- ambiguity in identifiers like foo-1 (the 1 is the version number).

-- | Type alias so we can use the shorter name PackageId.
type PackageId = PackageIdentifier

-- | The name and version of a package.
data pkgName :: PackageNamePackageIdentifier
    = PackageIdentifier {
        pkgName    :: PackageName, -- ^The name of this package, eg. foo
        pkgVersion :: Version -- ^the version of this package, eg 1.2
     }
     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, D:Eq :: (a -> a -> Bool) -> (a -> a -> Bool) -> T:Eq aEq, D:Ord ::
  Eq a =>
  (a -> a -> Ordering)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> a)
  -> (a -> a -> a)
  -> T:Ord aOrd)

instance D:Text :: (a -> Doc) -> (forall r. ReadP r a) -> T:Text aText PackageIdentifier where
  disp (PackageIdentifier n v) = case v :: Versionv of
    Version [] _ -> disp :: Text a => a -> Docdisp n :: Stringn -- if no version, don't show version.
    _            -> disp :: Text a => a -> Docdisp n :: Stringn (<>) :: Doc -> Doc -> Doc<> char :: Char -> DocDisp.char '-' (<>) :: Doc -> Doc -> Doc<> disp :: Text a => a -> Docdisp v :: Versionv

  parse = do
    n <- parse :: Text a => forall r. ReadP r aparse
    v <- (char :: Char -> ReadP r CharParse.char '-' (>>) :: Monad m => forall a b. m a -> m b -> m b>> parse :: Text a => forall r. ReadP r aparse) (<++) :: ReadP a a -> ReadP r a -> ReadP r a<++ return :: Monad m => forall a. a -> m areturn (Version :: [Int] -> [String] -> VersionVersion [] :: [a][] [] :: [a][])
    return :: Monad m => forall a. a -> m areturn (PackageIdentifier :: PackageName -> Version -> PackageIdentifierPackageIdentifier n :: Stringn v :: Versionv)

-- ------------------------------------------------------------
-- * Installed Package Ids
-- ------------------------------------------------------------

-- | An InstalledPackageId uniquely identifies an instance of an installed package.
-- There can be at most one package with a given 'InstalledPackageId'
-- in a package database, or overlay of databases.
--
newtype InstalledPackageId = InstalledPackageId String
 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,D:Eq :: (a -> a -> Bool) -> (a -> a -> Bool) -> T:Eq aEq,D:Ord ::
  Eq a =>
  (a -> a -> Ordering)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> Bool)
  -> (a -> a -> a)
  -> (a -> a -> a)
  -> T:Ord aOrd)

instance D:Text :: (a -> Doc) -> (forall r. ReadP r a) -> T:Text aText InstalledPackageId where
  disp (InstalledPackageId str) = text :: String -> Doctext str :: Stringstr

  parse = InstalledPackageId :: String -> InstalledPackageIdInstalledPackageId fmap :: Functor f => forall a b. (a -> b) -> f a -> f b`fmap` munch1 :: (Char -> Bool) -> ReadP r StringParse.munch1 abi_char :: Char -> Boolabi_char
   where abi_char c = isAlphaNum :: Char -> BoolChar.isAlphaNum c :: Charc (||) :: Bool -> Bool -> Bool|| c :: Charc elem :: Eq a => a -> [a] -> Bool`elem` ":-_."

-- ------------------------------------------------------------
-- * Package source dependencies
-- ------------------------------------------------------------

-- | Describes a dependency on a source package (API)
--
data Dependency = Dependency PackageName VersionRange
                  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, D:Eq :: (a -> a -> Bool) -> (a -> a -> Bool) -> T:Eq aEq)

instance D:Text :: (a -> Doc) -> (forall r. ReadP r a) -> T:Text aText Dependency where
  disp (Dependency name ver) =
    disp :: Text a => a -> Docdisp name :: PackageNamename (<+>) :: Doc -> Doc -> Doc<+> disp :: Text a => a -> Docdisp ver :: VersionRangever

  parse = do name <- parse :: Text a => forall r. ReadP r aparse
             skipSpaces :: ReadP r ()Parse.skipSpaces
             ver <- parse :: Text a => forall r. ReadP r aparse (<++) :: ReadP a a -> ReadP r a -> ReadP r a<++ return :: Monad m => forall a. a -> m areturn anyVersion :: VersionRangeanyVersion
             skipSpaces :: ReadP r ()Parse.skipSpaces
             return :: Monad m => forall a. a -> m areturn (Dependency :: PackageName -> VersionRange -> DependencyDependency name :: PackageNamename ver :: VersionRangever)

thisPackageVersion :: PackageIdentifier -> Dependency
thisPackageVersion (PackageIdentifier n v) =
  Dependency :: PackageName -> VersionRange -> DependencyDependency n :: Stringn (thisVersion :: Version -> VersionRangethisVersion v :: Versionv)

notThisPackageVersion :: PackageIdentifier -> Dependency
notThisPackageVersion (PackageIdentifier n v) =
  Dependency :: PackageName -> VersionRange -> DependencyDependency n :: Stringn (notThisVersion :: Version -> VersionRangenotThisVersion v :: Versionv)

-- | Simplify the 'VersionRange' expression in a 'Dependency'.
-- See 'simplifyVersionRange'.
--
simplifyDependency :: Dependency -> Dependency
simplifyDependency (Dependency name range) =
  Dependency :: PackageName -> VersionRange -> DependencyDependency name :: PackageNamename (simplifyVersionRange :: VersionRange -> VersionRangesimplifyVersionRange range :: VersionRangerange)

-- | Class of things that have a 'PackageIdentifier'
--
-- Types in this class are all notions of a package. This allows us to have
-- different types for the different phases that packages go though, from
-- simple name\/id, package description, configured or installed packages.
--
-- Not all kinds of packages can be uniquely identified by a
-- 'PackageIdentifier'. In particular, installed packages cannot, there may be
-- many installed instances of the same source package.
--
class Package pkg where
  packageId :: pkg -> PackageIdentifier

packageName    :: Package pkg => pkg -> PackageName
packageName     = pkgName :: PackageIdentifier -> PackageNamepkgName    (.) :: (b -> c) -> (a -> b) -> a -> c. packageId :: Package pkg => pkg -> PackageIdentifierpackageId

packageVersion :: Package pkg => pkg -> Version
packageVersion  = pkgVersion :: PackageIdentifier -> VersionpkgVersion (.) :: (b -> c) -> (a -> b) -> a -> c. packageId :: Package pkg => pkg -> PackageIdentifierpackageId

instance D:Package :: (pkg -> PackageIdentifier) -> T:Package pkgPackage PackageIdentifier where
  packageId = id :: a -> aid

-- | Subclass of packages that have specific versioned dependencies.
--
-- So for example a not-yet-configured package has dependencies on version
-- ranges, not specific versions. A configured or an already installed package
-- depends on exact versions. Some operations or data structures (like
--  dependency graphs) only make sense on this subclass of package types.
--
class Package pkg => PackageFixedDeps pkg where
  depends :: pkg -> [PackageIdentifier]