module Development.Shake.Derived where
import Control.Monad
import Control.Monad.IO.Class
import System.Process
import System.Directory
import System.Exit
import qualified Data.ByteString.Char8 as BS
import Development.Shake.Core
import Development.Shake.File
import Development.Shake.FilePath
system' :: FilePath -> [String] -> Action ()
system' path args = do
let path2 = toNative path
let cmd = unwords $ path2 : args
putLoud cmd
res <- traced ("system " ++ cmd) $ rawSystem path2 args
when (res /= ExitSuccess) $
error $ "System command failed:\n" ++ cmd
systemCwd :: FilePath -> FilePath -> [String] -> Action ()
systemCwd cwd path args = do
let path2 = toNative path
let cmd = unwords $ path2 : args
putLoud cmd
res <- traced ("system " ++ cmd) $ do
hdl <- runProcess path2 args (Just cwd) Nothing Nothing Nothing Nothing
waitForProcess hdl
when (res /= ExitSuccess) $
error $ "System command failed:\n" ++ cmd
systemOutput :: FilePath -> [String] -> Action (String, String)
systemOutput path args = do
let path2 = toNative path
let cmd = unwords $ path2 : args
putLoud cmd
(res,stdout,stderr) <- traced ("system' " ++ cmd) $ readProcessWithExitCode path2 args ""
when (res /= ExitSuccess) $
error $ "System command failed:\n" ++ cmd ++ "\n" ++ stderr
return (stdout, stderr)
copyFile' :: FilePath -> FilePath -> Action ()
copyFile' old new = need [old] >> liftIO (copyFile old new)
readFile' :: FilePath -> Action String
readFile' x = need [x] >> liftIO (readFile x)
writeFile' :: FilePath -> String -> Action ()
writeFile' name x = liftIO $ writeFile name x
readFileLines :: FilePath -> Action [String]
readFileLines = fmap lines . readFile'
writeFileLines :: FilePath -> [String] -> Action ()
writeFileLines name = writeFile' name . unlines
writeFileChanged :: FilePath -> String -> Action ()
writeFileChanged name x = liftIO $ do
b <- doesFileExist name
if not b then writeFile name x else do
orig <- BS.readFile name
let new = BS.pack x
when (orig /= new) $ BS.writeFile name new