2021-10-15 20:24:23 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE TypeApplications #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
|
|
|
{-# LANGUAGE DuplicateRecordFields #-}
|
|
|
|
{-# LANGUAGE RankNTypes #-}
|
|
|
|
|
|
|
|
module GHCup.OptParse.Upgrade where
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import GHCup
|
|
|
|
import GHCup.Errors
|
|
|
|
import GHCup.Types
|
2022-05-21 20:54:18 +00:00
|
|
|
import GHCup.Prelude.File
|
|
|
|
import GHCup.Prelude.Logger
|
2021-10-15 20:24:23 +00:00
|
|
|
|
2024-03-02 08:23:28 +00:00
|
|
|
import Control.Concurrent (threadDelay)
|
2021-10-15 20:24:23 +00:00
|
|
|
#if !MIN_VERSION_base(4,13,0)
|
|
|
|
import Control.Monad.Fail ( MonadFail )
|
|
|
|
#endif
|
|
|
|
import Control.Monad.Reader
|
|
|
|
import Control.Monad.Trans.Resource
|
|
|
|
import Data.Functor
|
|
|
|
import Data.Maybe
|
|
|
|
import Haskus.Utils.Variant.Excepts
|
|
|
|
import Options.Applicative hiding ( style )
|
|
|
|
import Prelude hiding ( appendFile )
|
|
|
|
import System.Exit
|
|
|
|
|
|
|
|
import qualified Data.Text as T
|
|
|
|
import Control.Exception.Safe (MonadMask)
|
|
|
|
import System.Environment
|
|
|
|
import GHCup.Utils
|
|
|
|
import System.FilePath
|
|
|
|
import GHCup.Types.Optics
|
2023-10-13 08:31:17 +00:00
|
|
|
import Data.Versions
|
2021-10-15 20:24:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---------------
|
|
|
|
--[ Options ]--
|
|
|
|
---------------
|
|
|
|
|
|
|
|
|
|
|
|
data UpgradeOpts = UpgradeInplace
|
|
|
|
| UpgradeAt FilePath
|
|
|
|
| UpgradeGHCupDir
|
2023-07-24 14:26:33 +00:00
|
|
|
deriving (Eq, Show)
|
2021-10-15 20:24:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---------------
|
|
|
|
--[ Parsers ]--
|
|
|
|
---------------
|
|
|
|
|
2022-05-02 17:54:37 +00:00
|
|
|
|
2021-10-15 20:24:23 +00:00
|
|
|
upgradeOptsP :: Parser UpgradeOpts
|
|
|
|
upgradeOptsP =
|
|
|
|
flag'
|
|
|
|
UpgradeInplace
|
|
|
|
(short 'i' <> long "inplace" <> help
|
2022-05-02 17:54:37 +00:00
|
|
|
"Upgrade ghcup in-place"
|
2021-10-15 20:24:23 +00:00
|
|
|
)
|
2022-05-02 17:54:37 +00:00
|
|
|
<|>
|
|
|
|
( UpgradeAt
|
2021-10-15 20:24:23 +00:00
|
|
|
<$> option
|
|
|
|
str
|
|
|
|
(short 't' <> long "target" <> metavar "TARGET_DIR" <> help
|
|
|
|
"Absolute filepath to write ghcup into"
|
2022-03-04 23:46:37 +00:00
|
|
|
<> completer (bashCompleter "file")
|
2021-10-15 20:24:23 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
<|> pure UpgradeGHCupDir
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
---------------------------
|
|
|
|
--[ Effect interpreters ]--
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
|
|
|
|
type UpgradeEffects = '[ DigestError
|
2022-12-21 16:31:41 +00:00
|
|
|
, ContentLengthError
|
2021-10-15 20:24:23 +00:00
|
|
|
, GPGError
|
|
|
|
, NoDownload
|
|
|
|
, NoUpdate
|
|
|
|
, FileDoesNotExistError
|
|
|
|
, CopyError
|
|
|
|
, DownloadFailed
|
2022-05-23 14:48:29 +00:00
|
|
|
, ToolShadowed
|
2021-10-15 20:24:23 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
runUpgrade :: MonadUnliftIO m
|
|
|
|
=> (ReaderT AppState m (VEither UpgradeEffects a) -> m (VEither UpgradeEffects a))
|
|
|
|
-> Excepts UpgradeEffects (ResourceT (ReaderT AppState m)) a
|
|
|
|
-> m (VEither UpgradeEffects a)
|
|
|
|
runUpgrade runAppState =
|
|
|
|
runAppState
|
|
|
|
. runResourceT
|
|
|
|
. runE
|
|
|
|
@UpgradeEffects
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
------------------
|
|
|
|
--[ Entrypoint ]--
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
upgrade :: ( Monad m
|
2021-11-14 15:24:13 +00:00
|
|
|
, MonadMask m
|
|
|
|
, MonadUnliftIO m
|
|
|
|
, MonadFail m
|
|
|
|
)
|
2021-10-15 20:24:23 +00:00
|
|
|
=> UpgradeOpts
|
|
|
|
-> Bool
|
2022-05-02 17:54:37 +00:00
|
|
|
-> Bool
|
2021-11-14 15:24:13 +00:00
|
|
|
-> Dirs
|
2021-10-15 20:24:23 +00:00
|
|
|
-> (forall a. ReaderT AppState m (VEither UpgradeEffects a) -> m (VEither UpgradeEffects a))
|
|
|
|
-> (ReaderT LeanAppState m () -> m ())
|
|
|
|
-> m ExitCode
|
2022-05-02 17:54:37 +00:00
|
|
|
upgrade uOpts force' fatal Dirs{..} runAppState runLogger = do
|
2021-10-15 20:24:23 +00:00
|
|
|
target <- case uOpts of
|
|
|
|
UpgradeInplace -> Just <$> liftIO getExecutablePath
|
|
|
|
(UpgradeAt p) -> pure $ Just p
|
|
|
|
UpgradeGHCupDir -> pure (Just (binDir </> "ghcup" <> exeExt))
|
|
|
|
|
|
|
|
runUpgrade runAppState (do
|
|
|
|
GHCupInfo { _ghcupDownloads = dls } <- lift getGHCupInfo
|
2024-03-02 08:23:28 +00:00
|
|
|
Just (tver, vi) <- pure $ getLatest dls GHCup
|
|
|
|
let latestVer = _tvVersion tver
|
|
|
|
forM_ (_viPreInstall vi) $ \msg -> do
|
|
|
|
lift $ logWarn msg
|
|
|
|
lift $ logWarn
|
|
|
|
"...waiting for 5 seconds, you can still abort..."
|
|
|
|
liftIO $ threadDelay 5000000 -- give the user a sec to intervene
|
|
|
|
v' <- liftE $ upgradeGHCup' target force' fatal latestVer
|
2021-10-15 20:24:23 +00:00
|
|
|
pure (v', dls)
|
|
|
|
) >>= \case
|
|
|
|
VRight (v', dls) -> do
|
|
|
|
let pretty_v = prettyVer v'
|
|
|
|
let vi = fromJust $ snd <$> getLatest dls GHCup
|
|
|
|
runLogger $ logInfo $
|
|
|
|
"Successfully upgraded GHCup to version " <> pretty_v
|
|
|
|
forM_ (_viPostInstall vi) $ \msg ->
|
|
|
|
runLogger $ logInfo msg
|
|
|
|
pure ExitSuccess
|
|
|
|
VLeft (V NoUpdate) -> do
|
|
|
|
runLogger $ logWarn "No GHCup update available"
|
|
|
|
pure ExitSuccess
|
|
|
|
VLeft e -> do
|
2022-12-19 16:10:19 +00:00
|
|
|
runLogger $ logError $ T.pack $ prettyHFError e
|
2021-10-15 20:24:23 +00:00
|
|
|
pure $ ExitFailure 11
|