Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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