module TH.RelativePaths where
import qualified Data.ByteString as BS
import qualified Data.ByteString.Lazy as LBS
import Data.List (find)
import qualified Data.Text as T
import qualified Data.Text.IO as T
import qualified Data.Text.Lazy as LT
import qualified Data.Text.Lazy.IO as LT
import Language.Haskell.TH (Q, Loc(loc_filename), location, runIO, reportWarning)
import Language.Haskell.TH.Syntax (addDependentFile)
import System.Directory (getDirectoryContents, getCurrentDirectory, setCurrentDirectory, doesFileExist)
import System.FilePath
qReadFileBS :: FilePath -> Q BS.ByteString
qReadFileBS fp = do
fp' <- pathRelativeToCabalPackage fp
addDependentFile fp'
runIO $ BS.readFile fp'
qReadFileLBS :: FilePath -> Q LBS.ByteString
qReadFileLBS fp = do
fp' <- pathRelativeToCabalPackage fp
addDependentFile fp'
runIO $ LBS.readFile fp'
qReadFileText :: FilePath -> Q T.Text
qReadFileText fp = do
fp' <- pathRelativeToCabalPackage fp
addDependentFile fp'
runIO $ T.readFile fp'
qReadFileLazyText :: FilePath -> Q LT.Text
qReadFileLazyText fp = do
fp' <- pathRelativeToCabalPackage fp
addDependentFile fp'
runIO $ LT.readFile fp'
qReadFileString :: FilePath -> Q String
qReadFileString fp = do
fp' <- pathRelativeToCabalPackage fp
addDependentFile fp'
runIO $ readFile fp'
withCabalPackageWorkDir :: Q a -> Q a
withCabalPackageWorkDir f = do
cwd' <- pathRelativeToCabalPackage "."
cwd <- runIO $ getCurrentDirectory
runIO $ setCurrentDirectory cwd'
x <- f
runIO $ setCurrentDirectory cwd
return x
pathRelativeToCabalPackage :: FilePath -> Q FilePath
pathRelativeToCabalPackage fp = do
loc <- location
mcabalFile <- runIO $ findCabalFile (loc_filename loc)
case mcabalFile of
Just cabalFile -> return (takeDirectory cabalFile </> fp)
Nothing -> do
reportWarning "Failed to find cabal file, in order to resolve relative paths in TH. Using current working directory instead."
cwd <- runIO getCurrentDirectory
return (cwd </> fp)
findCabalFile :: FilePath -> IO (Maybe FilePath)
findCabalFile dir = do
let parent = takeDirectory dir
contents <- getDirectoryContents parent
case find (\fp -> takeExtension fp == ".cabal") contents of
Nothing
| parent == dir -> return Nothing
| otherwise -> findCabalFile parent
Just fp -> return (Just (parent </> fp))