mirror of
https://github.com/typeable/file-embed.git
synced 2024-09-11 07:45:30 +03:00
added 'qAddDependentFile' to 'embedFileIfExists' and some tests
This commit is contained in:
parent
4a280ee4fc
commit
752a2b80a6
@ -91,24 +91,29 @@ embedFile fp =
|
||||
|
||||
-- | Maybe embed a single file in your source code depending on whether or not file exists.
|
||||
--
|
||||
-- Warning: Does not use `qAddDependentFile` to force a recompile depending on changes to or appearance of the file.
|
||||
-- Warning: When a build is compiled with the file missing, a recompile when the file exists might not trigger an embed of the file.
|
||||
-- You might try to fix this by doing a clean build.
|
||||
--
|
||||
-- > import qualified Data.ByteString
|
||||
-- >
|
||||
-- > maybeMyFile :: Maybe Data.ByteString.ByteString
|
||||
-- > maybeMyFile = $(embedFileIfExists "dirName/fileName")
|
||||
embedFileIfExists :: FilePath -> Q Exp
|
||||
embedFileIfExists fp = runIO maybeFile >>= maybeBsToExp
|
||||
embedFileIfExists fp = do
|
||||
mbs <- runIO maybeFile
|
||||
case mbs of
|
||||
Nothing -> [| Nothing |]
|
||||
Just bs -> do
|
||||
#if MIN_VERSION_template_haskell(2,7,0)
|
||||
qAddDependentFile fp
|
||||
#endif
|
||||
[| Just $(bsToExp bs) |]
|
||||
where
|
||||
maybeFile :: IO (Maybe B.ByteString)
|
||||
maybeFile =
|
||||
either (const Nothing) Just <$>
|
||||
tryJust (guard . isDoesNotExistError) (B.readFile fp)
|
||||
|
||||
maybeBsToExp :: Maybe B.ByteString -> Q Exp
|
||||
maybeBsToExp Nothing = [| Nothing |]
|
||||
maybeBsToExp (Just bs) = [| Just $(bsToExp bs) |]
|
||||
|
||||
-- | Embed a single existing file in your source code
|
||||
-- out of list a list of paths supplied.
|
||||
--
|
||||
|
@ -33,6 +33,7 @@ test-suite test
|
||||
main-is: main.hs
|
||||
hs-source-dirs: test
|
||||
build-depends: base
|
||||
, bytestring
|
||||
, file-embed
|
||||
, filepath
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
import Control.Monad (unless)
|
||||
import qualified Data.ByteString as B (ByteString)
|
||||
import Data.FileEmbed
|
||||
import System.FilePath ((</>))
|
||||
|
||||
@ -19,3 +20,9 @@ main = do
|
||||
]
|
||||
let str = $(embedStringFile "test/sample/foo") :: String
|
||||
filter (/= '\r') str @?= "foo\n"
|
||||
|
||||
let mbs = $(embedFileIfExists "test/sample/foo")
|
||||
mbs @?= Just "foo\r\n"
|
||||
|
||||
let mbs2 = $(embedFileIfExists "test/sample/foo2") :: Maybe B.ByteString
|
||||
mbs2 @?= Nothing
|
||||
|
Loading…
Reference in New Issue
Block a user