Skip to content

Commit

Permalink
Add makeRelativeToLocationPredicate
Browse files Browse the repository at this point in the history
  • Loading branch information
aveltras committed Aug 29, 2021
1 parent 063bd67 commit 31e0870
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# ChangeLog for file-embed

## 0.0.15.0

* Add `makeRelativeToLocationPredicate`

## 0.0.14.0

* Add `embedFileIfExists`
Expand Down
19 changes: 15 additions & 4 deletions Data/FileEmbed.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ module Data.FileEmbed
, injectFileWith
-- * Relative path manipulation
, makeRelativeToProject
, makeRelativeToLocationPredicate
-- * Internal
, stringToBs
, bsToExp
Expand Down Expand Up @@ -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
Expand All @@ -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"
2 changes: 1 addition & 1 deletion file-embed.cabal
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
Expand Down

0 comments on commit 31e0870

Please sign in to comment.