Package.hs 3.15 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
	PackageIdentifier(..),
simonmar's avatar
simonmar committed
44
	showPackageId, parsePackageId, parsePackageName,
45
  ) where
46

simonmar's avatar
simonmar committed
47
48
import Distribution.Version
import Distribution.Compat.ReadP as ReadP
49
import Data.Char ( isDigit, isAlphaNum )
simonmar's avatar
simonmar committed
50
import Data.List ( intersperse )
ijones's avatar
ijones committed
51

ijones's avatar
ijones committed
52
-- | The name and version of a package.
53
data PackageIdentifier
simonmar's avatar
simonmar committed
54
    = PackageIdentifier {
ijones's avatar
ijones committed
55
56
	pkgName    :: String, -- ^The name of this package, eg. foo
	pkgVersion :: Version -- ^the version of this package, eg 1.2
simonmar's avatar
simonmar committed
57
     }
58
     deriving (Read, Show, Eq, Ord)
59

ijones's avatar
ijones committed
60
-- |Creates a string like foo-1.2
simonmar's avatar
simonmar committed
61
showPackageId :: PackageIdentifier -> String
ijones's avatar
ijones committed
62
showPackageId (PackageIdentifier n (Version [] _)) = n -- if no version, don't show version.
simonmar's avatar
simonmar committed
63
64
65
showPackageId pkgid = 
  pkgName pkgid ++ '-': showVersion (pkgVersion pkgid)

66
parsePackageName :: ReadP r String
67
parsePackageName = do ns <- sepBy1 component (char '-')
simonmar's avatar
simonmar committed
68
                      return (concat (intersperse "-" ns))
69
  where component = do 
70
71
72
	   cs <- munch1 isAlphaNum
	   if all isDigit cs then pfail else return cs
	-- each component must contain an alphabetic character, to avoid
73
	-- ambiguity in identifiers like foo-1 (the 1 is the version number).
simonmar's avatar
simonmar committed
74

ijones's avatar
ijones committed
75
-- |A package ID looks like foo-1.2.
simonmar's avatar
simonmar committed
76
77
78
79
80
parsePackageId :: ReadP r PackageIdentifier
parsePackageId = do 
  n <- parsePackageName
  v <- (ReadP.char '-' >> parseVersion) <++ return (Version [] [])
  return PackageIdentifier{pkgName=n,pkgVersion=v}