2020-04-09 17:53:22 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2021-07-15 11:32:48 +00:00
|
|
|
{-# LANGUAGE BangPatterns #-}
|
2020-01-11 20:15:05 +00:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
|
|
|
{-# LANGUAGE FlexibleInstances #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
2020-03-21 21:19:37 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2022-02-05 18:39:00 +00:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2020-07-21 23:08:58 +00:00
|
|
|
{-|
|
|
|
|
Module : GHCup
|
|
|
|
Description : GHCup installation functions
|
|
|
|
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
|
|
|
|
|
|
|
This module contains the main functions that correspond
|
|
|
|
to the command line interface, like installation, listing versions
|
|
|
|
and so on.
|
|
|
|
|
|
|
|
These are the entry points.
|
|
|
|
-}
|
2022-05-21 20:54:18 +00:00
|
|
|
module GHCup (
|
|
|
|
module GHCup,
|
|
|
|
module GHCup.Cabal,
|
|
|
|
module GHCup.GHC,
|
|
|
|
module GHCup.HLS,
|
|
|
|
module GHCup.Stack,
|
|
|
|
module GHCup.List
|
|
|
|
) where
|
|
|
|
|
|
|
|
|
|
|
|
import GHCup.Cabal
|
2022-07-09 21:12:00 +00:00
|
|
|
import GHCup.GHC hiding ( GHCVer(..) )
|
|
|
|
import GHCup.HLS hiding ( HLSVer(..) )
|
2022-05-21 20:54:18 +00:00
|
|
|
import GHCup.Stack
|
|
|
|
import GHCup.List
|
2020-01-11 20:15:05 +00:00
|
|
|
import GHCup.Download
|
|
|
|
import GHCup.Errors
|
|
|
|
import GHCup.Platform
|
|
|
|
import GHCup.Types
|
|
|
|
import GHCup.Types.JSON ( )
|
|
|
|
import GHCup.Types.Optics
|
|
|
|
import GHCup.Utils
|
2022-05-21 20:54:18 +00:00
|
|
|
import GHCup.Prelude
|
|
|
|
import GHCup.Prelude.File
|
|
|
|
import GHCup.Prelude.Logger
|
|
|
|
import GHCup.Prelude.String.QQ
|
2020-01-11 20:15:05 +00:00
|
|
|
import GHCup.Version
|
|
|
|
|
|
|
|
import Control.Applicative
|
|
|
|
import Control.Exception.Safe
|
|
|
|
import Control.Monad
|
2020-04-09 17:53:22 +00:00
|
|
|
#if !MIN_VERSION_base(4,13,0)
|
|
|
|
import Control.Monad.Fail ( MonadFail )
|
|
|
|
#endif
|
2020-01-11 20:15:05 +00:00
|
|
|
import Control.Monad.Reader
|
|
|
|
import Control.Monad.Trans.Resource
|
|
|
|
hiding ( throwM )
|
|
|
|
import Data.ByteString ( ByteString )
|
2020-05-10 22:18:53 +00:00
|
|
|
import Data.Either
|
2020-01-11 20:15:05 +00:00
|
|
|
import Data.List
|
|
|
|
import Data.Maybe
|
2021-11-12 18:52:00 +00:00
|
|
|
import Data.Versions hiding ( patch )
|
2020-01-11 20:15:05 +00:00
|
|
|
import GHC.IO.Exception
|
|
|
|
import Haskus.Utils.Variant.Excepts
|
|
|
|
import Optics
|
|
|
|
import Prelude hiding ( abs
|
|
|
|
, writeFile
|
|
|
|
)
|
2021-05-14 21:09:45 +00:00
|
|
|
import System.Environment
|
|
|
|
import System.FilePath
|
2020-01-11 20:15:05 +00:00
|
|
|
import System.IO.Error
|
2020-09-12 14:41:17 +00:00
|
|
|
import Text.Regex.Posix
|
2022-05-21 20:54:18 +00:00
|
|
|
|
2020-04-25 10:06:41 +00:00
|
|
|
import qualified Data.Text as T
|
2022-05-14 15:58:11 +00:00
|
|
|
import qualified Streamly.Prelude as S
|
2022-05-23 14:48:29 +00:00
|
|
|
import Text.PrettyPrint.HughesPJClass (prettyShow)
|
2022-05-21 20:54:18 +00:00
|
|
|
|
|
|
|
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
2021-07-19 14:49:18 +00:00
|
|
|
---------------------
|
|
|
|
--[ Tool fetching ]--
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
|
|
|
fetchToolBindist :: ( MonadFail m
|
|
|
|
, MonadMask m
|
|
|
|
, MonadCatch m
|
|
|
|
, MonadReader env m
|
|
|
|
, HasDirs env
|
|
|
|
, HasSettings env
|
|
|
|
, HasPlatformReq env
|
|
|
|
, HasGHCupInfo env
|
2021-08-30 20:41:58 +00:00
|
|
|
, HasLog env
|
2021-07-19 14:49:18 +00:00
|
|
|
, MonadResource m
|
|
|
|
, MonadIO m
|
|
|
|
, MonadUnliftIO m
|
|
|
|
)
|
|
|
|
=> Version
|
|
|
|
-> Tool
|
|
|
|
-> Maybe FilePath
|
|
|
|
-> Excepts
|
|
|
|
'[ DigestError
|
2021-09-18 17:45:32 +00:00
|
|
|
, GPGError
|
2021-07-19 14:49:18 +00:00
|
|
|
, DownloadFailed
|
|
|
|
, NoDownload
|
|
|
|
]
|
|
|
|
m
|
|
|
|
FilePath
|
|
|
|
fetchToolBindist v t mfp = do
|
|
|
|
dlinfo <- liftE $ getDownloadInfo t v
|
|
|
|
liftE $ downloadCached' dlinfo Nothing mfp
|
|
|
|
|
|
|
|
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2022-05-21 20:54:18 +00:00
|
|
|
------------
|
|
|
|
--[ Nuke ]--
|
|
|
|
------------
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2020-05-10 22:18:53 +00:00
|
|
|
|
|
|
|
|
2021-06-25 08:24:38 +00:00
|
|
|
|
2021-07-18 12:39:49 +00:00
|
|
|
rmTool :: ( MonadReader env m
|
|
|
|
, HasDirs env
|
2021-08-30 20:41:58 +00:00
|
|
|
, HasLog env
|
2021-07-18 12:39:49 +00:00
|
|
|
, MonadFail m
|
|
|
|
, MonadMask m
|
|
|
|
, MonadUnliftIO m)
|
|
|
|
=> ListResult
|
2022-05-12 15:58:40 +00:00
|
|
|
-> Excepts '[NotInstalled, UninstallFailed] m ()
|
2021-06-22 14:39:26 +00:00
|
|
|
rmTool ListResult {lVer, lTool, lCross} = do
|
2021-06-22 08:59:26 +00:00
|
|
|
case lTool of
|
2021-07-02 21:26:07 +00:00
|
|
|
GHC ->
|
2021-06-22 08:59:26 +00:00
|
|
|
let ghcTargetVersion = GHCTargetVersion lCross lVer
|
2021-07-02 21:26:07 +00:00
|
|
|
in rmGHCVer ghcTargetVersion
|
|
|
|
HLS -> rmHLSVer lVer
|
2022-05-12 15:58:40 +00:00
|
|
|
Cabal -> liftE $ rmCabalVer lVer
|
|
|
|
Stack -> liftE $ rmStackVer lVer
|
2021-07-02 21:26:07 +00:00
|
|
|
GHCup -> lift rmGhcup
|
2021-06-22 08:59:26 +00:00
|
|
|
|
|
|
|
|
2021-07-18 12:39:49 +00:00
|
|
|
rmGhcupDirs :: ( MonadReader env m
|
|
|
|
, HasDirs env
|
2021-06-22 17:44:25 +00:00
|
|
|
, MonadIO m
|
2021-08-30 20:41:58 +00:00
|
|
|
, HasLog env
|
2021-06-24 04:38:12 +00:00
|
|
|
, MonadCatch m
|
|
|
|
, MonadMask m )
|
2021-07-02 21:26:07 +00:00
|
|
|
=> m [FilePath]
|
2021-06-22 17:44:25 +00:00
|
|
|
rmGhcupDirs = do
|
2021-07-02 21:26:07 +00:00
|
|
|
Dirs
|
2021-06-22 17:44:25 +00:00
|
|
|
{ baseDir
|
|
|
|
, binDir
|
|
|
|
, logsDir
|
|
|
|
, cacheDir
|
2021-07-22 13:45:08 +00:00
|
|
|
, recycleDir
|
2022-05-16 15:14:40 +00:00
|
|
|
, dbDir
|
2022-05-20 21:19:33 +00:00
|
|
|
, tmpDir
|
2021-07-18 12:39:49 +00:00
|
|
|
} <- getDirs
|
2021-06-22 17:44:25 +00:00
|
|
|
|
2022-05-13 19:35:34 +00:00
|
|
|
let envFilePath = fromGHCupPath baseDir </> "env"
|
2021-06-22 17:44:25 +00:00
|
|
|
|
2021-06-23 04:40:28 +00:00
|
|
|
confFilePath <- getConfigFilePath
|
|
|
|
|
2021-07-22 13:45:08 +00:00
|
|
|
handleRm $ rmEnvFile envFilePath
|
|
|
|
handleRm $ rmConfFile confFilePath
|
2022-05-11 13:47:08 +00:00
|
|
|
|
2021-07-29 09:51:47 +00:00
|
|
|
-- for xdg dirs, the order matters here
|
2022-05-16 15:14:40 +00:00
|
|
|
handleRm $ rmPathForcibly logsDir
|
2022-05-20 21:19:33 +00:00
|
|
|
handleRm $ rmPathForcibly tmpDir
|
2022-05-16 15:14:40 +00:00
|
|
|
handleRm $ rmPathForcibly cacheDir
|
2021-07-29 09:51:47 +00:00
|
|
|
|
2021-07-22 13:45:08 +00:00
|
|
|
handleRm $ rmBinDir binDir
|
2022-05-16 15:14:40 +00:00
|
|
|
handleRm $ rmPathForcibly recycleDir
|
|
|
|
handleRm $ rmPathForcibly dbDir
|
2021-10-17 18:39:49 +00:00
|
|
|
when isWindows $ do
|
2022-05-13 19:35:34 +00:00
|
|
|
logInfo $ "removing " <> T.pack (fromGHCupPath baseDir </> "msys64")
|
|
|
|
handleRm $ rmPathForcibly (baseDir `appendGHCupPath` "msys64")
|
2021-06-22 17:44:25 +00:00
|
|
|
|
2022-05-19 22:46:50 +00:00
|
|
|
handleRm $ removeEmptyDirsRecursive (fromGHCupPath baseDir)
|
2021-06-24 05:24:38 +00:00
|
|
|
|
2021-07-02 21:26:07 +00:00
|
|
|
-- report files in baseDir that are left-over after
|
|
|
|
-- the standard location deletions above
|
2022-05-13 19:35:34 +00:00
|
|
|
hideErrorDef [doesNotExistErrorType] [] $ reportRemainingFiles (fromGHCupPath baseDir)
|
2021-06-22 17:44:25 +00:00
|
|
|
|
|
|
|
where
|
2021-08-30 20:41:58 +00:00
|
|
|
handleRm :: (MonadReader env m, MonadCatch m, HasLog env, MonadIO m) => m () -> m ()
|
|
|
|
handleRm = handleIO (\e -> logDebug $ "Part of the cleanup action failed with error: " <> T.pack (displayException e) <> "\n"
|
2021-08-25 16:54:58 +00:00
|
|
|
<> "continuing regardless...")
|
2021-06-22 17:44:25 +00:00
|
|
|
|
2021-08-30 20:41:58 +00:00
|
|
|
rmEnvFile :: (HasLog env, MonadReader env m, HasDirs env, MonadMask m, MonadIO m, MonadCatch m) => FilePath -> m ()
|
2021-06-23 04:40:28 +00:00
|
|
|
rmEnvFile enFilePath = do
|
2021-08-30 20:41:58 +00:00
|
|
|
logInfo "Removing Ghcup Environment File"
|
2022-05-19 21:17:58 +00:00
|
|
|
hideErrorDef [permissionErrorType] () $ rmFileForce enFilePath
|
2021-06-22 17:44:25 +00:00
|
|
|
|
2021-08-30 20:41:58 +00:00
|
|
|
rmConfFile :: (HasLog env, MonadReader env m, HasDirs env, MonadMask m, MonadIO m, MonadCatch m) => FilePath -> m ()
|
2021-06-23 05:06:17 +00:00
|
|
|
rmConfFile confFilePath = do
|
2021-08-30 20:41:58 +00:00
|
|
|
logInfo "removing Ghcup Config File"
|
2022-05-19 21:17:58 +00:00
|
|
|
hideErrorDef [permissionErrorType] () $ rmFileForce confFilePath
|
2021-06-23 05:06:17 +00:00
|
|
|
|
2021-07-21 13:43:45 +00:00
|
|
|
rmBinDir :: (MonadReader env m, HasDirs env, MonadMask m, MonadIO m, MonadCatch m) => FilePath -> m ()
|
2021-10-17 18:39:49 +00:00
|
|
|
rmBinDir binDir
|
|
|
|
| isWindows = removeDirIfEmptyOrIsSymlink binDir
|
|
|
|
| otherwise = do
|
|
|
|
isXDGStyle <- liftIO useXDG
|
2022-07-11 17:49:08 +00:00
|
|
|
when (not isXDGStyle) $
|
|
|
|
removeDirIfEmptyOrIsSymlink binDir
|
2021-06-24 05:24:38 +00:00
|
|
|
|
2022-05-14 15:58:11 +00:00
|
|
|
reportRemainingFiles :: (MonadMask m, MonadIO m) => FilePath -> m [FilePath]
|
2021-07-02 21:26:07 +00:00
|
|
|
reportRemainingFiles dir = do
|
2022-05-14 15:58:11 +00:00
|
|
|
remainingFiles <- liftIO $ S.toList (getDirectoryContentsRecursiveUnsafe dir)
|
2021-06-29 09:01:13 +00:00
|
|
|
let normalizedFilePaths = fmap normalise remainingFiles
|
2021-06-15 12:00:30 +00:00
|
|
|
let sortedByDepthRemainingFiles = sortBy (flip compareFn) normalizedFilePaths
|
2021-07-02 21:26:07 +00:00
|
|
|
let remainingFilesAbsolute = fmap (dir </>) sortedByDepthRemainingFiles
|
2021-06-29 09:01:13 +00:00
|
|
|
|
2021-06-29 03:26:57 +00:00
|
|
|
pure remainingFilesAbsolute
|
|
|
|
|
2021-06-29 09:01:13 +00:00
|
|
|
where
|
|
|
|
calcDepth :: FilePath -> Int
|
|
|
|
calcDepth = length . filter isPathSeparator
|
|
|
|
|
|
|
|
compareFn :: FilePath -> FilePath -> Ordering
|
|
|
|
compareFn fp1 fp2 = compare (calcDepth fp1) (calcDepth fp2)
|
|
|
|
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2021-07-02 21:26:07 +00:00
|
|
|
|
|
|
|
|
2020-01-11 20:15:05 +00:00
|
|
|
------------------
|
|
|
|
--[ Debug info ]--
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
2021-07-18 12:39:49 +00:00
|
|
|
getDebugInfo :: ( Alternative m
|
|
|
|
, MonadFail m
|
|
|
|
, MonadReader env m
|
|
|
|
, HasDirs env
|
2021-08-30 20:41:58 +00:00
|
|
|
, HasLog env
|
2021-07-18 12:39:49 +00:00
|
|
|
, MonadCatch m
|
|
|
|
, MonadIO m
|
|
|
|
)
|
2020-01-11 20:15:05 +00:00
|
|
|
=> Excepts
|
|
|
|
'[NoCompatiblePlatform , NoCompatibleArch , DistroNotFound]
|
|
|
|
m
|
|
|
|
DebugInfo
|
|
|
|
getDebugInfo = do
|
2021-07-18 12:39:49 +00:00
|
|
|
Dirs {..} <- lift getDirs
|
2022-05-13 19:35:34 +00:00
|
|
|
let diBaseDir = fromGHCupPath baseDir
|
2020-08-05 19:50:39 +00:00
|
|
|
let diBinDir = binDir
|
2022-05-13 19:35:34 +00:00
|
|
|
diGHCDir <- fromGHCupPath <$> lift ghcupGHCBaseDir
|
|
|
|
let diCacheDir = fromGHCupPath cacheDir
|
2020-08-05 19:50:39 +00:00
|
|
|
diArch <- lE getArchitecture
|
2021-03-11 16:03:51 +00:00
|
|
|
diPlatform <- liftE getPlatform
|
2020-01-11 20:15:05 +00:00
|
|
|
pure $ DebugInfo { .. }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-21 20:54:18 +00:00
|
|
|
-------------------------
|
|
|
|
--[ GHCup upgrade etc ]--
|
|
|
|
-------------------------
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
2020-07-21 23:08:58 +00:00
|
|
|
-- | Upgrade ghcup and place it in @~\/.ghcup\/bin\/ghcup@,
|
|
|
|
-- if no path is provided.
|
2020-01-11 20:15:05 +00:00
|
|
|
upgradeGHCup :: ( MonadMask m
|
2021-07-18 12:39:49 +00:00
|
|
|
, MonadReader env m
|
|
|
|
, HasDirs env
|
|
|
|
, HasPlatformReq env
|
|
|
|
, HasGHCupInfo env
|
|
|
|
, HasSettings env
|
2020-01-11 20:15:05 +00:00
|
|
|
, MonadCatch m
|
2021-08-30 20:41:58 +00:00
|
|
|
, HasLog env
|
2020-01-11 20:15:05 +00:00
|
|
|
, MonadThrow m
|
2021-09-25 13:13:44 +00:00
|
|
|
, MonadFail m
|
2020-01-11 20:15:05 +00:00
|
|
|
, MonadResource m
|
|
|
|
, MonadIO m
|
2021-04-25 15:22:07 +00:00
|
|
|
, MonadUnliftIO m
|
2020-01-11 20:15:05 +00:00
|
|
|
)
|
2021-05-14 21:09:45 +00:00
|
|
|
=> Maybe FilePath -- ^ full file destination to write ghcup into
|
2020-04-15 11:57:44 +00:00
|
|
|
-> Bool -- ^ whether to force update regardless
|
|
|
|
-- of currently installed version
|
2022-05-02 17:54:37 +00:00
|
|
|
-> Bool -- ^ whether to throw an error if ghcup is shadowed
|
2020-01-11 20:15:05 +00:00
|
|
|
-> Excepts
|
|
|
|
'[ CopyError
|
|
|
|
, DigestError
|
2021-09-18 17:45:32 +00:00
|
|
|
, GPGError
|
|
|
|
, GPGError
|
2020-01-11 20:15:05 +00:00
|
|
|
, DownloadFailed
|
|
|
|
, NoDownload
|
2020-04-15 11:57:44 +00:00
|
|
|
, NoUpdate
|
2022-05-23 14:48:29 +00:00
|
|
|
, ToolShadowed
|
2020-01-11 20:15:05 +00:00
|
|
|
]
|
|
|
|
m
|
|
|
|
Version
|
2022-05-02 17:54:37 +00:00
|
|
|
upgradeGHCup mtarget force' fatal = do
|
2021-07-18 12:39:49 +00:00
|
|
|
Dirs {..} <- lift getDirs
|
|
|
|
GHCupInfo { _ghcupDownloads = dls } <- lift getGHCupInfo
|
|
|
|
|
2021-08-30 20:41:58 +00:00
|
|
|
lift $ logInfo "Upgrading GHCup..."
|
2022-04-29 14:47:11 +00:00
|
|
|
let latestVer = fst (fromJust (getLatest dls GHCup))
|
2021-11-02 00:22:06 +00:00
|
|
|
(Just ghcupPVPVer) <- pure $ pvpToVersion ghcUpVer ""
|
2021-09-25 13:13:44 +00:00
|
|
|
when (not force' && (latestVer <= ghcupPVPVer)) $ throwE NoUpdate
|
2021-07-19 14:49:18 +00:00
|
|
|
dli <- liftE $ getDownloadInfo GHCup latestVer
|
2022-05-13 19:35:34 +00:00
|
|
|
tmp <- fromGHCupPath <$> lift withGHCupTmpDir
|
2021-05-14 21:09:45 +00:00
|
|
|
let fn = "ghcup" <> exeExt
|
2021-09-18 17:45:32 +00:00
|
|
|
p <- liftE $ download (_dlUri dli) Nothing (Just (_dlHash dli)) tmp (Just fn) False
|
2021-05-14 21:09:45 +00:00
|
|
|
let destDir = takeDirectory destFile
|
2021-07-22 13:45:08 +00:00
|
|
|
destFile = fromMaybe (binDir </> fn) mtarget
|
2021-08-30 20:41:58 +00:00
|
|
|
lift $ logDebug $ "mkdir -p " <> T.pack destDir
|
2021-02-16 13:37:17 +00:00
|
|
|
liftIO $ createDirRecursive' destDir
|
2021-08-30 20:41:58 +00:00
|
|
|
lift $ logDebug $ "rm -f " <> T.pack destFile
|
2021-07-22 13:45:08 +00:00
|
|
|
lift $ hideError NoSuchThing $ recycleFile destFile
|
2021-08-30 20:41:58 +00:00
|
|
|
lift $ logDebug $ "cp " <> T.pack p <> " " <> T.pack destFile
|
2022-05-12 15:58:40 +00:00
|
|
|
copyFileE p destFile False
|
2021-02-16 13:37:17 +00:00
|
|
|
lift $ chmod_755 destFile
|
2021-02-21 18:58:32 +00:00
|
|
|
|
2021-03-11 16:03:51 +00:00
|
|
|
liftIO (isInPath destFile) >>= \b -> unless b $
|
2021-08-30 20:41:58 +00:00
|
|
|
lift $ logWarn $ T.pack (takeFileName destFile) <> " is not in PATH! You have to add it in order to use ghcup."
|
2021-02-21 18:58:32 +00:00
|
|
|
liftIO (isShadowed destFile) >>= \case
|
|
|
|
Nothing -> pure ()
|
2022-05-02 17:54:37 +00:00
|
|
|
Just pa
|
2022-05-23 14:48:29 +00:00
|
|
|
| fatal -> throwE (ToolShadowed GHCup pa destFile latestVer)
|
2022-05-02 17:54:37 +00:00
|
|
|
| otherwise ->
|
2022-05-23 14:48:29 +00:00
|
|
|
lift $ logWarn $ T.pack $ prettyShow (ToolShadowed GHCup pa destFile latestVer)
|
2021-02-21 18:58:32 +00:00
|
|
|
|
2020-01-11 20:15:05 +00:00
|
|
|
pure latestVer
|
|
|
|
|
|
|
|
|
2022-05-21 20:54:18 +00:00
|
|
|
-- assuming the current scheme of having just 1 ghcup bin, no version info is required.
|
|
|
|
rmGhcup :: ( MonadReader env m
|
|
|
|
, HasDirs env
|
|
|
|
, MonadIO m
|
|
|
|
, MonadCatch m
|
|
|
|
, HasLog env
|
|
|
|
, MonadMask m
|
|
|
|
, MonadUnliftIO m
|
|
|
|
)
|
|
|
|
=> m ()
|
|
|
|
rmGhcup = do
|
|
|
|
Dirs { .. } <- getDirs
|
|
|
|
let ghcupFilename = "ghcup" <> exeExt
|
|
|
|
let ghcupFilepath = binDir </> ghcupFilename
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2022-05-21 20:54:18 +00:00
|
|
|
currentRunningExecPath <- liftIO getExecutablePath
|
|
|
|
|
|
|
|
-- if paths do no exist, warn user, and continue to compare them, as is,
|
|
|
|
-- which should eventually fail and result in a non-standard install warning
|
|
|
|
|
|
|
|
p1 <- handleIO' doesNotExistErrorType
|
|
|
|
(handlePathNotPresent currentRunningExecPath)
|
|
|
|
(liftIO $ canonicalizePath currentRunningExecPath)
|
|
|
|
|
|
|
|
p2 <- handleIO' doesNotExistErrorType
|
|
|
|
(handlePathNotPresent ghcupFilepath)
|
|
|
|
(liftIO $ canonicalizePath ghcupFilepath)
|
|
|
|
|
|
|
|
let areEqualPaths = equalFilePath p1 p2
|
|
|
|
|
|
|
|
unless areEqualPaths $ logWarn $ nonStandardInstallLocationMsg currentRunningExecPath
|
|
|
|
|
|
|
|
if isWindows
|
|
|
|
then do
|
|
|
|
-- since it doesn't seem possible to delete a running exe on windows
|
|
|
|
-- we move it to temp dir, to be deleted at next reboot
|
|
|
|
tempFilepath <- mkGhcupTmpDir
|
|
|
|
hideError UnsupportedOperation $
|
|
|
|
liftIO $ hideError NoSuchThing $
|
|
|
|
moveFile ghcupFilepath (fromGHCupPath tempFilepath </> "ghcup")
|
|
|
|
else
|
|
|
|
-- delete it.
|
|
|
|
hideError doesNotExistErrorType $ rmFile ghcupFilepath
|
|
|
|
|
|
|
|
where
|
|
|
|
handlePathNotPresent fp _err = do
|
|
|
|
logDebug $ "Error: The path does not exist, " <> T.pack fp
|
|
|
|
pure fp
|
|
|
|
|
|
|
|
nonStandardInstallLocationMsg path = T.pack $
|
|
|
|
"current ghcup is invoked from a non-standard location: \n"
|
|
|
|
<> path <>
|
|
|
|
"\n you may have to uninstall it manually."
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2021-08-10 14:42:14 +00:00
|
|
|
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2022-05-21 20:54:18 +00:00
|
|
|
---------------
|
|
|
|
--[ Whereis ]--
|
|
|
|
---------------
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2021-07-12 13:40:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- | Reports the binary location of a given tool:
|
|
|
|
--
|
|
|
|
-- * for GHC, this reports: @~\/.ghcup\/ghc\/\<ver\>\/bin\/ghc@
|
|
|
|
-- * for cabal, this reports @~\/.ghcup\/bin\/cabal-\<ver\>@
|
|
|
|
-- * for hls, this reports @~\/.ghcup\/bin\/haskell-language-server-wrapper-\<ver\>@
|
|
|
|
-- * for stack, this reports @~\/.ghcup\/bin\/stack-\<ver\>@
|
|
|
|
-- * for ghcup, this reports the location of the currently running executable
|
2021-07-18 12:39:49 +00:00
|
|
|
whereIsTool :: ( MonadReader env m
|
|
|
|
, HasDirs env
|
2021-08-30 20:41:58 +00:00
|
|
|
, HasLog env
|
2021-07-12 13:40:42 +00:00
|
|
|
, MonadThrow m
|
|
|
|
, MonadFail m
|
|
|
|
, MonadIO m
|
|
|
|
, MonadCatch m
|
|
|
|
, MonadMask m
|
|
|
|
, MonadUnliftIO m
|
|
|
|
)
|
|
|
|
=> Tool
|
|
|
|
-> GHCTargetVersion
|
|
|
|
-> Excepts '[NotInstalled] m FilePath
|
|
|
|
whereIsTool tool ver@GHCTargetVersion {..} = do
|
2021-07-18 12:39:49 +00:00
|
|
|
dirs <- lift getDirs
|
2021-07-12 13:40:42 +00:00
|
|
|
|
|
|
|
case tool of
|
|
|
|
GHC -> do
|
|
|
|
whenM (lift $ fmap not $ ghcInstalled ver)
|
|
|
|
$ throwE (NotInstalled GHC ver)
|
2022-05-13 19:35:34 +00:00
|
|
|
bdir <- fromGHCupPath <$> lift (ghcupGHCDir ver)
|
2021-07-15 20:38:42 +00:00
|
|
|
pure (bdir </> "bin" </> ghcBinaryName ver)
|
2021-07-12 13:40:42 +00:00
|
|
|
Cabal -> do
|
|
|
|
whenM (lift $ fmap not $ cabalInstalled _tvVersion)
|
|
|
|
$ throwE (NotInstalled Cabal (GHCTargetVersion Nothing _tvVersion))
|
|
|
|
pure (binDir dirs </> "cabal-" <> T.unpack (prettyVer _tvVersion) <> exeExt)
|
|
|
|
HLS -> do
|
|
|
|
whenM (lift $ fmap not $ hlsInstalled _tvVersion)
|
|
|
|
$ throwE (NotInstalled HLS (GHCTargetVersion Nothing _tvVersion))
|
2022-02-05 18:11:56 +00:00
|
|
|
ifM (lift $ isLegacyHLS _tvVersion)
|
|
|
|
(pure (binDir dirs </> "haskell-language-server-wrapper-" <> T.unpack (prettyVer _tvVersion) <> exeExt))
|
|
|
|
$ do
|
2022-05-13 19:35:34 +00:00
|
|
|
bdir <- fromGHCupPath <$> lift (ghcupHLSDir _tvVersion)
|
2022-02-05 18:11:56 +00:00
|
|
|
pure (bdir </> "bin" </> "haskell-language-server-wrapper" <> exeExt)
|
2021-07-12 13:40:42 +00:00
|
|
|
|
|
|
|
Stack -> do
|
|
|
|
whenM (lift $ fmap not $ stackInstalled _tvVersion)
|
|
|
|
$ throwE (NotInstalled Stack (GHCTargetVersion Nothing _tvVersion))
|
|
|
|
pure (binDir dirs </> "stack-" <> T.unpack (prettyVer _tvVersion) <> exeExt)
|
|
|
|
GHCup -> do
|
|
|
|
currentRunningExecPath <- liftIO getExecutablePath
|
|
|
|
liftIO $ canonicalizePath currentRunningExecPath
|
2021-07-18 12:39:49 +00:00
|
|
|
|
2022-05-21 20:54:18 +00:00
|
|
|
|
2021-09-18 13:46:53 +00:00
|
|
|
-- | Doesn't work for cross GHC.
|
2021-09-12 03:54:04 +00:00
|
|
|
checkIfToolInstalled :: ( MonadIO m
|
|
|
|
, MonadReader env m
|
|
|
|
, HasDirs env
|
|
|
|
, MonadCatch m) =>
|
|
|
|
Tool ->
|
|
|
|
Version ->
|
|
|
|
m Bool
|
2022-02-09 17:57:59 +00:00
|
|
|
checkIfToolInstalled tool ver = checkIfToolInstalled' tool (mkTVer ver)
|
2021-09-12 03:54:04 +00:00
|
|
|
|
2022-05-21 20:54:18 +00:00
|
|
|
|
2022-02-09 17:57:59 +00:00
|
|
|
checkIfToolInstalled' :: ( MonadIO m
|
|
|
|
, MonadReader env m
|
|
|
|
, HasDirs env
|
|
|
|
, MonadCatch m) =>
|
|
|
|
Tool ->
|
|
|
|
GHCTargetVersion ->
|
|
|
|
m Bool
|
|
|
|
checkIfToolInstalled' tool ver =
|
2021-09-12 03:54:04 +00:00
|
|
|
case tool of
|
2022-02-09 17:57:59 +00:00
|
|
|
Cabal -> cabalInstalled (_tvVersion ver)
|
|
|
|
HLS -> hlsInstalled (_tvVersion ver)
|
|
|
|
Stack -> stackInstalled (_tvVersion ver)
|
|
|
|
GHC -> ghcInstalled ver
|
2021-09-12 03:54:04 +00:00
|
|
|
_ -> pure False
|
2021-07-18 12:39:49 +00:00
|
|
|
|
|
|
|
|
2021-09-25 19:09:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
--------------------------
|
|
|
|
--[ Garbage collection ]--
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
|
|
|
|
rmOldGHC :: ( MonadReader env m
|
|
|
|
, HasGHCupInfo env
|
|
|
|
, HasDirs env
|
|
|
|
, HasLog env
|
|
|
|
, MonadIO m
|
|
|
|
, MonadFail m
|
|
|
|
, MonadMask m
|
|
|
|
, MonadUnliftIO m
|
|
|
|
)
|
2022-05-12 15:58:40 +00:00
|
|
|
=> Excepts '[NotInstalled, UninstallFailed] m ()
|
2021-09-25 19:09:18 +00:00
|
|
|
rmOldGHC = do
|
|
|
|
GHCupInfo { _ghcupDownloads = dls } <- lift getGHCupInfo
|
|
|
|
let oldGHCs = mkTVer <$> toListOf (ix GHC % getTagged Old % to fst) dls
|
|
|
|
ghcs <- lift $ fmap rights getInstalledGHCs
|
|
|
|
forM_ ghcs $ \ghc -> when (ghc `elem` oldGHCs) $ rmGHCVer ghc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rmProfilingLibs :: ( MonadReader env m
|
|
|
|
, HasDirs env
|
|
|
|
, HasLog env
|
|
|
|
, MonadIO m
|
|
|
|
, MonadFail m
|
|
|
|
, MonadMask m
|
|
|
|
, MonadUnliftIO m
|
|
|
|
)
|
|
|
|
=> m ()
|
|
|
|
rmProfilingLibs = do
|
|
|
|
ghcs <- fmap rights getInstalledGHCs
|
|
|
|
|
|
|
|
let regexes :: [ByteString]
|
|
|
|
regexes = [[s|.*_p\.a$|], [s|.*\.p_hi$|]]
|
|
|
|
|
|
|
|
forM_ regexes $ \regex ->
|
|
|
|
forM_ ghcs $ \ghc -> do
|
|
|
|
d <- ghcupGHCDir ghc
|
2022-05-13 19:35:34 +00:00
|
|
|
-- TODO: audit findFilesDeep
|
2021-09-25 19:09:18 +00:00
|
|
|
matches <- liftIO $ handleIO (\_ -> pure []) $ findFilesDeep
|
|
|
|
d
|
|
|
|
(makeRegexOpts compExtended
|
|
|
|
execBlank
|
|
|
|
regex
|
|
|
|
)
|
|
|
|
forM_ matches $ \m -> do
|
2022-05-13 19:35:34 +00:00
|
|
|
let p = fromGHCupPath d </> m
|
2021-09-25 19:09:18 +00:00
|
|
|
logDebug $ "rm " <> T.pack p
|
|
|
|
rmFile p
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rmShareDir :: ( MonadReader env m
|
|
|
|
, HasDirs env
|
|
|
|
, HasLog env
|
|
|
|
, MonadIO m
|
|
|
|
, MonadFail m
|
|
|
|
, MonadMask m
|
|
|
|
, MonadUnliftIO m
|
|
|
|
)
|
|
|
|
=> m ()
|
|
|
|
rmShareDir = do
|
|
|
|
ghcs <- fmap rights getInstalledGHCs
|
|
|
|
forM_ ghcs $ \ghc -> do
|
|
|
|
d <- ghcupGHCDir ghc
|
2022-05-13 19:35:34 +00:00
|
|
|
let p = d `appendGHCupPath` "share"
|
|
|
|
logDebug $ "rm -rf " <> T.pack (fromGHCupPath p)
|
2021-09-25 19:09:18 +00:00
|
|
|
rmPathForcibly p
|
|
|
|
|
|
|
|
|
|
|
|
rmHLSNoGHC :: ( MonadReader env m
|
|
|
|
, HasDirs env
|
|
|
|
, HasLog env
|
|
|
|
, MonadIO m
|
|
|
|
, MonadMask m
|
2022-02-05 18:12:13 +00:00
|
|
|
, MonadFail m
|
|
|
|
, MonadUnliftIO m
|
2021-09-25 19:09:18 +00:00
|
|
|
)
|
2022-05-12 15:58:40 +00:00
|
|
|
=> Excepts '[NotInstalled, UninstallFailed] m ()
|
2021-09-25 19:09:18 +00:00
|
|
|
rmHLSNoGHC = do
|
|
|
|
Dirs {..} <- getDirs
|
|
|
|
ghcs <- fmap rights getInstalledGHCs
|
|
|
|
hlses <- fmap rights getInstalledHLSs
|
|
|
|
forM_ hlses $ \hls -> do
|
|
|
|
hlsGHCs <- fmap mkTVer <$> hlsGHCVersions' hls
|
2022-02-05 18:39:00 +00:00
|
|
|
let candidates = filter (`notElem` ghcs) hlsGHCs
|
2022-02-05 18:12:13 +00:00
|
|
|
if (length hlsGHCs - length candidates) <= 0
|
|
|
|
then rmHLSVer hls
|
|
|
|
else
|
|
|
|
forM_ candidates $ \ghc -> do
|
|
|
|
bins1 <- fmap (binDir </>) <$> hlsServerBinaries hls (Just $ _tvVersion ghc)
|
|
|
|
bins2 <- ifM (isLegacyHLS hls) (pure []) $ do
|
|
|
|
shs <- hlsInternalServerScripts hls (Just $ _tvVersion ghc)
|
|
|
|
bins <- hlsInternalServerBinaries hls (Just $ _tvVersion ghc)
|
|
|
|
libs <- hlsInternalServerLibs hls (_tvVersion ghc)
|
|
|
|
pure (shs ++ bins ++ libs)
|
|
|
|
forM_ (bins1 ++ bins2) $ \f -> do
|
2021-09-25 19:09:18 +00:00
|
|
|
logDebug $ "rm " <> T.pack f
|
|
|
|
rmFile f
|
2022-02-05 18:12:13 +00:00
|
|
|
pure ()
|
2021-09-25 19:09:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
rmCache :: ( MonadReader env m
|
|
|
|
, HasDirs env
|
|
|
|
, HasLog env
|
|
|
|
, MonadIO m
|
|
|
|
, MonadMask m
|
|
|
|
)
|
|
|
|
=> m ()
|
|
|
|
rmCache = do
|
|
|
|
Dirs {..} <- getDirs
|
2022-05-13 19:35:34 +00:00
|
|
|
contents <- liftIO $ listDirectory (fromGHCupPath cacheDir)
|
2021-09-25 19:09:18 +00:00
|
|
|
forM_ contents $ \f -> do
|
2022-05-13 19:35:34 +00:00
|
|
|
let p = fromGHCupPath cacheDir </> f
|
2021-09-25 19:09:18 +00:00
|
|
|
logDebug $ "rm " <> T.pack p
|
|
|
|
rmFile p
|
|
|
|
|
|
|
|
|
|
|
|
rmTmp :: ( MonadReader env m
|
|
|
|
, HasDirs env
|
|
|
|
, HasLog env
|
|
|
|
, MonadIO m
|
|
|
|
, MonadMask m
|
|
|
|
)
|
|
|
|
=> m ()
|
|
|
|
rmTmp = do
|
2022-05-13 19:35:34 +00:00
|
|
|
ghcup_dirs <- liftIO getGHCupTmpDirs
|
2021-09-25 19:09:18 +00:00
|
|
|
forM_ ghcup_dirs $ \f -> do
|
2022-05-13 19:35:34 +00:00
|
|
|
logDebug $ "rm -rf " <> T.pack (fromGHCupPath f)
|
|
|
|
rmPathForcibly f
|
2021-11-12 18:52:00 +00:00
|
|
|
|
|
|
|
|