packages feed

ghcup-0.2.7.0 / lib / GHCup / Prelude.hs

{-# LANGUAGE CPP #-}{-# LANGUAGE DataKinds #-}{-# LANGUAGE FlexibleContexts #-}{-# LANGUAGE FlexibleInstances #-}{-# LANGUAGE ScopedTypeVariables #-}{-# LANGUAGE TypeFamilies #-}{-# LANGUAGE TypeOperators #-}{-|Module      : GHCup.PreludeDescription : MegaParsec utilitiesCopyright   : (c) Julian Ospald, 2020License     : LGPL-3.0Maintainer  : hasufell@hasufell.deStability   : experimentalPortability : portableGHCup specific prelude. Lots of Excepts functionality.-}module GHCup.Prelude  (module GHCup.Prelude,   module GHCup.Prelude.Internal,   module GHCup.Prelude.JSON,   module GHCup.Prelude.File,   module GHCup.Prelude.Logger,   module GHCup.Prelude.Version,#if defined(IS_WINDOWS)   module GHCup.Prelude.Windows,#else   module GHCup.Prelude.Posix,#endif  )whereimport GHCup.Errorsimport GHCup.Prelude.Fileimport GHCup.Prelude.Internalimport GHCup.Prelude.JSONimport GHCup.Prelude.Loggerimport GHCup.Prelude.Versionimport GHCup.Types            ( EnvUnion (..) )import GHCup.Types.Optics     ( HasLog )#if defined(IS_WINDOWS)import GHCup.Prelude.Windows#elseimport GHCup.Prelude.Posix#endifimport           Control.Monad.IO.Classimport           Control.Monad.Readerimport           Data.List                      ( intercalate )import qualified Data.Map.Strict                as Mapimport qualified Data.Text                      as Timport           Data.Variant.Exceptsimport           System.Environment             ( getEnvironment )import           System.FilePathimport           Text.PrettyPrint.HughesPJClass ( Pretty, prettyShow )-- for some obscure reason... this won't type-check if we move it to a different modulecatchWarn :: forall es m env . ( Pretty (V es)                             , HFErrorProject (V es)                             , MonadReader env m                             , HasLog env                             , MonadIO m                             , Monad m) => Excepts es m () -> Excepts '[] m ()catchWarn = catchAllE @_ @es (\v -> lift $ logWarn (T.pack . prettyHFError $ v))prettyText :: Pretty a => a -> T.TextprettyText = T.pack . prettyShowrunBothE' :: forall e m a b .             ( Monad m             , Show (V e)             , Pretty (V e)             , HFErrorProject (V e)             , PopVariant InstallSetError e             , LiftVariant' e (InstallSetError ': e)             , e :<< (InstallSetError ': e)             )          => Excepts e m a          -> Excepts e m b          -> Excepts (InstallSetError ': e) m ()runBothE' a1 a2 = do   r1 <- lift $ runE @e a1   r2 <- lift $ runE @e a2   case (r1, r2) of      (VLeft e1, VLeft e2) -> throwE (InstallSetError e1 e2)      (VLeft e , _       ) -> throwSomeE e      (_       , VLeft e ) -> throwSomeE e      (VRight _, VRight _) -> pure ()augmentEnvironment :: [(String, String)] -> EnvUnion -> IO [(String, String)]augmentEnvironment (Map.fromList -> addEnv) PreferSpec = do cEnv <- Map.fromList <$> getEnvironment pure (Map.toList $ Map.union addEnv cEnv)augmentEnvironment (Map.fromList -> addEnv) PreferSystem = do cEnv <- Map.fromList <$> getEnvironment pure (Map.toList $ Map.union cEnv addEnv)augmentEnvironment addEnv OnlySpec = pure addEnvaddToPath :: [FilePath]          -> Bool         -- ^ if False will prepend          -> IO [(String, String)]addToPath paths append = do cEnv <- getEnvironment return $ addToPath' cEnv paths appendaddToPath' :: [(String, String)]          -> [FilePath]          -> Bool         -- ^ if False will prepend          -> [(String, String)]addToPath' cEnv' newPaths append =  let cEnv           = Map.fromList cEnv'      paths          = ["PATH", "Path"]      curPaths       = (\x -> maybe [] splitSearchPath (Map.lookup x cEnv)) =<< paths      {- HLINT ignore "Redundant bracket" -}      newPath        = intercalate [searchPathSeparator] (if append then (curPaths ++ newPaths) else (newPaths ++ curPaths))      envWithoutPath = foldr (\x y -> Map.delete x y) cEnv paths      pathVar        = if isWindows then "Path" else "PATH"      envWithNewPath = Map.toList $ Map.insert pathVar newPath envWithoutPath  in envWithNewPath