2021-05-14 21:09:45 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2020-10-24 20:03:00 +00:00
|
|
|
{-# LANGUAGE DataKinds #-}
|
2020-03-21 21:19:37 +00:00
|
|
|
{-# LANGUAGE OverloadedStrings #-}
|
2020-07-31 18:10:40 +00:00
|
|
|
{-# LANGUAGE FlexibleContexts #-}
|
2020-04-25 10:06:41 +00:00
|
|
|
{-# LANGUAGE ViewPatterns #-}
|
2022-05-13 19:35:34 +00:00
|
|
|
{-# LANGUAGE QuasiQuotes #-}
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2020-07-21 23:08:58 +00:00
|
|
|
{-|
|
|
|
|
Module : GHCup.Utils.Dirs
|
|
|
|
Description : Definition of GHCup directories
|
|
|
|
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-07-31 18:10:40 +00:00
|
|
|
module GHCup.Utils.Dirs
|
2021-07-18 12:39:49 +00:00
|
|
|
( getAllDirs
|
2021-05-14 21:09:45 +00:00
|
|
|
, ghcupBaseDir
|
2020-10-24 20:03:00 +00:00
|
|
|
, ghcupConfigFile
|
2021-04-11 20:15:43 +00:00
|
|
|
, ghcupCacheDir
|
2020-07-31 18:10:40 +00:00
|
|
|
, ghcupGHCBaseDir
|
|
|
|
, ghcupGHCDir
|
2022-02-05 00:53:04 +00:00
|
|
|
, ghcupHLSBaseDir
|
|
|
|
, ghcupHLSDir
|
2020-07-31 18:10:40 +00:00
|
|
|
, mkGhcupTmpDir
|
2020-10-24 20:03:00 +00:00
|
|
|
, parseGHCupGHCDir
|
2022-02-05 00:53:04 +00:00
|
|
|
, parseGHCupHLSDir
|
2020-07-31 18:10:40 +00:00
|
|
|
, relativeSymlink
|
2020-10-24 20:03:00 +00:00
|
|
|
, withGHCupTmpDir
|
2021-06-23 04:38:06 +00:00
|
|
|
, getConfigFilePath
|
2023-11-13 08:53:24 +00:00
|
|
|
, getConfigFilePath'
|
2021-06-24 17:18:38 +00:00
|
|
|
, useXDG
|
2021-07-22 13:45:08 +00:00
|
|
|
, cleanupTrash
|
2022-05-13 19:35:34 +00:00
|
|
|
|
|
|
|
, GHCupPath
|
|
|
|
, appendGHCupPath
|
|
|
|
, fromGHCupPath
|
|
|
|
, createTempGHCupDirectory
|
|
|
|
, getGHCupTmpDirs
|
|
|
|
|
|
|
|
, removeDirectory
|
|
|
|
, removeDirectoryRecursive
|
|
|
|
, removePathForcibly
|
|
|
|
|
2023-02-23 13:47:50 +00:00
|
|
|
, listDirectoryFiles
|
|
|
|
, listDirectoryDirs
|
|
|
|
|
2022-05-13 19:35:34 +00:00
|
|
|
-- System.Directory re-exports
|
|
|
|
, createDirectory
|
|
|
|
, createDirectoryIfMissing
|
|
|
|
, renameDirectory
|
|
|
|
, listDirectory
|
|
|
|
, getDirectoryContents
|
|
|
|
, getCurrentDirectory
|
|
|
|
, setCurrentDirectory
|
|
|
|
, withCurrentDirectory
|
|
|
|
, getHomeDirectory
|
|
|
|
, XdgDirectory(..)
|
|
|
|
, getXdgDirectory
|
|
|
|
, XdgDirectoryList(..)
|
|
|
|
, getXdgDirectoryList
|
|
|
|
, getAppUserDataDirectory
|
|
|
|
, getUserDocumentsDirectory
|
|
|
|
, getTemporaryDirectory
|
|
|
|
, removeFile
|
|
|
|
, renameFile
|
|
|
|
, renamePath
|
|
|
|
, getFileSize
|
|
|
|
, canonicalizePath
|
|
|
|
, makeAbsolute
|
|
|
|
, makeRelativeToCurrentDirectory
|
|
|
|
, doesPathExist
|
|
|
|
, doesFileExist
|
|
|
|
, doesDirectoryExist
|
|
|
|
, findExecutable
|
|
|
|
, findExecutables
|
|
|
|
, findExecutablesInDirectories
|
|
|
|
, findFile
|
|
|
|
, findFileWith
|
|
|
|
, findFilesWith
|
|
|
|
, exeExtension
|
|
|
|
, createFileLink
|
|
|
|
, createDirectoryLink
|
|
|
|
, removeDirectoryLink
|
|
|
|
, pathIsSymbolicLink
|
|
|
|
, getSymbolicLinkTarget
|
|
|
|
, Permissions
|
|
|
|
, emptyPermissions
|
|
|
|
, readable
|
|
|
|
, writable
|
|
|
|
, executable
|
|
|
|
, searchable
|
|
|
|
, setOwnerReadable
|
|
|
|
, setOwnerWritable
|
|
|
|
, setOwnerExecutable
|
|
|
|
, setOwnerSearchable
|
|
|
|
, getPermissions
|
|
|
|
, setPermissions
|
|
|
|
, copyPermissions
|
|
|
|
, getAccessTime
|
|
|
|
, getModificationTime
|
|
|
|
, setAccessTime
|
|
|
|
, setModificationTime
|
|
|
|
, isSymbolicLink
|
2020-07-31 18:10:40 +00:00
|
|
|
)
|
|
|
|
where
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
2020-10-24 20:03:00 +00:00
|
|
|
import GHCup.Errors
|
2020-04-25 10:06:41 +00:00
|
|
|
import GHCup.Types
|
2020-01-11 20:15:05 +00:00
|
|
|
import GHCup.Types.JSON ( )
|
2021-07-18 12:39:49 +00:00
|
|
|
import GHCup.Types.Optics
|
2022-05-21 20:54:18 +00:00
|
|
|
import GHCup.Prelude.MegaParsec
|
|
|
|
import GHCup.Prelude.File.Search
|
|
|
|
import GHCup.Prelude.String.QQ
|
|
|
|
import GHCup.Prelude.Logger.Internal (logWarn, logDebug)
|
|
|
|
#if defined(IS_WINDOWS)
|
|
|
|
import GHCup.Prelude.Windows ( isWindows )
|
|
|
|
#else
|
|
|
|
import GHCup.Prelude.Posix ( isWindows )
|
|
|
|
#endif
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2022-05-13 19:35:34 +00:00
|
|
|
import Control.DeepSeq (NFData, rnf)
|
2020-01-11 20:15:05 +00:00
|
|
|
import Control.Exception.Safe
|
|
|
|
import Control.Monad
|
2021-04-25 15:22:07 +00:00
|
|
|
import Control.Monad.IO.Unlift
|
2020-01-11 20:15:05 +00:00
|
|
|
import Control.Monad.Reader
|
2021-04-25 15:22:07 +00:00
|
|
|
import Control.Monad.Trans.Resource hiding (throwM)
|
2022-05-13 19:35:34 +00:00
|
|
|
import Data.List
|
|
|
|
import Data.ByteString ( ByteString )
|
2020-10-24 20:03:00 +00:00
|
|
|
import Data.Bifunctor
|
2020-01-11 20:15:05 +00:00
|
|
|
import Data.Maybe
|
2022-02-05 00:53:04 +00:00
|
|
|
import Data.Versions
|
2020-10-24 20:03:00 +00:00
|
|
|
import GHC.IO.Exception ( IOErrorType(NoSuchThing) )
|
|
|
|
import Haskus.Utils.Variant.Excepts
|
2023-02-23 13:47:50 +00:00
|
|
|
import Optics hiding ( uncons )
|
2022-05-14 15:58:11 +00:00
|
|
|
import Safe
|
2022-05-13 19:35:34 +00:00
|
|
|
import System.Directory hiding ( removeDirectory
|
|
|
|
, removeDirectoryRecursive
|
|
|
|
, removePathForcibly
|
|
|
|
, findFiles
|
|
|
|
)
|
|
|
|
import qualified System.Directory as SD
|
|
|
|
|
2021-05-14 21:09:45 +00:00
|
|
|
import System.Environment
|
|
|
|
import System.FilePath
|
|
|
|
import System.IO.Temp
|
2022-05-13 19:35:34 +00:00
|
|
|
import Text.Regex.Posix
|
2021-05-14 21:09:45 +00:00
|
|
|
|
|
|
|
import qualified Data.ByteString as BS
|
2021-04-25 15:22:07 +00:00
|
|
|
import qualified Data.Text as T
|
2021-10-21 21:17:26 +00:00
|
|
|
import qualified Data.Yaml.Aeson as Y
|
2020-04-25 10:06:41 +00:00
|
|
|
import qualified Text.Megaparsec as MP
|
2022-05-21 20:54:18 +00:00
|
|
|
import System.IO.Error (ioeGetErrorType)
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2022-05-13 19:35:34 +00:00
|
|
|
---------------------------
|
|
|
|
--[ GHCupPath utilities ]--
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
-- | A 'GHCupPath' is a safe sub-path that can be recursively deleted.
|
|
|
|
--
|
|
|
|
-- The constructor is not exported.
|
|
|
|
newtype GHCupPath = GHCupPath FilePath
|
|
|
|
deriving (Show, Eq, Ord)
|
|
|
|
|
|
|
|
instance NFData GHCupPath where
|
|
|
|
rnf (GHCupPath fp) = rnf fp
|
|
|
|
|
|
|
|
appendGHCupPath :: GHCupPath -> FilePath -> GHCupPath
|
|
|
|
appendGHCupPath (GHCupPath gp) fp = GHCupPath (gp </> fp)
|
|
|
|
|
|
|
|
fromGHCupPath :: GHCupPath -> FilePath
|
|
|
|
fromGHCupPath (GHCupPath gp) = gp
|
|
|
|
|
|
|
|
createTempGHCupDirectory :: GHCupPath -> FilePath -> IO GHCupPath
|
|
|
|
createTempGHCupDirectory (GHCupPath gp) d = GHCupPath <$> createTempDirectory gp d
|
|
|
|
|
|
|
|
|
|
|
|
getGHCupTmpDirs :: IO [GHCupPath]
|
|
|
|
getGHCupTmpDirs = do
|
2022-05-20 21:19:33 +00:00
|
|
|
tmpdir <- fromGHCupPath <$> ghcupTMPDir
|
2022-05-13 19:35:34 +00:00
|
|
|
ghcup_dirs <- handleIO (\_ -> pure []) $ findFiles
|
|
|
|
tmpdir
|
|
|
|
(makeRegexOpts compExtended
|
|
|
|
execBlank
|
|
|
|
([s|^ghcup-.*$|] :: ByteString)
|
|
|
|
)
|
2022-05-14 15:58:11 +00:00
|
|
|
pure (fmap (\p -> GHCupPath (tmpdir </> p)) $ filter (maybe False ("ghcup-" `isPrefixOf`) . lastMay . splitPath) ghcup_dirs)
|
2022-05-13 19:35:34 +00:00
|
|
|
|
|
|
|
|
2020-07-31 18:10:40 +00:00
|
|
|
------------------------------
|
|
|
|
--[ GHCup base directories ]--
|
|
|
|
------------------------------
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
2020-04-25 10:06:41 +00:00
|
|
|
-- | ~/.ghcup by default
|
2020-07-28 23:43:00 +00:00
|
|
|
--
|
|
|
|
-- If 'GHCUP_USE_XDG_DIRS' is set (to anything),
|
|
|
|
-- then uses 'XDG_DATA_HOME/ghcup' as per xdg spec.
|
2022-05-13 19:35:34 +00:00
|
|
|
ghcupBaseDir :: IO GHCupPath
|
2021-10-17 18:39:49 +00:00
|
|
|
ghcupBaseDir
|
|
|
|
| isWindows = do
|
|
|
|
bdir <- fromMaybe "C:\\" <$> lookupEnv "GHCUP_INSTALL_BASE_PREFIX"
|
2022-05-13 19:35:34 +00:00
|
|
|
pure (GHCupPath (bdir </> "ghcup"))
|
2021-10-17 18:39:49 +00:00
|
|
|
| otherwise = do
|
|
|
|
xdg <- useXDG
|
|
|
|
if xdg
|
|
|
|
then do
|
|
|
|
bdir <- lookupEnv "XDG_DATA_HOME" >>= \case
|
|
|
|
Just r -> pure r
|
|
|
|
Nothing -> do
|
|
|
|
home <- liftIO getHomeDirectory
|
|
|
|
pure (home </> ".local" </> "share")
|
2022-05-13 19:35:34 +00:00
|
|
|
pure (GHCupPath (bdir </> "ghcup"))
|
2021-10-17 18:39:49 +00:00
|
|
|
else do
|
|
|
|
bdir <- lookupEnv "GHCUP_INSTALL_BASE_PREFIX" >>= \case
|
|
|
|
Just r -> pure r
|
|
|
|
Nothing -> liftIO getHomeDirectory
|
2022-05-13 19:35:34 +00:00
|
|
|
pure (GHCupPath (bdir </> ".ghcup"))
|
2020-01-11 20:15:05 +00:00
|
|
|
|
2020-04-25 10:06:41 +00:00
|
|
|
|
2020-10-24 20:03:00 +00:00
|
|
|
-- | ~/.ghcup by default
|
|
|
|
--
|
|
|
|
-- If 'GHCUP_USE_XDG_DIRS' is set (to anything),
|
|
|
|
-- then uses 'XDG_CONFIG_HOME/ghcup' as per xdg spec.
|
2022-05-13 19:35:34 +00:00
|
|
|
ghcupConfigDir :: IO GHCupPath
|
2021-10-17 18:39:49 +00:00
|
|
|
ghcupConfigDir
|
|
|
|
| isWindows = ghcupBaseDir
|
|
|
|
| otherwise = do
|
|
|
|
xdg <- useXDG
|
|
|
|
if xdg
|
|
|
|
then do
|
|
|
|
bdir <- lookupEnv "XDG_CONFIG_HOME" >>= \case
|
|
|
|
Just r -> pure r
|
|
|
|
Nothing -> do
|
|
|
|
home <- liftIO getHomeDirectory
|
|
|
|
pure (home </> ".config")
|
2022-05-13 19:35:34 +00:00
|
|
|
pure (GHCupPath (bdir </> "ghcup"))
|
2021-10-17 18:39:49 +00:00
|
|
|
else do
|
|
|
|
bdir <- lookupEnv "GHCUP_INSTALL_BASE_PREFIX" >>= \case
|
|
|
|
Just r -> pure r
|
|
|
|
Nothing -> liftIO getHomeDirectory
|
2022-05-13 19:35:34 +00:00
|
|
|
pure (GHCupPath (bdir </> ".ghcup"))
|
2020-10-24 20:03:00 +00:00
|
|
|
|
|
|
|
|
2020-07-28 23:43:00 +00:00
|
|
|
-- | If 'GHCUP_USE_XDG_DIRS' is set (to anything),
|
|
|
|
-- then uses 'XDG_BIN_HOME' env var or defaults to '~/.local/bin'
|
|
|
|
-- (which, sadly is not strictly xdg spec).
|
2021-05-14 21:09:45 +00:00
|
|
|
ghcupBinDir :: IO FilePath
|
2021-10-17 18:39:49 +00:00
|
|
|
ghcupBinDir
|
2022-05-13 19:35:34 +00:00
|
|
|
| isWindows = (fromGHCupPath <$> ghcupBaseDir) <&> (</> "bin")
|
2021-10-17 18:39:49 +00:00
|
|
|
| otherwise = do
|
|
|
|
xdg <- useXDG
|
|
|
|
if xdg
|
|
|
|
then do
|
|
|
|
lookupEnv "XDG_BIN_HOME" >>= \case
|
|
|
|
Just r -> pure r
|
|
|
|
Nothing -> do
|
|
|
|
home <- liftIO getHomeDirectory
|
|
|
|
pure (home </> ".local" </> "bin")
|
2022-05-13 19:35:34 +00:00
|
|
|
else (fromGHCupPath <$> ghcupBaseDir) <&> (</> "bin")
|
2020-07-28 23:43:00 +00:00
|
|
|
|
2020-07-31 18:10:40 +00:00
|
|
|
|
2020-07-28 23:43:00 +00:00
|
|
|
-- | Defaults to '~/.ghcup/cache'.
|
|
|
|
--
|
|
|
|
-- If 'GHCUP_USE_XDG_DIRS' is set (to anything),
|
|
|
|
-- then uses 'XDG_CACHE_HOME/ghcup' as per xdg spec.
|
2022-05-13 19:35:34 +00:00
|
|
|
ghcupCacheDir :: IO GHCupPath
|
2021-10-17 18:39:49 +00:00
|
|
|
ghcupCacheDir
|
2022-05-13 19:35:34 +00:00
|
|
|
| isWindows = ghcupBaseDir <&> (\(GHCupPath gp) -> GHCupPath (gp </> "cache"))
|
2021-10-17 18:39:49 +00:00
|
|
|
| otherwise = do
|
|
|
|
xdg <- useXDG
|
|
|
|
if xdg
|
|
|
|
then do
|
|
|
|
bdir <- lookupEnv "XDG_CACHE_HOME" >>= \case
|
|
|
|
Just r -> pure r
|
|
|
|
Nothing -> do
|
|
|
|
home <- liftIO getHomeDirectory
|
|
|
|
pure (home </> ".cache")
|
2023-03-18 14:04:37 +00:00
|
|
|
pure (GHCupPath (bdir </> "ghcup" </> "cache"))
|
2022-05-13 19:35:34 +00:00
|
|
|
else ghcupBaseDir <&> (\(GHCupPath gp) -> GHCupPath (gp </> "cache"))
|
2020-07-28 23:43:00 +00:00
|
|
|
|
2020-07-31 18:10:40 +00:00
|
|
|
|
2020-07-28 23:43:00 +00:00
|
|
|
-- | Defaults to '~/.ghcup/logs'.
|
|
|
|
--
|
|
|
|
-- If 'GHCUP_USE_XDG_DIRS' is set (to anything),
|
|
|
|
-- then uses 'XDG_CACHE_HOME/ghcup/logs' as per xdg spec.
|
2022-05-13 19:35:34 +00:00
|
|
|
ghcupLogsDir :: IO GHCupPath
|
2021-10-17 18:39:49 +00:00
|
|
|
ghcupLogsDir
|
2022-05-13 19:35:34 +00:00
|
|
|
| isWindows = ghcupBaseDir <&> (\(GHCupPath gp) -> GHCupPath (gp </> "logs"))
|
2021-10-17 18:39:49 +00:00
|
|
|
| otherwise = do
|
|
|
|
xdg <- useXDG
|
|
|
|
if xdg
|
|
|
|
then do
|
|
|
|
bdir <- lookupEnv "XDG_CACHE_HOME" >>= \case
|
|
|
|
Just r -> pure r
|
|
|
|
Nothing -> do
|
|
|
|
home <- liftIO getHomeDirectory
|
|
|
|
pure (home </> ".cache")
|
2022-05-13 19:35:34 +00:00
|
|
|
pure (GHCupPath (bdir </> "ghcup" </> "logs"))
|
|
|
|
else ghcupBaseDir <&> (\(GHCupPath gp) -> GHCupPath (gp </> "logs"))
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
2022-05-12 15:58:40 +00:00
|
|
|
-- | Defaults to '~/.ghcup/db.
|
|
|
|
--
|
|
|
|
-- If 'GHCUP_USE_XDG_DIRS' is set (to anything),
|
|
|
|
-- then uses 'XDG_CACHE_HOME/ghcup/db as per xdg spec.
|
2022-05-13 19:35:34 +00:00
|
|
|
ghcupDbDir :: IO GHCupPath
|
2023-03-18 14:04:37 +00:00
|
|
|
ghcupDbDir = ghcupBaseDir <&> (\(GHCupPath gp) -> GHCupPath (gp </> "db"))
|
2022-05-12 15:58:40 +00:00
|
|
|
|
|
|
|
|
2021-07-22 13:45:08 +00:00
|
|
|
-- | '~/.ghcup/trash'.
|
|
|
|
-- Mainly used on windows to improve file removal operations
|
2022-05-13 19:35:34 +00:00
|
|
|
ghcupRecycleDir :: IO GHCupPath
|
|
|
|
ghcupRecycleDir = ghcupBaseDir <&> (\(GHCupPath gp) -> GHCupPath (gp </> "trash"))
|
2021-07-19 20:05:34 +00:00
|
|
|
|
|
|
|
|
2022-05-20 21:19:33 +00:00
|
|
|
-- | Defaults to '~/.ghcup/tmp.
|
|
|
|
--
|
|
|
|
-- If 'GHCUP_USE_XDG_DIRS' is set (to anything),
|
|
|
|
-- then uses 'XDG_CACHE_HOME/ghcup/tmp as per xdg spec.
|
|
|
|
ghcupTMPDir :: IO GHCupPath
|
|
|
|
ghcupTMPDir
|
|
|
|
| isWindows = ghcupBaseDir <&> (\(GHCupPath gp) -> GHCupPath (gp </> "tmp"))
|
|
|
|
| otherwise = do
|
|
|
|
xdg <- useXDG
|
|
|
|
if xdg
|
|
|
|
then do
|
|
|
|
bdir <- lookupEnv "XDG_CACHE_HOME" >>= \case
|
|
|
|
Just r -> pure r
|
|
|
|
Nothing -> do
|
|
|
|
home <- liftIO getHomeDirectory
|
|
|
|
pure (home </> ".cache")
|
|
|
|
pure (GHCupPath (bdir </> "ghcup" </> "tmp"))
|
|
|
|
else ghcupBaseDir <&> (\(GHCupPath gp) -> GHCupPath (gp </> "tmp"))
|
|
|
|
|
2021-07-19 20:05:34 +00:00
|
|
|
|
2021-07-18 12:39:49 +00:00
|
|
|
getAllDirs :: IO Dirs
|
|
|
|
getAllDirs = do
|
2021-07-22 13:45:08 +00:00
|
|
|
baseDir <- ghcupBaseDir
|
|
|
|
binDir <- ghcupBinDir
|
|
|
|
cacheDir <- ghcupCacheDir
|
|
|
|
logsDir <- ghcupLogsDir
|
|
|
|
confDir <- ghcupConfigDir
|
|
|
|
recycleDir <- ghcupRecycleDir
|
2022-05-20 21:19:33 +00:00
|
|
|
tmpDir <- ghcupTMPDir
|
2022-05-12 15:58:40 +00:00
|
|
|
dbDir <- ghcupDbDir
|
2020-07-31 18:10:40 +00:00
|
|
|
pure Dirs { .. }
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-10-24 20:03:00 +00:00
|
|
|
-------------------
|
|
|
|
--[ GHCup files ]--
|
|
|
|
-------------------
|
|
|
|
|
2021-06-23 04:38:06 +00:00
|
|
|
getConfigFilePath :: (MonadIO m) => m FilePath
|
|
|
|
getConfigFilePath = do
|
|
|
|
confDir <- liftIO ghcupConfigDir
|
2022-05-13 19:35:34 +00:00
|
|
|
pure $ fromGHCupPath confDir </> "config.yaml"
|
2020-10-24 20:03:00 +00:00
|
|
|
|
2023-11-13 08:53:24 +00:00
|
|
|
getConfigFilePath' :: (MonadReader env m, HasDirs env) => m FilePath
|
|
|
|
getConfigFilePath' = do
|
|
|
|
Dirs {..} <- getDirs
|
|
|
|
pure $ fromGHCupPath confDir </> "config.yaml"
|
|
|
|
|
|
|
|
|
2020-10-24 20:03:00 +00:00
|
|
|
ghcupConfigFile :: (MonadIO m)
|
|
|
|
=> Excepts '[JSONError] m UserSettings
|
|
|
|
ghcupConfigFile = do
|
2021-06-23 04:38:06 +00:00
|
|
|
filepath <- getConfigFilePath
|
2022-05-21 20:54:18 +00:00
|
|
|
contents <- liftIO $ handleIO (\e -> if NoSuchThing == ioeGetErrorType e then pure Nothing else liftIO $ ioError e) $ Just <$> BS.readFile filepath
|
2021-05-14 21:09:45 +00:00
|
|
|
case contents of
|
2020-10-24 20:03:00 +00:00
|
|
|
Nothing -> pure defaultUserSettings
|
2022-05-21 20:54:18 +00:00
|
|
|
Just contents' -> liftE
|
|
|
|
. veitherToExcepts @_ @'[JSONError]
|
|
|
|
. either (VLeft . V) VRight
|
|
|
|
. first (JSONDecodeError . displayException)
|
|
|
|
. Y.decodeEither'
|
|
|
|
$ contents'
|
2020-10-24 20:03:00 +00:00
|
|
|
|
|
|
|
|
2020-07-31 18:10:40 +00:00
|
|
|
-------------------------
|
|
|
|
--[ GHCup directories ]--
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
|
|
|
|
-- | ~/.ghcup/ghc by default.
|
2022-05-13 19:35:34 +00:00
|
|
|
ghcupGHCBaseDir :: (MonadReader env m, HasDirs env) => m GHCupPath
|
2020-07-31 18:10:40 +00:00
|
|
|
ghcupGHCBaseDir = do
|
2021-07-18 12:39:49 +00:00
|
|
|
Dirs {..} <- getDirs
|
2022-05-13 19:35:34 +00:00
|
|
|
pure (baseDir `appendGHCupPath` "ghc")
|
2020-07-31 18:10:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- | Gets '~/.ghcup/ghc/<ghcupGHCDir>'.
|
|
|
|
-- The dir may be of the form
|
|
|
|
-- * armv7-unknown-linux-gnueabihf-8.8.3
|
|
|
|
-- * 8.8.4
|
2021-07-18 12:39:49 +00:00
|
|
|
ghcupGHCDir :: (MonadReader env m, HasDirs env, MonadThrow m)
|
2020-07-31 18:10:40 +00:00
|
|
|
=> GHCTargetVersion
|
2022-05-13 19:35:34 +00:00
|
|
|
-> m GHCupPath
|
2020-07-31 18:10:40 +00:00
|
|
|
ghcupGHCDir ver = do
|
2021-05-14 21:09:45 +00:00
|
|
|
ghcbasedir <- ghcupGHCBaseDir
|
|
|
|
let verdir = T.unpack $ tVerToText ver
|
2022-05-13 19:35:34 +00:00
|
|
|
pure (ghcbasedir `appendGHCupPath` verdir)
|
2020-07-31 18:10:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
-- | See 'ghcupToolParser'.
|
2021-05-14 21:09:45 +00:00
|
|
|
parseGHCupGHCDir :: MonadThrow m => FilePath -> m GHCTargetVersion
|
|
|
|
parseGHCupGHCDir (T.pack -> fp) =
|
2020-07-31 18:10:40 +00:00
|
|
|
throwEither $ MP.parse ghcTargetVerP "" fp
|
|
|
|
|
2022-02-05 00:53:04 +00:00
|
|
|
parseGHCupHLSDir :: MonadThrow m => FilePath -> m Version
|
|
|
|
parseGHCupHLSDir (T.pack -> fp) =
|
|
|
|
throwEither $ MP.parse version' "" fp
|
|
|
|
|
2022-05-21 20:54:18 +00:00
|
|
|
-- TODO: inlined from GHCup.Prelude
|
|
|
|
throwEither :: (Exception a, MonadThrow m) => Either a b -> m b
|
|
|
|
throwEither a = case a of
|
|
|
|
Left e -> throwM e
|
|
|
|
Right r -> pure r
|
|
|
|
|
2022-02-05 00:53:04 +00:00
|
|
|
-- | ~/.ghcup/hls by default, for new-style installs.
|
2022-05-13 19:35:34 +00:00
|
|
|
ghcupHLSBaseDir :: (MonadReader env m, HasDirs env) => m GHCupPath
|
2022-02-05 00:53:04 +00:00
|
|
|
ghcupHLSBaseDir = do
|
|
|
|
Dirs {..} <- getDirs
|
2022-05-13 19:35:34 +00:00
|
|
|
pure (baseDir `appendGHCupPath` "hls")
|
2022-02-05 00:53:04 +00:00
|
|
|
|
|
|
|
-- | Gets '~/.ghcup/hls/<hls-ver>' for new-style installs.
|
|
|
|
ghcupHLSDir :: (MonadReader env m, HasDirs env, MonadThrow m)
|
|
|
|
=> Version
|
2022-05-13 19:35:34 +00:00
|
|
|
-> m GHCupPath
|
2022-02-05 00:53:04 +00:00
|
|
|
ghcupHLSDir ver = do
|
|
|
|
basedir <- ghcupHLSBaseDir
|
|
|
|
let verdir = T.unpack $ prettyVer ver
|
2022-05-13 19:35:34 +00:00
|
|
|
pure (basedir `appendGHCupPath` verdir)
|
2020-07-31 18:10:40 +00:00
|
|
|
|
2022-05-20 21:19:33 +00:00
|
|
|
|
2021-07-21 13:43:45 +00:00
|
|
|
mkGhcupTmpDir :: ( MonadReader env m
|
|
|
|
, HasDirs env
|
|
|
|
, MonadUnliftIO m
|
2021-08-30 20:41:58 +00:00
|
|
|
, HasLog env
|
2021-07-21 13:43:45 +00:00
|
|
|
, MonadCatch m
|
|
|
|
, MonadThrow m
|
|
|
|
, MonadMask m
|
|
|
|
, MonadIO m)
|
2022-05-13 19:35:34 +00:00
|
|
|
=> m GHCupPath
|
|
|
|
mkGhcupTmpDir = GHCupPath <$> do
|
2022-05-20 21:19:33 +00:00
|
|
|
Dirs { tmpDir } <- getDirs
|
|
|
|
liftIO $ createTempDirectory (fromGHCupPath tmpDir) "ghcup"
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
2021-07-21 13:43:45 +00:00
|
|
|
withGHCupTmpDir :: ( MonadReader env m
|
|
|
|
, HasDirs env
|
2021-08-30 20:41:58 +00:00
|
|
|
, HasLog env
|
|
|
|
, HasSettings env
|
2021-07-21 13:43:45 +00:00
|
|
|
, MonadUnliftIO m
|
|
|
|
, MonadCatch m
|
|
|
|
, MonadResource m
|
|
|
|
, MonadThrow m
|
|
|
|
, MonadMask m
|
|
|
|
, MonadIO m)
|
2022-05-13 19:35:34 +00:00
|
|
|
=> m GHCupPath
|
2023-01-01 15:50:12 +00:00
|
|
|
withGHCupTmpDir = do
|
|
|
|
Settings{keepDirs} <- getSettings
|
|
|
|
snd <$> withRunInIO (\run ->
|
|
|
|
run
|
|
|
|
$ allocate
|
|
|
|
(run mkGhcupTmpDir)
|
|
|
|
(\fp -> if -- we don't know whether there was a failure, so can only
|
|
|
|
-- decide for 'Always'
|
|
|
|
| keepDirs == Always -> pure ()
|
|
|
|
| otherwise -> handleIO (\e -> run
|
|
|
|
$ logDebug ("Resource cleanup failed for "
|
|
|
|
<> T.pack (fromGHCupPath fp)
|
|
|
|
<> ", error was: "
|
|
|
|
<> T.pack (displayException e)))
|
|
|
|
. removePathForcibly
|
|
|
|
$ fp))
|
2020-01-11 20:15:05 +00:00
|
|
|
|
|
|
|
|
2020-07-31 18:10:40 +00:00
|
|
|
|
|
|
|
|
2020-01-11 20:15:05 +00:00
|
|
|
--------------
|
|
|
|
--[ Others ]--
|
|
|
|
--------------
|
|
|
|
|
|
|
|
|
2020-07-28 23:43:00 +00:00
|
|
|
useXDG :: IO Bool
|
2021-05-14 21:09:45 +00:00
|
|
|
useXDG = isJust <$> lookupEnv "GHCUP_USE_XDG_DIRS"
|
2020-07-28 23:43:00 +00:00
|
|
|
|
|
|
|
|
2022-02-05 15:44:00 +00:00
|
|
|
-- | Like 'relpath'. Assumes the inputs are resolved in case of symlinks.
|
2021-05-14 21:09:45 +00:00
|
|
|
relativeSymlink :: FilePath -- ^ the path in which to create the symlink
|
|
|
|
-> FilePath -- ^ the symlink destination
|
|
|
|
-> FilePath
|
2022-03-17 21:51:17 +00:00
|
|
|
relativeSymlink p1 p2
|
|
|
|
| isWindows = p2 -- windows quickly gets into MAX_PATH issues so we don't care about relative symlinks
|
|
|
|
| otherwise =
|
|
|
|
let d1 = splitDirectories p1
|
|
|
|
d2 = splitDirectories p2
|
|
|
|
common = takeWhile (\(x, y) -> x == y) $ zip d1 d2
|
|
|
|
cPrefix = drop (length common) d1
|
|
|
|
in joinPath (replicate (length cPrefix) "..")
|
|
|
|
<> joinPath ([pathSeparator] : drop (length common) d2)
|
2020-07-28 23:43:00 +00:00
|
|
|
|
|
|
|
|
2021-07-22 13:45:08 +00:00
|
|
|
cleanupTrash :: ( MonadIO m
|
|
|
|
, MonadMask m
|
|
|
|
, MonadReader env m
|
2021-08-30 20:41:58 +00:00
|
|
|
, HasLog env
|
2021-07-22 13:45:08 +00:00
|
|
|
, HasDirs env
|
2021-08-30 20:41:58 +00:00
|
|
|
, HasSettings env
|
2021-07-22 13:45:08 +00:00
|
|
|
)
|
|
|
|
=> m ()
|
|
|
|
cleanupTrash = do
|
|
|
|
Dirs { recycleDir } <- getDirs
|
2022-05-13 19:35:34 +00:00
|
|
|
contents <- liftIO $ listDirectory (fromGHCupPath recycleDir)
|
2021-07-21 13:43:45 +00:00
|
|
|
if null contents
|
|
|
|
then pure ()
|
|
|
|
else do
|
2022-05-13 19:35:34 +00:00
|
|
|
logWarn ("Removing leftover files in " <> T.pack (fromGHCupPath recycleDir))
|
2021-07-22 13:45:08 +00:00
|
|
|
forM_ contents (\fp -> handleIO (\e ->
|
2021-08-30 20:41:58 +00:00
|
|
|
logDebug ("Resource cleanup failed for " <> T.pack fp <> ", error was: " <> T.pack (displayException e))
|
2022-05-13 19:35:34 +00:00
|
|
|
) $ liftIO $ removePathForcibly (recycleDir `appendGHCupPath` fp))
|
|
|
|
|
|
|
|
|
2023-02-23 13:47:50 +00:00
|
|
|
-- | List *actual files* in a directory, ignoring empty files and a couple
|
|
|
|
-- of blacklisted files, such as '.DS_Store' on mac.
|
|
|
|
listDirectoryFiles :: FilePath -> IO [FilePath]
|
|
|
|
listDirectoryFiles fp = do
|
|
|
|
listDirectory fp >>= filterM (doesFileExist . (fp </>)) <&> filter (\fp' -> not (isHidden fp') && not (isBlacklisted fp'))
|
|
|
|
|
|
|
|
-- | List *actual directories* in a directory, ignoring empty directories and a couple
|
|
|
|
-- of blacklisted files, such as '.DS_Store' on mac.
|
|
|
|
listDirectoryDirs :: FilePath -> IO [FilePath]
|
|
|
|
listDirectoryDirs fp = do
|
|
|
|
listDirectory fp >>= filterM (doesDirectoryExist . (fp </>)) <&> filter (\fp' -> not (isHidden fp') && not (isBlacklisted fp'))
|
|
|
|
|
|
|
|
isHidden :: FilePath -> Bool
|
|
|
|
isHidden fp'
|
|
|
|
| isWindows = False
|
|
|
|
| Just ('.', _) <- uncons fp' = True
|
|
|
|
| otherwise = False
|
|
|
|
|
|
|
|
isBlacklisted :: FilePath -> Bool
|
2023-02-23 15:15:08 +00:00
|
|
|
{- HLINT ignore "Use ==" -}
|
2023-02-23 13:47:50 +00:00
|
|
|
isBlacklisted fp' = fp' `elem` [".DS_Store"]
|
|
|
|
|
|
|
|
|
2022-05-13 19:35:34 +00:00
|
|
|
|
|
|
|
-- System.Directory re-exports with GHCupPath
|
|
|
|
|
|
|
|
removeDirectory :: GHCupPath -> IO ()
|
|
|
|
removeDirectory (GHCupPath fp) = SD.removeDirectory fp
|
|
|
|
|
|
|
|
removeDirectoryRecursive :: GHCupPath -> IO ()
|
|
|
|
removeDirectoryRecursive (GHCupPath fp) = SD.removeDirectoryRecursive fp
|
|
|
|
|
|
|
|
removePathForcibly :: GHCupPath -> IO ()
|
|
|
|
removePathForcibly (GHCupPath fp) = SD.removePathForcibly fp
|
|
|
|
|
|
|
|
|
2022-05-20 21:19:33 +00:00
|
|
|
|
2021-07-22 13:45:08 +00:00
|
|
|
|