@@ -39,6 +39,7 @@ module Data.FileEmbed
39 , injectFileWith
39 , injectFileWith
40 -- * Relative path manipulation
40 -- * Relative path manipulation
41 , makeRelativeToProject
41 , makeRelativeToProject
42+ , makeRelativeToLocationPredicate
42 -- * Internal
43 -- * Internal
43 , stringToBs
44 , stringToBs
@@ -91,24 +92,31 @@ embedFile fp =
92-- | Maybe embed a single file in your source code depending on whether or not file exists.
93-- | Maybe embed a single file in your source code depending on whether or not file exists.
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.
96-- > import qualified Data.ByteString
98-- > import qualified Data.ByteString
98-- > maybeMyFile :: Maybe Data.ByteString.ByteString
100-- > maybeMyFile :: Maybe Data.ByteString.ByteString
99-- > maybeMyFile = $(embedFileIfExists "dirName/fileName")
101-- > maybeMyFile = $(embedFileIfExists "dirName/fileName")
100embedFileIfExists :: FilePath -> Q Exp
104embedFileIfExists :: FilePath -> Q Exp
101−embedFileIfExists fp = runIO maybeFile >>= maybeBsToExp
105+embedFileIfExists fp = do
106+ mbs <- runIO maybeFile
108+ Nothing -> [| Nothing |]
110+#if MIN_VERSION_template_haskell(2,7,0)
111+ qAddDependentFile fp
113+ [| Just $(bsToExp bs) |]
103 maybeFile :: IO (Maybe B.ByteString)
115 maybeFile :: IO (Maybe B.ByteString)
104 maybeFile =
116 maybeFile =
105 either (const Nothing) Just <$>
117 either (const Nothing) Just <$>
106 tryJust (guard . isDoesNotExistError) (B.readFile fp)
118 tryJust (guard . isDoesNotExistError) (B.readFile fp)
108− maybeBsToExp :: Maybe B.ByteString -> Q Exp
109− maybeBsToExp Nothing = [| Nothing |]
110− maybeBsToExp (Just bs) = [| Just $(bsToExp bs) |]
112-- | Embed a single existing file in your source code
120-- | Embed a single existing file in your source code
113-- out of list a list of paths supplied.
121-- out of list a list of paths supplied.
@@ -401,7 +409,19 @@ available, you can use the non-@With@ variants.
402-- @since 0.0.10
410-- @since 0.0.10
403makeRelativeToProject :: FilePath -> Q FilePath
411makeRelativeToProject :: FilePath -> Q FilePath
404−makeRelativeToProject rel = do
412+makeRelativeToProject = makeRelativeToLocationPredicate $ (==) ".cabal" . takeExtension
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
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.
420+-- @$(makeRelativeToLocationPredicate ((==) ".cabal" . takeExtension) "data/foo.txt" >>= embedFile)@
423+makeRelativeToLocationPredicate :: (FilePath -> Bool) -> FilePath -> Q FilePath
424+makeRelativeToLocationPredicate isTargetFile rel = do
405 loc <- qLocation
425 loc <- qLocation
406 runIO $ do
426 runIO $ do
407 srcFP <- canonicalizePath $ loc_filename loc
427 srcFP <- canonicalizePath $ loc_filename loc
@@ -416,8 +436,6 @@ makeRelativeToProject rel = do
416 then return Nothing
436 then return Nothing
418 contents <- getDirectoryContents dir
438 contents <- getDirectoryContents dir
419− if any isCabalFile contents
439+ if any isTargetFile contents
420 then return (Just dir)
440 then return (Just dir)
421 else findProjectDir dir
441 else findProjectDir dir
423− isCabalFile fp = takeExtension fp == ".cabal"