ghcup-hs/app/ghcup-gen/Validate.hs

103 lines
3.0 KiB
Haskell
Raw Normal View History

2020-02-28 23:33:32 +00:00
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE QuasiQuotes #-}
module Validate where
import GHCup
import GHCup.Types
import Control.Monad
import Control.Exception.Safe
import Control.Monad.Reader.Class
import Control.Monad.IO.Class
import Control.Monad.Trans.Class ( lift )
2020-02-29 23:07:39 +00:00
import Control.Monad.Trans.Reader ( runReaderT )
2020-02-28 23:33:32 +00:00
import Data.List
import Data.String.Interpolate
import Data.Versions
import Data.IORef
import System.Exit
import Control.Monad.Logger
import qualified Data.Map.Strict as M
-- TODO: improve logging
data ValidationError = InternalError String
deriving Show
instance Exception ValidationError
-- TODO: test that GHC is in semver
2020-03-03 22:34:25 +00:00
-- TODO: check there's LATEST tag for every tool
2020-03-04 22:35:53 +00:00
-- TODO: check all tarballs can be downloaded
-- AND their checksum
-- TODO: check gpg keys of tarballs?
2020-02-28 23:33:32 +00:00
validate :: (Monad m, MonadLogger m, MonadThrow m, MonadIO m)
2020-03-03 00:59:19 +00:00
=> GHCupDownloads
2020-02-28 23:33:32 +00:00
-> m ExitCode
2020-03-03 00:59:19 +00:00
validate GHCupDownloads{..} = do
2020-02-28 23:33:32 +00:00
ref <- liftIO $ newIORef 0
2020-03-03 00:59:19 +00:00
-- * verify binary downloads * --
2020-02-28 23:33:32 +00:00
flip runReaderT ref $ do
-- unique tags
2020-03-03 00:59:19 +00:00
forM_ (M.toList _binaryDownloads) $ \(t, _) -> checkUniqueTags t
2020-02-28 23:33:32 +00:00
-- required platforms
2020-03-03 00:59:19 +00:00
forM_ (M.toList _binaryDownloads) $ \(t, versions) ->
2020-02-28 23:33:32 +00:00
forM_ (M.toList versions) $ \(v, vi) ->
forM_ (M.toList $ _viArch vi) $ \(arch, pspecs) -> do
checkHasRequiredPlatforms t v arch (M.keys pspecs)
2020-03-03 00:59:19 +00:00
2020-02-28 23:33:32 +00:00
-- exit
e <- liftIO $ readIORef ref
if e > 0 then pure $ ExitFailure e else pure ExitSuccess
where
checkHasRequiredPlatforms t v arch pspecs = do
let v' = prettyVer v
when (not $ any (== Linux UnknownLinux) pspecs) $ do
lift $ $(logError)
[i|Linux UnknownLinux missing for for #{t} #{v'} #{arch}|]
addError
when (not $ any (== Darwin) pspecs) $ do
lift $ $(logError) [i|Darwin missing for #{t} #{v'} #{arch}|]
addError
when (not $ any (== FreeBSD) pspecs) $ lift $ $(logWarn)
[i|FreeBSD missing for #{t} #{v'} #{arch}|]
checkUniqueTags tool = do
2020-03-03 00:59:19 +00:00
let allTags = join $ fmap snd $ availableToolVersions _binaryDownloads tool
2020-02-28 23:33:32 +00:00
let nonUnique =
fmap fst
. filter (\(_, b) -> not b)
<$> ( mapM
(\case
[] -> throwM $ InternalError "empty inner list"
(t : ts) ->
pure $ (t, ) $ if isUniqueTag t then ts == [] else True
)
. group
. sort
$ allTags
)
case join nonUnique of
[] -> pure ()
xs -> do
lift $ $(logError) [i|Tags not unique: #{xs}|]
addError
where
isUniqueTag Latest = True
isUniqueTag Recommended = True
addError :: (MonadReader (IORef Int) m, MonadIO m, Monad m) => m ()
addError = do
ref <- ask
liftIO $ modifyIORef ref (+ 1)