packages feed

file-embed 0.0.13.0 → 0.0.14.0

raw patch · 4 files changed

+44/−3 lines, 4 filesdep ~bytestringPVP ok

version bump matches the API change (PVP)

Dependency ranges changed: bytestring

API changes (from Hackage documentation)

+ Data.FileEmbed: embedFileIfExists :: FilePath -> Q Exp

Files

ChangeLog.md view
@@ -1,5 +1,9 @@ # ChangeLog for file-embed +## 0.0.14.0++* Add `embedFileIfExists`+ ## 0.0.13.0  * Ensure that directory listings are returned in sorted order for reproducibility [yesodweb/yesod#1684](https://github.com/yesodweb/yesod/issues/1684)
Data/FileEmbed.hs view
@@ -19,6 +19,7 @@ module Data.FileEmbed     ( -- * Embed at compile time       embedFile+    , embedFileIfExists     , embedOneFileOf     , embedDir     , embedDirListing@@ -60,13 +61,14 @@ #endif import System.Directory (doesDirectoryExist, doesFileExist,                          getDirectoryContents, canonicalizePath)-import Control.Exception (throw, ErrorCall(..))-import Control.Monad (filterM)+import Control.Exception (throw, tryJust, ErrorCall(..))+import Control.Monad (filterM, guard) import qualified Data.ByteString as B import qualified Data.ByteString.Char8 as B8 import Control.Arrow ((&&&), second) import Control.Applicative ((<$>)) import Data.ByteString.Unsafe (unsafePackAddressLen)+import System.IO.Error (isDoesNotExistError) import System.IO.Unsafe (unsafePerformIO) import System.FilePath ((</>), takeDirectory, takeExtension) import Data.String (fromString)@@ -86,6 +88,33 @@     qAddDependentFile fp >> #endif   (runIO $ B.readFile fp) >>= bsToExp++-- | Maybe embed a single file in your source code depending on whether or not file exists.+--+-- 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")+--+-- @since 0.0.14.0+embedFileIfExists :: FilePath -> Q Exp+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)  -- | Embed a single existing file in your source code -- out of list a list of paths supplied.
file-embed.cabal view
@@ -1,5 +1,5 @@ name:            file-embed-version:         0.0.13.0+version:         0.0.14.0 license:         BSD3 license-file:    LICENSE author:          Michael Snoyman <michael@snoyman.com>@@ -33,6 +33,7 @@     main-is: main.hs     hs-source-dirs: test     build-depends: base+                 , bytestring                  , file-embed                  , filepath 
test/main.hs view
@@ -2,6 +2,7 @@ {-# LANGUAGE OverloadedStrings #-}  import Control.Monad (unless)+import qualified Data.ByteString as B (ByteString, filter) import Data.FileEmbed import System.FilePath ((</>)) @@ -19,3 +20,9 @@         ]     let str = $(embedStringFile "test/sample/foo") :: String     filter (/= '\r') str @?= "foo\n"++    let mbs = $(embedFileIfExists "test/sample/foo")+    fmap (B.filter (/= fromIntegral (fromEnum '\r'))) mbs @?= Just "foo\n"++    let mbs2 = $(embedFileIfExists "test/sample/foo2") :: Maybe B.ByteString+    mbs2 @?= Nothing