Skip to content

HTTPS clone URL

Subversion checkout URL

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