git.iko.soy/typeable/file-embed
on 4a280ee help sign in
4 files changed+12−46
since splitsnapshots
unifiedsplit
ChangeLog.md−8 Data/FileEmbed.hs+10−28 file-embed.cabal+2−3 test/main.hs−7
ChangeLog.md−8
@@ -1,13 +1,5 @@
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−
113## 0.0.13.0
124
135* Ensure that directory listings are returned in sorted order for reproducibility [yesodweb/yesod#1684](https://github.com/yesodweb/yesod/issues/1684)
Data/FileEmbed.hs+10−28
@@ -39,7 +39,6 @@ module Data.FileEmbed
3939 , injectFileWith
4040 -- * Relative path manipulation
4141 , makeRelativeToProject
42− , makeRelativeToLocationPredicate
4342 -- * Internal
4443 , stringToBs
4544 , bsToExp
@@ -92,31 +91,24 @@ embedFile fp =
9291
9392-- | Maybe embed a single file in your source code depending on whether or not file exists.
9493--
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.
94+-- Warning: Does not use `qAddDependentFile` to force a recompile depending on changes to or appearance of the file.
9795--
9896-- > import qualified Data.ByteString
9997-- >
10098-- > maybeMyFile :: Maybe Data.ByteString.ByteString
10199-- > maybeMyFile = $(embedFileIfExists "dirName/fileName")
102−--
103−-- @since 0.0.14.0
104100embedFileIfExists :: FilePath -> Q Exp
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) |]
101+embedFileIfExists fp = runIO maybeFile >>= maybeBsToExp
114102 where
115103 maybeFile :: IO (Maybe B.ByteString)
116104 maybeFile =
117105 either (const Nothing) Just <$>
118106 tryJust (guard . isDoesNotExistError) (B.readFile fp)
119107
108+ maybeBsToExp :: Maybe B.ByteString -> Q Exp
109+ maybeBsToExp Nothing = [| Nothing |]
110+ maybeBsToExp (Just bs) = [| Just $(bsToExp bs) |]
111+
120112-- | Embed a single existing file in your source code
121113-- out of list a list of paths supplied.
122114--
@@ -409,19 +401,7 @@ available, you can use the non-@With@ variants.
409401--
410402-- @since 0.0.10
411403makeRelativeToProject :: FilePath -> Q FilePath
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
404+makeRelativeToProject rel = do
425405 loc <- qLocation
426406 runIO $ do
427407 srcFP <- canonicalizePath $ loc_filename loc
@@ -436,6 +416,8 @@ makeRelativeToLocationPredicate isTargetFile rel = do
436416 then return Nothing
437417 else do
438418 contents <- getDirectoryContents dir
439− if any isTargetFile contents
419+ if any isCabalFile contents
440420 then return (Just dir)
441421 else findProjectDir dir
422+
423+ isCabalFile fp = takeExtension fp == ".cabal"
file-embed.cabal+2−3
@@ -1,6 +1,6 @@
11name: file-embed
2−version: 0.0.15.0
3−license: BSD2
2+version: 0.0.13.0
3+license: BSD3
44license-file: LICENSE
55author: Michael Snoyman <michael@snoyman.com>
66maintainer: Michael Snoyman <michael@snoyman.com>
@@ -33,7 +33,6 @@ test-suite test
3333 main-is: main.hs
3434 hs-source-dirs: test
3535 build-depends: base
36− , bytestring
3736 , file-embed
3837 , filepath
3938
test/main.hs−7
@@ -2,7 +2,6 @@
22{-# LANGUAGE OverloadedStrings #-}
33
44import Control.Monad (unless)
5−import qualified Data.ByteString as B (ByteString, filter)
65import Data.FileEmbed
76import System.FilePath ((</>))
87
@@ -20,9 +19,3 @@ main = do
2019 ]
2120 let str = $(embedStringFile "test/sample/foo") :: String
2221 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