hledger/hledger-web/Hledger/Web/Files.hs
Simon Michael b5e1c42cc4 web: auto-create all required support files in ./.hledger/web at startup
This is a compromise to ease deployment and satisfy hamlet's requirements.
See the Hledger.Web.Files module for more info. Currently we exit after
creating the missing files since they are not created early enough for
hamlet.
2010-11-18 00:53:41 +00:00

45 lines
1.3 KiB
Haskell

{-# LANGUAGE TemplateHaskell #-}
{-|
Support files used by the web app are embedded here at compile time via
template haskell magic. This allows us minimise deployment hassle by
recreating them on the filesystem when needed (since hamlet can not use
the embedded files directly.) Installing on the filesystem has the added
benefit of making them easily customisable.
-}
module Hledger.Web.Files
(
files
,createFilesIfMissing
)
where
import Control.Monad
import qualified Data.ByteString as B
import Data.FileEmbed (embedDir)
import System.Directory
import Hledger.Web.Settings (datadir)
-- | An embedded copy of all files below the the hledger-web data
-- directory (@.hledger/web/@) at compile time, as (FilePath,ByteString)
-- pairs.
files :: [(FilePath, B.ByteString)]
files = $(embedDir datadir)
-- | If the hledger-web data directory (@.hledger/web/@) does not exist in
-- the current directory, create and fill it with the web app support
-- files (templates, stylesheets, images etc.) Returns True if the
-- directory was missing.
createFilesIfMissing :: IO Bool
createFilesIfMissing = do
exists <- doesDirectoryExist datadir
if exists
then return False
else do
createDirectoryIfMissing True datadir
setCurrentDirectory datadir
forM_ files $ \(f,d) -> B.writeFile f d
return True