Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 117 lines (96 sloc) 3.566 kB
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
1 -- | Portage package identifiers, which unlike Cabal ones include a category.
2 --
3 module Portage.PackageId (
4 Category(..),
5 PackageName(..),
6 PackageId(..),
494fa91 @dcoutts Use Portage.Version in Portage.PackageId
dcoutts authored
7 fromCabalPackageId,
8 toCabalPackageId,
88e7a14 @kolmodin Fix 'hackport list' to handle package names properly
kolmodin authored
9 parseFriendlyPackage,
10 normalizeCabalPackageName,
11 normalizeCabalPackageId
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
12 ) where
13
6dea19e @kolmodin Work on the Portage/ implementation
kolmodin authored
14 import qualified Data.Char as Char (isAlphaNum, isDigit, isSpace, toLower)
15 import Data.List (intersperse)
16 import qualified Text.PrettyPrint as Disp
17 import Text.PrettyPrint ((<>))
494fa91 @dcoutts Use Portage.Version in Portage.PackageId
dcoutts authored
18
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
19 import qualified Distribution.Package as Cabal
6dea19e @kolmodin Work on the Portage/ implementation
kolmodin authored
20 import Distribution.Text (Text(..),display)
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
21
22 import qualified Distribution.Compat.ReadP as Parse
6dea19e @kolmodin Work on the Portage/ implementation
kolmodin authored
23
24 import qualified Portage.Version as Portage
25
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
26 import qualified Text.PrettyPrint as Disp
27 import Text.PrettyPrint ((<>))
2fcffdb @kolmodin Add a parser for human friendly way to specify packages
kolmodin authored
28 import qualified Data.Char as Char (isAlphaNum, isDigit, isSpace, toLower)
3125bdc @kolmodin Import datatype from Cabal 1.5
kolmodin authored
29 import Data.List (intersperse)
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
30
31 newtype Category = Category String
32 deriving (Eq, Ord, Show, Read)
33
6dea19e @kolmodin Work on the Portage/ implementation
kolmodin authored
34 data PackageName = PackageName Category Cabal.PackageName
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
35 deriving (Eq, Ord, Show, Read)
36
6dea19e @kolmodin Work on the Portage/ implementation
kolmodin authored
37 data PackageId = PackageId { packageId :: PackageName, pkgVersion :: Portage.Version }
3125bdc @kolmodin Import datatype from Cabal 1.5
kolmodin authored
38 deriving (Eq, Ord, Show, Read)
39
6dea19e @kolmodin Work on the Portage/ implementation
kolmodin authored
40 {-
3125bdc @kolmodin Import datatype from Cabal 1.5
kolmodin authored
41 instance Text PN where
42 disp (PN n) = Disp.text n
43 parse = do
44 ns <- Parse.sepBy1 component (Parse.char '-')
45 return (PN (concat (intersperse "-" ns)))
46 where
47 component = do
48 cs <- Parse.munch1 Char.isAlphaNum
49 if all Char.isDigit cs then Parse.pfail else return cs
50 -- each component must contain an alphabetic character, to avoid
51 -- ambiguity in identifiers like foo-1 (the 1 is the version number).
6dea19e @kolmodin Work on the Portage/ implementation
kolmodin authored
52 -}
3125bdc @kolmodin Import datatype from Cabal 1.5
kolmodin authored
53
494fa91 @dcoutts Use Portage.Version in Portage.PackageId
dcoutts authored
54 fromCabalPackageId :: Category -> Cabal.PackageIdentifier -> PackageId
55 fromCabalPackageId category (Cabal.PackageIdentifier name version) =
88e7a14 @kolmodin Fix 'hackport list' to handle package names properly
kolmodin authored
56 PackageId (PackageName category (normalizeCabalPackageName name))
494fa91 @dcoutts Use Portage.Version in Portage.PackageId
dcoutts authored
57 (Portage.fromCabalVersion version)
88e7a14 @kolmodin Fix 'hackport list' to handle package names properly
kolmodin authored
58
59 normalizeCabalPackageName :: Cabal.PackageName -> Cabal.PackageName
60 normalizeCabalPackageName (Cabal.PackageName name) =
61 Cabal.PackageName (map Char.toLower name)
62
63 normalizeCabalPackageId :: Cabal.PackageIdentifier -> Cabal.PackageIdentifier
64 normalizeCabalPackageId (Cabal.PackageIdentifier name version) =
65 Cabal.PackageIdentifier (normalizeCabalPackageName name) version
494fa91 @dcoutts Use Portage.Version in Portage.PackageId
dcoutts authored
66
67 toCabalPackageId :: PackageId -> Maybe Cabal.PackageIdentifier
6dea19e @kolmodin Work on the Portage/ implementation
kolmodin authored
68 toCabalPackageId (PackageId (PackageName _cat name) version) =
69 fmap (Cabal.PackageIdentifier name)
6ec3592 Hackport now uses Cabal-1.6
Ivan.Miljenovic@gmail.com authored
70 (Portage.toCabalVersion version)
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
71
72 instance Text Category where
73 disp (Category c) = Disp.text c
74 parse = fmap Category (Parse.munch1 categoryChar)
75 where
76 categoryChar c = Char.isAlphaNum c || c == '-'
77
78 instance Text PackageName where
79 disp (PackageName category name) =
80 disp category <> Disp.char '/' <> disp name
81
82 parse = do
83 category <- parse
84 Parse.char '/'
85 name <- parse
86 return (PackageName category name)
87
88 instance Text PackageId where
89 disp (PackageId name version) =
90 disp name <> Disp.char '-' <> disp version
91
92 parse = do
93 name <- parse
94 Parse.char '-'
95 version <- parse
96 return (PackageId name version)
2fcffdb @kolmodin Add a parser for human friendly way to specify packages
kolmodin authored
97
6dea19e @kolmodin Work on the Portage/ implementation
kolmodin authored
98 parseFriendlyPackage :: String -> Maybe (Maybe Category, Cabal.PackageName, Maybe Portage.Version)
2fcffdb @kolmodin Add a parser for human friendly way to specify packages
kolmodin authored
99 parseFriendlyPackage str =
100 case [ p | (p,s) <- Parse.readP_to_S parser str
101 , all Char.isSpace s ] of
102 [] -> Nothing
103 (x:_) -> Just x
104 where
105 parser = do
106 mc <- Parse.option Nothing $ do
107 c <- parse
108 Parse.char '/'
109 return (Just c)
110 p <- parse
111 mv <- Parse.option Nothing $ do
112 Parse.char '-'
113 v <- parse
114 return (Just v)
115 return (mc, p, mv)
6ec3592 Hackport now uses Cabal-1.6
Ivan.Miljenovic@gmail.com authored
116
Something went wrong with that request. Please try again.