git.iko.soy/typeable/file-embed
on master help sign in
4 files changed+46−12
since splitsnapshots
unifiedsplit
ChangeLog.md+8 Data/FileEmbed.hs+28−10 file-embed.cabal+3−2 test/main.hs+7
ChangeLog.md+8
@@ -1,5 +1,13 @@
11# ChangeLog for file-embed
22
3+## 0.0.15.0
4+
5+* Add `makeRelativeToLocationPredicate`
6+
7+## 0.0.14.0
8+
9+* Add `embedFileIfExists`
10+
311## 0.0.13.0
412
513* Ensure that directory listings are returned in sorted order for reproducibility [yesodweb/yesod#1684](https://github.com/yesodweb/yesod/issues/1684)
Data/FileEmbed.hs+28−10
@@ -39,6 +39,7 @@ module Data.FileEmbed
3939 , injectFileWith
4040 -- * Relative path manipulation
4141 , makeRelativeToProject
42+ , makeRelativeToLocationPredicate
4243 -- * Internal
4344 , stringToBs
4445 , bsToExp
@@ -91,24 +92,31 @@ embedFile fp =
9192
9293-- | Maybe embed a single file in your source code depending on whether or not file exists.
9394--
94−-- Warning: Does not use `qAddDependentFile` to force a recompile depending on changes to or appearance of the file.
95+-- Warning: When a build is compiled with the file missing, a recompile when the file exists might not trigger an embed of the file.
96+-- You might try to fix this by doing a clean build.
9597--
9698-- > import qualified Data.ByteString
9799-- >
98100-- > maybeMyFile :: Maybe Data.ByteString.ByteString
99101-- > maybeMyFile = $(embedFileIfExists "dirName/fileName")
102+--
103+-- @since 0.0.14.0
100104embedFileIfExists :: FilePath -> Q Exp
101−embedFileIfExists fp = runIO maybeFile >>= maybeBsToExp
105+embedFileIfExists fp = do
106+ mbs <- runIO maybeFile
107+ case mbs of
108+ Nothing -> [| Nothing |]
109+ Just bs -> do
110+#if MIN_VERSION_template_haskell(2,7,0)
111+ qAddDependentFile fp
112+#endif
113+ [| Just $(bsToExp bs) |]
102114 where
103115 maybeFile :: IO (Maybe B.ByteString)
104116 maybeFile =
105117 either (const Nothing) Just <$>
106118 tryJust (guard . isDoesNotExistError) (B.readFile fp)
107119
108− maybeBsToExp :: Maybe B.ByteString -> Q Exp
109− maybeBsToExp Nothing = [| Nothing |]
110− maybeBsToExp (Just bs) = [| Just $(bsToExp bs) |]
111−
112120-- | Embed a single existing file in your source code
113121-- out of list a list of paths supplied.
114122--
@@ -401,7 +409,19 @@ available, you can use the non-@With@ variants.
401409--
402410-- @since 0.0.10
403411makeRelativeToProject :: FilePath -> Q FilePath
404−makeRelativeToProject rel = do
412+makeRelativeToProject = makeRelativeToLocationPredicate $ (==) ".cabal" . takeExtension
413+
414+-- | Take a predicate to infer the project root and a relative file path, the given file path is then attached to the inferred project root
415+--
416+-- This function looks at the source location of the Haskell file calling it,
417+-- finds the first parent directory with a file matching the given predicate, and uses that as the
418+-- root directory for fixing the relative path.
419+--
420+-- @$(makeRelativeToLocationPredicate ((==) ".cabal" . takeExtension) "data/foo.txt" >>= embedFile)@
421+--
422+-- @since 0.0.15.0
423+makeRelativeToLocationPredicate :: (FilePath -> Bool) -> FilePath -> Q FilePath
424+makeRelativeToLocationPredicate isTargetFile rel = do
405425 loc <- qLocation
406426 runIO $ do
407427 srcFP <- canonicalizePath $ loc_filename loc
@@ -416,8 +436,6 @@ makeRelativeToProject rel = do
416436 then return Nothing
417437 else do
418438 contents <- getDirectoryContents dir
419− if any isCabalFile contents
439+ if any isTargetFile contents
420440 then return (Just dir)
421441 else findProjectDir dir
422−
423− isCabalFile fp = takeExtension fp == ".cabal"
file-embed.cabal+3−2
@@ -1,6 +1,6 @@
11name: file-embed
2−version: 0.0.13.0
3−license: BSD3
2+version: 0.0.15.0
3+license: BSD2
44license-file: LICENSE
55author: Michael Snoyman <michael@snoyman.com>
66maintainer: Michael Snoyman <michael@snoyman.com>
@@ -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, filter)
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+ fmap (B.filter (/= fromIntegral (fromEnum '\r'))) mbs @?= Just "foo\n"
26+
27+ let mbs2 = $(embedFileIfExists "test/sample/foo2") :: Maybe B.ByteString
28+ mbs2 @?= Nothing