git.iko.soy/typeable/file-embed
on master help sign in
ChangeLog.md+8
since splitsnapshots
unifiedsplit
@@ -1,5 +1,13 @@
1# ChangeLog for file-embed
1# ChangeLog for file-embed
2
2
3+## 0.0.15.0
4+
5+* Add `makeRelativeToLocationPredicate`
6+
7+## 0.0.14.0
8+
9+* Add `embedFileIfExists`
10+
3## 0.0.13.0
11## 0.0.13.0
4
12
5* Ensure that directory listings are returned in sorted order for reproducibility [yesodweb/yesod#1684](https://github.com/yesodweb/yesod/issues/1684)
13* 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
39 , injectFileWith
39 , injectFileWith
40 -- * Relative path manipulation
40 -- * Relative path manipulation
41 , makeRelativeToProject
41 , makeRelativeToProject
42+ , makeRelativeToLocationPredicate
42 -- * Internal
43 -- * Internal
43 , stringToBs
44 , stringToBs
44 , bsToExp
45 , bsToExp
@@ -91,24 +92,31 @@ embedFile fp =
91
92
92-- | Maybe embed a single file in your source code depending on whether or not file exists.
93-- | Maybe embed a single file in your source code depending on whether or not file exists.
93--
94--
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.
95--
97--
96-- > import qualified Data.ByteString
98-- > import qualified Data.ByteString
97-- >
99-- >
98-- > maybeMyFile :: Maybe Data.ByteString.ByteString
100-- > maybeMyFile :: Maybe Data.ByteString.ByteString
99-- > maybeMyFile = $(embedFileIfExists "dirName/fileName")
101-- > maybeMyFile = $(embedFileIfExists "dirName/fileName")
102+--
103+-- @since 0.0.14.0
100embedFileIfExists :: FilePath -> Q Exp
104embedFileIfExists :: 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) |]
102 where
114 where
103 maybeFile :: IO (Maybe B.ByteString)
115 maybeFile :: IO (Maybe B.ByteString)
104 maybeFile =
116 maybeFile =
105 either (const Nothing) Just <$>
117 either (const Nothing) Just <$>
106 tryJust (guard . isDoesNotExistError) (B.readFile fp)
118 tryJust (guard . isDoesNotExistError) (B.readFile fp)
107
119
108− maybeBsToExp :: Maybe B.ByteString -> Q Exp
109− maybeBsToExp Nothing = [| Nothing |]
110− maybeBsToExp (Just bs) = [| Just $(bsToExp bs) |]
111−
112-- | Embed a single existing file in your source code
120-- | Embed a single existing file in your source code
113-- out of list a list of paths supplied.
121-- out of list a list of paths supplied.
114--
122--
@@ -401,7 +409,19 @@ available, you can use the non-@With@ variants.
401--
409--
402-- @since 0.0.10
410-- @since 0.0.10
403makeRelativeToProject :: FilePath -> Q FilePath
411makeRelativeToProject :: 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
405 loc <- qLocation
425 loc <- qLocation
406 runIO $ do
426 runIO $ do
407 srcFP <- canonicalizePath $ loc_filename loc
427 srcFP <- canonicalizePath $ loc_filename loc
@@ -416,8 +436,6 @@ makeRelativeToProject rel = do
416 then return Nothing
436 then return Nothing
417 else do
437 else do
418 contents <- getDirectoryContents dir
438 contents <- getDirectoryContents dir
419− if any isCabalFile contents
439+ if any isTargetFile contents
420 then return (Just dir)
440 then return (Just dir)
421 else findProjectDir dir
441 else findProjectDir dir
422−
423− isCabalFile fp = takeExtension fp == ".cabal"
file-embed.cabal+3−2
@@ -1,6 +1,6 @@
1name: file-embed
1name: file-embed
2−version: 0.0.13.0
2+version: 0.0.15.0
3−license: BSD3
3+license: BSD2
4license-file: LICENSE
4license-file: LICENSE
5author: Michael Snoyman <michael@snoyman.com>
5author: Michael Snoyman <michael@snoyman.com>
6maintainer: Michael Snoyman <michael@snoyman.com>
6maintainer: Michael Snoyman <michael@snoyman.com>
@@ -33,6 +33,7 @@ test-suite test
33 main-is: main.hs
33 main-is: main.hs
34 hs-source-dirs: test
34 hs-source-dirs: test
35 build-depends: base
35 build-depends: base
36+ , bytestring
36 , file-embed
37 , file-embed
37 , filepath
38 , filepath
38
39
test/main.hs+7
@@ -2,6 +2,7 @@
2{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE OverloadedStrings #-}
3
3
4import Control.Monad (unless)
4import Control.Monad (unless)
5+import qualified Data.ByteString as B (ByteString, filter)
5import Data.FileEmbed
6import Data.FileEmbed
6import System.FilePath ((</>))
7import System.FilePath ((</>))
7
8
@@ -19,3 +20,9 @@ main = do
19 ]
20 ]
20 let str = $(embedStringFile "test/sample/foo") :: String
21 let str = $(embedStringFile "test/sample/foo") :: String
21 filter (/= '\r') str @?= "foo\n"
22 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