module Development.Hake.HiddenTools (
runHake
, isOldThanSomeOf
, hakefileUpdateOption
, defaultTrgtStr
) where
import System.IO (openFile, hClose, IOMode(WriteMode))
import System.Directory (createDirectory, doesDirectoryExist,
doesFileExist, copyFile, getModificationTime)
import System.Exit (ExitCode(ExitSuccess))
import System.Time (ClockTime)
import System.Process (runProcess, waitForProcess)
import System.FilePath (takeFileName)
import Control.Monad.Utils (unlessM, ifM)
import Control.Applicative ((<$>), liftA2)
import Control.Exception (bracket)
hakeDir, defaultTrgtStr, hakefileUpdateOption :: String
hakeDir = "_hake/"
defaultTrgtStr = "default"
hakefileUpdateOption = "--hakefile-is-updated"
maybeGetModificationTime :: FilePath -> IO (Maybe ClockTime)
maybeGetModificationTime fn
= ifM (doesFileExist fn)
(Just <$> getModificationTime fn)
(return Nothing)
isOldThanSomeOf :: FilePath -> [FilePath] -> IO Bool
isOldThanSomeOf dfn sfns
= liftA2 ((myOr .) . map . (<)) (maybeGetModificationTime dfn)
(mapM maybeGetModificationTime sfns)
where
myOr [] = True
myOr bs = or bs
runHake :: FilePath -> FilePath -> [ FilePath ] -> [ String ] -> IO ExitCode
runHake src exe othrs args = do
let exePath = hakeDir ++ exe
exeSrc = hakeDir ++ exe ++ ".hs"
errFile = hakeDir ++ exe ++ ".error"
unlessM (doesFileExist src) $
error $ "runHake: " ++ src ++ " does not exist"
unlessM (and <$> mapM doesFileExist othrs) $
error $ "runHake: " ++ unwords othrs ++ " does not exist"
unlessM (doesDirectoryExist hakeDir) $ createDirectory hakeDir
othrsUD <- fmap or $ flip mapM othrs $ \fn -> do
let dist = hakeDir ++ takeFileName fn
ifM (isOldThanSomeOf dist [ fn ])
(copyFile fn dist >> return True)
( return False)
hakefileUD <-
ifM (isOldThanSomeOf exeSrc [ src ])
(readFile src >>= writeFile exeSrc >> return True )
( return False)
ret <- bracket (openFile errFile WriteMode) hClose $ \errH ->
runProcess "ghc" [ "--make", exe ] (Just hakeDir)
Nothing Nothing Nothing (Just errH) >>= waitForProcess
case ret of
ExitSuccess -> return ()
_ -> readFile errFile >>= putStr
let args_ = applyWhen (othrsUD || hakefileUD) (hakefileUpdateOption:) $
defaultElem defaultTrgtStr args
runProcess exePath args_ Nothing Nothing Nothing Nothing Nothing >>= waitForProcess
defaultElem :: a -> [a] -> [a]
defaultElem dflt [] = [dflt]
defaultElem _ lst = lst
applyWhen :: Bool -> (a -> a) -> a -> a
applyWhen b f = if b then f else id