2014-06-11 02:35:19 +00:00
|
|
|
From 2b41af230ea5675592e87a2362d9c17bcd8df1db Mon Sep 17 00:00:00 2001
|
|
|
|
From: dummy <dummy@example.com>
|
|
|
|
Date: Tue, 10 Jun 2014 19:00:44 +0000
|
2013-12-18 21:41:17 +00:00
|
|
|
Subject: [PATCH] remove TH
|
|
|
|
|
|
|
|
---
|
2014-06-11 02:35:19 +00:00
|
|
|
Data/FileEmbed.hs | 100 +++---------------------------------------------------
|
|
|
|
1 file changed, 5 insertions(+), 95 deletions(-)
|
2013-12-18 21:41:17 +00:00
|
|
|
|
|
|
|
diff --git a/Data/FileEmbed.hs b/Data/FileEmbed.hs
|
2014-06-11 02:35:19 +00:00
|
|
|
index aae9d5a..efdbb7b 100644
|
2013-12-18 21:41:17 +00:00
|
|
|
--- a/Data/FileEmbed.hs
|
|
|
|
+++ b/Data/FileEmbed.hs
|
2014-06-11 02:35:19 +00:00
|
|
|
@@ -17,19 +17,18 @@
|
2013-12-18 21:41:17 +00:00
|
|
|
-- > {-# LANGUAGE TemplateHaskell #-}
|
|
|
|
module Data.FileEmbed
|
|
|
|
( -- * Embed at compile time
|
|
|
|
- embedFile
|
|
|
|
- , embedOneFileOf
|
|
|
|
- , embedDir
|
|
|
|
- , getDir
|
|
|
|
+ -- embedFile
|
|
|
|
+ --, embedOneFileOf
|
|
|
|
+ --, embedDir
|
|
|
|
+ getDir
|
|
|
|
-- * Inject into an executable
|
|
|
|
#if MIN_VERSION_template_haskell(2,5,0)
|
|
|
|
- , dummySpace
|
|
|
|
+ --, dummySpace
|
|
|
|
#endif
|
|
|
|
, inject
|
|
|
|
, injectFile
|
2014-06-11 02:35:19 +00:00
|
|
|
-- * Internal
|
|
|
|
, stringToBs
|
|
|
|
- , bsToExp
|
|
|
|
) where
|
|
|
|
|
|
|
|
import Language.Haskell.TH.Syntax
|
|
|
|
@@ -57,85 +56,12 @@ import Data.ByteString.Unsafe (unsafePackAddressLen)
|
2013-12-18 21:41:17 +00:00
|
|
|
import System.IO.Unsafe (unsafePerformIO)
|
|
|
|
import System.FilePath ((</>))
|
|
|
|
|
|
|
|
--- | Embed a single file in your source code.
|
|
|
|
---
|
|
|
|
--- > import qualified Data.ByteString
|
|
|
|
--- >
|
|
|
|
--- > myFile :: Data.ByteString.ByteString
|
|
|
|
--- > myFile = $(embedFile "dirName/fileName")
|
|
|
|
-embedFile :: FilePath -> Q Exp
|
|
|
|
-embedFile fp =
|
|
|
|
-#if MIN_VERSION_template_haskell(2,7,0)
|
|
|
|
- qAddDependentFile fp >>
|
|
|
|
-#endif
|
|
|
|
- (runIO $ B.readFile fp) >>= bsToExp
|
|
|
|
-
|
|
|
|
--- | 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 = $(embedFile' [ "dirName/fileName", "src/dirName/fileName" ])
|
|
|
|
-embedOneFileOf :: [FilePath] -> Q Exp
|
|
|
|
-embedOneFileOf ps =
|
|
|
|
- (runIO $ readExistingFile ps) >>= \ ( path, content ) -> do
|
|
|
|
-#if MIN_VERSION_template_haskell(2,7,0)
|
|
|
|
- qAddDependentFile path
|
|
|
|
-#endif
|
|
|
|
- bsToExp content
|
|
|
|
- where
|
|
|
|
- readExistingFile :: [FilePath] -> IO ( FilePath, B.ByteString )
|
|
|
|
- readExistingFile xs = do
|
|
|
|
- ys <- filterM doesFileExist xs
|
|
|
|
- case ys of
|
|
|
|
- (p:_) -> B.readFile p >>= \ c -> return ( p, c )
|
|
|
|
- _ -> throw $ ErrorCall "Cannot find file to embed as resource"
|
|
|
|
-
|
|
|
|
--- | Embed a directory recursively in your source code.
|
|
|
|
---
|
|
|
|
--- > import qualified Data.ByteString
|
|
|
|
--- >
|
|
|
|
--- > myDir :: [(FilePath, Data.ByteString.ByteString)]
|
|
|
|
--- > myDir = $(embedDir "dirName")
|
|
|
|
-embedDir :: FilePath -> Q Exp
|
|
|
|
-embedDir fp = do
|
|
|
|
- typ <- [t| [(FilePath, B.ByteString)] |]
|
|
|
|
- e <- ListE <$> ((runIO $ fileList fp) >>= mapM (pairToExp fp))
|
|
|
|
- return $ SigE e typ
|
|
|
|
-
|
2014-06-11 02:35:19 +00:00
|
|
|
-- | Get a directory tree in the IO monad.
|
2013-12-18 21:41:17 +00:00
|
|
|
--
|
|
|
|
-- This is the workhorse of 'embedDir'
|
|
|
|
getDir :: FilePath -> IO [(FilePath, B.ByteString)]
|
|
|
|
getDir = fileList
|
|
|
|
|
|
|
|
-pairToExp :: FilePath -> (FilePath, B.ByteString) -> Q Exp
|
|
|
|
-pairToExp _root (path, bs) = do
|
|
|
|
-#if MIN_VERSION_template_haskell(2,7,0)
|
|
|
|
- qAddDependentFile $ _root ++ '/' : path
|
|
|
|
-#endif
|
|
|
|
- exp' <- bsToExp bs
|
|
|
|
- return $! TupE [LitE $ StringL path, exp']
|
|
|
|
-
|
|
|
|
-bsToExp :: B.ByteString -> Q Exp
|
2014-06-11 02:35:19 +00:00
|
|
|
-#if MIN_VERSION_template_haskell(2, 5, 0)
|
|
|
|
-bsToExp bs =
|
|
|
|
- return $ VarE 'unsafePerformIO
|
|
|
|
- `AppE` (VarE 'unsafePackAddressLen
|
|
|
|
- `AppE` LitE (IntegerL $ fromIntegral $ B8.length bs)
|
|
|
|
-#if MIN_VERSION_template_haskell(2, 8, 0)
|
|
|
|
- `AppE` LitE (StringPrimL $ B.unpack bs))
|
|
|
|
-#else
|
|
|
|
- `AppE` LitE (StringPrimL $ B8.unpack bs))
|
|
|
|
-#endif
|
|
|
|
-#else
|
2013-12-18 21:41:17 +00:00
|
|
|
-bsToExp bs = do
|
|
|
|
- helper <- [| stringToBs |]
|
|
|
|
- let chars = B8.unpack bs
|
|
|
|
- return $! AppE helper $! LitE $! StringL chars
|
2014-06-11 02:35:19 +00:00
|
|
|
-#endif
|
|
|
|
-
|
2013-12-18 21:41:17 +00:00
|
|
|
stringToBs :: String -> B.ByteString
|
|
|
|
stringToBs = B8.pack
|
2014-06-11 02:35:19 +00:00
|
|
|
|
|
|
|
@@ -177,22 +103,6 @@ padSize i =
|
2013-12-18 21:41:17 +00:00
|
|
|
let s = show i
|
|
|
|
in replicate (sizeLen - length s) '0' ++ s
|
|
|
|
|
|
|
|
-#if MIN_VERSION_template_haskell(2,5,0)
|
|
|
|
-dummySpace :: Int -> Q Exp
|
|
|
|
-dummySpace space = do
|
|
|
|
- let size = padSize space
|
|
|
|
- let start = magic ++ size
|
|
|
|
- let chars = LitE $ StringPrimL $
|
|
|
|
-#if MIN_VERSION_template_haskell(2,6,0)
|
|
|
|
- map (toEnum . fromEnum) $
|
|
|
|
-#endif
|
|
|
|
- start ++ replicate space '0'
|
|
|
|
- let len = LitE $ IntegerL $ fromIntegral $ length start + space
|
|
|
|
- upi <- [|unsafePerformIO|]
|
|
|
|
- pack <- [|unsafePackAddressLen|]
|
|
|
|
- getInner' <- [|getInner|]
|
|
|
|
- return $ getInner' `AppE` (upi `AppE` (pack `AppE` len `AppE` chars))
|
|
|
|
-#endif
|
|
|
|
|
|
|
|
inject :: B.ByteString -- ^ bs to inject
|
|
|
|
-> B.ByteString -- ^ original BS containing dummy
|
|
|
|
--
|
2014-06-11 02:35:19 +00:00
|
|
|
2.0.0
|
2013-12-18 21:41:17 +00:00
|
|
|
|