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