module Control.Shell.File
( rm, mv, cp
, input, output
, IO.IOMode (..)
, withFile, withBinaryFile
, openFile, openBinaryFile
) where
import qualified System.Directory as Dir
import qualified System.IO as IO
import Control.Shell.Base
input :: FilePath -> Shell String
input f = do
e <- getEnv
unsafeLiftIO $ readFile (absPath e f)
output :: FilePath -> String -> Shell ()
output f s = do
e <- getEnv
unsafeLiftIO $ writeFile (absPath e f) s
rm :: FilePath -> Shell ()
rm dir = do
e <- getEnv
unsafeLiftIO $ Dir.removeFile (absPath e dir)
mv :: FilePath -> FilePath -> Shell ()
mv from to = do
e <- getEnv
unsafeLiftIO $ do
targetIsDir <- Dir.doesDirectoryExist (absPath e to)
sourceIsDir <- Dir.doesDirectoryExist (absPath e from)
if sourceIsDir
then Dir.renameDirectory (absPath e from) (targetPath targetIsDir e)
else Dir.renameFile (absPath e from) (targetPath targetIsDir e)
where
targetPath True e = absPath e to </> takeBaseName from
targetPath False e = absPath e to
cp :: FilePath -> FilePath -> Shell ()
cp f t = do
e <- getEnv
let (from, to) = (absPath e f, absPath e t)
todir <- unsafeLiftIO $ Dir.doesDirectoryExist to
if todir
then cp from (to </> takeFileName from)
else liftIO $ Dir.copyFile from to
withFile :: FilePath -> IO.IOMode -> (IO.Handle -> Shell a) -> Shell a
withFile file mode f = joinResult $ do
e <- getEnv
unsafeLiftIO (IO.withFile (absPath e file) mode (shell . f))
withBinaryFile :: FilePath -> IO.IOMode -> (IO.Handle -> Shell a) -> Shell a
withBinaryFile file mode f = joinResult $ do
e <- getEnv
unsafeLiftIO (IO.withBinaryFile (absPath e file) mode (shell . f))
openFile :: FilePath -> IO.IOMode -> Shell IO.Handle
openFile file mode = do
e <- getEnv
unsafeLiftIO $ IO.openFile (absPath e file) mode
openBinaryFile :: FilePath -> IO.IOMode -> Shell IO.Handle
openBinaryFile file mode = do
e <- getEnv
unsafeLiftIO $ IO.openBinaryFile (absPath e file) mode