Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Packaging data files accessed from TemplateHaskell with Cabal.
-- A simpler alternative to
-- See
module HelloWorld (helloWorld)
import Language.Haskell.TH
import Language.Haskell.TH.Syntax (addDependentFile)
import System.FilePath
helloWorld :: Q Exp
helloWorld = do
loc <- location
let fileName = takeDirectory (loc_filename loc)
</> ".." </> "data" </> "test.txt"
str <- runIO $ do
-- We assume that the current module is in '<package_root>/src/'
-- and the data file is in '<package_root>/data'.
readFile fileName
addDependentFile fileName
litE . stringL $ str
{-# LANGUAGE TemplateHaskell #-}
module Main
import HelloWorld (helloWorld)
main :: IO ()
main = putStrLn $(helloWorld)
Hello, compile-time IO world!
name: th-extra-source-files
-- synopsis:
-- description:
license: GPL-3
license-file: LICENSE
author: Mikhail Glushenkov
-- copyright:
category: Testing
build-type: Simple
extra-source-files: data/test.txt
cabal-version: >=1.10
executable th-extra-source-files
main-is: Main.hs
-- other-modules:
other-extensions: TemplateHaskell
build-depends: base >=4.5 && <5,
template-haskell, filepath
hs-source-dirs: src
default-language: Haskell2010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment