@@ -39,6 +39,7 @@ module Data.FileEmbed
3939 , injectFileWith
4040 -- * Relative path manipulation
4141 , makeRelativeToProject
42+ , makeRelativeToLocationPredicate
4243 -- * Internal
4344 , stringToBs
4445 , bsToExp
@@ -91,24 +92,31 @@ embedFile fp =
9192
9293-- | Maybe embed a single file in your source code depending on whether or not file exists.
9394--
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.
9597--
9698-- > import qualified Data.ByteString
9799-- >
98100-- > maybeMyFile :: Maybe Data.ByteString.ByteString
99101-- > maybeMyFile = $(embedFileIfExists "dirName/fileName")
102+--
103+-- @since 0.0.14.0
100104embedFileIfExists :: 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) |]
102114 where
103115 maybeFile :: IO (Maybe B.ByteString)
104116 maybeFile =
105117 either (const Nothing) Just <$>
106118 tryJust (guard . isDoesNotExistError) (B.readFile fp)
107119
108− maybeBsToExp :: Maybe B.ByteString -> Q Exp
109− maybeBsToExp Nothing = [| Nothing |]
110− maybeBsToExp (Just bs) = [| Just $(bsToExp bs) |]
111−
112120-- | Embed a single existing file in your source code
113121-- out of list a list of paths supplied.
114122--
@@ -401,7 +409,19 @@ available, you can use the non-@With@ variants.
401409--
402410-- @since 0.0.10
403411makeRelativeToProject :: 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
405425 loc <- qLocation
406426 runIO $ do
407427 srcFP <- canonicalizePath $ loc_filename loc
@@ -416,8 +436,6 @@ makeRelativeToProject rel = do
416436 then return Nothing
417437 else do
418438 contents <- getDirectoryContents dir
419− if any isCabalFile contents
439+ if any isTargetFile contents
420440 then return (Just dir)
421441 else findProjectDir dir
422−
423− isCabalFile fp = takeExtension fp == ".cabal"