{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-}
module Path.CheckInstall
( warnInstallSearchPathIssues
) where
import Control.Monad.Extra ( (&&^), anyM )
import Stack.Prelude
import Stack.Types.Config ( HasConfig )
import qualified System.Directory as D
import qualified System.FilePath as FP
warnInstallSearchPathIssues ::
HasConfig env
=> FilePath
-> [String]
-> RIO env ()
warnInstallSearchPathIssues :: forall env. HasConfig env => FilePath -> [FilePath] -> RIO env ()
warnInstallSearchPathIssues FilePath
destDir [FilePath]
installed = do
searchPath <- IO [FilePath] -> RIO env [FilePath]
forall a. IO a -> RIO env a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO IO [FilePath]
FP.getSearchPath
destDirIsInPATH <- liftIO $
anyM
( \FilePath
dir -> FilePath -> IO Bool
D.doesDirectoryExist FilePath
dir
IO Bool -> IO Bool -> IO Bool
forall (m :: * -> *). Monad m => m Bool -> m Bool -> m Bool
&&^ (FilePath -> Bool) -> IO FilePath -> IO Bool
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (FilePath -> FilePath -> Bool
FP.equalFilePath FilePath
destDir) (FilePath -> IO FilePath
D.canonicalizePath FilePath
dir)
)
searchPath
if destDirIsInPATH
then forM_ installed $ \FilePath
exe -> do
(IO (Maybe FilePath) -> RIO env (Maybe FilePath)
forall a. IO a -> RIO env a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO (Maybe FilePath) -> RIO env (Maybe FilePath))
-> (FilePath -> IO (Maybe FilePath))
-> FilePath
-> RIO env (Maybe FilePath)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO (Maybe FilePath)
D.findExecutable) FilePath
exe RIO env (Maybe FilePath)
-> (Maybe FilePath -> RIO env ()) -> RIO env ()
forall a b. RIO env a -> (a -> RIO env b) -> RIO env b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= \case
Just FilePath
exePath -> do
exeDir <-
(IO FilePath -> RIO env FilePath
forall a. IO a -> RIO env a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO FilePath -> RIO env FilePath)
-> (FilePath -> IO FilePath) -> FilePath -> RIO env FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FilePath -> FilePath) -> IO FilePath -> IO FilePath
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap FilePath -> FilePath
FP.takeDirectory (IO FilePath -> IO FilePath)
-> (FilePath -> IO FilePath) -> FilePath -> IO FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> IO FilePath
D.canonicalizePath) FilePath
exePath
unless (exeDir `FP.equalFilePath` destDir) $
prettyWarnL
[ flow "The"
, style File . fromString $ exe
, flow "executable found on the PATH environment variable is"
, style File . fromString $ exePath
, flow "and not the version that was just installed."
, flow "This means that"
, style File . fromString $ exe
, "calls on the command line will not use this version."
]
Maybe FilePath
Nothing ->
[StyleDoc] -> RIO env ()
forall env (m :: * -> *).
(HasCallStack, HasTerm env, MonadReader env m, MonadIO m) =>
[StyleDoc] -> m ()
prettyWarnL
[ FilePath -> StyleDoc
flow FilePath
"Installation path"
, Style -> StyleDoc -> StyleDoc
style Style
Dir (StyleDoc -> StyleDoc)
-> (FilePath -> StyleDoc) -> FilePath -> StyleDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> StyleDoc
forall a. IsString a => FilePath -> a
fromString (FilePath -> StyleDoc) -> FilePath -> StyleDoc
forall a b. (a -> b) -> a -> b
$ FilePath
destDir
, FilePath -> StyleDoc
flow FilePath
"is on the PATH but the"
, Style -> StyleDoc -> StyleDoc
style Style
File (StyleDoc -> StyleDoc)
-> (FilePath -> StyleDoc) -> FilePath -> StyleDoc
forall b c a. (b -> c) -> (a -> b) -> a -> c
. FilePath -> StyleDoc
forall a. IsString a => FilePath -> a
fromString (FilePath -> StyleDoc) -> FilePath -> StyleDoc
forall a b. (a -> b) -> a -> b
$ FilePath
exe
, FilePath -> StyleDoc
flow FilePath
"executable that was just installed could not be found on \
\the PATH."
]
else
prettyWarnL
[ flow "Installation path "
, style Dir . fromString $ destDir
, "not found on the PATH environment variable."
]