module Summoner.GhcVer
( GhcVer (..)
, GhcMeta (..)
, Pvp (..)
, showGhcVer
, parseGhcVer
, latestLts
, baseVer
, cabalBaseVersions
, ghcTable
, oldGhcs
) where
import Data.List (maximum, minimum)
import Relude.Extra.Enum (inverseMap, universe)
import qualified Text.Show as Show
import qualified Data.Text as T
data GhcVer
= Ghc7103
| Ghc802
| Ghc822
| Ghc844
| Ghc865
| Ghc881
deriving stock (Eq, Ord, Show, Enum, Bounded)
showGhcVer :: GhcVer -> Text
showGhcVer = \case
Ghc7103 -> "7.10.3"
Ghc802 -> "8.0.2"
Ghc822 -> "8.2.2"
Ghc844 -> "8.4.4"
Ghc865 -> "8.6.5"
Ghc881 -> "8.8.1"
oldGhcs :: [GhcVer]
oldGhcs = [minBound .. Ghc802]
parseGhcVer :: Text -> Maybe GhcVer
parseGhcVer = inverseMap showGhcVer
latestLts :: GhcVer -> Text
latestLts = \case
Ghc7103 -> "lts-6.35"
Ghc802 -> "lts-9.21"
Ghc822 -> "lts-11.22"
Ghc844 -> "lts-12.26"
Ghc865 -> "lts-14.18"
Ghc881 -> "nightly-2019-12-25"
data Pvp = Pvp
{ pvpFirst :: Int
, pvpSecond :: Int
, pvpThird :: Int
, pvpFourth :: Int
}
instance Show Pvp where
show (Pvp a b c d) = intercalate "." $ map Show.show [a, b, c, d]
baseVerPvp :: GhcVer -> Pvp
baseVerPvp = \case
Ghc7103 -> Pvp 4 8 0 2
Ghc802 -> Pvp 4 9 1 0
Ghc822 -> Pvp 4 10 1 0
Ghc844 -> Pvp 4 11 1 0
Ghc865 -> Pvp 4 12 0 0
Ghc881 -> Pvp 4 13 0 0
baseVer :: GhcVer -> Text
baseVer = show . baseVerPvp
cabalBaseVersions :: [GhcVer] -> Text
cabalBaseVersions [] = ""
cabalBaseVersions [v] = "^>= " <> baseVer v
cabalBaseVersions ghcs = ">= " <> baseVer (minimum ghcs) <> " && < " <> upperBound
where
upperBound :: Text
upperBound = let Pvp{..} = baseVerPvp $ maximum ghcs in
show pvpFirst <> "." <> show (pvpSecond + 1)
data GhcMeta = GhcMeta
{ gmGhc :: !Text
, gmBase :: !Text
, gmResolver :: !Text
}
toGhcMeta :: GhcVer -> GhcMeta
toGhcMeta ghcVer = GhcMeta
{ gmGhc = "GHC-" <> showGhcVer ghcVer
, gmBase = "base-" <> baseVer ghcVer
, gmResolver = latestLts ghcVer
}
ghcTable :: [Text]
ghcTable = map (formatGhcMeta . toGhcMeta) universe
formatGhcMeta :: GhcMeta -> Text
formatGhcMeta GhcMeta{..} =
T.justifyLeft 12 ' ' gmGhc
<> " "
<> T.justifyLeft 14 ' ' gmBase
<> " "
<> T.justifyLeft 18 ' ' gmResolver