Use filepath for directory manipulation

This commit is contained in:
Michael Snoyman 2012-12-30 08:31:45 +02:00
parent e0add13538
commit c7b486b40f
4 changed files with 13 additions and 8 deletions

View File

@ -31,11 +31,11 @@ import System.Directory (doesDirectoryExist, doesFileExist,
import Control.Monad (filterM)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import Control.Arrow ((&&&), second, first)
import Control.Arrow ((&&&), second)
import Control.Applicative ((<$>))
import Data.Monoid (mappend)
import Data.ByteString.Unsafe (unsafePackAddressLen)
import System.IO.Unsafe (unsafePerformIO)
import System.FilePath ((</>))
-- | Embed a single file in your source code.
--
@ -90,14 +90,12 @@ notHidden ('.':_) = False
notHidden _ = True
fileList :: FilePath -> IO [(FilePath, B.ByteString)]
fileList top = map (first tail) <$> fileList' top ""
fileList top = fileList' top ""
fileList' :: FilePath -> FilePath -> IO [(FilePath, B.ByteString)]
fileList' realTop top = do
let prefix1 = top ++ "/"
prefix2 = realTop ++ prefix1
allContents <- filter notHidden <$> getDirectoryContents prefix2
let all' = map (mappend prefix1 &&& mappend prefix2) allContents
allContents <- filter notHidden <$> getDirectoryContents (realTop </> top)
let all' = map ((top </>) &&& (\x -> realTop </> top </> x)) allContents
files <- filterM (doesFileExist . snd) all' >>=
mapM (liftPair2 . second B.readFile)
dirs <- filterM (doesDirectoryExist . snd) all' >>=

View File

@ -20,6 +20,7 @@ library
, bytestring >= 0.9.1.4
, directory >= 1.0.0.3
, template-haskell
, filepath
exposed-modules: Data.FileEmbed
ghc-options: -Wall
@ -30,6 +31,7 @@ test-suite test
build-depends: base
, file-embed
, HUnit
, filepath
source-repository head
type: git

View File

@ -3,8 +3,12 @@
import Data.FileEmbed
import Test.HUnit ((@?=))
import System.FilePath ((</>))
main :: IO ()
main = do
let received = $(embedDir "test/sample")
received @?= [("foo", "foo\n")]
received @?=
[ ("foo", "foo\n")
, ("bar" </> "baz", "baz\n")
]

1
test/sample/bar/baz Normal file
View File

@ -0,0 +1 @@
baz