@@ -91,24 +91,29 @@ embedFile fp =
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.
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.
96-- > import qualified Data.ByteString
97-- > import qualified Data.ByteString
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
105+ Nothing -> [| Nothing |]
107+#if MIN_VERSION_template_haskell(2,7,0)
108+ qAddDependentFile fp
110+ [| Just $(bsToExp bs) |]
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)
108− maybeBsToExp :: Maybe B.ByteString -> Q Exp
109− maybeBsToExp Nothing = [| Nothing |]
110− maybeBsToExp (Just bs) = [| Just $(bsToExp bs) |]
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.