git.iko.soy/typeable/file-embed
on master help sign in
added 'qAddDependentFile' to 'embedFileIfExists' and some tests
752a2b8Charles Durham
Data/FileEmbed.hs+11−6
@@ -91,24 +91,29 @@ embedFile fp =
91
91
92-- | Maybe embed a single file in your source code depending on whether or not file exists.
92-- | Maybe embed a single file in your source code depending on whether or not file exists.
93--
93--
94−-- Warning: Does not use `qAddDependentFile` to force a recompile depending on changes to or appearance of the file.
94+-- Warning: When a build is compiled with the file missing, a recompile when the file exists might not trigger an embed of the file.
95+-- You might try to fix this by doing a clean build.
95--
96--
96-- > import qualified Data.ByteString
97-- > import qualified Data.ByteString
97-- >
98-- >
98-- > maybeMyFile :: Maybe Data.ByteString.ByteString
99-- > maybeMyFile :: Maybe Data.ByteString.ByteString
99-- > maybeMyFile = $(embedFileIfExists "dirName/fileName")
100-- > maybeMyFile = $(embedFileIfExists "dirName/fileName")
100embedFileIfExists :: FilePath -> Q Exp
101embedFileIfExists :: FilePath -> Q Exp
101−embedFileIfExists fp = runIO maybeFile >>= maybeBsToExp
102+embedFileIfExists fp = do
103+ mbs <- runIO maybeFile
104+ case mbs of
105+ Nothing -> [| Nothing |]
106+ Just bs -> do
107+#if MIN_VERSION_template_haskell(2,7,0)
108+ qAddDependentFile fp
109+#endif
110+ [| Just $(bsToExp bs) |]
102 where
111 where
103 maybeFile :: IO (Maybe B.ByteString)
112 maybeFile :: IO (Maybe B.ByteString)
104 maybeFile =
113 maybeFile =
105 either (const Nothing) Just <$>
114 either (const Nothing) Just <$>
106 tryJust (guard . isDoesNotExistError) (B.readFile fp)
115 tryJust (guard . isDoesNotExistError) (B.readFile fp)
107
116
108− maybeBsToExp :: Maybe B.ByteString -> Q Exp
109− maybeBsToExp Nothing = [| Nothing |]
110− maybeBsToExp (Just bs) = [| Just $(bsToExp bs) |]
111−
112-- | Embed a single existing file in your source code
117-- | Embed a single existing file in your source code
113-- out of list a list of paths supplied.
118-- out of list a list of paths supplied.
114--
119--
file-embed.cabal+1
@@ -33,6 +33,7 @@ test-suite test
33 main-is: main.hs
33 main-is: main.hs
34 hs-source-dirs: test
34 hs-source-dirs: test
35 build-depends: base
35 build-depends: base
36+ , bytestring
36 , file-embed
37 , file-embed
37 , filepath
38 , filepath
38
39
test/main.hs+7
@@ -2,6 +2,7 @@
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3
3
4import Control.Monad (unless)
4import Control.Monad (unless)
5+import qualified Data.ByteString as B (ByteString)
5import Data.FileEmbed
6import Data.FileEmbed
6import System.FilePath ((</>))
7import System.FilePath ((</>))
7
8
@@ -19,3 +20,9 @@ main = do
19 ]
20 ]
20 let str = $(embedStringFile "test/sample/foo") :: String
21 let str = $(embedStringFile "test/sample/foo") :: String
21 filter (/= '\r') str @?= "foo\n"
22 filter (/= '\r') str @?= "foo\n"
23+
24+ let mbs = $(embedFileIfExists "test/sample/foo")
25+ mbs @?= Just "foo\r\n"
26+
27+ let mbs2 = $(embedFileIfExists "test/sample/foo2") :: Maybe B.ByteString
28+ mbs2 @?= Nothing