From 31e08706ed4939cb0a67f688bfb6c05f513f0762 Mon Sep 17 00:00:00 2001 From: aveltras Date: Tue, 27 Jul 2021 09:20:24 +0200 Subject: [PATCH] Add makeRelativeToLocationPredicate --- ChangeLog.md | 4 ++++ Data/FileEmbed.hs | 19 +++++++++++++++---- file-embed.cabal | 2 +- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 9265ab3..582f1b5 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,9 @@ # ChangeLog for file-embed +## 0.0.15.0 + +* Add `makeRelativeToLocationPredicate` + ## 0.0.14.0 * Add `embedFileIfExists` diff --git a/Data/FileEmbed.hs b/Data/FileEmbed.hs index f9fc701..3b86175 100644 --- a/Data/FileEmbed.hs +++ b/Data/FileEmbed.hs @@ -39,6 +39,7 @@ module Data.FileEmbed , injectFileWith -- * Relative path manipulation , makeRelativeToProject + , makeRelativeToLocationPredicate -- * Internal , stringToBs , bsToExp @@ -408,7 +409,19 @@ available, you can use the non-@With@ variants. -- -- @since 0.0.10 makeRelativeToProject :: FilePath -> Q FilePath -makeRelativeToProject rel = do +makeRelativeToProject = makeRelativeToLocationPredicate $ (==) ".cabal" . takeExtension + +-- | 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 +-- +-- This function looks at the source location of the Haskell file calling it, +-- finds the first parent directory with a file matching the given predicate, and uses that as the +-- root directory for fixing the relative path. +-- +-- @$(makeRelativeToLocationPredicate ((==) ".cabal" . takeExtension) "data/foo.txt" >>= embedFile)@ +-- +-- @since 0.0.15.0 +makeRelativeToLocationPredicate :: (FilePath -> Bool) -> FilePath -> Q FilePath +makeRelativeToLocationPredicate isTargetFile rel = do loc <- qLocation runIO $ do srcFP <- canonicalizePath $ loc_filename loc @@ -423,8 +436,6 @@ makeRelativeToProject rel = do then return Nothing else do contents <- getDirectoryContents dir - if any isCabalFile contents + if any isTargetFile contents then return (Just dir) else findProjectDir dir - - isCabalFile fp = takeExtension fp == ".cabal" diff --git a/file-embed.cabal b/file-embed.cabal index bec9961..9246e58 100644 --- a/file-embed.cabal +++ b/file-embed.cabal @@ -1,5 +1,5 @@ name: file-embed -version: 0.0.14.0 +version: 0.0.15.0 license: BSD2 license-file: LICENSE author: Michael Snoyman