Skip to content
Newer
Older
100644 188 lines (169 sloc) 6.44 KB
e35e41e @kolmodin Make merging respect categories
kolmodin authored Aug 19, 2007
1 module Diff
a1730ad @kolmodin Switch to using Cabal's CLI API
kolmodin authored Aug 31, 2008
2 ( runDiff
3 , DiffMode(..)
e35e41e @kolmodin Make merging respect categories
kolmodin authored Aug 19, 2007
4 ) where
03de0f6 Adding diff command to HackPort
der_eq@freenet.de authored Sep 21, 2005
5
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
6 import Control.Monad ( forM_, mplus )
5078263 @kolmodin Make diffing respect categories
kolmodin authored Aug 18, 2007
7 import Data.Char
a1730ad @kolmodin Switch to using Cabal's CLI API
kolmodin authored Aug 31, 2008
8 import qualified Data.Map as Map
225135f @kolmodin Switch to using Portage.{PackageId,Version}
kolmodin authored Sep 7, 2008
9 import Network.URI
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
10 import Control.Exception ( assert )
11 import Data.Maybe ( fromJust, listToMaybe )
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored Feb 8, 2009
12 import Data.List ( sort, sortBy, groupBy )
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
13 import Data.Ord ( comparing )
5078263 @kolmodin Make diffing respect categories
kolmodin authored Aug 18, 2007
14
225135f @kolmodin Switch to using Portage.{PackageId,Version}
kolmodin authored Sep 7, 2008
15 import qualified Portage.Version as Portage
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
16 import qualified Portage.Overlay as Portage
17 import qualified Portage.Cabal as Portage
18 import qualified Portage.PackageId as Portage
19
20 import qualified Data.Version as Cabal
a1730ad @kolmodin Switch to using Cabal's CLI API
kolmodin authored Aug 31, 2008
21
22 -- cabal
23 import Distribution.Verbosity
225135f @kolmodin Switch to using Portage.{PackageId,Version}
kolmodin authored Sep 7, 2008
24 import Distribution.Text(display)
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
25 import qualified Distribution.Package as Cabal
26 import qualified Distribution.Simple.PackageIndex as Cabal
27 import qualified Distribution.InstalledPackageInfo as Cabal
28 import Distribution.Simple.Utils (equating)
29
30 -- cabal-install
31 import qualified Distribution.Client.IndexUtils as Index (getAvailablePackages )
32 import qualified Distribution.Client.Types as Cabal
33 import Distribution.Client.Utils (mergeBy, MergeResult(..))
a1730ad @kolmodin Switch to using Cabal's CLI API
kolmodin authored Aug 31, 2008
34
35 data DiffMode
36 = ShowAll
37 | ShowMissing
38 | ShowAdditions
39 | ShowNewer
40 | ShowCommon
eef57f0 @kolmodin Allow to diff by package name
kolmodin authored Oct 4, 2008
41 | ShowPackages [String]
a1730ad @kolmodin Switch to using Cabal's CLI API
kolmodin authored Aug 31, 2008
42 deriving Eq
03de0f6 Adding diff command to HackPort
der_eq@freenet.de authored Sep 21, 2005
43
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
44 type DiffState a = MergeResult a a
3eb19fc New diff style
der_eq@freenet.de authored Jul 30, 2007
45
5078263 @kolmodin Make diffing respect categories
kolmodin authored Aug 18, 2007
46 tabs :: String -> String
47 tabs str = let len = length str in str++(if len < 3*8
48 then replicate (3*8-len) ' '
49 else "")
50
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
51 showDiffState :: Cabal.PackageName -> DiffState Portage.Version -> String
52 showDiffState pkg st = (tabs (display pkg)) ++ " [" ++ (case st of
53 InBoth x y -> display x ++ (case compare x y of
a1730ad @kolmodin Switch to using Cabal's CLI API
kolmodin authored Aug 31, 2008
54 EQ -> "="
55 GT -> ">"
225135f @kolmodin Switch to using Portage.{PackageId,Version}
kolmodin authored Sep 7, 2008
56 LT -> "<") ++ display y
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
57 OnlyInLeft x -> display x ++ ">none"
58 OnlyInRight y -> "none<" ++ display y) ++ "]"
59
60 runDiff :: Verbosity -> FilePath -> DiffMode -> Cabal.Repo -> IO ()
61 runDiff verbosity overlayPath dm repo = do
62 -- get package list from hackage
63 pkgDB <- Index.getAvailablePackages verbosity [ repo ]
64 let (Cabal.AvailablePackageDb hackageIndex _) = pkgDB
65
66 -- get package list from the overlay
67 overlay0 <- (Portage.loadLazy overlayPath)
68 let overlayIndex = Portage.fromOverlay (Portage.reduceOverlay overlay0)
69
70 let (subHackage, subOverlay)
71 = case dm of
72 ShowPackages pkgs ->
73 (concatMap (Cabal.searchByNameSubstring hackageIndex) pkgs
74 ,concatMap (Cabal.searchByNameSubstring overlayIndex) pkgs)
75 _ ->
76 (Cabal.allPackages hackageIndex
77 ,Cabal.allPackages overlayIndex)
78 diff subHackage subOverlay dm
79
80 data PackageCompareInfo = PackageCompareInfo {
81 name :: Cabal.PackageName,
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored Feb 8, 2009
82 -- hackageVersions :: [ Cabal.Version ],
83 -- overlayVersions :: [ Cabal.Version ]
84 hackageVersion :: Maybe Cabal.Version,
85 overlayVersion :: Maybe Cabal.Version
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored Feb 8, 2009
86 } deriving Show
87
88 showPackageCompareInfo :: PackageCompareInfo -> String
89 showPackageCompareInfo pkgCmpInfo =
90 display (name pkgCmpInfo) ++ " ["
91 ++ hackageS ++ sign ++ overlayS ++ "]"
92 where
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored Feb 8, 2009
93 overlay = overlayVersion pkgCmpInfo
94 hackage = hackageVersion pkgCmpInfo
95 hackageS = maybe "none" display hackage
96 overlayS = maybe "none" display overlay
97 sign = case compare hackage overlay of
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored Feb 8, 2009
98 EQ -> "="
99 GT -> ">"
100 LT -> "<"
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
101
102 diff :: [Cabal.AvailablePackage]
103 -> [Portage.ExistingEbuild]
104 -> DiffMode
105 -> IO ()
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored Feb 8, 2009
106 diff hackage overlay dm = do
107 mapM_ (putStrLn . showPackageCompareInfo) pkgCmpInfos
108 where
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored Feb 8, 2009
109 merged = mergePackages (map (Portage.normalizeCabalPackageId . Cabal.packageId) hackage)
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored Feb 8, 2009
110 (map Portage.ebuildCabalId overlay)
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored Feb 8, 2009
111 pkgCmpInfos = filter pkgFilter (map (uncurry mergePackageInfo) merged)
112 pkgFilter :: PackageCompareInfo -> Bool
113 pkgFilter pkgCmpInfo =
114 let om = overlayVersion pkgCmpInfo
115 hm = hackageVersion pkgCmpInfo
116 st = case (om,hm) of
117 (Just ov, Just hv) -> InBoth ov hv
118 (Nothing, Just hv) -> OnlyInRight hv
119 (Just ov, Nothing) -> OnlyInLeft ov
120 in
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored Feb 8, 2009
121 case dm of
122 ShowAll -> True
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored Feb 8, 2009
123 ShowPackages _ -> True -- already filtered
124 ShowNewer -> case st of
125 InBoth o h -> h>o
126 _ -> False
127 ShowMissing -> case st of
3b9e456 @kolmodin Fix 'hackport diff additions' and 'hackport diff common'
kolmodin authored Feb 8, 2009
128 OnlyInLeft _ -> False
129 InBoth x y -> x < y
130 OnlyInRight _ -> True
131 ShowAdditions -> case st of
132 OnlyInLeft _ -> True
133 InBoth x y -> x > y
134 OnlyInRight _ -> False
135 ShowCommon -> case st of
136 OnlyInLeft _ -> False
137 InBoth x y -> x == y
138 OnlyInRight _ -> False
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored Feb 8, 2009
139 _ -> True
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored Feb 8, 2009
140
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
141 -- | We get the 'PackageCompareInfo' by combining the info for the overlay
142 -- and hackage versions of a package.
143 --
144 -- * We're building info about a various versions of a single named package so
145 -- the input package info records are all supposed to refer to the same
146 -- package name.
147 --
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored Feb 8, 2009
148 mergePackageInfo :: [Cabal.PackageIdentifier]
149 -> [Cabal.PackageIdentifier]
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
150 -> PackageCompareInfo
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored Feb 8, 2009
151 mergePackageInfo hackage overlay =
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
152 assert (length overlay + length hackage > 0) $
153 PackageCompareInfo {
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored Feb 8, 2009
154 name = combine Cabal.pkgName latestHackage
155 Cabal.pkgName latestOverlay,
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored Feb 8, 2009
156 -- hackageVersions = map Cabal.pkgVersion hackage,
157 -- overlayVersions = map Cabal.pkgVersion overlay
158 hackageVersion = fmap Cabal.pkgVersion latestHackage,
159 overlayVersion = fmap Cabal.pkgVersion latestOverlay
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
160 }
161 where
162 combine f x g y = fromJust (fmap f x `mplus` fmap g y)
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored Feb 8, 2009
163 latestHackage = latestOf hackage
164 latestOverlay = latestOf overlay
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored Feb 8, 2009
165 latestOf :: [Cabal.PackageIdentifier] -> Maybe Cabal.PackageIdentifier
8925e89 @kolmodin Add 'hackport diff missing'
kolmodin authored Feb 8, 2009
166 latestOf = listToMaybe . reverse . sortBy (comparing Cabal.pkgVersion)
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
167
168 -- | Rearrange installed and available packages into groups referring to the
169 -- same package by name. In the result pairs, the lists are guaranteed to not
170 -- both be empty.
171 --
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored Feb 8, 2009
172 mergePackages :: [Cabal.PackageIdentifier] -> [Cabal.PackageIdentifier]
173 -> [([Cabal.PackageIdentifier], [Cabal.PackageIdentifier])]
174 mergePackages hackage overlay =
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
175 map collect
176 $ mergeBy (\i a -> fst i `compare` fst a)
8543927 @kolmodin Make 'hackport diff' work again
kolmodin authored Feb 8, 2009
177 (groupOn Cabal.pkgName hackage)
178 (groupOn Cabal.pkgName overlay)
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
179 where
180 collect (OnlyInLeft (_,is) ) = (is, [])
181 collect ( InBoth (_,is) (_,as)) = (is, as)
182 collect (OnlyInRight (_,as)) = ([], as)
5078263 @kolmodin Make diffing respect categories
kolmodin authored Aug 18, 2007
183
8273e68 @kolmodin Update the CLI and start using cabal-install features
kolmodin authored Feb 7, 2009
184 groupOn :: Ord key => (a -> key) -> [a] -> [(key,[a])]
185 groupOn key = map (\xs -> (key (head xs), xs))
186 . groupBy (equating key)
187 . sortBy (comparing key)
Something went wrong with that request. Please try again.