{-# LANGUAGE OverloadedStrings #-}
module System.Directory.Extra
( copyTemplate
)
where
import Control.Monad ( filterM, forM_ )
import Data.Aeson ( Value (..) )
import qualified Data.ByteString.Lazy as B
import Data.Text.Lazy ( pack, unpack )
import Data.Text.Lazy.Encoding ( encodeUtf8 )
import Distribution.Simple.Utils ( getDirectoryContentsRecursive )
import System.Directory ( createDirectoryIfMissing,
doesFileExist )
import System.FilePath ( makeRelative, splitFileName,
takeDirectory, (</>) )
import Text.Microstache ( compileMustacheFile,
compileMustacheText,
renderMustache )
copyTemplate :: FilePath -> Value -> FilePath -> IO ()
copyTemplate :: FilePath -> Value -> FilePath -> IO ()
copyTemplate FilePath
templateDir Value
subst FilePath
targetDir = do
[FilePath]
tmplContents <- (FilePath -> FilePath) -> [FilePath] -> [FilePath]
forall a b. (a -> b) -> [a] -> [b]
map (FilePath
templateDir FilePath -> FilePath -> FilePath
</>) ([FilePath] -> [FilePath])
-> ([FilePath] -> [FilePath]) -> [FilePath] -> [FilePath]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (FilePath -> Bool) -> [FilePath] -> [FilePath]
forall a. (a -> Bool) -> [a] -> [a]
filter (FilePath -> [FilePath] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [FilePath
"..", FilePath
"."])
([FilePath] -> [FilePath]) -> IO [FilePath] -> IO [FilePath]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> IO [FilePath]
getDirectoryContentsRecursive FilePath
templateDir
[FilePath]
tmplFiles <- (FilePath -> IO Bool) -> [FilePath] -> IO [FilePath]
forall (m :: * -> *) a.
Applicative m =>
(a -> m Bool) -> [a] -> m [a]
filterM FilePath -> IO Bool
doesFileExist [FilePath]
tmplContents
[FilePath] -> (FilePath -> IO ()) -> IO ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [FilePath]
tmplFiles ((FilePath -> IO ()) -> IO ()) -> (FilePath -> IO ()) -> IO ()
forall a b. (a -> b) -> a -> b
$ \FilePath
fp -> do
let fullPath :: FilePath
fullPath = FilePath
targetDir FilePath -> FilePath -> FilePath
</> FilePath
newFP
where
newFP :: FilePath
newFP = (ParseError -> FilePath)
-> (Template -> FilePath) -> Either ParseError Template -> FilePath
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (FilePath -> ParseError -> FilePath
forall a b. a -> b -> a
const FilePath
relFP)
(Text -> FilePath
unpack (Text -> FilePath) -> (Template -> Text) -> Template -> FilePath
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Template -> Value -> Text
`renderMustache` Value
subst))
Either ParseError Template
fpAsTemplateE
relFP :: FilePath
relFP = FilePath -> FilePath -> FilePath
makeRelative FilePath
templateDir FilePath
fp
fpAsTemplateE :: Either ParseError Template
fpAsTemplateE = PName -> Text -> Either ParseError Template
compileMustacheText PName
"fp" (FilePath -> Text
pack FilePath
relFP)
ByteString
contents <- Text -> ByteString
encodeUtf8 (Text -> ByteString)
-> (Template -> Text) -> Template -> ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Template -> Value -> Text
`renderMustache` Value
subst)
(Template -> ByteString) -> IO Template -> IO ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> FilePath -> IO Template
compileMustacheFile FilePath
fp
let dirName :: FilePath
dirName = (FilePath, FilePath) -> FilePath
forall a b. (a, b) -> a
fst ((FilePath, FilePath) -> FilePath)
-> (FilePath, FilePath) -> FilePath
forall a b. (a -> b) -> a -> b
$ FilePath -> (FilePath, FilePath)
splitFileName FilePath
fullPath
Bool -> FilePath -> IO ()
createDirectoryIfMissing Bool
True FilePath
dirName
FilePath -> ByteString -> IO ()
B.writeFile FilePath
fullPath ByteString
contents