{-# LANGUAGE CPP #-}
module Foreign.Hoppy.Generator.Common (
fromMaybeM,
fromEitherM,
maybeFail,
for,
listSubst,
zipWithM,
writeFileIfDifferent,
capitalize,
lowerFirst,
upperFirst,
) where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative ((<$>))
#endif
import Control.Exception (evaluate)
import Control.Monad (when)
import Data.Char (toLower, toUpper)
import System.Directory (doesFileExist)
import System.IO (IOMode (ReadMode), hGetContents, withFile)
fromMaybeM :: Monad m => m a -> Maybe a -> m a
fromMaybeM = flip maybe return
fromEitherM :: Monad m => (e -> m a) -> Either e a -> m a
fromEitherM = flip either return
maybeFail :: Monad m => String -> Maybe a -> m a
maybeFail = fromMaybeM . fail
for :: [a] -> (a -> b) -> [b]
for = flip map
listSubst :: Eq a => a -> a -> [a] -> [a]
listSubst x x' = map $ \y -> if y == x then x' else y
zipWithM :: Monad m => (a -> b -> m c) -> [a] -> [b] -> m [c]
zipWithM f xs ys = sequence $ zipWith f xs ys
writeFileIfDifferent :: FilePath -> String -> IO ()
writeFileIfDifferent path newContents = do
exists <- doesFileExist path
doWrite <- if exists
then (newContents /=) <$> readStrictly
else return True
when doWrite $ writeFile path newContents
where readStrictly = withFile path ReadMode $ \handle -> do
contents <- hGetContents handle
_ <- evaluate $ length contents
return contents
capitalize :: String -> String
capitalize "" = ""
capitalize (c:cs) = toUpper c : map toLower cs
lowerFirst :: String -> String
lowerFirst "" = ""
lowerFirst (c:cs) = toLower c : cs
upperFirst :: String -> String
upperFirst "" = ""
upperFirst (c:cs) = toUpper c : cs