@@ -39,6 +39,7 @@ module Data.FileEmbed
3939 , injectFileWith
4040 -- * Relative path manipulation
4141 , makeRelativeToProject
42+ , makeRelativeToLocationPredicate
4243 -- * Internal
4344 , stringToBs
4445 , bsToExp
@@ -98,6 +99,8 @@ embedFile fp =
9899-- >
99100-- > maybeMyFile :: Maybe Data.ByteString.ByteString
100101-- > maybeMyFile = $(embedFileIfExists "dirName/fileName")
102+--
103+-- @since 0.0.14.0
101104embedFileIfExists :: FilePath -> Q Exp
102105embedFileIfExists fp = do
103106 mbs <- runIO maybeFile
@@ -406,7 +409,19 @@ available, you can use the non-@With@ variants.
406409--
407410-- @since 0.0.10
408411makeRelativeToProject :: FilePath -> Q FilePath
409−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
410425 loc <- qLocation
411426 runIO $ do
412427 srcFP <- canonicalizePath $ loc_filename loc
@@ -421,8 +436,6 @@ makeRelativeToProject rel = do
421436 then return Nothing
422437 else do
423438 contents <- getDirectoryContents dir
424− if any isCabalFile contents
439+ if any isTargetFile contents
425440 then return (Just dir)
426441 else findProjectDir dir
427−
428− isCabalFile fp = takeExtension fp == ".cabal"