From cfb96fd316e3b983bbb235891b991226832eb4b1 Mon Sep 17 00:00:00 2001 From: Michael Snoyman Date: Wed, 6 Aug 2014 13:47:34 +0300 Subject: [PATCH] add-handler: check if file exists #798 --- yesod-bin/AddHandler.hs | 11 +++++++++-- yesod-bin/yesod-bin.cabal | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/yesod-bin/AddHandler.hs b/yesod-bin/AddHandler.hs index 27fb40b5..ca3a6ce3 100644 --- a/yesod-bin/AddHandler.hs +++ b/yesod-bin/AddHandler.hs @@ -6,7 +6,8 @@ import Data.Char (isLower, toLower, isSpace) import Data.List (isPrefixOf, isSuffixOf) import qualified Data.Text as T import qualified Data.Text.IO as TIO -import System.Directory (getDirectoryContents) +import System.Directory (getDirectoryContents, doesFileExist) +import Control.Monad (when) -- strict readFile readFile :: FilePath -> IO String @@ -29,6 +30,12 @@ addHandler = do c:_ | isLower c -> error "Name must start with an upper case letter" | otherwise -> return () + + -- Check that the handler file doesn't already exist + let handlerFile = concat ["Handler/", name, ".hs"] + exists <- doesFileExist handlerFile + when exists $ error $ "File already exists: " ++ show handlerFile + putStr "Enter route pattern (ex: /entry/#EntryId): " hFlush stdout pattern <- getLine @@ -41,7 +48,7 @@ addHandler = do modify "Application.hs" $ fixApp name modify cabal $ fixCabal name modify "config/routes" $ fixRoutes name pattern methods - writeFile ("Handler/" ++ name ++ ".hs") $ mkHandler name pattern methods + writeFile handlerFile $ mkHandler name pattern methods fixApp :: String -> String -> String fixApp name = diff --git a/yesod-bin/yesod-bin.cabal b/yesod-bin/yesod-bin.cabal index 529fca00..73305cff 100644 --- a/yesod-bin/yesod-bin.cabal +++ b/yesod-bin/yesod-bin.cabal @@ -1,5 +1,5 @@ name: yesod-bin -version: 1.2.12.1 +version: 1.2.12.2 license: MIT license-file: LICENSE author: Michael Snoyman