public
Last active

Packaging data files accessed from TemplateHaskell with Cabal.

  • Download Gist
HelloWorld.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
-- A simpler alternative to http://www.alfredodinapoli.com/posts/2013-08-17-copy-your-project-resources-at-configure-time.html
-- See http://www.reddit.com/r/haskell/comments/1kjpop/copy_your_project_resources_at_configure_time/
 
module HelloWorld (helloWorld)
where
 
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
Main.hs
Haskell
1 2 3 4 5 6 7 8 9
{-# LANGUAGE TemplateHaskell #-}
 
module Main
where
 
import HelloWorld (helloWorld)
 
main :: IO ()
main = putStrLn $(helloWorld)
test.txt
1
Hello, compile-time IO world!
th-extra-source-files.cabal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
name: th-extra-source-files
version: 0.1.0.0
-- synopsis:
-- description:
license: GPL-3
license-file: LICENSE
author: Mikhail Glushenkov
maintainer: mikhail.glushenkov@gmail.com
-- 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.