module MyLib ( findReplaceAndCopyFile, findReplaceInFile, replaceMarkerWithFile, replaceMarkerWithFile2, replaceMarkerWithFile3 ) where import Data.List (intercalate) import Data.List.Split (splitOn) import System.IO (stdout, hFlush, hPutStr) import Type.Reflection (typeOf) import System.Directory replaceMarkerWithFile_ :: FilePath -> FilePath -> String -> FilePath -> IO () replaceMarkerWithFile_ inFile outFile marker injectionFile = do inFileText <- readFile inFile injectText <- readFile injectionFile let replace from to = intercalate to . splitOn from in writeFile outFile (replace marker injectText inFileText) findReplaceAndCopyFile :: FilePath -> FilePath -> String -> String -> IO () findReplaceAndCopyFile f g a b = do s <- readFile f let replace from to = intercalate to . splitOn from in writeFile g (replace a b s) deleteAndMoveBack :: FilePath -> FilePath -> IO () deleteAndMoveBack a b = do removeFile a renameFile b a findReplaceInFile :: FilePath -> FilePath -> String -> String -> IO () findReplaceInFile a b f r = do findReplaceAndCopyFile a b f r deleteAndMoveBack a b replaceMarkerWithFile :: FilePath -> FilePath -> String -> FilePath -> IO () replaceMarkerWithFile a b f r = do replaceMarkerWithFile_ a b f r deleteAndMoveBack a b replaceMarkerWithFile2 :: FilePath -> FilePath -> String -> FilePath -> IO () replaceMarkerWithFile2 a b f r = do replaceMarkerWithFile_ a b f r replaceMarkerWithFile3_ :: FilePath -> String -> FilePath -> IO () replaceMarkerWithFile3_ inFile marker injectionFile = do inFileText <- readFile inFile injectText <- readFile injectionFile let replace from to = intercalate to . splitOn from in putStr (replace marker injectText inFileText) xx :: Bool -> FilePath -> String -> FilePath -> IO () xx True a b c = do replaceMarkerWithFile3_ a b c xx False a _ _ = do inStr <- readFile a putStr inStr replaceMarkerWithFile3 :: FilePath -> String -> FilePath -> IO () replaceMarkerWithFile3 a f r = do j <- doesFileExist r xx j a f r
module Main where import MyLib import System.Environment main :: IO () main = do args <- getArgs progName <- getProgName -- IO String runWithArgs progName args runWithArgs "replaceNarmer" args = do goReplaceMarkerWithFile args runWithArgs "replaceNarmer2" args = do goReplaceMarkerWithFile2 args runWithArgs "replaceNarmer3" args = do goReplaceMarkerWithFile3 args runWithArgs "findAndReplace" args = do goFindReplaceInFile args runWithArgs x _ = do putStrLn ("Unrecognised program name for invocation: " ++ x) -- |The 'enfile' function takes a string of a filename and returns the desired filename for a new file enfile :: String -> String enfile a = a ++ ".tmp" goReplaceMarkerWithFile3 :: [String] -> IO () goReplaceMarkerWithFile3 [a,f,r] = do replaceMarkerWithFile3 a f r goReplaceMarkerWithFile3 _ = do putStrLn "Not enough arguments." goReplaceMarkerWithFile2 :: [String] -> IO () goReplaceMarkerWithFile2 [a,b,f,r] = do replaceMarkerWithFile2 a b f r goReplaceMarkerWithFile2 _ = do putStrLn "Not enough arguments." goReplaceMarkerWithFile :: [String] -> IO () goReplaceMarkerWithFile [a,b,f,r] = do replaceMarkerWithFile a b f r goReplaceMarkerWithFile [a,f,r] = do replaceMarkerWithFile a (enfile a) f r goReplaceMarkerWithFile _ = do putStrLn "Not enough arguments." goFindReplaceInFile :: [String] -> IO () goFindReplaceInFile [a,b,f,r] = do findReplaceAndCopyFile a b f r goFindReplaceInFile [a,f,r] = do findReplaceAndCopyFile a (enfile a) f r goFindReplaceInFile _ = do putStrLn "Not enough arguments."