git.iko.soy/typeable/file-embed
on master help sign in
Merge pull request #5 from kuznero/master

Embed existing file out of paths supplied

commit5749b41e1aa8080f685ba66293c8cb6b834a2a35 authorMichael Snoyman <michael@snoyman.com> dateWed 27 Nov 2013, 06:29 (13 years ago) parent59d847c954879f
1 file changed+24
Data/FileEmbed.hs+24
Data/FileEmbed.hs+24
@@ -18,6 +18,7 @@
1818module Data.FileEmbed
1919 ( -- * Embed at compile time
2020 embedFile
21+ , embedOneFileOf
2122 , embedDir
2223 , getDir
2324 -- * Inject into an executable
@@ -45,6 +46,7 @@ import Language.Haskell.TH.Syntax
4546 )
4647import System.Directory (doesDirectoryExist, doesFileExist,
4748 getDirectoryContents)
49+import Control.Exception (throw, ErrorCall(..))
4850import Control.Monad (filterM)
4951import qualified Data.ByteString as B
5052import qualified Data.ByteString.Char8 as B8
@@ -67,6 +69,28 @@ embedFile fp =
6769#endif
6870 (runIO $ B.readFile fp) >>= bsToExp
6971
72+-- | Embed a single existing file in your source code
73+-- out of list a list of paths supplied.
74+--
75+-- > import qualified Data.ByteString
76+-- >
77+-- > myFile :: Data.ByteString.ByteString
78+-- > myFile = $(embedFile' [ "dirName/fileName", "src/dirName/fileName" ])
79+embedOneFileOf :: [FilePath] -> Q Exp
80+embedOneFileOf ps =
81+ (runIO $ readExistingFile ps) >>= \ ( path, content ) -> do
82+#if MIN_VERSION_template_haskell(2,7,0)
83+ qAddDependentFile path
84+#endif
85+ bsToExp content
86+ where
87+ readExistingFile :: [FilePath] -> IO ( FilePath, B.ByteString )
88+ readExistingFile xs = do
89+ ys <- filterM doesFileExist xs
90+ case ys of
91+ (p:_) -> B.readFile p >>= \ c -> return ( p, c )
92+ _ -> throw $ ErrorCall "Cannot find file to embed as resource"
93+
7094-- | Embed a directory recursively in your source code.
7195--
7296-- > import qualified Data.ByteString