2020-01-11 20:15:05 +00:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
|
|
|
|
|
|
|
|
2020-07-21 23:08:58 +00:00
|
|
|
{-|
|
|
|
|
Module : GHCup.Version
|
2020-11-20 17:37:48 +00:00
|
|
|
Description : Version information and version handling.
|
2020-07-21 23:08:58 +00:00
|
|
|
Copyright : (c) Julian Ospald, 2020
|
2020-07-30 18:04:02 +00:00
|
|
|
License : LGPL-3.0
|
2020-07-21 23:08:58 +00:00
|
|
|
Maintainer : hasufell@hasufell.de
|
|
|
|
Stability : experimental
|
2021-05-14 21:09:45 +00:00
|
|
|
Portability : portable
|
2020-07-21 23:08:58 +00:00
|
|
|
-}
|
2020-01-11 20:15:05 +00:00
|
|
|
module GHCup.Version where
|
|
|
|
|
2020-11-20 17:37:48 +00:00
|
|
|
import GHCup.Types
|
2021-02-25 14:33:52 +00:00
|
|
|
import Paths_ghcup (version)
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2021-02-25 14:33:52 +00:00
|
|
|
import Data.Version (Version(versionBranch))
|
2020-04-09 14:59:25 +00:00
|
|
|
import URI.ByteString
|
|
|
|
import URI.ByteString.QQ
|
|
|
|
|
2021-02-25 14:33:52 +00:00
|
|
|
import qualified Data.List.NonEmpty as NE
|
2020-04-17 14:56:56 +00:00
|
|
|
import qualified Data.Text as T
|
2022-05-21 20:54:18 +00:00
|
|
|
import qualified Data.Versions as V
|
|
|
|
import Control.Exception.Safe (MonadThrow)
|
|
|
|
import Data.Text (Text)
|
|
|
|
import Data.List.NonEmpty (NonEmpty((:|)))
|
|
|
|
import Data.List (intersperse)
|
|
|
|
import Control.Monad.Catch (throwM)
|
|
|
|
import GHCup.Errors (ParseError(..))
|
2020-04-17 14:56:56 +00:00
|
|
|
|
2020-08-09 15:39:02 +00:00
|
|
|
-- | This reflects the API version of the YAML.
|
2021-09-02 19:27:31 +00:00
|
|
|
--
|
|
|
|
-- Note that when updating this, CI requires that the file exsists AND the same file exists at
|
|
|
|
-- 'https://www.haskell.org/ghcup/exp/ghcup-<ver>.yaml' with some newlines added.
|
2020-04-09 14:59:25 +00:00
|
|
|
ghcupURL :: URI
|
2022-02-26 14:30:16 +00:00
|
|
|
ghcupURL = [uri|https://raw.githubusercontent.com/haskell/ghcup-metadata/master/ghcup-0.0.7.yaml|]
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2020-08-09 15:39:02 +00:00
|
|
|
-- | The current ghcup version.
|
2022-05-21 20:54:18 +00:00
|
|
|
ghcUpVer :: V.PVP
|
|
|
|
ghcUpVer = V.PVP . NE.fromList . fmap fromIntegral $ versionBranch version
|
2020-04-17 14:56:56 +00:00
|
|
|
|
2020-07-21 23:08:58 +00:00
|
|
|
-- | ghcup version as numeric string.
|
2020-04-17 14:56:56 +00:00
|
|
|
numericVer :: String
|
2022-05-21 20:54:18 +00:00
|
|
|
numericVer = T.unpack . V.prettyPVP $ ghcUpVer
|
2020-11-20 17:37:48 +00:00
|
|
|
|
2022-05-21 20:54:18 +00:00
|
|
|
versionCmp :: V.Versioning -> VersionCmp -> Bool
|
2020-11-20 17:37:48 +00:00
|
|
|
versionCmp ver1 (VR_gt ver2) = ver1 > ver2
|
|
|
|
versionCmp ver1 (VR_gteq ver2) = ver1 >= ver2
|
|
|
|
versionCmp ver1 (VR_lt ver2) = ver1 < ver2
|
|
|
|
versionCmp ver1 (VR_lteq ver2) = ver1 <= ver2
|
|
|
|
versionCmp ver1 (VR_eq ver2) = ver1 == ver2
|
|
|
|
|
2022-05-21 20:54:18 +00:00
|
|
|
versionRange :: V.Versioning -> VersionRange -> Bool
|
2023-07-05 08:44:47 +00:00
|
|
|
versionRange ver' (SimpleRange cmps) = all (versionCmp ver') cmps
|
2020-11-20 17:37:48 +00:00
|
|
|
versionRange ver' (OrRange cmps range) =
|
|
|
|
versionRange ver' (SimpleRange cmps) || versionRange ver' range
|
|
|
|
|
2022-05-21 20:54:18 +00:00
|
|
|
pvpToVersion :: MonadThrow m => V.PVP -> Text -> m V.Version
|
|
|
|
pvpToVersion pvp_ rest =
|
|
|
|
either (\_ -> throwM $ ParseError "Couldn't convert PVP to Version") pure . V.version . (<> rest) . V.prettyPVP $ pvp_
|
|
|
|
|
|
|
|
-- | Convert a version to a PVP and unparsable rest.
|
|
|
|
--
|
|
|
|
-- -- prop> \v -> let (Just (pvp', r)) = versionToPVP v in pvpToVersion pvp' r === Just v
|
|
|
|
versionToPVP :: MonadThrow m => V.Version -> m (V.PVP, Text)
|
|
|
|
versionToPVP (V.Version (Just _) _ _ _) = throwM $ ParseError "Unexpected epoch"
|
|
|
|
versionToPVP v = either (\_ -> (, rest v) <$> alternative v) (pure . (, mempty)) . V.pvp . V.prettyVer $ v
|
|
|
|
where
|
|
|
|
alternative :: MonadThrow m => V.Version -> m V.PVP
|
|
|
|
alternative v' = case NE.takeWhile isDigit (V._vChunks v') of
|
|
|
|
[] -> throwM $ ParseError "Couldn't convert Version to PVP"
|
|
|
|
xs -> pure $ pvpFromList (unsafeDigit <$> xs)
|
|
|
|
|
|
|
|
rest :: V.Version -> Text
|
|
|
|
rest (V.Version _ cs pr me) =
|
|
|
|
let chunks = NE.dropWhile isDigit cs
|
|
|
|
ver = intersperse (T.pack ".") . chunksAsT $ chunks
|
|
|
|
me' = maybe [] (\m -> [T.pack "+",m]) me
|
|
|
|
pr' = foldable [] (T.pack "-" :) $ intersperse (T.pack ".") (chunksAsT pr)
|
|
|
|
prefix = case (ver, pr', me') of
|
|
|
|
(_:_, _, _) -> T.pack "."
|
|
|
|
_ -> T.pack ""
|
|
|
|
in prefix <> mconcat (ver <> pr' <> me')
|
|
|
|
where
|
|
|
|
chunksAsT :: Functor t => t V.VChunk -> t Text
|
|
|
|
chunksAsT = fmap (foldMap f)
|
|
|
|
where
|
|
|
|
f :: V.VUnit -> Text
|
|
|
|
f (V.Digits i) = T.pack $ show i
|
|
|
|
f (V.Str s) = s
|
|
|
|
|
|
|
|
foldable :: Foldable f => f b -> (f a -> f b) -> f a -> f b
|
|
|
|
foldable d g f | null f = d
|
|
|
|
| otherwise = g f
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isDigit :: V.VChunk -> Bool
|
|
|
|
isDigit (V.Digits _ :| []) = True
|
|
|
|
isDigit _ = False
|
|
|
|
|
|
|
|
unsafeDigit :: V.VChunk -> Int
|
|
|
|
unsafeDigit (V.Digits x :| []) = fromIntegral x
|
|
|
|
unsafeDigit _ = error "unsafeDigit: wrong input"
|
|
|
|
|
|
|
|
pvpFromList :: [Int] -> V.PVP
|
|
|
|
pvpFromList = V.PVP . NE.fromList . fmap fromIntegral
|