git.iko.soy/typeable/file-embed
on master help sign in
added 'qAddDependentFile' to 'embedFileIfExists' and some tests
commit752a2b80a670ded37027e97c0a1070c2ffa089ee authorCharles Durham <cpdurham@protonmail.com> dateMon 10 May 2021, 10:06 (5 years ago) parent4a280ee
3 files changed+19−6
Data/FileEmbed.hs+11−6 file-embed.cabal+1 test/main.hs+7
Data/FileEmbed.hs+11−6
@@ -91,24 +91,29 @@ embedFile fp =
9191
9292-- | Maybe embed a single file in your source code depending on whether or not file exists.
9393--
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.
9596--
9697-- > import qualified Data.ByteString
9798-- >
9899-- > maybeMyFile :: Maybe Data.ByteString.ByteString
99100-- > maybeMyFile = $(embedFileIfExists "dirName/fileName")
100101embedFileIfExists :: 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) |]
102111 where
103112 maybeFile :: IO (Maybe B.ByteString)
104113 maybeFile =
105114 either (const Nothing) Just <$>
106115 tryJust (guard . isDoesNotExistError) (B.readFile fp)
107116
108− maybeBsToExp :: Maybe B.ByteString -> Q Exp
109− maybeBsToExp Nothing = [| Nothing |]
110− maybeBsToExp (Just bs) = [| Just $(bsToExp bs) |]
111−
112117-- | Embed a single existing file in your source code
113118-- out of list a list of paths supplied.
114119--
file-embed.cabal+1
@@ -33,6 +33,7 @@ test-suite test
3333 main-is: main.hs
3434 hs-source-dirs: test
3535 build-depends: base
36+ , bytestring
3637 , file-embed
3738 , filepath
3839
test/main.hs+7
@@ -2,6 +2,7 @@
22{-# LANGUAGE OverloadedStrings #-}
33
44import Control.Monad (unless)
5+import qualified Data.ByteString as B (ByteString)
56import Data.FileEmbed
67import System.FilePath ((</>))
78
@@ -19,3 +20,9 @@ main = do
1920 ]
2021 let str = $(embedStringFile "test/sample/foo") :: String
2122 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