Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 118 lines (97 sloc) 3.635 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(..),
9e6e1bc @kolmodin Make Portage.Dependency use PackageName instead of just a String
kolmodin authored
8 mkPackageName,
494fa91 @dcoutts Use Portage.Version in Portage.PackageId
dcoutts authored
9 fromCabalPackageId,
10 toCabalPackageId,
88e7a14 @kolmodin Fix 'hackport list' to handle package names properly
kolmodin authored
11 parseFriendlyPackage,
12 normalizeCabalPackageName,
13 normalizeCabalPackageId
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
14 ) where
15
16 import qualified Distribution.Package as Cabal
c3c8420 @kolmodin Merge changes
kolmodin authored
17 import Distribution.Text (Text(..))
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
18
19 import qualified Distribution.Compat.ReadP as Parse
6dea19e @kolmodin Work on the Portage/ implementation
kolmodin authored
20
21 import qualified Portage.Version as Portage
22
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
23 import qualified Text.PrettyPrint as Disp
24 import Text.PrettyPrint ((<>))
1cec758 @trofi Some more -Wall fixes
trofi authored
25 import qualified Data.Char as Char (isAlphaNum, isSpace, toLower)
26 -- import qualified Data.Char as Char (isDigit)
27 -- import Data.List (intersperse)
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
28
f309365 @kolmodin Reimplement 'hackport merge'
kolmodin authored
29 newtype Category = Category { unCategory :: String }
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
30 deriving (Eq, Ord, Show, Read)
31
6dea19e @kolmodin Work on the Portage/ implementation
kolmodin authored
32 data PackageName = PackageName Category Cabal.PackageName
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 PackageId = PackageId { packageId :: PackageName, pkgVersion :: Portage.Version }
3125bdc @kolmodin Import datatype from Cabal 1.5
kolmodin authored
36 deriving (Eq, Ord, Show, Read)
37
6dea19e @kolmodin Work on the Portage/ implementation
kolmodin authored
38 {-
3125bdc @kolmodin Import datatype from Cabal 1.5
kolmodin authored
39 instance Text PN where
40 disp (PN n) = Disp.text n
41 parse = do
42 ns <- Parse.sepBy1 component (Parse.char '-')
43 return (PN (concat (intersperse "-" ns)))
44 where
45 component = do
46 cs <- Parse.munch1 Char.isAlphaNum
47 if all Char.isDigit cs then Parse.pfail else return cs
48 -- each component must contain an alphabetic character, to avoid
49 -- ambiguity in identifiers like foo-1 (the 1 is the version number).
6dea19e @kolmodin Work on the Portage/ implementation
kolmodin authored
50 -}
3125bdc @kolmodin Import datatype from Cabal 1.5
kolmodin authored
51
9e6e1bc @kolmodin Make Portage.Dependency use PackageName instead of just a String
kolmodin authored
52 mkPackageName :: String -> String -> PackageName
53 mkPackageName cat package = PackageName (Category cat) (Cabal.PackageName package)
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
2593f89 @trofi PackageId.hs -Werror fixes
trofi authored
85 _ <- Parse.char '/'
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
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
2593f89 @trofi PackageId.hs -Werror fixes
trofi authored
95 _ <- Parse.char '-'
7b0d238 @dcoutts Add Portage.PackageId types and parser and pretty printer
dcoutts authored
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
2593f89 @trofi PackageId.hs -Werror fixes
trofi authored
109 _ <- Parse.char '/'
2fcffdb @kolmodin Add a parser for human friendly way to specify packages
kolmodin authored
110 return (Just c)
111 p <- parse
112 mv <- Parse.option Nothing $ do
2593f89 @trofi PackageId.hs -Werror fixes
trofi authored
113 _ <- Parse.char '-'
2fcffdb @kolmodin Add a parser for human friendly way to specify packages
kolmodin authored
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.