Don't keep partial autogen file when exception occurs

This commit is contained in:
Paul Rouse 2016-11-09 09:31:28 +00:00
parent b074279832
commit 4ab830c4d9
2 changed files with 5 additions and 3 deletions

View File

@ -17,8 +17,9 @@ module Yesod.Default.Util
import qualified Data.ByteString.Lazy as L import qualified Data.ByteString.Lazy as L
import Data.Text (Text, pack, unpack) import Data.Text (Text, pack, unpack)
import Yesod.Core -- purposely using complete import so that Haddock will see addStaticContent import Yesod.Core -- purposely using complete import so that Haddock will see addStaticContent
import Control.Exception (onException)
import Control.Monad (when, unless) import Control.Monad (when, unless)
import System.Directory (doesFileExist, createDirectoryIfMissing) import System.Directory (doesFileExist, createDirectoryIfMissing, removeFile)
import Language.Haskell.TH.Syntax import Language.Haskell.TH.Syntax
import Text.Lucius (luciusFile, luciusFileReload) import Text.Lucius (luciusFile, luciusFileReload)
import Text.Julius (juliusFile, juliusFileReload) import Text.Julius (juliusFile, juliusFileReload)
@ -43,7 +44,7 @@ addStaticContentExternal
addStaticContentExternal minify hash staticDir toRoute ext' _ content = do addStaticContentExternal minify hash staticDir toRoute ext' _ content = do
liftIO $ createDirectoryIfMissing True statictmp liftIO $ createDirectoryIfMissing True statictmp
exists <- liftIO $ doesFileExist fn' exists <- liftIO $ doesFileExist fn'
unless exists $ liftIO $ L.writeFile fn' content' unless exists $ liftIO $ L.writeFile fn' content' `onException` remove fn'
return $ Just $ Right (toRoute ["tmp", pack fn], []) return $ Just $ Right (toRoute ["tmp", pack fn], [])
where where
fn, statictmp, fn' :: FilePath fn, statictmp, fn' :: FilePath
@ -52,6 +53,7 @@ addStaticContentExternal minify hash staticDir toRoute ext' _ content = do
fn = hash content ++ '.' : unpack ext' fn = hash content ++ '.' : unpack ext'
statictmp = staticDir ++ "/tmp/" statictmp = staticDir ++ "/tmp/"
fn' = statictmp ++ fn fn' = statictmp ++ fn
remove f = doesFileExist f >>= \x -> when x $ removeFile f
content' :: L.ByteString content' :: L.ByteString
content' content'

View File

@ -1,5 +1,5 @@
name: yesod name: yesod
version: 1.4.3 version: 1.4.3.1
license: MIT license: MIT
license-file: LICENSE license-file: LICENSE
author: Michael Snoyman <michael@snoyman.com> author: Michael Snoyman <michael@snoyman.com>