module Distribution.Types.VersionRange (
VersionRange(..),
anyVersion, noVersion,
thisVersion, notThisVersion,
laterVersion, earlierVersion,
orLaterVersion, orEarlierVersion,
unionVersionRanges, intersectVersionRanges,
withinVersion,
majorBoundVersion,
withinRange,
foldVersionRange,
normaliseVersionRange,
stripParensVersionRange,
hasUpperBound,
hasLowerBound,
VersionRangeF (..),
cataVersionRange,
anaVersionRange,
hyloVersionRange,
projectVersionRange,
embedVersionRange,
wildcardUpperBound,
majorUpperBound,
isWildcardRange,
) where
import Distribution.Compat.Prelude
import Prelude ()
import Distribution.Types.Version
import Distribution.Pretty
import Distribution.Parsec.Class
import Distribution.Text
import Text.PrettyPrint ((<+>))
import qualified Text.PrettyPrint as Disp
import qualified Distribution.Compat.ReadP as Parse
import qualified Distribution.Compat.Parsec as P
data VersionRange
= AnyVersion
| ThisVersion Version
| LaterVersion Version
| OrLaterVersion Version
| EarlierVersion Version
| OrEarlierVersion Version
| WildcardVersion Version
| MajorBoundVersion Version
| UnionVersionRanges VersionRange VersionRange
| IntersectVersionRanges VersionRange VersionRange
| VersionRangeParens VersionRange
deriving (Data, Eq, Generic, Read, Show, Typeable)
instance Binary VersionRange
instance NFData VersionRange where rnf = genericRnf
anyVersion :: VersionRange
anyVersion = AnyVersion
noVersion :: VersionRange
noVersion = IntersectVersionRanges (LaterVersion v) (EarlierVersion v)
where v = mkVersion [1]
thisVersion :: Version -> VersionRange
thisVersion = ThisVersion
notThisVersion :: Version -> VersionRange
notThisVersion v = UnionVersionRanges (EarlierVersion v) (LaterVersion v)
laterVersion :: Version -> VersionRange
laterVersion = LaterVersion
orLaterVersion :: Version -> VersionRange
orLaterVersion = OrLaterVersion
earlierVersion :: Version -> VersionRange
earlierVersion = EarlierVersion
orEarlierVersion :: Version -> VersionRange
orEarlierVersion = OrEarlierVersion
unionVersionRanges :: VersionRange -> VersionRange -> VersionRange
unionVersionRanges = UnionVersionRanges
intersectVersionRanges :: VersionRange -> VersionRange -> VersionRange
intersectVersionRanges = IntersectVersionRanges
withinVersion :: Version -> VersionRange
withinVersion = WildcardVersion
majorBoundVersion :: Version -> VersionRange
majorBoundVersion = MajorBoundVersion
data VersionRangeF a
= AnyVersionF
| ThisVersionF Version
| LaterVersionF Version
| OrLaterVersionF Version
| EarlierVersionF Version
| OrEarlierVersionF Version
| WildcardVersionF Version
| MajorBoundVersionF Version
| UnionVersionRangesF a a
| IntersectVersionRangesF a a
| VersionRangeParensF a
deriving (Data, Eq, Generic, Read, Show, Typeable, Functor, Foldable, Traversable)
projectVersionRange :: VersionRange -> VersionRangeF VersionRange
projectVersionRange AnyVersion = AnyVersionF
projectVersionRange (ThisVersion v) = ThisVersionF v
projectVersionRange (LaterVersion v) = LaterVersionF v
projectVersionRange (OrLaterVersion v) = OrLaterVersionF v
projectVersionRange (EarlierVersion v) = EarlierVersionF v
projectVersionRange (OrEarlierVersion v) = OrEarlierVersionF v
projectVersionRange (WildcardVersion v) = WildcardVersionF v
projectVersionRange (MajorBoundVersion v) = MajorBoundVersionF v
projectVersionRange (UnionVersionRanges a b) = UnionVersionRangesF a b
projectVersionRange (IntersectVersionRanges a b) = IntersectVersionRangesF a b
projectVersionRange (VersionRangeParens a) = VersionRangeParensF a
cataVersionRange :: (VersionRangeF a -> a) -> VersionRange -> a
cataVersionRange f = c where c = f . fmap c . projectVersionRange
embedVersionRange :: VersionRangeF VersionRange -> VersionRange
embedVersionRange AnyVersionF = AnyVersion
embedVersionRange (ThisVersionF v) = ThisVersion v
embedVersionRange (LaterVersionF v) = LaterVersion v
embedVersionRange (OrLaterVersionF v) = OrLaterVersion v
embedVersionRange (EarlierVersionF v) = EarlierVersion v
embedVersionRange (OrEarlierVersionF v) = OrEarlierVersion v
embedVersionRange (WildcardVersionF v) = WildcardVersion v
embedVersionRange (MajorBoundVersionF v) = MajorBoundVersion v
embedVersionRange (UnionVersionRangesF a b) = UnionVersionRanges a b
embedVersionRange (IntersectVersionRangesF a b) = IntersectVersionRanges a b
embedVersionRange (VersionRangeParensF a) = VersionRangeParens a
anaVersionRange :: (a -> VersionRangeF a) -> a -> VersionRange
anaVersionRange g = a where a = embedVersionRange . fmap a . g
foldVersionRange :: a
-> (Version -> a)
-> (Version -> a)
-> (Version -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> VersionRange -> a
foldVersionRange anyv this later earlier union intersect = fold
where
fold = cataVersionRange alg
alg AnyVersionF = anyv
alg (ThisVersionF v) = this v
alg (LaterVersionF v) = later v
alg (OrLaterVersionF v) = union (this v) (later v)
alg (EarlierVersionF v) = earlier v
alg (OrEarlierVersionF v) = union (this v) (earlier v)
alg (WildcardVersionF v) = fold (wildcard v)
alg (MajorBoundVersionF v) = fold (majorBound v)
alg (UnionVersionRangesF v1 v2) = union v1 v2
alg (IntersectVersionRangesF v1 v2) = intersect v1 v2
alg (VersionRangeParensF v) = v
wildcard v = intersectVersionRanges
(orLaterVersion v)
(earlierVersion (wildcardUpperBound v))
majorBound v = intersectVersionRanges
(orLaterVersion v)
(earlierVersion (majorUpperBound v))
hyloVersionRange :: (VersionRangeF VersionRange -> VersionRange)
-> (VersionRange -> VersionRangeF VersionRange)
-> VersionRange -> VersionRange
hyloVersionRange f g = h where h = f . fmap h . g
normaliseVersionRange :: VersionRange -> VersionRange
normaliseVersionRange = hyloVersionRange embed projectVersionRange
where
embed (UnionVersionRangesF (ThisVersion v) (LaterVersion v')) | v == v' =
orLaterVersion v
embed (UnionVersionRangesF (LaterVersion v) (ThisVersion v')) | v == v' =
orLaterVersion v
embed (UnionVersionRangesF (ThisVersion v) (EarlierVersion v')) | v == v' =
orEarlierVersion v
embed (UnionVersionRangesF (EarlierVersion v) (ThisVersion v')) | v == v' =
orEarlierVersion v
embed vr = embedVersionRange vr
stripParensVersionRange :: VersionRange -> VersionRange
stripParensVersionRange = hyloVersionRange embed projectVersionRange
where
embed (VersionRangeParensF vr) = vr
embed vr = embedVersionRange vr
withinRange :: Version -> VersionRange -> Bool
withinRange v = foldVersionRange
True
(\v' -> v == v')
(\v' -> v > v')
(\v' -> v < v')
(||)
(&&)
wildcardUpperBound :: Version -> Version
wildcardUpperBound = alterVersion $
\lowerBound -> init lowerBound ++ [last lowerBound + 1]
isWildcardRange :: Version -> Version -> Bool
isWildcardRange ver1 ver2 = check (versionNumbers ver1) (versionNumbers ver2)
where check (n:[]) (m:[]) | n+1 == m = True
check (n:ns) (m:ms) | n == m = check ns ms
check _ _ = False
majorUpperBound :: Version -> Version
majorUpperBound = alterVersion $ \numbers -> case numbers of
[] -> [0,1]
[m1] -> [m1,1]
(m1:m2:_) -> [m1,m2+1]
instance Pretty VersionRange where
pretty = fst . cataVersionRange alg
where
alg AnyVersionF = (Disp.text "-any", 0 :: Int)
alg (ThisVersionF v) = (Disp.text "==" <<>> pretty v, 0)
alg (LaterVersionF v) = (Disp.char '>' <<>> pretty v, 0)
alg (OrLaterVersionF v) = (Disp.text ">=" <<>> pretty v, 0)
alg (EarlierVersionF v) = (Disp.char '<' <<>> pretty v, 0)
alg (OrEarlierVersionF v) = (Disp.text "<=" <<>> pretty v, 0)
alg (WildcardVersionF v) = (Disp.text "==" <<>> dispWild v, 0)
alg (MajorBoundVersionF v) = (Disp.text "^>=" <<>> pretty v, 0)
alg (UnionVersionRangesF (r1, p1) (r2, p2)) =
(punct 1 p1 r1 <+> Disp.text "||" <+> punct 2 p2 r2 , 2)
alg (IntersectVersionRangesF (r1, p1) (r2, p2)) =
(punct 0 p1 r1 <+> Disp.text "&&" <+> punct 1 p2 r2 , 1)
alg (VersionRangeParensF (r, _)) =
(Disp.parens r, 0)
dispWild ver =
Disp.hcat (Disp.punctuate (Disp.char '.') (map Disp.int $ versionNumbers ver))
<<>> Disp.text ".*"
punct p p' | p < p' = Disp.parens
| otherwise = id
instance Parsec VersionRange where
parsec = expr
where
expr = do P.spaces
t <- term
P.spaces
(do _ <- P.string "||"
P.spaces
e <- expr
return (unionVersionRanges t e)
<|>
return t)
term = do f <- factor
P.spaces
(do _ <- P.string "&&"
P.spaces
t <- term
return (intersectVersionRanges f t)
<|>
return f)
factor = P.choice
$ parens expr
: parseAnyVersion
: parseNoVersion
: parseWildcardRange
: map parseRangeOp rangeOps
parseAnyVersion = P.string "-any" >> return anyVersion
parseNoVersion = P.string "-none" >> return noVersion
parseWildcardRange = P.try $ do
_ <- P.string "=="
P.spaces
branch <- some (P.integral <* P.char '.')
_ <- P.char '*'
return (withinVersion (mkVersion branch))
parens p = P.between
(P.char '(' >> P.spaces)
(P.char ')' >> P.spaces)
(do a <- p
P.spaces
return (VersionRangeParens a))
parseRangeOp (s,f) = P.try (P.string s *> P.spaces *> fmap f parsec)
rangeOps = [ ("<", earlierVersion),
("<=", orEarlierVersion),
(">", laterVersion),
(">=", orLaterVersion),
("^>=", majorBoundVersion),
("==", thisVersion) ]
instance Text VersionRange where
parse = expr
where
expr = do Parse.skipSpaces
t <- term
Parse.skipSpaces
(do _ <- Parse.string "||"
Parse.skipSpaces
e <- expr
return (UnionVersionRanges t e)
Parse.+++
return t)
term = do f <- factor
Parse.skipSpaces
(do _ <- Parse.string "&&"
Parse.skipSpaces
t <- term
return (IntersectVersionRanges f t)
Parse.+++
return f)
factor = Parse.choice $ parens expr
: parseAnyVersion
: parseNoVersion
: parseWildcardRange
: map parseRangeOp rangeOps
parseAnyVersion = Parse.string "-any" >> return AnyVersion
parseNoVersion = Parse.string "-none" >> return noVersion
parseWildcardRange = do
_ <- Parse.string "=="
Parse.skipSpaces
branch <- Parse.sepBy1 digits (Parse.char '.')
_ <- Parse.char '.'
_ <- Parse.char '*'
return (WildcardVersion (mkVersion branch))
parens p = Parse.between (Parse.char '(' >> Parse.skipSpaces)
(Parse.char ')' >> Parse.skipSpaces)
(do a <- p
Parse.skipSpaces
return (VersionRangeParens a))
digits = do
firstDigit <- Parse.satisfy isDigit
if firstDigit == '0'
then return 0
else do rest <- Parse.munch isDigit
return (read (firstDigit : rest))
parseRangeOp (s,f) = Parse.string s >> Parse.skipSpaces >> fmap f parse
rangeOps = [ ("<", EarlierVersion),
("<=", orEarlierVersion),
(">", LaterVersion),
(">=", orLaterVersion),
("^>=", MajorBoundVersion),
("==", ThisVersion) ]
hasUpperBound :: VersionRange -> Bool
hasUpperBound = foldVersionRange
False
(const True)
(const False)
(const True)
(&&) (||)
hasLowerBound :: VersionRange -> Bool
hasLowerBound = foldVersionRange
False
(const True)
(const True)
(const False)
(&&) (||)