Skip to content

Commit

Permalink
Merge pull request #41 from aveltras/build-system-agnostic-project-path
Browse files Browse the repository at this point in the history
Add build system agnostic version of makeRelativeToProject
  • Loading branch information
snoyberg authored Aug 29, 2021
2 parents 063bd67 + 31e0870 commit 548430d
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 548430d

Please sign in to comment.