Skip to content

Instantly share code, notes, and snippets.

@23Skidoo
Last active December 21, 2015 05:39
Show Gist options
  • Save 23Skidoo/6258379 to your computer and use it in GitHub Desktop.
Save 23Skidoo/6258379 to your computer and use it in GitHub Desktop.
Packaging data files accessed from TemplateHaskell with Cabal.
-- 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
{-# LANGUAGE TemplateHaskell #-}
module Main
where
import HelloWorld (helloWorld)
main :: IO ()
main = putStrLn $(helloWorld)
Hello, compile-time IO world!
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment