Reputation: 1057
I want my TemplateHaskell expression recompiled when the dependent file changes and in case it doesn't exist, use a fallback file. But once the dependent file is created I want to also recompile.
The following is an example code:
import Control.Monad.Cont (MonadIO (liftIO))
import Language.Haskell.TH.Syntax (Lift (liftTyped))
import Language.Haskell.TH.Syntax.Compat (SpliceQ, liftSplice)
import System.Directory (doesFileExist)
myFn :: SpliceQ String
myFn =
let filePath = "...."
fallbackPath = "...."
in do
liftSplice (addDependentFile filePath >>= liftTyped)
exists <- liftIO (doesFileExist filePath)
let path = if exists then filePath else fallbackPath
... -- Then e.g. simply load the file using `path` and return contents
The problem is that addDependentFile
fails in case the file doesn't exist. And in case I run addDependentFile
only when exists
is True
, it doesn't recompile once the file gets created.
How to achieve this? (In case it's not possible to achieve, I also have a workaround in mind - i.e. once the dependent file doesn't exist and fallback is used, let it recompile every time. Is at least this possible to do?)
Upvotes: 1
Views: 75
Reputation: 33389
Since Template Haskell can only register static files as dependencies, you probably need to configure your build system one level up, using Setup.hs
hooks, as described here in the docs: Cabal User Guide: More complex packages.
Upvotes: 0