make LTS branch pre-releases
[cabal.git] / Cabal-syntax / src / Distribution / Types / MungedPackageId.hs
blobf1e0904586d53b640f97122d4cbc0e166bc6ed9f
1 {-# LANGUAGE DeriveDataTypeable #-}
2 {-# LANGUAGE DeriveGeneric #-}
4 module Distribution.Types.MungedPackageId
5 ( MungedPackageId (..)
6 , computeCompatPackageId
7 ) where
9 import Distribution.Compat.Prelude
10 import Prelude ()
12 import Distribution.Parsec
13 import Distribution.Pretty
14 import Distribution.Types.LibraryName
15 import Distribution.Types.MungedPackageName
16 import Distribution.Types.PackageId
17 import Distribution.Version (Version, nullVersion)
19 import qualified Text.PrettyPrint as Disp
21 -- | A simple pair of a 'MungedPackageName' and 'Version'. 'MungedPackageName' is to
22 -- 'MungedPackageId' as 'PackageName' is to 'PackageId'. See 'MungedPackageName' for more
23 -- info.
24 data MungedPackageId = MungedPackageId
25 { mungedName :: MungedPackageName
26 -- ^ The combined package and component name. see documentation for
27 -- 'MungedPackageName'.
28 , mungedVersion :: Version
29 -- ^ The version of this package / component, eg 1.2
31 deriving (Generic, Read, Show, Eq, Ord, Typeable, Data)
33 instance Binary MungedPackageId
34 instance Structured MungedPackageId
36 -- |
38 -- >>> prettyShow $ MungedPackageId (MungedPackageName "servant" LMainLibName) (mkVersion [1,2,3])
39 -- "servant-1.2.3"
41 -- >>> prettyShow $ MungedPackageId (MungedPackageName "servant" (LSubLibName "lackey")) (mkVersion [0,1,2])
42 -- "z-servant-z-lackey-0.1.2"
43 instance Pretty MungedPackageId where
44 pretty (MungedPackageId n v)
45 | v == nullVersion = pretty n -- if no version, don't show version.
46 | otherwise = pretty n <<>> Disp.char '-' <<>> pretty v
48 -- |
50 -- >>> simpleParsec "foo-bar-0" :: Maybe MungedPackageId
51 -- Just (MungedPackageId {mungedName = MungedPackageName (PackageName "foo-bar") LMainLibName, mungedVersion = mkVersion [0]})
53 -- >>> simpleParsec "foo-bar" :: Maybe MungedPackageId
54 -- Just (MungedPackageId {mungedName = MungedPackageName (PackageName "foo-bar") LMainLibName, mungedVersion = mkVersion []})
56 -- >>> simpleParsec "z-foo-bar-z-baz-0" :: Maybe MungedPackageId
57 -- Just (MungedPackageId {mungedName = MungedPackageName (PackageName "foo-bar") (LSubLibName (UnqualComponentName "baz")), mungedVersion = mkVersion [0]})
59 -- >>> simpleParsec "foo-bar-0-0" :: Maybe MungedPackageId
60 -- Nothing
62 -- >>> simpleParsec "foo-bar.0" :: Maybe MungedPackageId
63 -- Nothing
65 -- >>> simpleParsec "foo-bar.4-2" :: Maybe MungedPackageId
66 -- Nothing
67 instance Parsec MungedPackageId where
68 parsec = do
69 PackageIdentifier pn v <- parsec
70 return $ MungedPackageId (decodeCompatPackageName pn) v
72 instance NFData MungedPackageId where
73 rnf (MungedPackageId name version) = rnf name `seq` rnf version
75 computeCompatPackageId :: PackageId -> LibraryName -> MungedPackageId
76 computeCompatPackageId (PackageIdentifier pn vr) ln =
77 MungedPackageId (MungedPackageName pn ln) vr
79 -- $setup
80 -- >>> :seti -XOverloadedStrings
81 -- >>> import Distribution.Types.Version