module Darcs.UI.RunHook
( runPosthook
, runPrehook
)
where
import Darcs.Prelude
import System.Directory ( withCurrentDirectory )
import System.Exit ( ExitCode(..) )
import System.Process ( system )
import System.IO ( hPutStrLn, stderr )
import Control.Monad ( when )
import Darcs.UI.Options.All ( HookConfig(..), Verbosity(..) )
import Darcs.Util.Path ( AbsolutePath, toFilePath )
import Darcs.Util.Prompt ( promptYorn )
runPosthook :: HookConfig -> Verbosity -> AbsolutePath -> IO ExitCode
runPosthook :: HookConfig -> Verbosity -> AbsolutePath -> IO ExitCode
runPosthook (HookConfig Maybe String
mPostHook Bool
askPostHook) Verbosity
verb AbsolutePath
repodir
= do ph <- String -> Maybe String -> Bool -> IO (Maybe String)
getHook String
"Posthook" Maybe String
mPostHook Bool
askPostHook
withCurrentDirectory (toFilePath repodir) $ runHook verb "Posthook" ph
runPrehook :: HookConfig -> Verbosity -> AbsolutePath -> IO ExitCode
runPrehook :: HookConfig -> Verbosity -> AbsolutePath -> IO ExitCode
runPrehook (HookConfig Maybe String
mPreHookCmd Bool
askPreHook) Verbosity
verb AbsolutePath
repodir =
do ph <- String -> Maybe String -> Bool -> IO (Maybe String)
getHook String
"Prehook" Maybe String
mPreHookCmd Bool
askPreHook
withCurrentDirectory (toFilePath repodir) $ runHook verb "Prehook" ph
getHook :: String -> Maybe String -> Bool -> IO (Maybe String)
getHook :: String -> Maybe String -> Bool -> IO (Maybe String)
getHook String
name Maybe String
mPostHookCmd Bool
askHook =
case Maybe String
mPostHookCmd of
Maybe String
Nothing -> Maybe String -> IO (Maybe String)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe String
forall a. Maybe a
Nothing
Just String
command ->
if Bool
askHook
then do yorn <-
String -> IO Bool
promptYorn
(String
"The following command is set to execute:\n"String -> String -> String
forall a. [a] -> [a] -> [a]
++String
commandString -> String -> String
forall a. [a] -> [a] -> [a]
++
String
"\nExecute this command now?")
if yorn
then return $ Just command
else putStrLn (name ++ " cancelled...") >> return Nothing
else Maybe String -> IO (Maybe String)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe String -> IO (Maybe String))
-> Maybe String -> IO (Maybe String)
forall a b. (a -> b) -> a -> b
$ String -> Maybe String
forall a. a -> Maybe a
Just String
command
runHook :: Verbosity -> String -> Maybe String -> IO ExitCode
runHook :: Verbosity -> String -> Maybe String -> IO ExitCode
runHook Verbosity
_ String
_ Maybe String
Nothing = ExitCode -> IO ExitCode
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ExitCode
ExitSuccess
runHook Verbosity
verb String
cname (Just String
command) =
do ec <- String -> IO ExitCode
system String
command
when (verb /= Quiet) $
if ec == ExitSuccess
then putStrLn $ cname++" ran successfully."
else hPutStrLn stderr $ cname++" failed!"
return ec