Package.hs 5.12 KB
Newer Older
1 2 3 4 5 6 7
-----------------------------------------------------------------------------
-- |
-- Module      :  Distribution.Package
-- Copyright   :  Isaac Jones 2003-2004
-- 
-- Maintainer  :  Isaac Jones <ijones@syntaxpolice.org>
-- Stability   :  alpha
simonmar's avatar
simonmar committed
8
-- Portability :  portable
9
--
ijones's avatar
ijones committed
10
-- Packages are fundamentally just a name and a version.
11

ijones's avatar
ijones committed
12
{- All rights reserved.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

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. -}

42
module Distribution.Package (
43
	-- * Package ids
44
	PackageIdentifier(..),
simonmar's avatar
simonmar committed
45
	showPackageId, parsePackageId, parsePackageName,
46

47 48 49 50
        -- * Package dependencies
        Dependency(..),
        showDependency, parseDependency,

51
	-- * Package classes
52
	Package(..), packageName, packageVersion,
53
	PackageFixedDeps(..),
54
  ) where
55

simonmar's avatar
simonmar committed
56 57
import Distribution.Version
import Distribution.Compat.ReadP as ReadP
58 59
import qualified Text.PrettyPrint as Pretty
import Text.PrettyPrint ((<+>))
60
import Data.Char ( isDigit, isAlphaNum )
simonmar's avatar
simonmar committed
61
import Data.List ( intersperse )
ijones's avatar
ijones committed
62

ijones's avatar
ijones committed
63
-- | The name and version of a package.
64
data PackageIdentifier
simonmar's avatar
simonmar committed
65
    = PackageIdentifier {
ijones's avatar
ijones committed
66 67
	pkgName    :: String, -- ^The name of this package, eg. foo
	pkgVersion :: Version -- ^the version of this package, eg 1.2
simonmar's avatar
simonmar committed
68
     }
69
     deriving (Read, Show, Eq, Ord)
70

ijones's avatar
ijones committed
71
-- |Creates a string like foo-1.2
simonmar's avatar
simonmar committed
72
showPackageId :: PackageIdentifier -> String
ijones's avatar
ijones committed
73
showPackageId (PackageIdentifier n (Version [] _)) = n -- if no version, don't show version.
simonmar's avatar
simonmar committed
74
showPackageId pkgid = 
75
  pkgName pkgid ++ '-': showVersion (packageVersion pkgid)
simonmar's avatar
simonmar committed
76

77
parsePackageName :: ReadP r String
78
parsePackageName = do ns <- sepBy1 component (char '-')
simonmar's avatar
simonmar committed
79
                      return (concat (intersperse "-" ns))
80
  where component = do 
81 82 83
	   cs <- munch1 isAlphaNum
	   if all isDigit cs then pfail else return cs
	-- each component must contain an alphabetic character, to avoid
84
	-- ambiguity in identifiers like foo-1 (the 1 is the version number).
simonmar's avatar
simonmar committed
85

ijones's avatar
ijones committed
86
-- |A package ID looks like foo-1.2.
simonmar's avatar
simonmar committed
87 88 89 90 91
parsePackageId :: ReadP r PackageIdentifier
parsePackageId = do 
  n <- parsePackageName
  v <- (ReadP.char '-' >> parseVersion) <++ return (Version [] [])
  return PackageIdentifier{pkgName=n,pkgVersion=v}
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
-- ------------------------------------------------------------
-- * Package dependencies
-- ------------------------------------------------------------

data Dependency = Dependency String VersionRange
                  deriving (Read, Show, Eq)

showDependency :: Dependency -> Pretty.Doc
showDependency (Dependency name ver) =
  Pretty.text name <+> Pretty.text (showVersionRange ver)

parseDependency :: ReadP r Dependency
parseDependency = do name <- parsePackageName
                     skipSpaces
                     ver <- parseVersionRange <++ return AnyVersion
                     skipSpaces
                     return $ Dependency name ver

111 112 113 114 115 116 117 118 119
-- | Class of things that can be identified by 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.
--
class Package pkg where
  packageId :: pkg -> PackageIdentifier

120 121 122 123 124 125
packageName    :: Package pkg => pkg -> String
packageName     = pkgName    . packageId

packageVersion :: Package pkg => pkg -> Version
packageVersion  = pkgVersion . packageId

126 127 128 129 130 131 132 133 134 135 136 137
instance Package PackageIdentifier where
  packageId = id

-- | 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]