@@ -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"