packages feed

ghcup-0.2.7.0 / lib / GHCup / Command / Nuke.hs

{-# LANGUAGE CPP #-}{-# LANGUAGE DataKinds #-}{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE MultiParamTypeClasses #-}{-# LANGUAGE OverloadedStrings #-}{-|Module      : GHCup.Command.NukeDescription : GHCup nukeCopyright   : (c) Julian Ospald, 2020License     : LGPL-3.0Maintainer  : hasufell@hasufell.deStability   : experimentalPortability : portableThis module contains the main functions that correspondto the command line interface, like installation, listing versionsand so on.These are the entry points.-}module GHCup.Command.Nuke whereimport GHCup.Preludeimport GHCup.Query.GHCupDirsimport GHCup.Typesimport GHCup.Types.Opticsimport Conduit                ( sourceToList )import Control.Applicativeimport Control.Exception.Safeimport Control.Monad#if !MIN_VERSION_base(4,13,0)import Control.Monad.Fail ( MonadFail )#endifimport Control.Monad.Readerimport Control.Monad.Trans.Resource hiding ( throwM )import Data.Listimport Prelude                      hiding ( abs, writeFile )import System.FilePathimport System.IO.Errorimport qualified Data.Text as T    ------------    --[ Nuke ]--    ------------rmGhcupDirs :: ( MonadReader env m               , HasDirs env               , MonadIO m               , MonadUnliftIO m               , HasLog env               , MonadCatch m               , MonadMask m )            => m [FilePath]rmGhcupDirs = do  Dirs    { baseDir    , binDir    , logsDir    , cacheDir    , recycleDir    , dbDir    , tmpDir    } <- getDirs  let envFilePath = fromGHCupPath baseDir </> "env"  confFilePath <- getConfigFilePath  handleRm $ rmEnvFile  envFilePath  handleRm $ rmConfFile confFilePath  -- for xdg dirs, the order matters here  handleRm $ rmPathForcibly logsDir  handleRm $ rmPathForcibly tmpDir  handleRm $ rmPathForcibly cacheDir  handleRm $ rmBinDir binDir  handleRm $ rmPathForcibly recycleDir  handleRm $ rmPathForcibly dbDir  when isWindows $ do    logInfo $ "removing " <> T.pack (fromGHCupPath baseDir </> "msys64")    handleRm $ rmPathForcibly (baseDir `appendGHCupPath` "msys64")  handleRm $ removeEmptyDirsRecursive (fromGHCupPath baseDir)  -- report files in baseDir that are left-over after  -- the standard location deletions above  hideErrorDef [doesNotExistErrorType] [] $ reportRemainingFiles (fromGHCupPath baseDir)  where    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"                                <> "continuing regardless...")    rmEnvFile :: (HasLog env, MonadReader env m, HasDirs env, MonadMask m, MonadIO m, MonadCatch m) => FilePath -> m ()    rmEnvFile enFilePath = do      logInfo "Removing Ghcup Environment File"      hideErrorDef [permissionErrorType] () $ rmFileForce enFilePath    rmConfFile :: (HasLog env, MonadReader env m, HasDirs env, MonadMask m, MonadIO m, MonadCatch m) => FilePath -> m ()    rmConfFile confFilePath = do      logInfo "removing Ghcup Config File"      hideErrorDef [permissionErrorType] () $ rmFileForce confFilePath    rmBinDir :: (MonadReader env m, HasDirs env, MonadMask m, MonadIO m, MonadCatch m) => FilePath -> m ()    rmBinDir binDir      | isWindows = removeDirIfEmptyOrIsSymlink binDir      | otherwise = do          isXDGStyle <- liftIO useXDG          when (not isXDGStyle) $            removeDirIfEmptyOrIsSymlink binDir    reportRemainingFiles :: (MonadMask m, MonadIO m, MonadUnliftIO m) => FilePath -> m [FilePath]    reportRemainingFiles dir = do      remainingFiles <- runResourceT $ sourceToList $ getDirectoryContentsRecursiveUnsafe dir      let normalizedFilePaths = fmap normalise remainingFiles      let sortedByDepthRemainingFiles = sortBy (flip compareFn) normalizedFilePaths      let remainingFilesAbsolute = fmap (dir </>) sortedByDepthRemainingFiles      pure remainingFilesAbsolute      where        calcDepth :: FilePath -> Int        calcDepth = length . filter isPathSeparator        compareFn :: FilePath -> FilePath -> Ordering        compareFn fp1 fp2 = compare (calcDepth fp1) (calcDepth fp2)