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