file-embed-0.0.14.0: Use Template Haskell to embed file contents directly.

Data.FileEmbed

Description

This module uses template Haskell. Following is a simplified explanation of usage for those unfamiliar with calling Template Haskell functions.

The function embedFile in this modules embeds a file into the executable that you can use it at runtime. A file is represented as a ByteString. However, as you can see below, the type signature indicates a value of type Q Exp will be returned. In order to convert this into a ByteString, you must use Template Haskell syntax, e.g.:

$(embedFile "myfile.txt") This expression will have type ByteString. Be certain to enable the TemplateHaskell language extension, usually by adding the following to the top of your module: {-# LANGUAGE TemplateHaskell #-} Synopsis # Embed at compile time Embed a single file in your source code. import qualified Data.ByteString myFile :: Data.ByteString.ByteString myFile =$(embedFile "dirName/fileName")

Maybe embed a single file in your source code depending on whether or not file exists.

Warning: When a build is compiled with the file missing, a recompile when the file exists might not trigger an embed of the file. You might try to fix this by doing a clean build.

import qualified Data.ByteString

maybeMyFile :: Maybe Data.ByteString.ByteString
maybeMyFile = $(embedFileIfExists "dirName/fileName") Since: 0.0.14.0 Embed a single existing file in your source code out of list a list of paths supplied. import qualified Data.ByteString myFile :: Data.ByteString.ByteString myFile =$(embedOneFileOf [ "dirName/fileName", "src/dirName/fileName" ])

Embed a directory recursively in your source code.

import qualified Data.ByteString

myDir :: [(FilePath, Data.ByteString.ByteString)]
myDir = $(embedDir "dirName") Embed a directory listing recursively in your source code. myFiles :: [FilePath] myFiles =$(embedDirListing "dirName")

Since: 0.0.11

Get a directory tree in the IO monad.

This is the workhorse of embedDir

# Embed as a IsString

Embed a single file in your source code.

import Data.String

myFile :: IsString a => a

Since: 0.0.10