{-# LANGUAGE OverloadedStrings #-}
module System.Directory.Extra
( copyTemplate
, CopyTemplateException(..)
)
where
import Control.Exception ( Exception, IOException, catch,
throwIO )
import Control.Monad ( filterM, forM_ )
import Data.Aeson ( Value (..) )
import qualified Data.ByteString.Lazy as B
import Data.List ( isInfixOf )
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,
(</>) )
import Text.Microstache ( MustacheException (..), Template,
compileMustacheFile,
compileMustacheText,
renderMustache )
import Text.Parsec.Error ( Message (..), errorMessages,
errorPos )
import Text.Parsec.Pos ( sourceColumn, sourceLine )
copyTemplate :: FilePath -> Value -> FilePath -> IO ()
copyTemplate :: [Char] -> Value -> [Char] -> IO ()
copyTemplate [Char]
templateDir Value
subst [Char]
targetDir = do
tmplContents <- ([Char] -> [Char]) -> [[Char]] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map ([Char]
templateDir [Char] -> [Char] -> [Char]
</>) ([[Char]] -> [[Char]])
-> ([[Char]] -> [[Char]]) -> [[Char]] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ([Char] -> Bool) -> [[Char]] -> [[Char]]
forall a. (a -> Bool) -> [a] -> [a]
filter ([Char] -> [[Char]] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`notElem` [[Char]
"..", [Char]
"."])
([[Char]] -> [[Char]]) -> IO [[Char]] -> IO [[Char]]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Char] -> IO [[Char]]
getDirectoryContentsRecursiveE [Char]
templateDir
tmplFiles <- filterM doesFileExist tmplContents
forM_ tmplFiles $ \[Char]
fp -> do
let fullPath :: [Char]
fullPath = [Char]
targetDir [Char] -> [Char] -> [Char]
</> [Char]
newFP
where
newFP :: [Char]
newFP = (ParseError -> [Char])
-> (Template -> [Char]) -> Either ParseError Template -> [Char]
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ([Char] -> ParseError -> [Char]
forall a b. a -> b -> a
const [Char]
relFP)
(Text -> [Char]
unpack (Text -> [Char]) -> (Template -> Text) -> Template -> [Char]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Template -> Value -> Text
`renderMustache` Value
subst))
Either ParseError Template
fpAsTemplateE
relFP :: [Char]
relFP = [Char] -> [Char] -> [Char]
makeRelative [Char]
templateDir [Char]
fp
fpAsTemplateE :: Either ParseError Template
fpAsTemplateE = PName -> Text -> Either ParseError Template
compileMustacheText PName
"fp" ([Char] -> Text
pack [Char]
relFP)
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
<$> [Char] -> IO Template
compileMustacheFileE [Char]
fp
let dirName = ([Char], [Char]) -> [Char]
forall a b. (a, b) -> a
fst (([Char], [Char]) -> [Char]) -> ([Char], [Char]) -> [Char]
forall a b. (a -> b) -> a -> b
$ [Char] -> ([Char], [Char])
splitFileName [Char]
fullPath
createDirectoryIfMissingE True dirName
writeFileE fullPath contents
newtype CopyTemplateException = CopyTemplateException String
instance Show CopyTemplateException where
show :: CopyTemplateException -> [Char]
show (CopyTemplateException [Char]
s) = [Char]
s
instance Exception CopyTemplateException
getDirectoryContentsRecursiveE :: FilePath -> IO [FilePath]
getDirectoryContentsRecursiveE :: [Char] -> IO [[Char]]
getDirectoryContentsRecursiveE [Char]
s =
IO [[Char]] -> (IOException -> IO [[Char]]) -> IO [[Char]]
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catch ([Char] -> IO [[Char]]
getDirectoryContentsRecursive [Char]
s) IOException -> IO [[Char]]
handler
where
handler :: IOException -> IO [FilePath]
handler :: IOException -> IO [[Char]]
handler IOException
e = CopyTemplateException -> IO [[Char]]
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO ([Char] -> CopyTemplateException
CopyTemplateException (IOException -> [Char]
forall a. Show a => a -> [Char]
show IOException
e))
createDirectoryIfMissingE :: Bool -> FilePath -> IO ()
createDirectoryIfMissingE :: Bool -> [Char] -> IO ()
createDirectoryIfMissingE Bool
parents [Char]
fp =
IO () -> (IOException -> IO ()) -> IO ()
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catch (Bool -> [Char] -> IO ()
createDirectoryIfMissing Bool
parents [Char]
fp) IOException -> IO ()
handler
where
handler :: IOException -> IO ()
handler :: IOException -> IO ()
handler IOException
e
| [Char]
"createDirectory: permission denied" [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` IOException -> [Char]
forall a. Show a => a -> [Char]
show IOException
e
= CopyTemplateException -> IO ()
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (CopyTemplateException -> IO ()) -> CopyTemplateException -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> CopyTemplateException
CopyTemplateException ([Char] -> CopyTemplateException)
-> [Char] -> CopyTemplateException
forall a b. (a -> b) -> a -> b
$
[Char]
fp [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"Error creating target directory (permission denied)"
| Bool
otherwise
= CopyTemplateException -> IO ()
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (CopyTemplateException -> IO ()) -> CopyTemplateException -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> CopyTemplateException
CopyTemplateException ([Char] -> CopyTemplateException)
-> [Char] -> CopyTemplateException
forall a b. (a -> b) -> a -> b
$ [Char]
fp [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ IOException -> [Char]
forall a. Show a => a -> [Char]
show IOException
e
writeFileE :: FilePath -> B.ByteString -> IO ()
writeFileE :: [Char] -> ByteString -> IO ()
writeFileE [Char]
fp ByteString
contents =
IO () -> (IOException -> IO ()) -> IO ()
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catch ([Char] -> ByteString -> IO ()
B.writeFile [Char]
fp ByteString
contents) IOException -> IO ()
handler
where
handler :: IOException -> IO ()
handler :: IOException -> IO ()
handler IOException
e
| [Char]
"permission denied" [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` IOException -> [Char]
forall a. Show a => a -> [Char]
show IOException
e
= CopyTemplateException -> IO ()
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (CopyTemplateException -> IO ()) -> CopyTemplateException -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> CopyTemplateException
CopyTemplateException ([Char] -> CopyTemplateException)
-> [Char] -> CopyTemplateException
forall a b. (a -> b) -> a -> b
$
[Char]
fp [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"Error creating target file (permission denied)"
| [Char]
"resource exhausted" [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` IOException -> [Char]
forall a. Show a => a -> [Char]
show IOException
e
= CopyTemplateException -> IO ()
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (CopyTemplateException -> IO ()) -> CopyTemplateException -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> CopyTemplateException
CopyTemplateException ([Char] -> CopyTemplateException)
-> [Char] -> CopyTemplateException
forall a b. (a -> b) -> a -> b
$
[Char]
fp [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"No space left on device"
| Bool
otherwise
= CopyTemplateException -> IO ()
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (CopyTemplateException -> IO ()) -> CopyTemplateException -> IO ()
forall a b. (a -> b) -> a -> b
$ [Char] -> CopyTemplateException
CopyTemplateException ([Char] -> CopyTemplateException)
-> [Char] -> CopyTemplateException
forall a b. (a -> b) -> a -> b
$ [Char]
fp [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ IOException -> [Char]
forall a. Show a => a -> [Char]
show IOException
e
compileMustacheFileE :: FilePath -> IO Template
compileMustacheFileE :: [Char] -> IO Template
compileMustacheFileE [Char]
fp = do
IO Template -> (IOException -> IO Template) -> IO Template
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catch (IO Template -> (MustacheException -> IO Template) -> IO Template
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catch ([Char] -> IO Template
compileMustacheFile [Char]
fp) MustacheException -> IO Template
handler) IOException -> IO Template
handlerIO
where
handler :: MustacheException -> IO Template
handler :: MustacheException -> IO Template
handler (MustacheParserException ParseError
p) = do
let pos :: SourcePos
pos = ParseError -> SourcePos
errorPos ParseError
p
line :: Line
line = SourcePos -> Line
sourceLine SourcePos
pos
column :: Line
column = SourcePos -> Line
sourceColumn SourcePos
pos
messages :: [Char]
messages = [[Char]] -> [Char]
keepHead ([[Char]] -> [Char]) -> [[Char]] -> [Char]
forall a b. (a -> b) -> a -> b
$ (Message -> [Char]) -> [Message] -> [[Char]]
forall a b. (a -> b) -> [a] -> [b]
map Message -> [Char]
showMessage ([Message] -> [[Char]]) -> [Message] -> [[Char]]
forall a b. (a -> b) -> a -> b
$ ParseError -> [Message]
errorMessages ParseError
p
CopyTemplateException -> IO Template
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (CopyTemplateException -> IO Template)
-> CopyTemplateException -> IO Template
forall a b. (a -> b) -> a -> b
$ [Char] -> CopyTemplateException
CopyTemplateException ([Char] -> CopyTemplateException)
-> [Char] -> CopyTemplateException
forall a b. (a -> b) -> a -> b
$
[Char]
fp [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
":" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Line -> [Char]
forall a. Show a => a -> [Char]
show Line
line [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
":" [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Line -> [Char]
forall a. Show a => a -> [Char]
show Line
column [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
messages
handler MustacheException
e = do
CopyTemplateException -> IO Template
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (CopyTemplateException -> IO Template)
-> CopyTemplateException -> IO Template
forall a b. (a -> b) -> a -> b
$ [Char] -> CopyTemplateException
CopyTemplateException ([Char] -> CopyTemplateException)
-> [Char] -> CopyTemplateException
forall a b. (a -> b) -> a -> b
$ [Char]
fp [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ MustacheException -> [Char]
forall a. Show a => a -> [Char]
show MustacheException
e
handlerIO :: IOException -> IO Template
handlerIO :: IOException -> IO Template
handlerIO IOException
e
| [Char]
"hGetContents: invalid argument" [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` IOException -> [Char]
forall a. Show a => a -> [Char]
show IOException
e
= CopyTemplateException -> IO Template
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (CopyTemplateException -> IO Template)
-> CopyTemplateException -> IO Template
forall a b. (a -> b) -> a -> b
$ [Char] -> CopyTemplateException
CopyTemplateException ([Char] -> CopyTemplateException)
-> [Char] -> CopyTemplateException
forall a b. (a -> b) -> a -> b
$
[Char]
fp [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"Invalid UTF-8 byte sequence"
| [Char]
"invalid byte sequence" [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` IOException -> [Char]
forall a. Show a => a -> [Char]
show IOException
e
= CopyTemplateException -> IO Template
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (CopyTemplateException -> IO Template)
-> CopyTemplateException -> IO Template
forall a b. (a -> b) -> a -> b
$ [Char] -> CopyTemplateException
CopyTemplateException ([Char] -> CopyTemplateException)
-> [Char] -> CopyTemplateException
forall a b. (a -> b) -> a -> b
$
[Char]
fp [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"Invalid UTF-8 byte sequence"
| [Char]
"openFile: permission denied" [Char] -> [Char] -> Bool
forall a. Eq a => [a] -> [a] -> Bool
`isInfixOf` IOException -> [Char]
forall a. Show a => a -> [Char]
show IOException
e
= CopyTemplateException -> IO Template
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (CopyTemplateException -> IO Template)
-> CopyTemplateException -> IO Template
forall a b. (a -> b) -> a -> b
$ [Char] -> CopyTemplateException
CopyTemplateException ([Char] -> CopyTemplateException)
-> [Char] -> CopyTemplateException
forall a b. (a -> b) -> a -> b
$ [Char]
fp [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
"Permission denied"
| Bool
otherwise
= CopyTemplateException -> IO Template
forall e a. (HasCallStack, Exception e) => e -> IO a
throwIO (CopyTemplateException -> IO Template)
-> CopyTemplateException -> IO Template
forall a b. (a -> b) -> a -> b
$ [Char] -> CopyTemplateException
CopyTemplateException ([Char] -> CopyTemplateException)
-> [Char] -> CopyTemplateException
forall a b. (a -> b) -> a -> b
$ [Char]
fp [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
": " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ IOException -> [Char]
forall a. Show a => a -> [Char]
show IOException
e
showMessage :: Message -> String
showMessage :: Message -> [Char]
showMessage (SysUnExpect [Char]
s) = [Char]
"Unexpected " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
s
showMessage (UnExpect [Char]
s) = [Char]
"Unexpected " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
s
showMessage (Expect [Char]
s) = [Char]
"Expected " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ [Char]
s
showMessage (Message [Char]
s) = [Char]
s
keepHead :: [String] -> String
keepHead :: [[Char]] -> [Char]
keepHead ([Char]
a:[[Char]]
_) = [Char]
a
keepHead [[Char]]
_ = [Char]
""