Last active
December 21, 2015 05:39
-
-
Save 23Skidoo/6258379 to your computer and use it in GitHub Desktop.
Packaging data files accessed from TemplateHaskell with Cabal.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
module Main | |
where | |
import HelloWorld (helloWorld) | |
main :: IO () | |
main = putStrLn $(helloWorld) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hello, compile-time IO world! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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